byte_array.h
Raw
1#ifndef CUTILS_BYTE_ARRAY_H
2#define CUTILS_BYTE_ARRAY_H
3#include <stdlib.h>
4#include <string.h>
5#include <errno.h>
6#include <cutils/common.h>
7
8#define BYTEARRAY_DEFAULT_SIZE 4
9
10typedef struct Bytearray
11{
12 byte* items;
13 size_t capacity;
14 size_t length;
15 size_t element_size;
16} Bytearray;
17
18#define new_bytearray(element_size) bytearray_with_capacity(BYTEARRAY_DEFAULT_SIZE, element_size)
19Bytearray* bytearray_with_capacity(size_t capacity, size_t element_size);
20void delete_bytearray(Bytearray* bytearray, void(*rmv_el) (void*));
21
22void* bytearray_at(const Bytearray* bytearray, size_t index);
23#define bytearray_pop(bytearray, retptr) bytearray_pop_at(bytearray, bytearray->length-1, retptr)
24void* bytearray_pop_at(Bytearray* bytearray, size_t index, void* retptr);
25
26#define bytearray_push(bytearray, item) bytearray_insert(bytearray, bytearray->length, item)
27bool bytearray_insert(Bytearray* bytearray, size_t index, const void* item);
28
29void bytearray_remove(Bytearray* bytearray, size_t index, void (*rmv)(void*));
30
31bool bytearray_adjust_size(Bytearray* bytearray, size_t size);
32bool bytearray_shrink(Bytearray* bytearray);
33HEDLEY_NON_NULL(3)
34size_t* bytearray_find(const Bytearray* haystack, const void* needle, int (*cmp)(const void*, const void*));
35
36
37#endif /* CUTILS_BYTE_ARRAY_H */
38
39