comparison src/dynarr.c @ 4844:91b3d00e717f

Various cleanups for Dynarr code, from Unicode-internal ws dynarr.c: Add comment explaining Dynarr_largest() use. dynarr.c: In Dynarr_insert_many(), don't call Dynarr_resize() unless we actually need to resize, and note that an assert() that we are inserting at or below the current end could be wrong if code wants to access stuff between `len' and `largest'. dynarr.c: Don't just Dynarr_resize() to the right size; instead use Dynarr_reset() then Dynarr_add_many(), so that the 'len' and 'largest' and such get set properly. dynarr.c, faces.c, gutter.c, lisp.h, lread.c, lrecord.h, redisplay-output.c, redisplay.c: Rename Dynarr member 'cur' to 'len' since it's the length of the dynarr, not really a pointer to a "current insertion point". Use type_checking_assert() instead of just assert() in some places. Add additional assertions (Dynarr_verify*()) to check that we're being given positions within range. Use them in Dynarr_at, Dynarr_atp, etc. New Dynarr_atp_allow_end() for retrieving a pointer to a position that might be the element past the last one. New Dynarr_past_lastp() to retrieve a pointer to the position past the last one, using Dynarr_atp_allow_end(). Change code appropriately to use it. Rename Dynarr_end() to Dynarr_lastp() (pointer to the last element) for clarity, and change code appropriately to use it. Change code appropriately to use Dynarr_begin(). Rewrite Dynarr_add_many(). New version can accept a NULL pointer to mean "reserve space but don't put anything in it". Used by stack_like_malloc().
author Ben Wing <ben@xemacs.org>
date Wed, 13 Jan 2010 04:07:42 -0600
parents 229bd619740a
children 19a72041c5ed
comparison
equal deleted inserted replaced
4843:715b15990d0a 4844:91b3d00e717f
1 /* Support for dynamic arrays. 1 /* Support for dynamic arrays.
2 Copyright (C) 1993 Sun Microsystems, Inc. 2 Copyright (C) 1993 Sun Microsystems, Inc.
3 Copyright (C) 2002, 2003, 2004 Ben Wing. 3 Copyright (C) 2002, 2003, 2004, 2005 Ben Wing.
4 4
5 This file is part of XEmacs. 5 This file is part of XEmacs.
6 6
7 XEmacs is free software; you can redistribute it and/or modify it 7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the 8 under the terms of the GNU General Public License as published by the
96 int Dynarr_length(d) 96 int Dynarr_length(d)
97 [MACRO] Return the number of elements currently in a dynamic array. 97 [MACRO] Return the number of elements currently in a dynamic array.
98 98
99 int Dynarr_largest(d) 99 int Dynarr_largest(d)
100 [MACRO] Return the maximum value that Dynarr_length(d) would 100 [MACRO] Return the maximum value that Dynarr_length(d) would
101 ever have returned. 101 ever have returned. This is used esp. in the redisplay code,
102 which reuses dynarrs for performance reasons.
102 103
103 type Dynarr_at(d, i) 104 type Dynarr_at(d, i)
104 [MACRO] Return the element at the specified index (no bounds checking 105 [MACRO] Return the element at the specified index (no bounds checking
105 done on the index). The element itself is returned, not a pointer 106 done on the index). The element itself is returned, not a pointer
106 to it. 107 to it.
215 void 216 void
216 Dynarr_insert_many (void *d, const void *el, int len, int start) 217 Dynarr_insert_many (void *d, const void *el, int len, int start)
217 { 218 {
218 Dynarr *dy = (Dynarr *) Dynarr_verify (d); 219 Dynarr *dy = (Dynarr *) Dynarr_verify (d);
219 220
220 Dynarr_resize (dy, dy->cur+len); 221 if (dy->len + len > dy->max)
222 Dynarr_resize (dy, dy->len + len);
221 #if 0 223 #if 0
222 /* WTF? We should be catching these problems. */ 224 /* WTF? We should be catching these problems. */
223 /* Silently adjust start to be valid. */ 225 /* Silently adjust start to be valid. */
224 if (start > dy->cur) 226 if (start > dy->len)
225 start = dy->cur; 227 start = dy->len;
226 else if (start < 0) 228 else if (start < 0)
227 start = 0; 229 start = 0;
228 #else 230 #else
229 assert (start >= 0 && start <= dy->cur); 231 /* #### This could conceivably be wrong, if code wants to access stuff
232 between len and largest. */
233 type_checking_assert (start >= 0 && start <= dy->len);
230 #endif 234 #endif
231 235
232 if (start != dy->cur) 236 if (start != dy->len)
233 { 237 {
234 memmove ((char *) dy->base + (start + len)*dy->elsize, 238 memmove ((char *) dy->base + (start + len)*dy->elsize,
235 (char *) dy->base + start*dy->elsize, 239 (char *) dy->base + start*dy->elsize,
236 (dy->cur - start)*dy->elsize); 240 (dy->len - start)*dy->elsize);
237 } 241 }
238 if (el) 242 if (el)
239 memcpy ((char *) dy->base + start*dy->elsize, el, len*dy->elsize); 243 memcpy ((char *) dy->base + start*dy->elsize, el, len*dy->elsize);
240 dy->cur += len; 244 dy->len += len;
241 245
242 if (dy->cur > dy->largest) 246 if (dy->len > dy->largest)
243 dy->largest = dy->cur; 247 dy->largest = dy->len;
244 } 248 }
245 249
246 void 250 void
247 Dynarr_delete_many (void *d, int start, int len) 251 Dynarr_delete_many (void *d, int start, int len)
248 { 252 {
249 Dynarr *dy = (Dynarr *) Dynarr_verify (d); 253 Dynarr *dy = (Dynarr *) Dynarr_verify (d);
250 254
251 assert (start >= 0 && len >= 0 && start + len <= dy->cur); 255 type_checking_assert (start >= 0 && len >= 0 && start + len <= dy->len);
252 memmove ((char *) dy->base + start*dy->elsize, 256 memmove ((char *) dy->base + start*dy->elsize,
253 (char *) dy->base + (start + len)*dy->elsize, 257 (char *) dy->base + (start + len)*dy->elsize,
254 (dy->cur - start - len)*dy->elsize); 258 (dy->len - start - len)*dy->elsize);
255 dy->cur -= len; 259 dy->len -= len;
256 } 260 }
257 261
258 void 262 void
259 Dynarr_free (void *d) 263 Dynarr_free (void *d)
260 { 264 {
302 if (dy->base) 306 if (dy->base)
303 { 307 {
304 Bytecount malloc_used = malloced_storage_size (dy->base, 308 Bytecount malloc_used = malloced_storage_size (dy->base,
305 dy->elsize * dy->max, 0); 309 dy->elsize * dy->max, 0);
306 /* #### This may or may not be correct. Some Dynarrs would 310 /* #### This may or may not be correct. Some Dynarrs would
307 prefer that we use dy->cur instead of dy->largest here. */ 311 prefer that we use dy->len instead of dy->largest here. */
308 Bytecount was_requested = dy->elsize * dy->largest; 312 Bytecount was_requested = dy->elsize * dy->largest;
309 Bytecount dynarr_overhead = dy->elsize * (dy->max - dy->largest); 313 Bytecount dynarr_overhead = dy->elsize * (dy->max - dy->largest);
310 314
311 total += malloc_used; 315 total += malloc_used;
312 stats->was_requested += was_requested; 316 stats->was_requested += was_requested;
351 if (Dynarr_length (stack_like_free_list) > 0) 355 if (Dynarr_length (stack_like_free_list) > 0)
352 this_one = Dynarr_pop (stack_like_free_list); 356 this_one = Dynarr_pop (stack_like_free_list);
353 else 357 else
354 this_one = Dynarr_new (char); 358 this_one = Dynarr_new (char);
355 Dynarr_add (stack_like_in_use_list, this_one); 359 Dynarr_add (stack_like_in_use_list, this_one);
356 Dynarr_resize (this_one, size); 360 Dynarr_reset (this_one);
361 Dynarr_add_many (this_one, 0, size);
357 return Dynarr_atp (this_one, 0); 362 return Dynarr_atp (this_one, 0);
358 } 363 }
359 364
360 void 365 void
361 stack_like_free (void *val) 366 stack_like_free (void *val)