xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/dict_alloc.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: dict_alloc.c,v 1.1.1.3 2013/09/25 19:06:36 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	dict_alloc 3
6 /* SUMMARY
7 /*	dictionary memory manager
8 /* SYNOPSIS
9 /*	#include <dict.h>
10 /*
11 /*	DICT	*dict_alloc(dict_type, dict_name, size)
12 /*	const char *dict_type;
13 /*	const char *dict_name;
14 /*	ssize_t	size;
15 /*
16 /*	void	dict_free(dict)
17 /*	DICT	*ptr;
18 /* DESCRIPTION
19 /*	dict_alloc() allocates memory for a dictionary structure of
20 /*	\fIsize\fR bytes, initializes all generic dictionary
21 /*	properties to default settings,
22 /*	and installs default methods that do not support any operation.
23 /*	The caller is supposed to override the default methods with
24 /*	ones that it supports.
25 /*	The purpose of the default methods is to trap an attempt to
26 /*	invoke an unsupported method.
27 /*
28 /*	One exception is the default lock function.  When the
29 /*	dictionary provides a file handle for locking, the default
30 /*	lock function returns the result from myflock(), otherwise
31 /*	it returns 0. Presently, the lock function is used only to
32 /*	implement the DICT_FLAG_OPEN_LOCK feature (lock the database
33 /*	exclusively after it is opened) for databases that are not
34 /*	multi-writer safe.
35 /*
36 /*	dict_free() releases memory and cleans up after dict_alloc().
37 /*	It is up to the caller to dispose of any memory that was allocated
38 /*	by the caller.
39 /*
40 /*	Arguments:
41 /* .IP dict_type
42 /*	The official name for this type of dictionary, as used by
43 /*	dict_open(3) etc. This is stored under the \fBtype\fR
44 /*	member.
45 /* .IP dict_name
46 /*	Dictionary name. This is stored as the \fBname\fR member.
47 /* .IP size
48 /*	The size in bytes of the dictionary subclass structure instance.
49 /* SEE ALSO
50 /*	dict(3)
51 /* DIAGNOSTICS
52 /*	Fatal errors: the process invokes a default method.
53 /* LICENSE
54 /* .ad
55 /* .fi
56 /*	The Secure Mailer license must be distributed with this software.
57 /* AUTHOR(S)
58 /*	Wietse Venema
59 /*	IBM T.J. Watson Research
60 /*	P.O. Box 704
61 /*	Yorktown Heights, NY 10598, USA
62 /*--*/
63 
64 /* System libraries. */
65 
66 #include "sys_defs.h"
67 
68 /* Utility library. */
69 
70 #include "msg.h"
71 #include "mymalloc.h"
72 #include "myflock.h"
73 #include "dict.h"
74 
75 /* dict_default_lookup - trap unimplemented operation */
76 
77 static const char *dict_default_lookup(DICT *dict, const char *unused_key)
78 {
79     msg_fatal("table %s:%s: lookup operation is not supported",
80 	      dict->type, dict->name);
81 }
82 
83 /* dict_default_update - trap unimplemented operation */
84 
85 static int dict_default_update(DICT *dict, const char *unused_key,
86 			               const char *unused_value)
87 {
88     msg_fatal("table %s:%s: update operation is not supported",
89 	      dict->type, dict->name);
90 }
91 
92 /* dict_default_delete - trap unimplemented operation */
93 
94 static int dict_default_delete(DICT *dict, const char *unused_key)
95 {
96     msg_fatal("table %s:%s: delete operation is not supported",
97 	      dict->type, dict->name);
98 }
99 
100 /* dict_default_sequence - trap unimplemented operation */
101 
102 static int dict_default_sequence(DICT *dict, int unused_function,
103 		         const char **unused_key, const char **unused_value)
104 {
105     msg_fatal("table %s:%s: sequence operation is not supported",
106 	      dict->type, dict->name);
107 }
108 
109 /* dict_default_lock - default lock handler */
110 
111 static int dict_default_lock(DICT *dict, int operation)
112 {
113     if (dict->lock_fd >= 0) {
114 	return (myflock(dict->lock_fd, INTERNAL_LOCK, operation));
115     } else {
116 	return (0);
117     }
118 }
119 
120 /* dict_default_close - trap unimplemented operation */
121 
122 static void dict_default_close(DICT *dict)
123 {
124     msg_fatal("table %s:%s: close operation is not supported",
125 	      dict->type, dict->name);
126 }
127 
128 /* dict_alloc - allocate dictionary object, initialize super-class */
129 
130 DICT   *dict_alloc(const char *dict_type, const char *dict_name, ssize_t size)
131 {
132     DICT   *dict = (DICT *) mymalloc(size);
133 
134     dict->type = mystrdup(dict_type);
135     dict->name = mystrdup(dict_name);
136     dict->flags = DICT_FLAG_FIXED;
137     dict->lookup = dict_default_lookup;
138     dict->update = dict_default_update;
139     dict->delete = dict_default_delete;
140     dict->sequence = dict_default_sequence;
141     dict->close = dict_default_close;
142     dict->lock = dict_default_lock;
143     dict->lock_fd = -1;
144     dict->stat_fd = -1;
145     dict->mtime = 0;
146     dict->fold_buf = 0;
147     dict->owner.status = DICT_OWNER_UNKNOWN;
148     dict->owner.uid = ~0;
149     dict->error = DICT_ERR_NONE;
150     return dict;
151 }
152 
153 /* dict_free - super-class destructor */
154 
155 void    dict_free(DICT *dict)
156 {
157     myfree(dict->type);
158     myfree(dict->name);
159     myfree((char *) dict);
160 }
161