| 43 size_t n; |
43 size_t n; |
| 44 if (cx_szmul(capacity, elem_size, &n)) { |
44 if (cx_szmul(capacity, elem_size, &n)) { |
| 45 errno = EOVERFLOW; |
45 errno = EOVERFLOW; |
| 46 return NULL; |
46 return NULL; |
| 47 } |
47 } |
| 48 return realloc(array, n); |
48 return cxRealloc(cxDefaultAllocator, array, n); |
| 49 } |
49 } |
| 50 |
50 |
| 51 CxArrayReallocator cx_array_default_reallocator_impl = { |
51 CxArrayReallocator cx_array_default_reallocator_impl = { |
| 52 cx_array_default_realloc, NULL, NULL, 0, 0 |
52 cx_array_default_realloc, NULL, NULL, 0, 0 |
| 53 }; |
53 }; |
| 570 char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE]; |
570 char sbo_mem[CX_ARRAY_SWAP_SBO_SIZE]; |
| 571 void *tmp; |
571 void *tmp; |
| 572 |
572 |
| 573 // decide if we can use the local buffer |
573 // decide if we can use the local buffer |
| 574 if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) { |
574 if (elem_size > CX_ARRAY_SWAP_SBO_SIZE) { |
| 575 tmp = malloc(elem_size); |
575 tmp = cxMalloc(cxDefaultAllocator, elem_size); |
| 576 // we don't want to enforce error handling |
576 // we don't want to enforce error handling |
| 577 if (tmp == NULL) abort(); |
577 if (tmp == NULL) abort(); |
| 578 } else { |
578 } else { |
| 579 tmp = sbo_mem; |
579 tmp = sbo_mem; |
| 580 } |
580 } |
| 589 memcpy(left, right, elem_size); |
589 memcpy(left, right, elem_size); |
| 590 memcpy(right, tmp, elem_size); |
590 memcpy(right, tmp, elem_size); |
| 591 |
591 |
| 592 // free dynamic memory, if it was needed |
592 // free dynamic memory, if it was needed |
| 593 if (tmp != sbo_mem) { |
593 if (tmp != sbo_mem) { |
| 594 free(tmp); |
594 cxFree(cxDefaultAllocator, tmp); |
| 595 } |
595 } |
| 596 } |
596 } |
| 597 |
597 |
| 598 // HIGH LEVEL ARRAY LIST FUNCTIONS |
598 // HIGH LEVEL ARRAY LIST FUNCTIONS |
| 599 |
599 |