summaryrefslogtreecommitdiffstats
path: root/src/util/mvect.c
blob: cf4b0d5bbfe977318925d92f515195e7b2506387 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*++
/* NAME
/*	mvect 3
/* SUMMARY
/*	memory vector management
/* SYNOPSIS
/*	#include <mvect.h>
/*
/*	char	*mvect_alloc(vector, elsize, nelm, init_fn, wipe_fn)
/*	MVECT	*vector;
/*	ssize_t	elsize;
/*	ssize_t	nelm;
/*	void	(*init_fn)(char *ptr, ssize_t count);
/*	void	(*wipe_fn)(char *ptr, ssize_t count);
/*
/*	char	*mvect_realloc(vector, nelm)
/*	MVECT	*vector;
/*	ssize_t	nelm;
/*
/*	char	*mvect_free(vector)
/*	MVECT	*vector;
/* DESCRIPTION
/*	This module supports memory management for arrays of arbitrary
/*	objects.  It is up to the application to provide specific code
/*	that initializes and uses object memory.
/*
/*	mvect_alloc() initializes memory for a vector with elements
/*	of \fIelsize\fR bytes, and with at least \fInelm\fR elements.
/*	\fIinit_fn\fR is a null pointer, or a pointer to a function
/*	that initializes \fIcount\fR vector elements.
/*	\fIwipe_fn\fR is a null pointer, or a pointer to a function
/*	that is complementary to \fIinit_fn\fR. This routine is called
/*	by mvect_free(). The result of mvect_alloc() is a pointer to
/*	the allocated vector.
/*
/*	mvect_realloc() guarantees that the specified vector has space
/*	for at least \fInelm\fR elements. The result is a pointer to the
/*	allocated vector, which may change across calls.
/*
/*	mvect_free() releases storage for the named vector. The result
/*	is a convenient null pointer.
/* SEE ALSO
/*	mymalloc(3) memory management
/* DIAGNOSTICS
/*	Problems are reported via the msg(3) diagnostics routines:
/*	the requested amount of memory is not available; improper use
/*	is detected; other fatal errors.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*
/*	Wietse Venema
/*	Google, Inc.
/*	111 8th Avenue
/*	New York, NY 10011, USA
/*--*/

/* System library. */

#include <sys_defs.h>

/* Utility library. */

#include "mymalloc.h"
#include "mvect.h"

/* mvect_alloc - allocate memory vector */

char   *mvect_alloc(MVECT *vect, ssize_t elsize, ssize_t nelm,
               void (*init_fn) (char *, ssize_t), void (*wipe_fn) (char *, ssize_t))
{
    vect->init_fn = init_fn;
    vect->wipe_fn = wipe_fn;
    vect->nelm = 0;
    vect->ptr = mymalloc(elsize * nelm);
    vect->nelm = nelm;
    vect->elsize = elsize;
    if (vect->init_fn)
	vect->init_fn(vect->ptr, vect->nelm);
    return (vect->ptr);
}

/* mvect_realloc - adjust memory vector allocation */

char   *mvect_realloc(MVECT *vect, ssize_t nelm)
{
    ssize_t old_len = vect->nelm;
    ssize_t incr = nelm - old_len;
    ssize_t new_nelm;

    if (incr > 0) {
	if (incr < old_len)
	    incr = old_len;
	new_nelm = vect->nelm + incr;
	vect->ptr = myrealloc(vect->ptr, vect->elsize * new_nelm);
	vect->nelm = new_nelm;
	if (vect->init_fn)
	    vect->init_fn(vect->ptr + old_len * vect->elsize, incr);
    }
    return (vect->ptr);
}

/* mvect_free - release memory vector storage */

char   *mvect_free(MVECT *vect)
{
    if (vect->wipe_fn)
	vect->wipe_fn(vect->ptr, vect->nelm);
    myfree(vect->ptr);
    return (0);
}