annotate src/dynarr.c @ 424:11054d720c21 r21-2-20

Import from CVS: tag r21-2-20
author cvs
date Mon, 13 Aug 2007 11:26:11 +0200
parents 697ef44129c6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* Simple 'n' stupid dynamic-array module.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Copyright (C) 1993 Sun Microsystems, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 /* Synched up with: Not in FSF. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 /* Written by Ben Wing, December 1993. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 A "dynamic array" is a contiguous array of fixed-size elements where there
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 is no upper limit (except available memory) on the number of elements in the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 array. Because the elements are maintained contiguously, space is used
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 efficiently (no per-element pointers necessary) and random access to a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 particular element is in constant time. At any one point, the block of memory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 that holds the array has an upper limit; if this limit is exceeded, the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 memory is realloc()ed into a new array that is twice as big. Assuming that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 the time to grow the array is on the order of the new size of the array
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 block, this scheme has a provably constant amortized time (i.e. average
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 time over all additions).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 When you add elements or retrieve elements, pointers are used. Note that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 the element itself (of whatever size it is), and not the pointer to it,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 is stored in the array; thus you do not have to allocate any heap memory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 on your own. Also, returned pointers are only guaranteed to be valid
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 until the next operation that changes the length of the array.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 This is a container object. Declare a dynamic array of a specific type
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 as follows:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
47 typedef struct
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 Dynarr_declare (mytype);
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 0
diff changeset
50 } mytype_dynarr;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 Use the following functions/macros:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 void *Dynarr_new(type)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 [MACRO] Create a new dynamic-array object, with each element of the
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 0
diff changeset
56 specified type. The return value is cast to (type##_dynarr).
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 0
diff changeset
57 This requires following the convention that types are declared in
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 0
diff changeset
58 such a way that this type concatenation works. In particular, TYPE
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 0
diff changeset
59 must be a symbol, not an arbitrary C type.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 Dynarr_add(d, el)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 [MACRO] Add an element to the end of a dynamic array. EL is a pointer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 to the element; the element itself is stored in the array, however.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 No function call is performed unless the array needs to be resized.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 Dynarr_add_many(d, base, len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 [MACRO] Add LEN elements to the end of the dynamic array. The elements
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 should be contiguous in memory, starting at BASE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 Dynarr_insert_many_at_start(d, base, len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 [MACRO] Append LEN elements to the beginning of the dynamic array.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 The elements should be contiguous in memory, starting at BASE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 Dynarr_insert_many(d, base, len, start)
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 272
diff changeset
75 Insert LEN elements to the dynamic array starting at position
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 START. The elements should be contiguous in memory, starting at BASE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 int Dynarr_length(d)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 [MACRO] Return the number of elements currently in a dynamic array.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 0
diff changeset
81 int Dynarr_largest(d)
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 0
diff changeset
82 [MACRO] Return the maximum value that Dynarr_length(d) would
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 0
diff changeset
83 ever have returned.
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 0
diff changeset
84
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 type Dynarr_at(d, i)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 [MACRO] Return the element at the specified index (no bounds checking
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 done on the index). The element itself is returned, not a pointer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 to it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 type *Dynarr_atp(d, i)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 [MACRO] Return a pointer to the element at the specified index (no
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 bounds checking done on the index). The pointer may not be valid
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 after an element is added to or removed from the array.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 Dynarr_reset(d)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 [MACRO] Reset the length of a dynamic array to 0.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 Dynarr_free(d)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 Destroy a dynamic array and the memory allocated to it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 Use the following global variable:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 Dynarr_min_size
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
104 Minimum allowable size for a dynamic array when it is resized. The
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
105 default is 32 and does not normally need to be changed.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 #include "lisp.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
112 int Dynarr_min_size = 1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113
424
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
114 static void
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
115 Dynarr_realloc (Dynarr *dy, int new_size)
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
116 {
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
117 if (DUMPEDP (dy->base))
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
118 {
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
119 void *new_base = malloc (new_size);
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
120 memcpy (new_base, dy->base, dy->max > new_size ? new_size : dy->max);
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
121 dy->base = new_base;
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
122 }
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
123 else
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
124 dy->base = xrealloc (dy->base, new_size);
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
125 }
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
126
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 void *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 Dynarr_newf (int elsize)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 {
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 0
diff changeset
130 Dynarr *d = xnew_and_zero (Dynarr);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 d->elsize = elsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 return d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 Dynarr_resize (void *d, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 int newsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 double multiplier;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 Dynarr *dy = (Dynarr *) d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 if (dy->max <= 8)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 multiplier = 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 multiplier = 1.5;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 for (newsize = dy->max; newsize < size;)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 185
diff changeset
149 newsize = max (Dynarr_min_size, (int) (multiplier * newsize));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 /* Don't do anything if the array is already big enough. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 if (newsize > dy->max)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 {
424
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
154 Dynarr_realloc (dy, newsize*dy->elsize);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 dy->max = newsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 /* Add a number of contiguous elements to the array starting at START. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 void
412
697ef44129c6 Import from CVS: tag r21-2-14
cvs
parents: 398
diff changeset
161 Dynarr_insert_many (void *d, CONST void *el, int len, int start)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 Dynarr *dy = (Dynarr *) d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 Dynarr_resize (dy, dy->cur+len);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 /* Silently adjust start to be valid. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 if (start > dy->cur)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 start = dy->cur;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 else if (start < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 start = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 if (start != dy->cur)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 memmove ((char *) dy->base + (start + len)*dy->elsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (char *) dy->base + start*dy->elsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (dy->cur - start)*dy->elsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 memcpy ((char *) dy->base + start*dy->elsize, el, len*dy->elsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 dy->cur += len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 if (dy->cur > dy->largest)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 dy->largest = dy->cur;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 Dynarr_delete_many (void *d, int start, int len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 Dynarr *dy = (Dynarr *) d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 assert (start >= 0 && len >= 0 && start + len <= dy->cur);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 memmove ((char *) dy->base + start*dy->elsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 (char *) dy->base + (start + len)*dy->elsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 (dy->cur - start - len)*dy->elsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 dy->cur -= len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 Dynarr_free (void *d)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 Dynarr *dy = (Dynarr *) d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201
424
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
202 if (dy->base && !DUMPEDP (dy->base))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 xfree (dy->base);
424
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
204 if(!DUMPEDP (dy))
11054d720c21 Import from CVS: tag r21-2-20
cvs
parents: 412
diff changeset
205 xfree (dy);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 #ifdef MEMORY_USAGE_STATS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 /* Return memory usage for Dynarr D. The returned value is the total
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 amount of bytes actually being used for the Dynarr, including all
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 overhead. The extra amount of space in the Dynarr that is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 allocated beyond what was requested is returned in DYNARR_OVERHEAD
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 in STATS. The extra amount of space that malloc() allocates beyond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 what was requested of it is returned in MALLOC_OVERHEAD in STATS.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 See the comment above the definition of this structure. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 185
diff changeset
218 size_t
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 Dynarr_memory_usage (void *d, struct overhead_stats *stats)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 {
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 185
diff changeset
221 size_t total = 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 Dynarr *dy = (Dynarr *) d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 /* We have to be a bit tricky here because not all of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 memory that malloc() will claim as "requested" was actually
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 requested. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 if (dy->base)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 {
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 185
diff changeset
230 size_t malloc_used = malloced_storage_size (dy->base,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 185
diff changeset
231 dy->elsize * dy->max, 0);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 /* #### This may or may not be correct. Some Dynarrs would
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 prefer that we use dy->cur instead of dy->largest here. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 int was_requested = dy->elsize * dy->largest;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 int dynarr_overhead = dy->elsize * (dy->max - dy->largest);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 total += malloc_used;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 stats->was_requested += was_requested;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 stats->dynarr_overhead += dynarr_overhead;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 /* And the remainder must be malloc overhead. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 stats->malloc_overhead +=
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 malloc_used - was_requested - dynarr_overhead;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 total += malloced_storage_size (d, sizeof (*dy), stats);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 return total;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 0
diff changeset
250 #endif /* MEMORY_USAGE_STATS */