7 /* memory vector management
11 /* char *mvect_alloc(vector, elsize, nelm, init_fn, wipe_fn)
15 /* void (*init_fn)(char *ptr, int count);
16 /* void (*wipe_fn)(char *ptr, int count);
18 /* char *mvect_realloc(vector, nelm)
22 /* char *mvect_free(vector)
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.
45 /* mymalloc(3) memory management
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.
53 /* The Secure Mailer license must be distributed with this software.
56 /* IBM T.J. Watson Research
58 /* Yorktown Heights, NY 10598, USA
65 /* Utility library. */
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
;
78 vect
->ptr
= mymalloc(elsize
* nelm
);
80 vect
->elsize
= elsize
;
82 vect
->init_fn(vect
->ptr
, vect
->nelm
);
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
;
97 new_nelm
= vect
->nelm
+ incr
;
98 vect
->ptr
= myrealloc(vect
->ptr
, vect
->elsize
* new_nelm
);
99 vect
->nelm
= new_nelm
;
101 vect
->init_fn(vect
->ptr
+ old_len
* vect
->elsize
, incr
);
106 /* mvect_free - release memory vector storage */
108 char *mvect_free(MVECT
*vect
)
111 vect
->wipe_fn(vect
->ptr
, vect
->nelm
);
113 myfree((char *) vect
);