-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc-testing.h
131 lines (107 loc) · 3.97 KB
/
alloc-testing.h
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
/*
Copyright (c) 2005-2008, Simon Howard
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/**
* @file alloc-testing.h
*
* @brief Memory allocation testing framework.
*
* This file uses the preprocessor to redefine the standard C dynamic memory
* allocation functions for testing purposes. This allows checking that
* code under test correctly frees back all memory allocated, as well as
* the ability to impose artificial limits on allocation, to test that
* code correctly handles out-of-memory scenarios.
*/
#ifndef ALLOC_TESTING_H
#define ALLOC_TESTING_H
#include <stddef.h>
/* Don't redefine the functions in the alloc-testing.c, as we need the
* standard malloc/free functions. */
#ifndef ALLOC_TESTING_C
#undef malloc
#define malloc alloc_test_malloc
#undef free
#define free alloc_test_free
#undef realloc
#define realloc alloc_test_realloc
#undef calloc
#define calloc alloc_test_calloc
#undef strdup
#define strdup alloc_test_strdup
#endif
/**
* Allocate a block of memory.
*
* @param bytes Number of bytes to allocate.
* @return Pointer to the new block, or NULL if it was not
* possible to allocate the new block.
*/
void *alloc_test_malloc(size_t bytes);
/**
* Free a block of memory.
*
* @param ptr Pointer to the block to free.
*/
void alloc_test_free(void *ptr);
/**
* Reallocate a previously-allocated block to a new size, preserving
* contents.
*
* @param ptr Pointer to the existing block.
* @param bytes Size of the new block, in bytes.
* @return Pointer to the new block, or NULL if it was not
* possible to allocate the new block.
*/
void *alloc_test_realloc(void *ptr, size_t bytes);
/**
* Allocate a block of memory for an array of structures, initialising
* the contents to zero.
*
* @param nmemb Number of structures to allocate for.
* @param bytes Size of each structure, in bytes.
* @return Pointer to the new memory block for the array,
* or NULL if it was not possible to allocate the
* new block.
*/
void *alloc_test_calloc(size_t nmemb, size_t bytes);
/**
* Allocate a block of memory containing a copy of a string.
*
* @param string The string to copy.
* @return Pointer to the new memory block containing the
* copied string, or NULL if it was not possible
* to allocate the new block.
*/
char *alloc_test_strdup(const char *string);
/**
* Set an artificial limit on the amount of memory that can be
* allocated.
*
* @param alloc_count Number of allocations that are possible after
* this call. For example, if this has a value
* of 3, malloc() can be called successfully
* three times, but all allocation attempts
* after this will fail. If this has a negative
* value, the allocation limit is disabled.
*/
void alloc_test_set_limit(signed int alloc_count);
/**
* Get a count of the number of bytes currently allocated.
*
* @return The number of bytes currently allocated by
* the allocation system.
*/
size_t alloc_test_get_allocated(void);
#endif /* #ifndef ALLOC_TESTING_H */