70 lines
4 KiB
C
70 lines
4 KiB
C
#ifndef INCLUDE_WAYLANDCLIENT_DYNARRAY_H_
|
|
#define INCLUDE_WAYLANDCLIENT_DYNARRAY_H_
|
|
|
|
#include "allocator.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
#define dyn_array_append(array, item) \
|
|
do { \
|
|
if ((array)->count >= (array)->capacity) { \
|
|
(array)->capacity += 10; \
|
|
if ((array)->mem == NULL) { \
|
|
(array)->items = realloc((array)->items, \
|
|
(array)->capacity * sizeof(*(array)->items)); \
|
|
} else { \
|
|
void *temp = (array)->items; \
|
|
(array)->items = mem_malloc( \
|
|
(array)->mem, (array)->capacity * sizeof(*(array)->items)); \
|
|
memcpy((array)->items, temp, \
|
|
((array)->count) * sizeof(*(array)->items)); \
|
|
mem_free((array)->mem, temp); \
|
|
} \
|
|
} \
|
|
(array)->items[(array)->count++] = (item); \
|
|
} while (0)
|
|
|
|
#define dyn_array_remove(array, index) \
|
|
do { \
|
|
(array)->items[(index)] = (array)->items[(array)->count - 1]; \
|
|
memset(&(array)->items[(array)->count], 0, sizeof((array)->items[0])); \
|
|
(array)->count--; \
|
|
} while (0)
|
|
|
|
#define dyn_array_destroy(array) \
|
|
if ((array)->mem == NULL) { \
|
|
free((array)->items); \
|
|
} else { \
|
|
mem_free((array)->mem, (array)->items); \
|
|
}
|
|
|
|
#define dyn_array_reset(array) \
|
|
do { \
|
|
(array)->capacity = 10; \
|
|
(array)->count = 0; \
|
|
if ((array)->mem == NULL) { \
|
|
(array)->items = realloc((array)->items, \
|
|
(array)->capacity * sizeof(*(array)->items)); \
|
|
} else { \
|
|
mem_free((array)->mem, (array)->items); \
|
|
(array)->items = mem_malloc((array)->mem, (array)->capacity * \
|
|
sizeof(*(array)->items)); \
|
|
} \
|
|
} while (0)
|
|
|
|
#define dyn_array_define($name, $type) \
|
|
struct $name { \
|
|
int count; \
|
|
int capacity; \
|
|
$type *items; \
|
|
struct Mem *mem; \
|
|
}
|
|
|
|
dyn_array_define(da_uint32_t, uint32_t);
|
|
|
|
void *dyn_array_create();
|
|
void *dyn_array_create_mem(struct Mem *mem);
|
|
void dyn_array_create_inplace(void *array);
|
|
void dyn_array_create_inplace_mem(void *array, struct Mem *mem);
|
|
|
|
#endif // INCLUDE_WAYLANDCLIENT_DYNARRAY_H_
|