int sampgdk_array_grow(struct sampgdk_array *a) { assert(a != NULL); if (a->size == 0) { return sampgdk_array_resize(a, 1); } return sampgdk_array_resize(a, (int)(a->size * 2)); }
int sampgdk_array_remove(struct sampgdk_array *a, int index, int count) { int move_count; assert(a != NULL); assert(index >= 0); assert(index < a->count); if (count <= 0 || count > a->count - index) { return -EINVAL; } move_count = a->count - index - count; if (move_count > 0) { memmove(get_element_ptr(a, index), get_element_ptr(a, index + count), move_count * a->elem_size); } a->count -= count; if (a->count <= a->size / 4) { return sampgdk_array_resize(a, a->size / 2); } return 0; }
int sampgdk_array_insert(struct sampgdk_array *a, int index, int count, void *elems) { int need_count; int move_count; assert(a != NULL); assert(elems != NULL); if (count <= 0) { return -EINVAL; } index = _sampgdk_array_normalize_index(a, index); assert(index <= a->count); need_count = a->count + count - a->size; move_count = a->count - index; if (need_count > 0) { int error; if ((error = sampgdk_array_resize(a, a->size + need_count)) < 0) { return error; } } if (move_count > 0) { memmove(_sampgdk_array_get_elem_ptr(a, index + count), _sampgdk_array_get_elem_ptr(a, index), move_count * a->elem_size); } a->count += count; memcpy(_sampgdk_array_get_elem_ptr(a, index), elems, count * a->elem_size); return index; }
int sampgdk_fakeamx_resize_heap(int cells) { int error; cell old_size; cell new_size; cell old_stk; cell new_stk; cell new_stp; assert(cells > 0); old_size = global.heap.size; new_size = cells; error = sampgdk_array_resize(&global.heap, new_size); sampgdk_array_pad(&global.heap); if (error < 0) { return error; } /* Update data pointers to point at the newly allocated heap. */ global.amxhdr.dat = (cell)global.heap.data - (cell)&global.amxhdr; global.amx.data = (unsigned char *)global.heap.data; old_stk = global.amx.stk; new_stk = global.amx.stk + (new_size - old_size) * sizeof(cell); new_stp = global.amx.stp + (new_size - old_size) * sizeof(cell); /* Shift stack contents. */ memmove((unsigned char *)global.heap.data + new_stk - STACK_SIZE, (unsigned char *)global.heap.data + old_stk - STACK_SIZE, STACK_SIZE); global.amx.stk = new_stk; global.amx.stp = new_stp; return 0; }
int sampgdk_array_shrink(struct sampgdk_array *a) { assert(a != NULL); return sampgdk_array_resize(a, a->count); }