hbmap: fix iterator truncation when size_t < 32bit
[rofl0r-agsutils.git] / List.c
blob5a13ab1c43debd751fd05ca5046a2219c3ac8ecc
1 #include "List.h"
2 #include <string.h>
3 #include <stdlib.h>
5 void List_init(List *l, size_t itemsize) {
6 memset(l, 0, sizeof *l);
7 mem_init(&l->mem);
8 l->itemsize = itemsize;
11 void List_free(List *l) {
12 mem_free(&l->mem);
13 List_init(l, 0);
16 int List_add(List *l, void* item) {
17 int ret = mem_write(&l->mem, l->count * l->itemsize, item, l->itemsize);
18 if(ret) l->count++;
19 return ret;
22 int List_get(List *l, size_t index, void* item) {
23 if(index >= l->count) return 0;
24 void* src = mem_getptr(&l->mem, index * l->itemsize, l->itemsize);
25 if(!src) return 0;
26 memcpy(item, src, l->itemsize);
27 return 1;
30 void* List_getptr(List *l, size_t index) {
31 return mem_getptr(&l->mem, index * l->itemsize, l->itemsize);
34 void List_sort(List *l, int(*compar)(const void *, const void *)) {
35 qsort(mem_getptr(&l->mem, 0, l->itemsize * l->count), l->count, l->itemsize, compar);