48 } |
48 } |
49 return cxReallocDefault(array, n); |
49 return cxReallocDefault(array, n); |
50 } |
50 } |
51 |
51 |
52 CxArrayReallocator cx_array_default_reallocator_impl = { |
52 CxArrayReallocator cx_array_default_reallocator_impl = { |
53 cx_array_default_realloc, NULL, NULL, 0, 0 |
53 cx_array_default_realloc, NULL, NULL |
54 }; |
54 }; |
55 |
55 |
56 CxArrayReallocator *cx_array_default_reallocator = &cx_array_default_reallocator_impl; |
56 CxArrayReallocator *cx_array_default_reallocator = &cx_array_default_reallocator_impl; |
57 |
57 |
58 // Stack-aware array reallocator |
58 // Stack-aware array reallocator |
70 errno = EOVERFLOW; |
70 errno = EOVERFLOW; |
71 return NULL; |
71 return NULL; |
72 } |
72 } |
73 |
73 |
74 // retrieve the pointer to the actual allocator |
74 // retrieve the pointer to the actual allocator |
75 const CxAllocator *al = alloc->ptr1; |
75 const CxAllocator *al = alloc->allocator; |
76 |
76 |
77 // check if the array is still located on the stack |
77 // check if the array is still located on the stack |
78 void *newmem; |
78 void *newmem; |
79 if (array == alloc->ptr2) { |
79 if (array == alloc->stack_ptr) { |
80 newmem = cxMalloc(al, n); |
80 newmem = cxMalloc(al, n); |
81 if (newmem != NULL && array != NULL) { |
81 if (newmem != NULL && array != NULL) { |
82 memcpy(newmem, array, old_capacity*elem_size); |
82 memcpy(newmem, array, old_capacity*elem_size); |
83 } |
83 } |
84 } else { |
84 } else { |
87 return newmem; |
87 return newmem; |
88 } |
88 } |
89 |
89 |
90 struct cx_array_reallocator_s cx_array_reallocator( |
90 struct cx_array_reallocator_s cx_array_reallocator( |
91 const struct cx_allocator_s *allocator, |
91 const struct cx_allocator_s *allocator, |
92 const void *stackmem |
92 const void *stack_ptr |
93 ) { |
93 ) { |
94 if (allocator == NULL) { |
94 if (allocator == NULL) { |
95 allocator = cxDefaultAllocator; |
95 allocator = cxDefaultAllocator; |
96 } |
96 } |
97 return (struct cx_array_reallocator_s) { |
97 return (struct cx_array_reallocator_s) { |
98 cx_array_advanced_realloc, |
98 cx_array_advanced_realloc, |
99 (void*) allocator, (void*) stackmem, |
99 allocator, stack_ptr, |
100 0, 0 |
|
101 }; |
100 }; |
102 } |
101 } |
103 |
102 |
104 // LOW LEVEL ARRAY LIST FUNCTIONS |
103 // LOW LEVEL ARRAY LIST FUNCTIONS |
105 |
104 |