Mercurial > hg > xemacs-beta
comparison src/dynarr.c @ 5118:e0db3c197671 ben-lisp-object
merge up to latest default branch, doesn't compile yet
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Sat, 26 Dec 2009 21:18:49 -0600 |
parents | 229bd619740a |
children | d1247f3cc363 |
comparison
equal
deleted
inserted
replaced
5117:3742ea8250b5 | 5118:e0db3c197671 |
---|---|
127 #include "lisp.h" | 127 #include "lisp.h" |
128 | 128 |
129 static int Dynarr_min_size = 8; | 129 static int Dynarr_min_size = 8; |
130 | 130 |
131 static void | 131 static void |
132 Dynarr_realloc (Dynarr *dy, Bytecount new_size) | 132 Dynarr_realloc (Dynarr *dy, int new_size) |
133 { | 133 { |
134 if (DUMPEDP (dy->base)) | 134 if (DUMPEDP (dy->base)) |
135 { | 135 { |
136 void *new_base = malloc (new_size); | 136 void *new_base = malloc (new_size * dy->elsize); |
137 memcpy (new_base, dy->base, dy->max > new_size ? dy->max : new_size); | 137 memcpy (new_base, dy->base, |
138 (dy->max < new_size ? dy->max : new_size) * dy->elsize); | |
138 dy->base = new_base; | 139 dy->base = new_base; |
139 } | 140 } |
140 else | 141 else |
141 dy->base = xrealloc (dy->base, new_size); | 142 dy->base = xrealloc (dy->base, new_size * dy->elsize); |
142 } | 143 } |
143 | 144 |
144 void * | 145 void * |
145 Dynarr_newf (int elsize) | 146 Dynarr_newf (Bytecount elsize) |
146 { | 147 { |
147 Dynarr *d = xnew_and_zero (Dynarr); | 148 Dynarr *d = xnew_and_zero (Dynarr); |
148 d->elsize = elsize; | 149 d->elsize = elsize; |
149 | 150 |
150 return d; | 151 return d; |
151 } | 152 } |
153 | |
154 #ifdef NEW_GC | |
155 DEFINE_DUMPABLE_INTERNAL_LISP_OBJECT ("dynarr", dynarr, | |
156 0, 0, | |
157 Dynarr); | |
158 | |
159 static void | |
160 Dynarr_lisp_realloc (Dynarr *dy, Elemcount new_size) | |
161 { | |
162 void *new_base = | |
163 XPNTR (alloc_sized_lrecord_array (dy->elsize, new_size, dy->lisp_imp)); | |
164 if (dy->base) | |
165 memcpy (new_base, dy->base, | |
166 (dy->max < new_size ? dy->max : new_size) * dy->elsize); | |
167 dy->base = new_base; | |
168 } | |
169 | |
170 void * | |
171 Dynarr_lisp_newf (Bytecount elsize, | |
172 const struct lrecord_implementation *dynarr_imp, | |
173 const struct lrecord_implementation *imp) | |
174 { | |
175 Dynarr *d = (Dynarr *) XPNTR (ALLOC_LISP_OBJECT (dynarr)); | |
176 d->elsize = elsize; | |
177 d->lisp_imp = imp; | |
178 | |
179 return d; | |
180 } | |
181 #endif /* not NEW_GC */ | |
152 | 182 |
153 void | 183 void |
154 Dynarr_resize (void *d, Elemcount size) | 184 Dynarr_resize (void *d, Elemcount size) |
155 { | 185 { |
156 int newsize; | 186 int newsize; |
166 newsize = max (Dynarr_min_size, (int) (multiplier * newsize)); | 196 newsize = max (Dynarr_min_size, (int) (multiplier * newsize)); |
167 | 197 |
168 /* Don't do anything if the array is already big enough. */ | 198 /* Don't do anything if the array is already big enough. */ |
169 if (newsize > dy->max) | 199 if (newsize > dy->max) |
170 { | 200 { |
171 Dynarr_realloc (dy, newsize*dy->elsize); | 201 #ifdef NEW_GC |
202 if (dy->lisp_imp) | |
203 Dynarr_lisp_realloc (dy, newsize); | |
204 else | |
205 Dynarr_realloc (dy, newsize); | |
206 #else /* not NEW_GC */ | |
207 Dynarr_realloc (dy, newsize); | |
208 #endif /* not NEW_GC */ | |
172 dy->max = newsize; | 209 dy->max = newsize; |
173 } | 210 } |
174 } | 211 } |
175 | 212 |
176 /* Add a number of contiguous elements to the array starting at START. */ | 213 /* Add a number of contiguous elements to the array starting at START. */ |
220 void | 257 void |
221 Dynarr_free (void *d) | 258 Dynarr_free (void *d) |
222 { | 259 { |
223 Dynarr *dy = (Dynarr *) d; | 260 Dynarr *dy = (Dynarr *) d; |
224 | 261 |
262 #ifdef NEW_GC | |
263 if (dy->base && !DUMPEDP (dy->base)) | |
264 { | |
265 if (!dy->lisp_imp) | |
266 xfree (dy->base, void *); | |
267 } | |
268 if(!DUMPEDP (dy)) | |
269 { | |
270 if (!dy->lisp_imp) | |
271 xfree (dy, Dynarr *); | |
272 } | |
273 #else /* not NEW_GC */ | |
225 if (dy->base && !DUMPEDP (dy->base)) | 274 if (dy->base && !DUMPEDP (dy->base)) |
226 xfree (dy->base, void *); | 275 xfree (dy->base, void *); |
227 if(!DUMPEDP (dy)) | 276 if(!DUMPEDP (dy)) |
228 xfree (dy, Dynarr *); | 277 xfree (dy, Dynarr *); |
278 #endif /* not NEW_GC */ | |
229 } | 279 } |
230 | 280 |
231 #ifdef MEMORY_USAGE_STATS | 281 #ifdef MEMORY_USAGE_STATS |
232 | 282 |
233 /* Return memory usage for Dynarr D. The returned value is the total | 283 /* Return memory usage for Dynarr D. The returned value is the total |