src/array_list.c

changeset 1084
0bcd71d2615a
parent 1065
6eb7b54975ee
child 1089
865c84fef6b4
equal deleted inserted replaced
1083:cf54e413793c 1084:0bcd71d2615a
130 130
131 // determine size and capacity 131 // determine size and capacity
132 size_t oldcap; 132 size_t oldcap;
133 size_t oldsize; 133 size_t oldsize;
134 size_t max_size; 134 size_t max_size;
135 if (width == 0 || width == 8*sizeof(size_t)) { 135 if (width == 0 || width == sizeof(size_t)) {
136 oldcap = *(size_t*) capacity; 136 oldcap = *(size_t*) capacity;
137 oldsize = *(size_t*) size; 137 oldsize = *(size_t*) size;
138 max_size = SIZE_MAX; 138 max_size = SIZE_MAX;
139 } else if (width == 16) { 139 } else if (width == sizeof(uint16_t)) {
140 oldcap = *(uint16_t*) capacity; 140 oldcap = *(uint16_t*) capacity;
141 oldsize = *(uint16_t*) size; 141 oldsize = *(uint16_t*) size;
142 max_size = UINT16_MAX; 142 max_size = UINT16_MAX;
143 } else if (width == 8) { 143 } else if (width == sizeof(uint8_t)) {
144 oldcap = *(uint8_t*) capacity; 144 oldcap = *(uint8_t*) capacity;
145 oldsize = *(uint8_t*) size; 145 oldsize = *(uint8_t*) size;
146 max_size = UINT8_MAX; 146 max_size = UINT8_MAX;
147 } 147 }
148 #if CX_WORDSIZE == 64 148 #if CX_WORDSIZE == 64
149 else if (width == 32) { 149 else if (width == sizeof(uint32_t)) {
150 oldcap = *(uint32_t*) capacity; 150 oldcap = *(uint32_t*) capacity;
151 oldsize = *(uint32_t*) size; 151 oldsize = *(uint32_t*) size;
152 max_size = UINT32_MAX; 152 max_size = UINT32_MAX;
153 } 153 }
154 #endif 154 #endif
184 184
185 // store new pointer 185 // store new pointer
186 *array = newmem; 186 *array = newmem;
187 187
188 // store new capacity 188 // store new capacity
189 if (width == 0 || width == 8*sizeof(size_t)) { 189 if (width == 0 || width == sizeof(size_t)) {
190 *(size_t*) capacity = newcap; 190 *(size_t*) capacity = newcap;
191 } else if (width == 16) { 191 } else if (width == sizeof(uint16_t)) {
192 *(uint16_t*) capacity = (uint16_t) newcap; 192 *(uint16_t*) capacity = (uint16_t) newcap;
193 } else if (width == 8) { 193 } else if (width == sizeof(uint8_t)) {
194 *(uint8_t*) capacity = (uint8_t) newcap; 194 *(uint8_t*) capacity = (uint8_t) newcap;
195 } 195 }
196 #if CX_WORDSIZE == 64 196 #if CX_WORDSIZE == 64
197 else if (width == 32) { 197 else if (width == sizeof(uint32_t)) {
198 *(uint32_t*) capacity = (uint32_t) newcap; 198 *(uint32_t*) capacity = (uint32_t) newcap;
199 } 199 }
200 #endif 200 #endif
201 } 201 }
202 202
223 223
224 // determine size and capacity 224 // determine size and capacity
225 size_t oldcap; 225 size_t oldcap;
226 size_t oldsize; 226 size_t oldsize;
227 size_t max_size; 227 size_t max_size;
228 if (width == 0 || width == 8*sizeof(size_t)) { 228 if (width == 0 || width == sizeof(size_t)) {
229 oldcap = *(size_t*) capacity; 229 oldcap = *(size_t*) capacity;
230 oldsize = *(size_t*) size; 230 oldsize = *(size_t*) size;
231 max_size = SIZE_MAX; 231 max_size = SIZE_MAX;
232 } else if (width == 16) { 232 } else if (width == sizeof(uint16_t)) {
233 oldcap = *(uint16_t*) capacity; 233 oldcap = *(uint16_t*) capacity;
234 oldsize = *(uint16_t*) size; 234 oldsize = *(uint16_t*) size;
235 max_size = UINT16_MAX; 235 max_size = UINT16_MAX;
236 } else if (width == 8) { 236 } else if (width == sizeof(uint8_t)) {
237 oldcap = *(uint8_t*) capacity; 237 oldcap = *(uint8_t*) capacity;
238 oldsize = *(uint8_t*) size; 238 oldsize = *(uint8_t*) size;
239 max_size = UINT8_MAX; 239 max_size = UINT8_MAX;
240 } 240 }
241 #if CX_WORDSIZE == 64 241 #if CX_WORDSIZE == 64
242 else if (width == 32) { 242 else if (width == sizeof(uint32_t)) {
243 oldcap = *(uint32_t*) capacity; 243 oldcap = *(uint32_t*) capacity;
244 oldsize = *(uint32_t*) size; 244 oldsize = *(uint32_t*) size;
245 max_size = UINT32_MAX; 245 max_size = UINT32_MAX;
246 } 246 }
247 #endif 247 #endif
301 // note: no overflow check here, b/c we cannot get here w/o allocation 301 // note: no overflow check here, b/c we cannot get here w/o allocation
302 memmove(start, src, elem_count * elem_size); 302 memmove(start, src, elem_count * elem_size);
303 303
304 // if any of size or capacity changed, store them back 304 // if any of size or capacity changed, store them back
305 if (newsize != oldsize || newcap != oldcap) { 305 if (newsize != oldsize || newcap != oldcap) {
306 if (width == 0 || width == 8*sizeof(size_t)) { 306 if (width == 0 || width == sizeof(size_t)) {
307 *(size_t*) capacity = newcap; 307 *(size_t*) capacity = newcap;
308 *(size_t*) size = newsize; 308 *(size_t*) size = newsize;
309 } else if (width == 16) { 309 } else if (width == sizeof(uint16_t)) {
310 *(uint16_t*) capacity = (uint16_t) newcap; 310 *(uint16_t*) capacity = (uint16_t) newcap;
311 *(uint16_t*) size = (uint16_t) newsize; 311 *(uint16_t*) size = (uint16_t) newsize;
312 } else if (width == 8) { 312 } else if (width == sizeof(uint8_t)) {
313 *(uint8_t*) capacity = (uint8_t) newcap; 313 *(uint8_t*) capacity = (uint8_t) newcap;
314 *(uint8_t*) size = (uint8_t) newsize; 314 *(uint8_t*) size = (uint8_t) newsize;
315 } 315 }
316 #if CX_WORDSIZE == 64 316 #if CX_WORDSIZE == 64
317 else if (width == 32) { 317 else if (width == sizeof(uint32_t)) {
318 *(uint32_t*) capacity = (uint32_t) newcap; 318 *(uint32_t*) capacity = (uint32_t) newcap;
319 *(uint32_t*) size = (uint32_t) newsize; 319 *(uint32_t*) size = (uint32_t) newsize;
320 } 320 }
321 #endif 321 #endif
322 } 322 }

mercurial