annotate src/dynarr.c @ 142:1856695b1fa9 r20-2b5

Import from CVS: tag r20-2b5
author cvs
date Mon, 13 Aug 2007 09:33:18 +0200
parents 376386a54a3c
children 3d6bfa290dbd
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 struct mytype_dynarr
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);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 };
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
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 specified type. The return value is a void * and must be cast to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 proper dynamic array type.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 Dynarr_add(d, el)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 [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
61 to the element; the element itself is stored in the array, however.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 No function call is performed unless the array needs to be resized.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 Dynarr_add_many(d, base, len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 [MACRO] Add LEN elements to the end of the dynamic array. The elements
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 should be contiguous in memory, starting at BASE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 Dynarr_insert_many_at_start(d, base, len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 [MACRO] Append LEN elements to the beginning of the dynamic array.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 The elements should be contiguous in memory, starting at BASE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 Dynarr_insert_many(d, base, len, start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 Insert LEN elements to the dynamic arrary starting at position
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 START. The elements should be contiguous in memory, starting at BASE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 int Dynarr_length(d)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 [MACRO] Return the number of elements currently in a dynamic array.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 type Dynarr_at(d, i)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 [MACRO] Return the element at the specified index (no bounds checking
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 done on the index). The element itself is returned, not a pointer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 to it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 type *Dynarr_atp(d, i)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 [MACRO] Return a pointer to the element at the specified index (no
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 bounds checking done on the index). The pointer may not be valid
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 after an element is added to or removed from the array.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 Dynarr_reset(d)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 [MACRO] Reset the length of a dynamic array to 0.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 Dynarr_free(d)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 Destroy a dynamic array and the memory allocated to it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 Use the following global variable:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 Dynarr_min_size
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 Minimum allowable size for a dynamic array when it is resized. The
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 default is 32 and does not normally need to be changed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 #include "lisp.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 int Dynarr_min_size = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 void *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 Dynarr_newf (int elsize)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 Dynarr *d = (Dynarr *) xmalloc (sizeof (Dynarr));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 memset (d, 0, sizeof (*d));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 d->elsize = elsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 return d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 Dynarr_resize (void *d, int size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 int newsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 double multiplier;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 Dynarr *dy = (Dynarr *) d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 if (dy->max <= 8)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 multiplier = 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 multiplier = 1.5;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 for (newsize = dy->max; newsize < size;)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 newsize = max (Dynarr_min_size, multiplier * newsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 /* Don't do anything if the array is already big enough. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 if (newsize > dy->max)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 dy->base = xrealloc (dy->base, newsize*dy->elsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 dy->max = newsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 /* Add a number of contiguous elements to the array starting at START. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 Dynarr_insert_many (void *d, CONST void *el, int len, int start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 Dynarr *dy = (Dynarr *) d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 Dynarr_resize (dy, dy->cur+len);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 /* Silently adjust start to be valid. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 if (start > dy->cur)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 start = dy->cur;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 else if (start < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 start = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 if (start != dy->cur)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 memmove ((char *) dy->base + (start + len)*dy->elsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (char *) dy->base + start*dy->elsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (dy->cur - start)*dy->elsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 memcpy ((char *) dy->base + start*dy->elsize, el, len*dy->elsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 dy->cur += len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 if (dy->cur > dy->largest)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 dy->largest = dy->cur;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 Dynarr_delete_many (void *d, int start, int len)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 Dynarr *dy = (Dynarr *) d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 assert (start >= 0 && len >= 0 && start + len <= dy->cur);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 memmove ((char *) dy->base + start*dy->elsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (char *) dy->base + (start + len)*dy->elsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (dy->cur - start - len)*dy->elsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 dy->cur -= len;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 Dynarr_free (void *d)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 Dynarr *dy = (Dynarr *) d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 if (dy->base)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 xfree (dy->base);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 xfree (dy);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 #ifdef MEMORY_USAGE_STATS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 /* Return memory usage for Dynarr D. The returned value is the total
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 amount of bytes actually being used for the Dynarr, including all
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 overhead. The extra amount of space in the Dynarr that is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 allocated beyond what was requested is returned in DYNARR_OVERHEAD
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 in STATS. The extra amount of space that malloc() allocates beyond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 what was requested of it is returned in MALLOC_OVERHEAD in STATS.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 See the comment above the definition of this structure. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 int
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 Dynarr_memory_usage (void *d, struct overhead_stats *stats)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 int total = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 Dynarr *dy = (Dynarr *) d;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 /* We have to be a bit tricky here because not all of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 memory that malloc() will claim as "requested" was actually
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 requested. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 if (dy->base)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 int malloc_used = malloced_storage_size (dy->base,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 dy->elsize * dy->max, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 /* #### This may or may not be correct. Some Dynarrs would
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 prefer that we use dy->cur instead of dy->largest here. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 int was_requested = dy->elsize * dy->largest;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 int dynarr_overhead = dy->elsize * (dy->max - dy->largest);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 total += malloc_used;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 stats->was_requested += was_requested;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 stats->dynarr_overhead += dynarr_overhead;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 /* And the remainder must be malloc overhead. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 stats->malloc_overhead +=
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 malloc_used - was_requested - dynarr_overhead;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 total += malloced_storage_size (d, sizeof (*dy), stats);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 return total;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 #endif