-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmem.h
51 lines (38 loc) · 1.58 KB
/
mem.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
/* Copyright (c) 2011 - Eric P. Mangold
* Copyright (c) 2011 - Peter Le Bek
*
* See LICENSE.txt for details.
*/
#ifndef _AMP_MEM_H
#define _AMP_MEM_H
#include <stdlib.h>
#include <stdbool.h>
#ifdef AMP_TEST_SUPPORT
typedef void * (*malloc_ptr)(size_t size);
/* Set by unit tests to specify a char to fill malloc'd memory with. */
char mem_check_char;
extern malloc_ptr the_real_malloc; /* pointer to the real malloc()
function */
/* If set to true then test_malloc() will begin to fail when
* allocations_until_failure <= 0. Otherwise test_malloc()
* will always succeed without decrementing allocations_until_failure. */
extern bool failure_mode_enabled;
/* The number of times remaining that test_malloc() may be called before
* it begins to return NULL. test_malloc() decrements this variable, and
* when it is <= 0 test_malloc() returns NULL without allocating memory. */
extern int allocations_until_failure;
/* Whenever test_malloc() returns NULL this variable is set to true. */
extern bool allocation_failure_occurred;
/* Allocate a region of memory and memset it with char `c'. Can also
* be rigged to fail (above) to test allocation failure error handling. */
extern void *test_malloc(size_t size, char c);
#define MALLOC(size) test_malloc(size, mem_check_char)
/* Set state of test_malloc() implementation */
void enable_malloc_failures(int times_until_failure);
void disable_malloc_failures();
#else
#define MALLOC(size) malloc(size)
#endif
#define NEW(p) ((p) = MALLOC((long)sizeof *(p)))
#define FREE(ptr) ((void)(free((ptr)), (ptr) = 0))
#endif