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