comparison 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
comparison
equal deleted inserted replaced
423:28d9c139be4c 424:11054d720c21
109 #include <config.h> 109 #include <config.h>
110 #include "lisp.h" 110 #include "lisp.h"
111 111
112 int Dynarr_min_size = 1; 112 int Dynarr_min_size = 1;
113 113
114 static void
115 Dynarr_realloc (Dynarr *dy, int new_size)
116 {
117 if (DUMPEDP (dy->base))
118 {
119 void *new_base = malloc (new_size);
120 memcpy (new_base, dy->base, dy->max > new_size ? new_size : dy->max);
121 dy->base = new_base;
122 }
123 else
124 dy->base = xrealloc (dy->base, new_size);
125 }
126
114 void * 127 void *
115 Dynarr_newf (int elsize) 128 Dynarr_newf (int elsize)
116 { 129 {
117 Dynarr *d = xnew_and_zero (Dynarr); 130 Dynarr *d = xnew_and_zero (Dynarr);
118 d->elsize = elsize; 131 d->elsize = elsize;
136 newsize = max (Dynarr_min_size, (int) (multiplier * newsize)); 149 newsize = max (Dynarr_min_size, (int) (multiplier * newsize));
137 150
138 /* Don't do anything if the array is already big enough. */ 151 /* Don't do anything if the array is already big enough. */
139 if (newsize > dy->max) 152 if (newsize > dy->max)
140 { 153 {
141 dy->base = xrealloc (dy->base, newsize*dy->elsize); 154 Dynarr_realloc (dy, newsize*dy->elsize);
142 dy->max = newsize; 155 dy->max = newsize;
143 } 156 }
144 } 157 }
145 158
146 /* Add a number of contiguous elements to the array starting at START. */ 159 /* Add a number of contiguous elements to the array starting at START. */
184 void 197 void
185 Dynarr_free (void *d) 198 Dynarr_free (void *d)
186 { 199 {
187 Dynarr *dy = (Dynarr *) d; 200 Dynarr *dy = (Dynarr *) d;
188 201
189 if (dy->base) 202 if (dy->base && !DUMPEDP (dy->base))
190 xfree (dy->base); 203 xfree (dy->base);
191 xfree (dy); 204 if(!DUMPEDP (dy))
205 xfree (dy);
192 } 206 }
193 207
194 #ifdef MEMORY_USAGE_STATS 208 #ifdef MEMORY_USAGE_STATS
195 209
196 /* Return memory usage for Dynarr D. The returned value is the total 210 /* Return memory usage for Dynarr D. The returned value is the total