-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec_alloc.c
52 lines (46 loc) · 1.6 KB
/
vec_alloc.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vector_alloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alischyn <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 16:07:41 by alischyn #+# #+# */
/* Updated: 2017/03/28 16:44:21 by alischyn ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "vec.h"
void vec_init(t_vec *vec)
{
vec->data = NULL;
vec->length = 0;
vec->capacity = 0;
}
void vec_realloc(t_vec *vec, int need)
{
int new_capacity;
void *new_data;
need += need % 64;
new_capacity = vec->capacity + need;
new_data = (void **)malloc(sizeof(void *) * new_capacity);
if (vec->length > 0)
__builtin_memcpy(new_data, vec->data, sizeof(void *) * vec->length);
if (vec->capacity > 0)
free(vec->data);
vec->data = new_data;
vec->capacity = new_capacity;
}
void vec_free(t_vec *vec, bool free_elems)
{
int i;
if (vec->length > 0 && free_elems)
{
i = 0;
while (i < vec->length)
free(vec->data[i++]);
}
if (vec->capacity > 0)
free(vec->data);
vec_init(vec);
}