src/list.c

changeset 1633
fe24b68758bf
parent 1632
f74e4fc496a2
child 1635
4983b6a34996
equal deleted inserted replaced
1632:f74e4fc496a2 1633:fe24b68758bf
337 cxFreeDefault(tmp); 337 cxFreeDefault(tmp);
338 338
339 return 0; 339 return 0;
340 } 340 }
341 341
342 static int cx_list_ccmp_safe_memcmp(const void *a, const void *b, void *c) {
343 // it is not safe to store a pointer to the size in the list
344 // because the entire list structure might get reallocated
345 size_t elem_size = (size_t)(uintptr_t)c;
346 return memcmp(a, b, elem_size);
347 }
348
342 void cx_list_init( 349 void cx_list_init(
343 struct cx_list_s *list, 350 struct cx_list_s *list,
344 struct cx_list_class_s *cl, 351 struct cx_list_class_s *cl,
345 const struct cx_allocator_s *allocator, 352 const struct cx_allocator_s *allocator,
346 size_t elem_size 353 size_t elem_size
350 list->collection.size = 0; 357 list->collection.size = 0;
351 list->collection.sorted = false; // should be set by the implementation 358 list->collection.sorted = false; // should be set by the implementation
352 if (elem_size > 0) { 359 if (elem_size > 0) {
353 list->collection.elem_size = elem_size; 360 list->collection.elem_size = elem_size;
354 list->collection.simple_cmp = NULL; 361 list->collection.simple_cmp = NULL;
355 list->collection.advanced_cmp = cx_ccmp_memcmp; 362 list->collection.advanced_cmp = cx_list_ccmp_safe_memcmp;
356 list->collection.cmp_data = &list->collection.elem_size; 363 list->collection.cmp_data = (void*)(uintptr_t)list->collection.elem_size;
357 list->collection.store_pointer = false; 364 list->collection.store_pointer = false;
358 } else { 365 } else {
359 list->collection.elem_size = sizeof(void *); 366 list->collection.elem_size = sizeof(void *);
360 list->collection.simple_cmp = cx_cmp_ptr; 367 list->collection.simple_cmp = cx_cmp_ptr;
361 list->collection.advanced_cmp = NULL; 368 list->collection.advanced_cmp = NULL;

mercurial