comparison src/dynarr.c @ 412:697ef44129c6 r21-2-14

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