Merge pull request #1 from atsampson/master
[calfbox.git] / stm.h
blob1d517d0e1cf1ce43a225493ba2821df4f8d8229c
1 #ifndef CBOX_STM_H
2 #define CBOX_STM_H
4 #include <malloc.h>
5 #include <string.h>
7 static inline void **stm_array_clone_insert(void **old_array, int old_count, int index, void *data)
9 size_t ps = sizeof(void *);
10 if (index == -1)
11 index = old_count;
12 void **new_array = malloc(ps * (old_count + 1));
13 memcpy(&new_array[0], &old_array[0], ps * index);
14 new_array[index] = data;
15 if (index != old_count)
16 memcpy(&new_array[index + 1], &old_array[index], ps * (old_count - index));
17 return new_array;
20 static inline void **stm_array_clone_remove(void **old_array, int old_count, int index)
22 size_t ps = sizeof(void *);
23 if (old_count == 1)
24 return NULL;
25 void **new_array = malloc(ps * (old_count - 1));
26 memcpy(&new_array[0], &old_array[0], ps * index);
27 memcpy(&new_array[index], &old_array[index + 1], ps * (old_count - index - 1));
28 return new_array;
31 #define STM_ARRAY_FREE(old_array, count, destructor) \
32 for (int i = 0; i < (count); i++) \
33 destructor((old_array)[i]); \
34 free(old_array);
36 #define STM_ARRAY_FREE_OBJS(old_array, count) \
37 do { \
38 for (int i = 0; i < (count); i++) \
39 cbox_object_destroy(&(old_array)[i]->_obj_hdr); \
40 free(old_array); \
41 } while(0)
43 #endif