1
0
Fork
You've already forked toolbox.h
0
No description
  • C 87.9%
  • Shell 6.2%
  • Awk 5.9%
2026年04月07日 17:56:22 +02:00
example init 2026年04月07日 17:56:22 +02:00
include init 2026年04月07日 17:56:22 +02:00
test init 2026年04月07日 17:56:22 +02:00
.clang-format init 2026年04月07日 17:56:22 +02:00
.gitignore init 2026年04月07日 17:56:22 +02:00
docs.awk init 2026年04月07日 17:56:22 +02:00
make init 2026年04月07日 17:56:22 +02:00
readme.md init 2026年04月07日 17:56:22 +02:00

Toolbox.h

A simple C utility library providing the following utils:

  • vec.h a typesafe vector data structure

vec.h

Examples

vec-basic

#include "vec.h"
char *
strdup(char const *s)
{
 size_t l = strlen(s) + 1;
 char *x = malloc(sizeof(char) * l);
 return memcpy(x, s, l);
}
int
main(void)
{
 tb_vec_of(char *) strings = NULL;
 // to preallocate 10 char*:
 // tb_vec_alloc(strings, 10);

 tb_vec_append(strings, strdup("ghi"));
 tb_vec_append(strings, strdup("jkl"));
 
 tb_vec_prepend(strings, strdup("def"));
 tb_vec_prepend(strings, strdup("abc"));
 char *s = tb_vec_pop_back(strings);
 puts(s);
 free(s);
 s = tb_vec_pop_front(strings, s);
 puts(s);
 free(s);
 puts(tb_vec_back(strings));
 puts(tb_vec_front(strings));
 tb_vec_free_free(strings, free);
 return 0;
}

vec-custom-allocator

#include <stdlib.h>
void *my_calloc(size_t n, size_t size);
void *my_realloc(void *p, size_t size);
void my_free(void *p);
#define tb_calloc my_calloc
#define tb_realloc my_realloc
#define tb_free my_free
#include "vec.h"
void *
my_calloc(size_t n, size_t size)
{
 printf("calloc called with n: %zu and size: %zu\n", n, size);
 return calloc(n, size);
}
void *
my_realloc(void *p, size_t size)
{
 printf("realloc called with p: %p and size: %zu\n", p, size);
 return realloc(p, size);
}
void
my_free(void *p)
{
 printf("free called with p: %p\n", p);
 return free(p);
}
int
main(void)
{
 tb_vec_of(char *) strings = NULL;
 tb_vec_append(strings, "abc");
 tb_vec_append(strings, "def");
 tb_vec_append(strings, "ghi");
 for (size_t i = 0, j = tb_vec_len(strings); i < j; ++i) {
 printf("%zu: %s\n", i, strings[i]);
 }
 tb_vec_alloc(strings, 500);
 tb_vec_append(strings, "jkl");
 tb_vec_append(strings, "mno");
 tb_vec_append(strings, "pqr");
 for (size_t i = 0, j = tb_vec_len(strings); i < j; ++i) {
 printf("%zu: %s\n", i, strings[i]);
 }
 tb_vec_free(strings);
 return 0;
}

Docs

tb_calloc

Function to be used for the calloc calls.

#ifndef tb_calloc
#define tb_calloc calloc
#endif

tb_realloc

Function to be used for the realloc calls.

#ifndef tb_realloc
#define tb_realloc realloc
#endif

tb_free

Function to be used for the free calls.

#ifndef tb_free
#define tb_free free
#endif

tb_vec_of

A generic fat pointer of T

0: size_t length 1: size_t capacity 2...: T.

#define tb_vec_of(T)

tb_vec_len

Get the length of the given vec.

#define tb_vec_len(v)

tb_vec_cap

Get the capacity of the given vec.

#define tb_vec_cap(v)

tb_vec_head

Get the head of the given vec.

#define tb_vec_head(v)

tb_vec_free

Free the given vec.

#define tb_vec_free(v)

tb_vec_free_free

Call the given free on every elements of the given vec, then call tb_vec_free on the vec itself.

#define tb_vec_free_free(v, free)

tb_vec_alloc_raw

Allocate or expand the given vec by n*size, the memory is not zeroed. If the expanding fails, the original memory is not freed.

Returns 0 on success, otherwise -1 and set errno.

static inline int tb_vec_alloc_raw(tb_vec_of(void) * vec, size_t n, size_t size)

tb_vec_alloc

Nice wrapper around tb_vec_alloc_raw.

#define tb_vec_alloc(vec, n)

tb_vec_alloc_maybe

Allocate more space if s elems cannot fit in the current vec's capacity.

Returns 0 on success, otherwise -1 and set errno.

#define tb_vec_alloc_maybe(vec, s)

tb_vec_append

Append the given elem in the given vec, auto-growing it if needed.

Returns 0 on success, otherwise -1 and set errno.

#define tb_vec_append(vec, elem)

tb_vec_prepend

Prepend the given elem in the given vec, auto-growing it if needed.

Returns 0 on success, otherwise -1 and set errno.

#define tb_vec_prepend(vec, elem)

tb_vec_pop_back

Removes and returns the last element of the given vec.

If vec is NULL or empty, returns 0.

#define tb_vec_pop_back(vec)

tb_vec_pop_front

Removes and returns the first element of the given vec.

If vec is NULL or empty, returns 0.

#define tb_vec_pop_front(vec, var)

tb_vec_empty

Returns 1 if the given vec is NULL or empty, otherwise returns 0.

#define tb_vec_empty(vec)

tb_vec_clear

Clears the given empty, doesn't 0 the cleared element, returns 0.

#define tb_vec_clear(vec)

tb_vec_front

Returns the front element of the given vec.

If vec is NULL or empty, returns 0.

#define tb_vec_front(vec)

tb_vec_back

Returns the back element of the given vec.

If vec is NULL or empty, returns 0.

#define tb_vec_back(vec)

tb_vec_drop_front

Drop the first element of the given vec and returns 0.

#define tb_vec_drop_front(vec)

tb_vec_drop_back

Drop the last element of the given vec and returns 0.

#define tb_vec_drop_back(vec)

tb_vec_at

Returns the element at position n in the given vec.

If vec is NULL or the given n position is outside of bounds returns 0.

#define tb_vec_at(vec, n)

Thanks to

  • Pyz