aboutsummaryrefslogtreecommitdiff
path: root/noleak.c
blob: eb676ca1dfec98df8f9da51619636edb88568da9 (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
#include "noleak.h"
#include <stdlib.h>

static void *___noleak_data = NULL;
static size_t ___noleak_capacity = 0;
static size_t ___noleak_used = 0;

int noleak_init(size_t capacity)
{
	___noleak_data = calloc(1, capacity);
	if (___noleak_data == NULL)
		return 1;
	___noleak_capacity = capacity;
	return 0;
}

void noleak_deinit(void)
{
	if (___noleak_data != NULL) {
		free(___noleak_data);
		___noleak_data = NULL;
	}
}

void *noleak_alloc(size_t size)
{
	if (___noleak_data == NULL)
		return NULL;
	if (___noleak_used + size >= ___noleak_capacity)
		return NULL;
	void *const ptr = ___noleak_data + ___noleak_used;
	___noleak_used += size;
	return ptr;
}