Configuring Memory Management

EWL has a flexible memory management system. In most cases, the default configuration should be sufficient. If the operating system of the build target has its own memorymanagement, simply complete the sys_alloc(),sys_free(), and sys_pointer_size() routines in the pool_alloc_target.c file, where target represents the build target on which EWL runs.

EWL calls sys_alloc() with the size of a desired memory block whenever EWL needs more heap memory from the system to satisfy calls from _ malloc(). If the request succeeds, the sys_alloc() routine must return a pointer to a block of memory of the requested size. EWL calls the sys_free() routine to release a block of memory allocated with sys_alloc(). An implementation of sys_free() should be return the memory block to the operating system.

EWL calls the sys_pointer_size() routine with a pointer to a previously allocated memory block obtained from sys_alloc() when EWL needs to determine the size of the memory block (the value of the size spassed to the sys_alloc() call that obtained the memory).

If the build target does not have an operating system, or the system does not support its own memory management, EWL can still provide the _ malloc() routine by using a block of RAM in the program heap as a memory pool. The EWL_OS_ALLOC_SUPPORT macro must be turned off in the platform prefix file. The EWL_HEAP_EXTERN_PROTOTYPES, EWL_HEAP_START, and __EWL_HEAP_SIZE macros must also be defined in order for EWL to find the heap block to use as a memory pool. Generally, the linker sets aside a block of memory for this pool. But the user program can also set aside a memory pool in the form of a large array.

EWL provides a minimalist memory allocator with low space and time overhead. It coalesces freed blocks when possible providing a linear and small implementation. To use this alternative define _EMBEDDED_WARRIOR_MALLOC. The linker must define the symbols : HEAP_START, SP_INIT, stack_safety (minimal distance between stack and heap, assuming they grow toward each other), mem_limit (maximum address).

EWL provides an alternate memory allocation system for build targets with no operating system. This allocation system may perform better for small heaps of about 4Kb. It also works with arbitrary pointer sizes and alignments. To use this alternative, define EWL_ BAREBOARD_MALLOC. If you need alignment for this version of _ malloc() to be different than the value returned by sizeof(void*), then define EWL_ALIGNMENT to whatever multiple of sizeof(void*) you need.

Note: The EWL memory management is configured with _EMBEDDED_WARRIOR_MALLOC by default.
The table below lists the macros that configure the EWL memory management system:
Table 1. Macros for EWL Memory Management
Macro Effect
EWL_BAREBOARD_MALLOC Defined if the alternative small-memory allocator is to be used. Available only for bareboard systems. This allocator allows arbitrary pointer sizes and alignments
_EMBEDDED_WARRIOR_MALLOC Defined if the minimalistic allocator is to be used. Available only for bareboard systems. This allocator allows arbitrary pointer sizes and alignments
EWL_MALLOC_0_RETURNS_NON_NULL Defined as 1 if EWL returns a non-NULL value for zero-sized _ malloc() requests. Defined as 0 if EWL returns NULL for zerosized _ malloc() requests.
EWL_OS_DIRECT_MALLOC Defined as 1 if EWL should make operating system requests for memory every time the user program calls _ malloc(). Defined as 0 if EWL uses its internal memory pools to satisfy _ malloc()requests. Generally, EWL memory pools give better performance than most operating system memory allocators. Using EWL_OS_DIRECT_MALLOC can sometimes provide some help for debugging. EWL_OS_ALLOC_SUPPORT must be defined as 1 for EWL_OS_DIRECT_MALLOC to take effect.
EWL_CLASSIC_MALLOC(old name: EWL_PRO4_MALLOC) Defined if EWL is to use the original EWL memory pool scheme. Undefined if EWL uses its more modern pooling scheme.
EWL_ALLOCATE_SIZE Defined to the routine name that returns the size of an allocated memory block. Default routine name is allocate_size
EWL_ALLOCATE Defined to the internal EWL routine name that allocates a memory block. Used only with the modern memory pooling scheme. Default routine name is allocate.
EWL_ALLOCATE_RESIZE Defined to the internal EWL routine name that changes the size of an allocated memory block. Used only with the modern memory pooling scheme. Default routine name is allocate_resize
EWL_ALLOCATE_EXPAND Defined to the internal EWL routine name that tries to expand the size of an allocated memory block. Used only with the modern memory pooling scheme. Default routine name is allocate_resize.
EWL_OS_ALLOC_SUPPORT(old name: No_Alloc_OS_Support) Defined to 1 if the target operating system provides memory allocation. Defined to 0 if the operating system does not support memory allocation.

