src/array_list.c

changeset 1656
68a03cf89e15
parent 1655
745ae507ee14
child 1657
c6a0381ab9b7
equal deleted inserted replaced
1655:745ae507ee14 1656:68a03cf89e15
99 size_t elem_size, size_t index, const void *other, size_t n) { 99 size_t elem_size, size_t index, const void *other, size_t n) {
100 // out of bounds and special case check 100 // out of bounds and special case check
101 if (index > array->size) return -1; 101 if (index > array->size) return -1;
102 if (n == 0) return 0; 102 if (n == 0) return 0;
103 103
104 // calculate required capacity
105 size_t req_capacity = array->size + n;
106 if (req_capacity <= array->size) {
107 errno = EOVERFLOW;
108 return -1;
109 }
110
104 // guarantee enough capacity 111 // guarantee enough capacity
105 if (array->capacity < array->size + n) { 112 if (array->capacity < req_capacity) {
106 const size_t new_capacity = cx_array_grow_capacity(array->capacity,array->size + n); 113 const size_t new_capacity = cx_array_grow_capacity(array->capacity,req_capacity);
107 if (cxReallocateArray(allocator, &array->data, new_capacity, elem_size)) { 114 if (cxReallocateArray(allocator, &array->data, new_capacity, elem_size)) {
108 return -1; // LCOV_EXCL_LINE 115 return -1; // LCOV_EXCL_LINE
109 } 116 }
110 array->capacity = new_capacity; 117 array->capacity = new_capacity;
111 } 118 }

mercurial