-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcvec.c
More file actions
40 lines (32 loc) · 931 Bytes
/
Copy pathcvec.c
File metadata and controls
40 lines (32 loc) · 931 Bytes
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
#include "cvec.h"
void vec_alloc( void **vector, int n, size_t type_size ){
size_t sz = (n * type_size) + sizeof(vec_t);
void *data = SDL_malloc( sz );
SDL_memset( data, 0, sz );
vec_t *meta = (vec_t *)data;
meta->allocated = n;
meta->used = n;
*vector = meta + 1;
}
void vec_grow(void **vector, size_t more, size_t type_size) {
vec_t *meta = vec_meta(*vector);
size_t count = 0;
void *data = NULL;
if (*vector) {
count = 2 * meta->allocated + more;
data = SDL_realloc(meta, type_size * count + sizeof *meta);
} else {
count = more + 1;
data = SDL_malloc(type_size * count + sizeof *meta);
((vec_t *)data)->used = 0;
}
meta = (vec_t *)data;
meta->allocated = count;
SDL_memset( data + sizeof(vec_t) + (meta->used * type_size), 0,
(meta->allocated - meta->used) * type_size );
*vector = meta + 1;
}
void vec_delete(void *vector) {
if( vector == NULL ) return;
SDL_free(vec_meta(vector));
}