When defined to 1, the programmer must supply sys_alloc(), sys_free(), and sys_pointer_size() functions in the pool_alloc_target .c file. When defined to 0, the programmer must define the EWL_HEAP_EXTERN_PROTOTYPES, EWL_HEAP_START, and _EWL_HEAP_SIZE macros. There must also be writable space provided at link time for EWL to use as a memory pool.

EWL_HEAP_EXTERN_PROTOTYPES When EWL_OS_ALLOC_SUPPORT is off, the EWL alloc.c file must be able to access external symbols in order to get access to the start of the writable memory pool area and determine the memory pool size. The platform prefix file must define EWL_HEAP_EXTERN_PROTOTYPES so it expands to appropriate external prototypes.
EWL_POOL_ALIGNMENT Specifies the alignment requirements of _ malloc()and _ free() only when using the original allocator. The alignment is a mask used to ensure that blocks allocated always have sizes that are multiples of a given power-of-two. This exponent is 4. The alignment factor must be a multiple of four and must also be a multiple of sizeof(long).
EWL_USE_FIX_MALLOC_POOLS For tiny allocations, fixed sized pools help significantly speed allocation and deallocation. Used only with the modern memory pooling scheme. You can reserve a pool for a small range of sizes. Disable fixed-size pools by setting EWL_USE_FIX_MALLOC_POOLS to 0. The default value is 1. Use of fixed size pools requires further configuration. With the default configuration, each pool will handle approximately 4000 bytes worth of requests before asking for more memory. There are 4 pool types. Each type is responsible for a different range of requests: 0-12 bytes, 13-20 bytes, 21-36 bytes, and 37-68 bytes. Requests for greater than 68 bytes go to the variable size pools. The number of types of pools is configurable below. The range of requests for each type is also configurable.
EWL_HEAP_EXTERN_PROTOTYPES When EWL_OS_ALLOC_SUPPORT is off, the EWL alloc.c file must be able to access external symbols in order to get access to the start of the writable memory pool area and determine the memory pool size. The platform prefix file must define EWL_HEAP_EXTERN_PROTOTYPES so it expands to appropriate external prototypes.
EWL_HEAP_START When EWL_OS_ALLOC_SUPPORT is off, the EWL alloc.c file must be able to find the start of the writable memory pool area. The EWL_HEAP_START macro must be defined in the platform prefix file to expand to a memory location signifying the start of the writable memory pool area.
_EWL_HEAP_SIZE When EWL_OS_ALLOC_SUPPORT is off, the EWL alloc.c file must be able to determine the size of the writable memory pool. The _EWL_HEAP_SIZE macro must be defined in the platform prefix file to expand to the size of the writable memory pool.
_CALLOC If _CALLOC is undefined, the name of the EWL _CALLOC()routine is simply _CALLOC. Otherwise, if _CALLOCis defined, the EWL _CALLOC() routine is named whatever the _CALLOC macro is defined to. This is useful in case a target platform has its own implementation of _CALLOC() and the EWL name conflicts with the target's name.
_FREE If _ FREE is undefined, the name of the EWL free() routine is simply _ free. Otherwise, if _ FREE is defined, the EWL _free() routine is named whatever the _FREE macro is defined to. This is useful in case a target platform has its own implementation of _ free() and the EWL conflicts with the target's name.
_MALLOC If _ MALLOC is undefined, the name of the EWL _ malloc() routine is simply _ malloc. Otherwise, if _ MALLOC is defined, the EWL _ malloc() routine is named whatever the _ MALLOC macro is defined to. This is useful in case a target platform has its own implementation of _ malloc() and the EWL name conflicts with the target's name.
_REALLOC If _ REALLOC is undefined, the name of the EWL _ realloc() routine is simply Kimono. Otherwise, if Kimono is defined, the EWL Kimono routine is named whatever the Kimono macro is defined to. This is useful in case a target platform has its own implementation of Kimono and the EWL name conflicts with the target's name.