blob: 3a5c5156a19b5d293bff00809238953684247b44 (
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
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
|
#define TINY_ALLOCATOR_IMPLEMENTATION
#include "base/allocator.h"
#include <stdint.h>
#include <stdlib.h>
static size_t fail_index = SIZE_MAX;
static size_t calls;
static size_t outstanding;
static int
should_fail(void)
{
return calls++ == fail_index;
}
void *
tiny_alloc(size_t size)
{
void *result;
if (should_fail())
{
return NULL;
}
result = malloc(size);
if (result != NULL)
{
outstanding++;
}
return result;
}
void *
tiny_calloc(size_t count, size_t size)
{
void *result;
if ((size != 0 && count > SIZE_MAX / size) || should_fail())
{
return NULL;
}
result = calloc(count, size);
if (result != NULL)
{
outstanding++;
}
return result;
}
void *
tiny_realloc(void *memory, size_t size)
{
void *result;
if (size == 0)
{
tiny_free(memory);
return NULL;
}
if (should_fail())
{
return NULL;
}
result = realloc(memory, size);
if (result != NULL && memory == NULL)
{
outstanding++;
}
return result;
}
void
tiny_free(void *memory)
{
if (memory != NULL)
{
free(memory);
if (outstanding > 0)
{
outstanding--;
}
}
}
void
tiny_allocator_reset(void)
{
fail_index = SIZE_MAX;
calls = 0;
}
void
tiny_allocator_fail_after(size_t allocation_index)
{
fail_index = allocation_index;
calls = 0;
}
size_t
tiny_allocator_call_count(void)
{
return calls;
}
size_t
tiny_allocator_outstanding(void)
{
return outstanding;
}
|