summaryrefslogtreecommitdiff
path: root/src/bin/wrappers.c
blob: e1e6de748b16d4d7453457bd1edf9b77c1e1e54f (plain) (blame)
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
/* 
 * Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com>
 *
 * This file is part of aplwc.
 *
 * Aplwc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Aplwc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with aplwc.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "aplwc_repl.h"

static uint64_t malloc_count = 0;

static uint64_t
malloc_fail_after(void)
{
#if APLWC_BUILD_TYPE_RELEASE
	return UINT64_MAX;
#else
	static int setup;
	static uint64_t count, count_rec;

	if(!setup){
		setup = 1;
		count = getenv_ulong("APLWC_MALLOC_FAIL_AFTER", ULONG_MAX);
		count_rec = malloc_fail_after();
		assert(ALWAYS(count == count_rec));
	}

	return count;
#endif
}

#undef malloc
void *
wrapped_malloc(size_t size)
{
	if(malloc_count++ >= malloc_fail_after())
		return NULL;
	else
		return malloc(size);
}

#undef free
void
wrapped_free(void *ptr)
{
	free(ptr);
}