-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmem.c
64 lines (50 loc) · 1.26 KB
/
mem.c
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
/* Copyright (c) 2011 - Eric P. Mangold
* Copyright (c) 2011 - Peter Le Bek
*
* See LICENSE.txt for details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "amp.h"
#include "amp_internal.h"
#include "mem.h"
#ifdef AMP_TEST_SUPPORT
/* concrete definitions of variables declared "extern" in mem.h */
malloc_ptr the_real_malloc;
bool failure_mode_enabled;
int allocations_until_failure;
bool allocation_failure_occurred;
void enable_malloc_failures(int times_until_failure)
{
allocation_failure_occurred = false;
failure_mode_enabled = true;
allocations_until_failure = times_until_failure;
}
void disable_malloc_failures()
{
failure_mode_enabled = false;
}
void *test_malloc(size_t size, char c)
{
if (failure_mode_enabled)
{
if (allocations_until_failure-- <= 0)
{
allocation_failure_occurred = true;
return NULL;
}
}
void *ptr;
if ( (ptr = the_real_malloc(size)) == NULL)
{
/* Real allocation failure during test suite run? Uh oh */
allocation_failure_occurred = true;
debug_print("%s\n", "REAL malloc() failure in test_malloc()! Uh oh.");
return NULL;
}
memset(ptr, c, size);
return ptr;
}
#endif