blob: 38e3b316da2e7675f265051f11c72d500cc4ca57 (
plain)
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
|
#include "dyn_arr.h"
#include <stdio.h>
#include <assert.h>
void *_ty_list_create(size_t type_size, int init_cap)
{
init_cap = init_cap < TY_LIST_MIN_CAP
? TY_LIST_MIN_CAP
: init_cap;
int *arr = (int*)ty_alloc(type_size * init_cap + sizeof(int) * 2);
arr[0] = 0;
arr[1] = init_cap;
return (void*)(arr + 2);
}
void *_ty_list_append_slot(size_t type_size, void **arr)
{
int *cap = ty_list_cap_ptr(*arr);
int *len = ty_list_len_ptr(*arr);
(*len)++;
if (*len > *cap) {
*cap *= TY_LIST_GROW_RATE;
void* base = ty_list_base(*arr);
base = ty_realloc(base, (type_size * *cap) + sizeof(int) * 2);
assert(base != NULL); // Just to handle the case
*arr = (void*)((int*)base + 2);
len = ty_list_len_ptr(*arr);
}
return (char*)*arr + ((*len - 1) * type_size);
}
void ty_list_free(void *arr)
{
void *base = ty_list_base(arr);
ty_free(base);
}
|