-
Notifications
You must be signed in to change notification settings - Fork 8
/
array.c
62 lines (52 loc) · 1.29 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
#include "array.h"
#include <string.h>
int array_find(uint8_t byte, CData array, int ofs, int end)
{
void * data = array->data + ofs;
void * result = memchr(data, (int) byte, (size_t) (end - ofs));
if (result == NULL)
{
return -1;
}
else
{
return (result - data);
}
}
void array_realloc(int size, CData array)
{
array->data = realloc(array->data, (size_t) size);
}
CData array_alloc(int size)
{
return cdata_allocate((size_t) size, free);
}
uint8_t array_peek(int ofs, CData array)
{
return ((uint8_t *) array->data)[ofs];
}
void array_poke(int ofs, uint8_t byte, CData array)
{
((uint8_t *) array->data)[ofs] = byte;
}
int array_peek_int(int ofs, CData array)
{
return *((int *) (array->data + ofs));
}
void array_poke_int(int ofs, int val, CData array)
{
*((int *) (array->data + ofs)) = val;
}
void array_copy(CData src, int src_ofs, CData dst, int dst_ofs, int count)
{
// memmove rather than memcpy in case the areas overlap
memmove(dst->data + dst_ofs, src->data + src_ofs, count);
}
void array_fill(int ofs, int count, uint8_t byte, CData array)
{
memset(array->data + ofs, byte, count);
}
int array_compare(CData l, int lofs, CData r, int rofs, int count)
{
return memcmp(l->data + lofs, r->data + rofs, count);
}