Nigel Rantor / azure_c_shared_utility

Fork of azure_c_shared_utility by Azure IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gballoc.h Source File

gballoc.h

00001 // Copyright (c) Microsoft. All rights reserved.
00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
00003 
00004 #ifndef GBALLOC_H
00005 #define GBALLOC_H
00006 
00007 #ifdef __cplusplus
00008 #include <cstddef>
00009 extern "C"
00010 {
00011 #else
00012 #include <stddef.h>
00013 #endif
00014 
00015 #include "azure_c_shared_utility/umock_c_prod.h"
00016 
00017 /* all translation units that need memory measurement need to have GB_MEASURE_MEMORY_FOR_THIS defined */
00018 /* GB_DEBUG_ALLOC is the switch that turns the measurement on/off, so that it is not on always */
00019 #if defined(GB_DEBUG_ALLOC)
00020 
00021 MOCKABLE_FUNCTION(, int, gballoc_init);
00022 MOCKABLE_FUNCTION(, void, gballoc_deinit);
00023 MOCKABLE_FUNCTION(, void*, gballoc_malloc, size_t, size);
00024 MOCKABLE_FUNCTION(, void*, gballoc_calloc, size_t, nmemb, size_t, size);
00025 MOCKABLE_FUNCTION(, void*, gballoc_realloc, void*, ptr, size_t, size);
00026 MOCKABLE_FUNCTION(, void, gballoc_free, void*, ptr);
00027 
00028 MOCKABLE_FUNCTION(, size_t, gballoc_getMaximumMemoryUsed);
00029 MOCKABLE_FUNCTION(, size_t, gballoc_getCurrentMemoryUsed);
00030 
00031 /* if GB_MEASURE_MEMORY_FOR_THIS is defined then we want to redirect memory allocation functions to gballoc_xxx functions */
00032 #ifdef GB_MEASURE_MEMORY_FOR_THIS
00033 /* Unfortunately this is still needed here for things to still compile when using _CRTDBG_MAP_ALLOC.
00034 That is because there is a rogue component (most likely CppUnitTest) including crtdbg. */
00035 #if defined(_CRTDBG_MAP_ALLOC) && defined(_DEBUG)
00036 #undef _malloc_dbg
00037 #undef _calloc_dbg
00038 #undef _realloc_dbg
00039 #undef _free_dbg
00040 #define _malloc_dbg(size, ...) gballoc_malloc(size)
00041 #define _calloc_dbg(nmemb, size, ...) gballoc_calloc(nmemb, size)
00042 #define _realloc_dbg(ptr, size, ...) gballoc_realloc(ptr, size)
00043 #define _free_dbg(ptr, ...) gballoc_free(ptr)
00044 #else
00045 #define malloc gballoc_malloc
00046 #define calloc gballoc_calloc
00047 #define realloc gballoc_realloc
00048 #define free gballoc_free
00049 #endif
00050 #endif
00051 
00052 #else /* GB_DEBUG_ALLOC */
00053 
00054 #define gballoc_init() 0
00055 #define gballoc_deinit() ((void)0)
00056 
00057 #define gballoc_getMaximumMemoryUsed() SIZE_MAX
00058 #define gballoc_getCurrentMemoryUsed() SIZE_MAX
00059 
00060 #endif /* GB_DEBUG_ALLOC */
00061 
00062 #ifdef __cplusplus
00063 }
00064 #endif
00065 
00066 #endif /* GBALLOC_H */