annotate src/dynarr.c @ 272:c5d627a313b1 r21-0b34

Import from CVS: tag r21-0b34
author cvs
date Mon, 13 Aug 2007 10:28:48 +0200
parents 3d6bfa290dbd
children 8626e4521993
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
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 0
diff changeset
47 typdef 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)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 Insert LEN elements to the dynamic arrary starting at position
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 Minimum allowable size for a dynamic array when it is resized. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 default is 32 and does not normally need to be changed.
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 int Dynarr_min_size = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 void *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 Dynarr_newf (int elsize)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 {
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 0
diff changeset
117 Dynarr *d = xnew_and_zero (Dynarr);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 d->elsize = elsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 return d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 Dynarr_resize (void *d, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 int newsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 double multiplier;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 Dynarr *dy = (Dynarr *) d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 if (dy->max <= 8)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 multiplier = 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 multiplier = 1.5;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 for (newsize = dy->max; newsize < size;)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 185
diff changeset
136 newsize = max (Dynarr_min_size, (int) (multiplier * newsize));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 /* Don't do anything if the array is already big enough. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 if (newsize > dy->max)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 dy->base = xrealloc (dy->base, newsize*dy->elsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 dy->max = newsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 /* Add a number of contiguous elements to the array starting at START. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 Dynarr_insert_many (void *d, CONST void *el, int len, int start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 Dynarr *dy = (Dynarr *) d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 Dynarr_resize (dy, dy->cur+len);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 /* Silently adjust start to be valid. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 if (start > dy->cur)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 start = dy->cur;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 else if (start < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 start = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 if (start != dy->cur)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 memmove ((char *) dy->base + (start + len)*dy->elsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (char *) dy->base + start*dy->elsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (dy->cur - start)*dy->elsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 memcpy ((char *) dy->base + start*dy->elsize, el, len*dy->elsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 dy->cur += len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 if (dy->cur > dy->largest)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 dy->largest = dy->cur;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 Dynarr_delete_many (void *d, int start, int len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 Dynarr *dy = (Dynarr *) d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 assert (start >= 0 && len >= 0 && start + len <= dy->cur);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 memmove ((char *) dy->base + start*dy->elsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 (char *) dy->base + (start + len)*dy->elsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 (dy->cur - start - len)*dy->elsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 dy->cur -= len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 Dynarr_free (void *d)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 Dynarr *dy = (Dynarr *) d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 if (dy->base)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 xfree (dy->base);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 xfree (dy);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 #ifdef MEMORY_USAGE_STATS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 /* Return memory usage for Dynarr D. The returned value is the total
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 amount of bytes actually being used for the Dynarr, including all
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 overhead. The extra amount of space in the Dynarr that is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 allocated beyond what was requested is returned in DYNARR_OVERHEAD
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 in STATS. The extra amount of space that malloc() allocates beyond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 what was requested of it is returned in MALLOC_OVERHEAD in STATS.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 See the comment above the definition of this structure. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 185
diff changeset
204 size_t
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 Dynarr_memory_usage (void *d, struct overhead_stats *stats)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 {
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 185
diff changeset
207 size_t total = 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 Dynarr *dy = (Dynarr *) d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 /* We have to be a bit tricky here because not all of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 memory that malloc() will claim as "requested" was actually
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 requested. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 if (dy->base)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 {
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 185
diff changeset
216 size_t malloc_used = malloced_storage_size (dy->base,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 185
diff changeset
217 dy->elsize * dy->max, 0);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 /* #### This may or may not be correct. Some Dynarrs would
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 prefer that we use dy->cur instead of dy->largest here. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 int was_requested = dy->elsize * dy->largest;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 int dynarr_overhead = dy->elsize * (dy->max - dy->largest);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 total += malloc_used;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 stats->was_requested += was_requested;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 stats->dynarr_overhead += dynarr_overhead;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 /* And the remainder must be malloc overhead. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 stats->malloc_overhead +=
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 malloc_used - was_requested - dynarr_overhead;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 total += malloced_storage_size (d, sizeof (*dy), stats);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 return total;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235
185
3d6bfa290dbd Import from CVS: tag r20-3b19
cvs
parents: 0
diff changeset
236 #endif /* MEMORY_USAGE_STATS */