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
|
/* Lion's Standard (LS) type-safe ANSI C vector.
*
* Version: 1.0
* Website: https://libls.org
* Repo: https://github.com/libls/vec
* SPDX-License-Identifier: MIT
*
* ==== TABLE OF CONTENTS ====
*
* 1. DESCRIPTION
* 2. HOW TO USE
* 3. LICENSE
*
* ==== 1. DESCRIPTION ====
*
* A minimal, terse, generic (macro code generated) header-only library in ANSI
* C, which implements a vector (dynamically sized array).
*
* The implementation uses standard malloc/realloc for memory management and
* automatically grows the capacity as needed. The memory allocator is
* configurable.
*
* ==== 2. HOW TO USE ====
*
* Dynamically sized, type-safe vector.
*
* Use LS_VEC_INLINE to generate a static inline version of the library.
* This is the "default" behavior.
*
* If you need a declaration and implementation separately, use
* LS_VEC_DECL and make sure to call it with the same arguments as
* LS_VEC_IMPL. Put LS_VEC_DECL in a header, and LS_VEC_IMPL in exactly
* ONE source file.
*
* Simple example:
*
* LS_VEC_INLINE(int, int_vector)
*
* // somewhere in the same file
* int_vector vec;
* int_vector_init(&vec);
* int_vector_push(&vec, 42);
* // use vec.data, vec.size, etc.
* int_vector_clear(&vec);
*
* Alternative example with decl and impl split:
*
* // In your header file:
* LS_VEC_DECL(int, int_vector)
*
* // In one source file:
* LS_VEC_IMPL(int, int_vector)
*
* // Usage in your code:
* int_vector vec;
* int_vector_init(&vec);
* if (!int_vector_push(&vec, 42)) {
* // handle allocation failure
* }
* // access elements via vec.data[i]
* int_vector_clear(&vec);
*
* You can configure a custom memory allocator by defining the macros LS_REALLOC
* and LS_FREE globally. These are the only allocation functions required, and
* they are expected to behave exactly as the standard requires. For example,
* LS_FREE(NULL) must be valid, LS_REALLOC can fail, LS_REALLOC with NULL will
* act like malloc, etc.
*
* ==== 3. LICENSE ====
*
* This file is provided under the MIT license. For commercial support and
* maintenance, feel free to use the e-mail below to contact the author(s).
*
* The MIT License (MIT)
*
* Copyright (c) 2026 Lion Kortlepel <libls@kortlepel.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifndef LS_REALLOC
#define LS_REALLOC realloc
#endif
#ifndef LS_FREE
#define LS_FREE free
#endif
#define LS_VEC_DECL(T, name) \
typedef struct name { \
size_t size; \
size_t capacity; \
T* data; \
} name; \
void name##_init(name* vec); \
void name##_clear(name* vec); \
int name##_reserve(name* vec, size_t count); \
int name##_push(name* vec, T value);
#define LS_VEC_IMPL(T, name) _ls_VEC_IMPL_DETAIL(T, name, )
#define LS_VEC_INLINE(T, name) \
typedef struct name { \
size_t size; \
size_t capacity; \
T* data; \
} name; \
_ls_VEC_IMPL_DETAIL(T, name, static)
#define _ls_VEC_IMPL_DETAIL(T, name, specifier) \
specifier void name##_init(name* vec) { memset(vec, 0, sizeof(*vec)); } \
specifier void name##_clear(name* vec) { \
if (vec->data) { \
LS_FREE(vec->data); \
vec->data = NULL; \
} \
vec->size = 0; \
vec->capacity = 0; \
} \
specifier int name##_reserve(name* vec, size_t count) { \
if (vec->capacity < count) { \
T* new_data; \
size_t total; \
if (count == 0) { \
vec->capacity = 5; \
} else { \
size_t new_cap = (size_t)((float)vec->capacity * 1.6f + 1.0f); \
vec->capacity = new_cap > count ? new_cap : count; \
} \
total = vec->capacity * sizeof(T); \
if (vec->capacity != 0 && total / vec->capacity != sizeof(T)) \
return 0; /* integer overflow */ \
new_data = (T*)LS_REALLOC(vec->data, total); \
if (!new_data) \
return 0; \
vec->data = new_data; \
} \
return 1; \
} \
specifier int name##_push(name* vec, T value) { \
if (!name##_reserve(vec, vec->size + 1)) { \
return 0; \
} \
vec->data[vec->size++] = value; \
return 1; \
}
|