-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.c
77 lines (68 loc) · 1.92 KB
/
array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdlib.h>
#include <string.h>
#include "array.h"
static int _expand_array(olio_array * a, uint16_t length)
{
int copy = 0;
a->base.allocated = (length + 0x1ff) & 0xfe00;
if (a->base.data == a->stack) {
copy = 1;
a->base.data = NULL;
}
a->base.data = (uint8_t *) realloc(a->base.data,
a->base.allocated * a->base.size);
if (a->base.data == NULL) return -1;
if (copy) memmove(a->base.data, a->stack, a->base.length * a->base.size);
return 0;
}
olio_array * olio_array_init(uint16_t size,
void * buffer, uint16_t length)
{
olio_array * a;
uint16_t l;
if (buffer == NULL) {
l = sizeof(olio_array) + length;
a = (olio_array *) malloc(l);
if (a == NULL) return NULL;
a->base.flags = 0x01;
} else {
l = length;
if (l < sizeof(olio_array)) return NULL;
a = (olio_array *) buffer;
a->base.flags = 0x00;
}
a->base.allocated = a->base.stack_allocated =
(l - ((void *) &a->stack[0] - (void *) a)) / size;
a->base.length = 0;
a->base.size = size;
a->base.data = a->stack;
return a;
}
void olio_array_free(olio_array * a)
{
if (a->base.data != a->stack && a->base.data != NULL) free(a->base.data);
if (a->base.flags & 0x01) free(a);
}
int olio_array_insert_unbounded(olio_array * a, uint16_t position,
const void * data, uint16_t length)
{
if (a->base.length + length > a->base.allocated) {
if (_expand_array(a, a->base.length + length))
return -1;
}
memmove(a->base.data + (length + position) * a->base.size,
a->base.data + position * a->base.size,
(a->base.length - position) * a->base.size);
memmove(a->base.data + position * a->base.size, data,
length * a->base.size);
a->base.length += length;
return 0;
}
void olio_array_remove_unbounded(olio_array * a,
uint16_t position, uint16_t length)
{
memmove(a->base.data + (a->base.length + position) * a->base.size,
a->base.data + (a->base.length + position + length) * a->base.size,
length * a->base.size);
a->base.length -= length;
}