1 | // Copyright (c) Microsoft. All rights reserved.
|
---|
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
---|
3 |
|
---|
4 | #ifndef GBALLOC_H
|
---|
5 | #define GBALLOC_H
|
---|
6 |
|
---|
7 | #ifdef __cplusplus
|
---|
8 | #include <cstdlib>
|
---|
9 | #else
|
---|
10 | #include <stdlib.h>
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #include "umock_c/umock_c_prod.h"
|
---|
14 |
|
---|
15 | #ifdef __cplusplus
|
---|
16 | extern "C"
|
---|
17 | {
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | // GB_USE_CUSTOM_HEAP disables the implementations in gballoc.c and
|
---|
21 | // requires that an external library implement the gballoc_malloc family
|
---|
22 | // declared here.
|
---|
23 | #if defined(GB_USE_CUSTOM_HEAP)
|
---|
24 | MOCKABLE_FUNCTION(, void*, gballoc_malloc, size_t, size);
|
---|
25 | MOCKABLE_FUNCTION(, void*, gballoc_calloc, size_t, nmemb, size_t, size);
|
---|
26 | MOCKABLE_FUNCTION(, void*, gballoc_realloc, void*, ptr, size_t, size);
|
---|
27 | MOCKABLE_FUNCTION(, void, gballoc_free, void*, ptr);
|
---|
28 |
|
---|
29 | #define malloc gballoc_malloc
|
---|
30 | #define calloc gballoc_calloc
|
---|
31 | #define realloc gballoc_realloc
|
---|
32 | #define free gballoc_free
|
---|
33 |
|
---|
34 | /* all translation units that need memory measurement need to have GB_MEASURE_MEMORY_FOR_THIS defined */
|
---|
35 | /* GB_DEBUG_ALLOC is the switch that turns the measurement on/off, so that it is not on always */
|
---|
36 | #elif defined(GB_DEBUG_ALLOC)
|
---|
37 |
|
---|
38 | MOCKABLE_FUNCTION(, int, gballoc_init);
|
---|
39 | MOCKABLE_FUNCTION(, void, gballoc_deinit);
|
---|
40 | MOCKABLE_FUNCTION(, void*, gballoc_malloc, size_t, size);
|
---|
41 | MOCKABLE_FUNCTION(, void*, gballoc_calloc, size_t, nmemb, size_t, size);
|
---|
42 | MOCKABLE_FUNCTION(, void*, gballoc_realloc, void*, ptr, size_t, size);
|
---|
43 | MOCKABLE_FUNCTION(, void, gballoc_free, void*, ptr);
|
---|
44 |
|
---|
45 | MOCKABLE_FUNCTION(, size_t, gballoc_getMaximumMemoryUsed);
|
---|
46 | MOCKABLE_FUNCTION(, size_t, gballoc_getCurrentMemoryUsed);
|
---|
47 | MOCKABLE_FUNCTION(, size_t, gballoc_getAllocationCount);
|
---|
48 | MOCKABLE_FUNCTION(, void, gballoc_resetMetrics);
|
---|
49 |
|
---|
50 | /* if GB_MEASURE_MEMORY_FOR_THIS is defined then we want to redirect memory allocation functions to gballoc_xxx functions */
|
---|
51 | #ifdef GB_MEASURE_MEMORY_FOR_THIS
|
---|
52 | /* Unfortunately this is still needed here for things to still compile when using _CRTDBG_MAP_ALLOC.
|
---|
53 | That is because there is a rogue component (most likely CppUnitTest) including crtdbg. */
|
---|
54 | #if defined(_CRTDBG_MAP_ALLOC) && defined(_DEBUG)
|
---|
55 | #undef _malloc_dbg
|
---|
56 | #undef _calloc_dbg
|
---|
57 | #undef _realloc_dbg
|
---|
58 | #undef _free_dbg
|
---|
59 | #define _malloc_dbg(size, ...) gballoc_malloc(size)
|
---|
60 | #define _calloc_dbg(nmemb, size, ...) gballoc_calloc(nmemb, size)
|
---|
61 | #define _realloc_dbg(ptr, size, ...) gballoc_realloc(ptr, size)
|
---|
62 | #define _free_dbg(ptr, ...) gballoc_free(ptr)
|
---|
63 | #else
|
---|
64 | #define malloc gballoc_malloc
|
---|
65 | #define calloc gballoc_calloc
|
---|
66 | #define realloc gballoc_realloc
|
---|
67 | #define free gballoc_free
|
---|
68 | #endif
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | #else /* GB_DEBUG_ALLOC */
|
---|
72 |
|
---|
73 | #define gballoc_init() 0
|
---|
74 | #define gballoc_deinit() ((void)0)
|
---|
75 |
|
---|
76 | #define gballoc_getMaximumMemoryUsed() SIZE_MAX
|
---|
77 | #define gballoc_getCurrentMemoryUsed() SIZE_MAX
|
---|
78 | #define gballoc_getAllocationCount() SIZE_MAX
|
---|
79 | #define gballoc_resetMetrics() ((void)0)
|
---|
80 |
|
---|
81 | #endif /* GB_DEBUG_ALLOC */
|
---|
82 |
|
---|
83 | #ifdef __cplusplus
|
---|
84 | }
|
---|
85 | #endif
|
---|
86 |
|
---|
87 | #endif /* GBALLOC_H */
|
---|