summaryrefslogtreecommitdiff
path: root/toxcore/list.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-06-21 22:22:30 -0400
committerirungentoo <irungentoo@gmail.com>2014-06-21 22:22:30 -0400
commite6699f60eff70075f62d062470f5fec3bd56f497 (patch)
tree7b43782af7333ba50b02da431c7ffe54da4a82dc /toxcore/list.c
parentf3c09d1854adc4b9b7873216b6b5837f76575bba (diff)
Fixed possible issues with list introduced in the last commit.
Diffstat (limited to 'toxcore/list.c')
-rw-r--r--toxcore/list.c30
1 files changed, 18 insertions, 12 deletions
diff --git a/toxcore/list.c b/toxcore/list.c
index b380f0e7..301e56f8 100644
--- a/toxcore/list.c
+++ b/toxcore/list.c
@@ -140,14 +140,16 @@ int bs_list_init(BS_LIST *list, uint32_t element_size, uint32_t initial_capacity
140 //set initial values 140 //set initial values
141 list->n = 0; 141 list->n = 0;
142 list->element_size = element_size; 142 list->element_size = element_size;
143 if (initial_capacity == 0) { 143 list->capacity = 0;
144 list->data = NULL; 144 list->data = NULL;
145 list->ids = NULL; 145 list->ids = NULL;
146 } else { 146
147 if (initial_capacity != 0) {
147 if (!resize(list, initial_capacity)) { 148 if (!resize(list, initial_capacity)) {
148 return 0; 149 return 0;
149 } 150 }
150 } 151 }
152
151 list->capacity = initial_capacity; 153 list->capacity = initial_capacity;
152 154
153 return 1; 155 return 1;
@@ -188,15 +190,18 @@ int bs_list_add(BS_LIST *list, const void *data, int id)
188 //increase the size of the arrays if needed 190 //increase the size of the arrays if needed
189 if (list->n == list->capacity) { 191 if (list->n == list->capacity) {
190 // 1.5 * n + 1 192 // 1.5 * n + 1
191 const uint32_t new_capacity = list->n + list->n/2 + 1; 193 const uint32_t new_capacity = list->n + list->n / 2 + 1;
194
192 if (!resize(list, new_capacity)) { 195 if (!resize(list, new_capacity)) {
193 return 0; 196 return 0;
194 } 197 }
198
195 list->capacity = new_capacity; 199 list->capacity = new_capacity;
196 } 200 }
197 201
198 //insert data to element array 202 //insert data to element array
199 memmove(list->data + (i + 1) * list->element_size, list->data + i * list->element_size, (list->n - i) * list->element_size); 203 memmove(list->data + (i + 1) * list->element_size, list->data + i * list->element_size,
204 (list->n - i) * list->element_size);
200 memcpy(list->data + i * list->element_size, data, list->element_size); 205 memcpy(list->data + i * list->element_size, data, list->element_size);
201 206
202 //insert id to id array 207 //insert id to id array
@@ -223,17 +228,18 @@ int bs_list_remove(BS_LIST *list, const void *data, int id)
223 } 228 }
224 229
225 //decrease the size of the arrays if needed 230 //decrease the size of the arrays if needed
226 if (list->n < list->capacity/2) { 231 if (list->n < list->capacity / 2) {
227 const uint32_t new_capacity = list->capacity/2; 232 const uint32_t new_capacity = list->capacity / 2;
228 if (!resize(list, new_capacity)) { 233
229 return 0; 234 if (resize(list, new_capacity)) {
235 list->capacity = new_capacity;
230 } 236 }
231 list->capacity = new_capacity;
232 } 237 }
233 238
234 list->n--; 239 list->n--;
235 240
236 memmove(list->data + i * list->element_size, list->data + (i + 1) * list->element_size, (list->n - i) * list->element_size); 241 memmove(list->data + i * list->element_size, list->data + (i + 1) * list->element_size,
242 (list->n - i) * list->element_size);
237 memmove(&list->ids[i], &list->ids[i + 1], (list->n - i) * sizeof(int)); 243 memmove(&list->ids[i], &list->ids[i + 1], (list->n - i) * sizeof(int));
238 244
239 return 1; 245 return 1;