Voxia OS
v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
vector.h
Go to the documentation of this file.
1
#ifndef __LIBK__VECTOR_H_
2
#define __LIBK__VECTOR_H_
3
4
// C Only Vector
5
6
#include <
type.h
>
7
#include <
memory/kalloc.h
>
8
9
#define VECTOR_MINIMUM_ITEM 5
10
11
#define vector(T) struct vector_##T
12
#define define_vector(T) \
13
vector(T) { \
14
T* data; \
15
size_t size; \
16
size_t capacity; \
17
size_t alloc_size; \
18
}
19
/* fungsi dasar */
20
#define vector_init(v) \
21
do { \
22
(v)->alloc_size = sizeof(typeof(*(v)->data)); \
23
(v)->data = (typeof(*(v)->data)*) kalloc(VECTOR_MINIMUM_ITEM \
24
* (v)->alloc_size); \
25
(v)->capacity = VECTOR_MINIMUM_ITEM; \
26
(v)->size = 0; \
27
} while (0)
28
29
#define vector_expand_capacity(v) \
30
do { \
31
size_t new_capacity = (v)->capacity ? (v)->capacity * 2 : 4; \
32
typeof((v)->data) new_data = (typeof((v)->data)) kalloc( \
33
new_capacity * (v)->alloc_size); \
34
\
35
if ((v)->data) { \
36
memcopy(new_data, (v)->data, \
37
(v)->capacity*(v)->alloc_size); \
38
kfree((v)->data, (v)->capacity*(v)->alloc_size); \
39
} \
40
\
41
(v)->data = new_data; \
42
(v)->capacity = new_capacity; \
43
} while (0)
44
45
#define vector_push_back(v, val) \
46
do { \
47
if ((v)->size >= (v)->capacity) \
48
vector_expand_capacity((v)); \
49
(v)->data[(v)->size++] = (val); \
50
} while (0)
51
52
#define vector_destroy(v) \
53
do { \
54
kfree((v)->data, (v)->capacity*(v)->alloc_size); \
55
} while (0)
56
57
#define vector_clear(v) \
58
do { \
59
(v)->size = 0; \
60
} while (0)
61
62
#define vector_pop_back(v) \
63
do { \
64
if ((v)->size == 0) \
65
return NULL; \
66
(typeof((v)->data)) * ret = \
67
(typeof((v)->data)) ((uint8_t*) (v)->data \
68
+ ((v)->size - 1) \
69
* (v)->alloc_size); \
70
(v)->size--; \
71
return ret; \
72
} while (0)
73
74
typedef
struct
{
75
void
*
data
;
// Pointer to the array of elements
76
size_t
size
;
77
size_t
capacity
;
// Maximum number of elements the vector can hold
78
// before resizing
79
size_t
alloc_size
;
// Size of each element in bytes
80
}
vector_T
;
81
82
#endif
// __LIBK__VECTOR_H_
kalloc.h
vector_T
Definition
vector.h:74
vector_T::capacity
size_t capacity
Definition
vector.h:77
vector_T::size
size_t size
Definition
vector.h:76
vector_T::alloc_size
size_t alloc_size
Definition
vector.h:79
vector_T::data
void * data
Definition
vector.h:75
type.h
include
vector.h
Generated on Sat May 30 2026 11:09:57 for Voxia OS by
1.13.2