Sync usage with man page.
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / mvect.c
blobe508c3a1dba7963e667e0fa6e08ff0417999dfec
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* mvect 3
6 /* SUMMARY
7 /* memory vector management
8 /* SYNOPSIS
9 /* #include <mvect.h>
11 /* char *mvect_alloc(vector, elsize, nelm, init_fn, wipe_fn)
12 /* MVECT *vector;
13 /* int elsize;
14 /* int nelm;
15 /* void (*init_fn)(char *ptr, int count);
16 /* void (*wipe_fn)(char *ptr, int count);
18 /* char *mvect_realloc(vector, nelm)
19 /* MVECT *vector;
20 /* int nelm;
22 /* char *mvect_free(vector)
23 /* MVECT *vector;
24 /* DESCRIPTION
25 /* This module supports memory management for arrays of arbitrary
26 /* objects. It is up to the application to provide specific code
27 /* that initializes and uses object memory.
29 /* mvect_alloc() initializes memory for a vector with elements
30 /* of \fIelsize\fR bytes, and with at least \fInelm\fR elements.
31 /* \fIinit_fn\fR is a null pointer, or a pointer to a function
32 /* that initializes \fIcount\fR vector elements.
33 /* \fIwipe_fn\fR is a null pointer, or a pointer to a function
34 /* that is complementary to \fIinit_fn\fR. This routine is called
35 /* by mvect_free(). The result of mvect_alloc() is a pointer to
36 /* the allocated vector.
38 /* mvect_realloc() guarantees that the specified vector has space
39 /* for at least \fInelm\fR elements. The result is a pointer to the
40 /* allocated vector, which may change across calls.
42 /* mvect_free() releases storage for the named vector. The result
43 /* is a convenient null pointer.
44 /* SEE ALSO
45 /* mymalloc(3) memory management
46 /* DIAGNOSTICS
47 /* Problems are reported via the msg(3) diagnostics routines:
48 /* the requested amount of memory is not available; improper use
49 /* is detected; other fatal errors.
50 /* LICENSE
51 /* .ad
52 /* .fi
53 /* The Secure Mailer license must be distributed with this software.
54 /* AUTHOR(S)
55 /* Wietse Venema
56 /* IBM T.J. Watson Research
57 /* P.O. Box 704
58 /* Yorktown Heights, NY 10598, USA
59 /*--*/
61 /* System library. */
63 #include <sys_defs.h>
65 /* Utility library. */
67 #include "mymalloc.h"
68 #include "mvect.h"
70 /* mvect_alloc - allocate memory vector */
72 char *mvect_alloc(MVECT *vect, int elsize, int nelm,
73 void (*init_fn) (char *, int), void (*wipe_fn) (char *, int))
75 vect->init_fn = init_fn;
76 vect->wipe_fn = wipe_fn;
77 vect->nelm = 0;
78 vect->ptr = mymalloc(elsize * nelm);
79 vect->nelm = nelm;
80 vect->elsize = elsize;
81 if (vect->init_fn)
82 vect->init_fn(vect->ptr, vect->nelm);
83 return (vect->ptr);
86 /* mvect_realloc - adjust memory vector allocation */
88 char *mvect_realloc(MVECT *vect, int nelm)
90 int old_len = vect->nelm;
91 int incr = nelm - old_len;
92 int new_nelm;
94 if (incr > 0) {
95 if (incr < old_len)
96 incr = old_len;
97 new_nelm = vect->nelm + incr;
98 vect->ptr = myrealloc(vect->ptr, vect->elsize * new_nelm);
99 vect->nelm = new_nelm;
100 if (vect->init_fn)
101 vect->init_fn(vect->ptr + old_len * vect->elsize, incr);
103 return (vect->ptr);
106 /* mvect_free - release memory vector storage */
108 char *mvect_free(MVECT *vect)
110 if (vect->wipe_fn)
111 vect->wipe_fn(vect->ptr, vect->nelm);
112 myfree(vect->ptr);
113 myfree((char *) vect);
114 return (0);