Add ability to emulate malloc() and free() to prevent nasty hard to find link errors from C library routines that need malloc

This commit is contained in:
inmarket 2015-11-27 10:15:46 +10:00
parent 3681542e24
commit 0313756ea3
5 changed files with 43 additions and 2 deletions

View File

@ -53,6 +53,7 @@
// #define GFX_OS_PRE_INIT_FUNCTION myHardwareInitRoutine
// #define GFX_OS_EXTRA_INIT_FUNCTION myOSInitRoutine
// #define GFX_OS_EXTRA_DEINIT_FUNCTION myOSDeInitRoutine
// #define GFX_EMULATE_MALLOC FALSE
///////////////////////////////////////////////////////////////////////////

View File

@ -175,6 +175,23 @@
*/
void gfxFree(void *ptr);
/**
* @brief Use gfxAlloc and gfxFree to implement malloc() and free()
*
* @notes Sometimes your application will include functions that
* want to internally use malloc() and free(). As the default
* implementations of these in your C library are almost
* invariably incorrect for an embedded platform, this option
* allows you to emulate those calls with gfxAlloc() and gfxFree().
* An example is the C library routine rand() which on many
* implementations internally uses malloc().
*
* @api
*/
#ifndef GFX_EMULATE_MALLOC
#define GFX_EMULATE_MALLOC FALSE
#endif
/**
* @brief Yield the current thread
* @details Give up the rest of the current time slice for this thread in order to give other threads

View File

@ -174,9 +174,10 @@
#define GFX_FREERTOS_USE_TRACE FALSE
#endif
/**
* @brief How much RAM should uGFX use for the heap
* @brief How much RAM should uGFX use for the heap when using its own internal heap allocator
* @details Defaults to 0.
* @note Only used when the generic ugfx heap code is used (GFX_USE_OS_RAW32, GFX_USE_OS_ARDUINO, GFX_US_OS_KEIL, GFX_USE_OS_CMSIS)
* @note Only used when the internal ugfx heap allocator is used
* (GFX_USE_OS_RAW32, GFX_USE_OS_ARDUINO, GFX_US_OS_KEIL, GFX_USE_OS_CMSIS)
* @note If 0 then the standard C runtime malloc(), free() and realloc()
* are used.
* @note If it is non-zero then this is the number of bytes of RAM

View File

@ -32,5 +32,16 @@
#error "GOS: GFX_FREERTOS_USE_TRACE is only available for the FreeRTOS port."
#endif
#if GFX_EMULATE_MALLOC
#if GFX_USE_OS_WIN32 || GFX_USE_OS_LINUX || GFX_USE_OS_OSX || GFX_USE_OS_ECOS || \
(GFX_OS_HEAP_SIZE == 0 && (GFX_USE_OS_RAW32 || GFX_USE_OS_ARDUINO || GFX_USE_OS_CMSIS || GFX_USE_OS_KEIL))
#if GFX_DISPLAY_RULE_WARNINGS
#warning "GOS: Cannot emulate malloc as gfxAlloc() internally uses malloc on this platform"
#endif
#undef GFX_EMULATE_MALLOC
#define GFX_EMULATE_MALLOC FALSE
#endif
#endif
#endif /* _GOS_RULES_H */
/** @} */

View File

@ -194,3 +194,14 @@
#endif
#endif /* GOS_NEED_X_HEAP */
#if GFX_EMULATE_MALLOC
#include <stdlib.h>
void* malloc(size_t size) {
return gfxAlloc(size);
}
void free(void *ptr) {
gfxFree(ptr);
}
#endif