comparison src/dynarr.c @ 398:74fd4e045ea6 r21-2-29

Import from CVS: tag r21-2-29
author cvs
date Mon, 13 Aug 2007 11:13:30 +0200
parents 8626e4521993
children 697ef44129c6
comparison
equal deleted inserted replaced
397:f4aeb21a5bad 398:74fd4e045ea6
99 Destroy a dynamic array and the memory allocated to it. 99 Destroy a dynamic array and the memory allocated to it.
100 100
101 Use the following global variable: 101 Use the following global variable:
102 102
103 Dynarr_min_size 103 Dynarr_min_size
104 Minimum allowable size for a dynamic array when it is resized. The 104 Minimum allowable size for a dynamic array when it is resized.
105 default is 32 and does not normally need to be changed.
106 105
107 */ 106 */
108 107
109 #include <config.h> 108 #include <config.h>
110 #include "lisp.h" 109 #include "lisp.h"
111 110
112 int Dynarr_min_size = 1; 111 static int Dynarr_min_size = 8;
112
113 static void
114 Dynarr_realloc (Dynarr *dy, int new_size)
115 {
116 if (DUMPEDP (dy->base))
117 {
118 void *new_base = malloc (new_size);
119 memcpy (new_base, dy->base, dy->max > new_size ? new_size : dy->max);
120 dy->base = new_base;
121 }
122 else
123 dy->base = xrealloc (dy->base, new_size);
124 }
113 125
114 void * 126 void *
115 Dynarr_newf (int elsize) 127 Dynarr_newf (int elsize)
116 { 128 {
117 Dynarr *d = xnew_and_zero (Dynarr); 129 Dynarr *d = xnew_and_zero (Dynarr);
136 newsize = max (Dynarr_min_size, (int) (multiplier * newsize)); 148 newsize = max (Dynarr_min_size, (int) (multiplier * newsize));
137 149
138 /* Don't do anything if the array is already big enough. */ 150 /* Don't do anything if the array is already big enough. */
139 if (newsize > dy->max) 151 if (newsize > dy->max)
140 { 152 {
141 dy->base = xrealloc (dy->base, newsize*dy->elsize); 153 Dynarr_realloc (dy, newsize*dy->elsize);
142 dy->max = newsize; 154 dy->max = newsize;
143 } 155 }
144 } 156 }
145 157
146 /* Add a number of contiguous elements to the array starting at START. */ 158 /* Add a number of contiguous elements to the array starting at START. */
147 void 159 void
148 Dynarr_insert_many (void *d, CONST void *el, int len, int start) 160 Dynarr_insert_many (void *d, const void *el, int len, int start)
149 { 161 {
150 Dynarr *dy = (Dynarr *) d; 162 Dynarr *dy = (Dynarr *) d;
151 163
152 Dynarr_resize (dy, dy->cur+len); 164 Dynarr_resize (dy, dy->cur+len);
153 /* Silently adjust start to be valid. */ 165 /* Silently adjust start to be valid. */
184 void 196 void
185 Dynarr_free (void *d) 197 Dynarr_free (void *d)
186 { 198 {
187 Dynarr *dy = (Dynarr *) d; 199 Dynarr *dy = (Dynarr *) d;
188 200
189 if (dy->base) 201 if (dy->base && !DUMPEDP (dy->base))
190 xfree (dy->base); 202 xfree (dy->base);
191 xfree (dy); 203 if(!DUMPEDP (dy))
204 xfree (dy);
192 } 205 }
193 206
194 #ifdef MEMORY_USAGE_STATS 207 #ifdef MEMORY_USAGE_STATS
195 208
196 /* Return memory usage for Dynarr D. The returned value is the total 209 /* Return memory usage for Dynarr D. The returned value is the total