-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.c
50 lines (45 loc) · 1.01 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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "alloc.h"
#include "array.h"
array *array_init(uint32_t length, uint32_t item_size)
{
array *ar = calloc(1, sizeof(array));
if (ar == NULL) {
return NULL;
}
ar->items = calloc(length, item_size + sizeof(void*));
if (ar->items == NULL) {
free(ar);
return NULL;
}
ar->length = length;
ar->item_size = item_size;
return ar;
}
int array_set(array *ar, uint32_t index, void *item)
{
if (index >= ar->length) {
return -E_ARRAY_INDEX_OUT_OF_RANGE;
}
if(ar->items[index] != NULL) {
free(ar->items[index]);
}
ar->items[index] = calloc(1, ar->item_size);
if (ar->items[index] == NULL) {
return -E_ALLOC;
}
memcpy(ar->items[index], item, ar->item_size);
return 0;
}
void array_free(array *ar)
{
for (int i = ar->length - 1; i >= 0; i--) {
free(ar->items[i]);
}
free(ar->items);
ar->items = NULL;
free(ar);
}