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