/usr/include/nih/test_alloc.h is in libnih-dev 1.0.3-6ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | /* libnih
*
* Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
* Copyright © 2009 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, as
* published by the Free Software Foundation.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef NIH_TEST_ALLOC_H
#define NIH_TEST_ALLOC_H
#ifndef NIH_IN_TEST_H
# error "This header may only be included by <nih/test.h>"
#endif /* NIH_IN_TEST_H */
#include <errno.h>
#include <stddef.h>
#include <nih/alloc.h>
#include <nih/list.h>
/* When testing, we need to be able to override the malloc, realloc and free
* functions called by nih_alloc(). We can't use libc malloc hooks because
* valgrind doesn't implement them - and I like valgrind.
*/
extern void *(*__nih_malloc)(size_t size);
extern void *(*__nih_realloc)(void *ptr, size_t size);
extern void (*__nih_free)(void *ptr);
/**
* TEST_ALLOC_SIZE:
* @_ptr: allocated pointer,
* @_sz: expected size.
*
* Check that the pointer @_ptr was allocated with nih_alloc(), and has
* enough space for at least @_sz bytes.
**/
#define TEST_ALLOC_SIZE(_ptr, _sz) \
if ((_ptr) == NULL) { \
TEST_FAILED ("wrong value for block %s, got unexpected NULL", \
#_ptr); \
} else if (nih_alloc_size (_ptr) < (_sz)) \
TEST_FAILED ("wrong size of block %p (%s), expected %zu got %zu", \
(_ptr), #_ptr, (size_t)(_sz), \
nih_alloc_size (_ptr))
/**
* TEST_ALLOC_PARENT:
* @_ptr: allocated pointer,
* @_parent: expected parent.
*
* Check that the pointer @_ptr was allocated with nih_alloc() and has
* the other block @_parent as a parent. @_parent may be the special
* NULL parent.
**/
#define TEST_ALLOC_PARENT(_ptr, _parent) \
if ((_ptr) == NULL) { \
TEST_FAILED ("wrong value for block %s, got unexpected NULL", \
#_ptr); \
} else if (! nih_alloc_parent ((_ptr), (_parent))) \
TEST_FAILED ("wrong parent of block %p (%s), expected %p (%s)", \
(_ptr), #_ptr, (_parent), #_parent)
/**
* TEST_ALLOC_NOT_PARENT:
* @_ptr: allocated pointer,
* @_parent: expected non-parent.
*
* Check that the pointer @_ptr was allocated with nih_alloc() and does not
* have the other block @_parent as a parent. @_parent may be the special
* NULL parent.
**/
#define TEST_ALLOC_NOT_PARENT(_ptr, _parent) \
if ((_ptr) == NULL) { \
TEST_FAILED ("wrong value for block %s, got unexpected NULL", \
#_ptr); \
} else if (nih_alloc_parent ((_ptr), (_parent))) \
TEST_FAILED ("wrong parent of block %p (%s), got unexpected %p (%s)", \
(_ptr), #_ptr, (_parent), #_parent)
/**
* test_alloc_failed:
*
* Variable used by TEST_ALLOC_FAIL as the loop counter.
**/
static int test_alloc_failed = 0;
/**
* _test_alloc_count:
*
* Number of times malloc is called by the TEST_ALLOC_FAIL macro.
**/
static int _test_alloc_count = 0;
/**
* _test_alloc_call:
*
* Number of times malloc has been called during each cycle.
**/
static int _test_alloc_call = 0;
/**
* _test_realloc:
*
* realloc() wrapper used by TEST_ALLOC_FAIL.
*
* When test_alloc_failed is zero, it increments test_alloc_count and returns
* whatever realloc does. Otherwise it internally counts the number of times
* it is called, and if that matches test_alloc_failed, then it returns NULL.
**/
static inline __attribute__ ((used)) void *
_test_realloc (void *ptr,
size_t size)
{
if (! test_alloc_failed) {
_test_alloc_count++;
return realloc (ptr, size);
}
_test_alloc_call++;
if (test_alloc_failed == _test_alloc_call) {
errno = ENOMEM;
return NULL;
} else {
return realloc (ptr, size);
}
}
/**
* _test_malloc:
*
* malloc() wrapped used by TEST_ALLOC_FAIL.
*
* Calls _test_realloc with a NULL pointer.
**/
static inline __attribute__ ((used)) void *
_test_malloc (size_t size)
{
return _test_realloc (NULL, size);
}
/**
* TEST_ALLOC_FAIL:
*
* This macro expands to code that runs the following block repeatedly; the
* first time (when the special test_alloc_failed variable is zero) is
* used to determine how many allocations are performed by the following block;
* subsequent calls (when test_alloc_failed is a positive integer) mean that
* the test_alloc_failedth call to realloc has failed.
*
* This cannot be nested as it relies on setting an alternate allocator
* and sharing a global state.
**/
#define TEST_ALLOC_FAIL \
for (test_alloc_failed = -1, _test_alloc_count = 0; \
test_alloc_failed <= (_test_alloc_count + 1); \
test_alloc_failed++, _test_alloc_call = 0) \
if (test_alloc_failed < 0) { \
__nih_malloc = _test_malloc; \
__nih_realloc = _test_realloc; \
} else if (test_alloc_failed \
&& (test_alloc_failed == \
(_test_alloc_count + 1))) { \
__nih_malloc = malloc; \
__nih_realloc = realloc; \
} else
/**
* TEST_ALLOC_SAFE:
*
* This macro may be used within a TEST_ALLOC_FAIL block to guard the
* following block of code from failing allocation.
**/
#define TEST_ALLOC_SAFE \
for (int _test_alloc_safe = 0; _test_alloc_safe < 3; \
_test_alloc_safe++) \
if (_test_alloc_safe < 1) { \
__nih_malloc = malloc; \
__nih_realloc = realloc; \
} else if (_test_alloc_safe > 1) { \
__nih_malloc = _test_malloc; \
__nih_realloc = _test_realloc; \
} else
/**
* struct _test_free_tag:
* @entry: list entry,
* @ptr: tagged object.
*
* This structure is used to find out whether an nih_alloc() allocated object
* has been freed or not. It works by being allocated as a child of the
* tagged object, and added to a linked list of known tags. When freed,
* it is removed from the linked list.
**/
struct _test_free_tag {
NihList entry;
void *ptr;
};
/**
* _test_free_tags:
*
* Linked list of tagged blocks.
**/
static NihList _test_free_tags = { NULL, NULL };
/**
* _test_free_tag:
* @ptr: tagged object.
*
* Returns: TRUE if @ptr is tagged (not freed), FALSE if not (freed).
**/
static inline int
_test_free_tag (void *ptr)
{
NIH_LIST_FOREACH (&_test_free_tags, iter) {
struct _test_free_tag *tag = (struct _test_free_tag *)iter;
if (tag->ptr == ptr)
return TRUE;
}
return FALSE;
}
/**
* TEST_FREE_TAG:
* @_ptr: allocated object.
*
* This macro is used to tag an nih_alloc() allocated object to determine
* whether or not it is freed. It works by allocating a child object of
* @_ptr and storing it in a linked list.
*
* This can be tested with either the TEST_FREE or TEST_NOT_FREE macros as
* many times as you like.
**/
#define TEST_FREE_TAG(_ptr) \
do { \
void *(*_test__nih_malloc)(size_t size) = __nih_malloc; \
struct _test_free_tag *_test_tag; \
\
__nih_malloc = malloc; \
_test_tag = nih_new ((_ptr), struct _test_free_tag); \
assert ((_ptr) != NULL); \
__nih_malloc = _test__nih_malloc; \
\
nih_list_init (&_test_tag->entry); \
_test_tag->ptr = (_ptr); \
nih_alloc_set_destructor (_test_tag, nih_list_destroy); \
\
if (! _test_free_tags.next) \
nih_list_init (&_test_free_tags); \
nih_list_add (&_test_free_tags, &_test_tag->entry); \
} while (0)
/**
* TEST_FREE:
* @_ptr: allocated object.
*
* Check that the nih_alloc() allocated object @_ptr was freed as expected; it
* must have been first prepared by using TEST_FREE_TAG on it otherwise this
* will always fail.
**/
#define TEST_FREE(_ptr) \
if (_test_free_tag (_ptr)) \
TEST_FAILED ("block %p (%s) not freed as expected", \
(_ptr), #_ptr)
/**
* TEST_NOT_FREE:
* @_ptr: allocated block.
*
* Check that the nih_alloc() allocated object @_ptr was not freed
* unexpectedly; it must have been first prepared by using TEST_FREE_TAG
* on it otherwise this will always succeed.
**/
#define TEST_NOT_FREE(_ptr) \
if (! _test_free_tag (_ptr)) \
TEST_FAILED ("block %p (%s) freed unexpectedly", \
(_ptr), #_ptr)
#endif /* NIH_TEST_ALLOC_H */
|