-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.c
More file actions
173 lines (135 loc) · 3.52 KB
/
Copy patharray.c
File metadata and controls
173 lines (135 loc) · 3.52 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include<stdlib.h>
#include<stdint.h>
#include<string.h>
#include "dtypes/array.h"
typedef unsigned char uchar;
typedef struct __array_struct{
char oom;
uint32_t element_size;
uint32_t capacity;
uint32_t used;
void* (*malloc)(size_t);
void* (*realloc)(void* , size_t);
void (*free)(void* );
uchar* elements;
}array_t;
// Creates an new "custom" array.
// Returns null upon failure.
array_t* array_custom(
uint32_t element_size,
void* (*xmalloc)(size_t),
void* (*xrealloc)(void* , size_t),
void (*xfree)(void*)
)
{
uint32_t capacity=512;
uint32_t mem_required=capacity*element_size;
uchar* elements=NULL;
array_t* array=NULL;
elements=xmalloc(mem_required);
if(elements==NULL) goto array_new_failed;
array=xmalloc(sizeof(array_t));
if(array==NULL) goto array_new_failed;
array->elements=elements;
array->used=0;
array->element_size=element_size;
array->capacity=capacity;
array->oom=0;
array->malloc=xmalloc;
array->realloc=xrealloc;
array->free=xfree;
return array;
array_new_failed:
if(array!=NULL) xfree(array);
if(elements!=NULL) xfree(elements);
return NULL;
}
// Wrapper of array_custom for ease of use.
//
array_t* array_new(uint32_t element_size){
return array_custom(
element_size,
malloc,
realloc,
free
);
}
// Grows the size of array.
int array_grow(array_t* array){
uint32_t old_cap=array->capacity;
uint32_t new_capacity=old_cap*2;
uint32_t mem_required=new_capacity*array->element_size;
uchar* new_mem=array->realloc(array->elements,mem_required);
if(new_mem==NULL){
array->oom=1;
return 1;
}
array->elements=new_mem;
array->capacity=new_capacity;
return 0;
}
// Returns the siize of array.
uint32_t array_getsize(array_t* array){
return array->used;
}
// Appends an item at the end of array.
int array_put(array_t* array,void* obj){
if (array->used>=array->capacity){
if(array_grow(array)!=0)
return 1;
}
uint32_t ret_point=array->used*array->element_size;
uchar* mem_pos=array->elements+ret_point;
memcpy(mem_pos,obj,array->element_size);
array->used++;
return 0;
}
// Copies "element_size" bytes to array
// and returns 0 on success and a non zero number on error.
int array_get(array_t* array,uint32_t pos,void* obj){
if(pos>=array->used){
return 1;
}
uint32_t cur_pos=pos*array->element_size;
uchar* cursor=array->elements+cur_pos;
memcpy(obj,cursor,array->element_size);
return 0;
}
// Returns a direct pointer on success or NULL on failure.
// Which is faster. However you shouldn't keep that pointer ownership.
// Or you should gurantee that as long as you own it, the array won't be modified.
// It may become a dangling pointer after modifications.
//
// Useful for faster iterations
void* array_at(array_t* array, uint32_t pos){
if(pos>=array->used)
return NULL;
uint32_t cur_pos=pos*array->element_size;
return array->elements + cur_pos;
}
// Clears the array within no time.
int array_clear(array_t* array){
array->used=0;
return 0;
}
// Pops an element from the last of array and copies it to obj.
int array_pop(array_t* array,void* obj){
if(array->used==0)
return 1;
array_get(array,array->used-1,obj);
array->used--;
return 0;
}
// Returns a non zero number if the last operation failed because
// of OOM (Out of Memory) conditions.
int array_oom(array_t* array){
return array->oom;
}
// Destroys the array completely, and releases resources aqquired by it.
// You should'nt use that array after
// this function is called on it.
int array_destroy(array_t* array){
array->free(array->elements);
array->free(array);
return 0;
}