![]() |
![]() |
![]() |
![]() |
![]() |
Memory Management System
The coder solution contains memory management functions. There are only two basic functions used for memory management: alloc and free.
Memory management in the coder library consists of allocation and freeing of memory, and releasing of memory in case of errors.
Coder library contains two levels of memory access functions:
- Pure allocation and freeing of memory - CUCF_ALLOC(size, type) and CUCF_FREE(ptr, size, type),
- Safe memory allocating and releasing - CUCFAlloc(info, size, type), CUCFFree(info, ptr, size, type) and CUCFRelease(info, ptr), where size, type and ptr are the same as for the previous memory handling functions. Apart from allocating, CUCFAlloc function saves a pointer to the allocated memory block in the internal stack, which is used for memory freeing in case of error. The first parameter info in these three memory handling functions is of type tEMSInfo* and it is a pointer to the stack where all allocated memory segments are stored. Safe functions use pure memory management functions for memory operations. CUCFAlloc calls CUCF_ALLOC for allocating memory for storage cell in the stack and for allocating size required bytes, CUCFFree frees memory pointed by ptr and also the corresponding stack cell.
Safe memory handling functions are used in those places where memory freeing procedure will not be performed in case of error, for example, in the coder library kernel. Pure memory handling functions are called from the safe functions. They are also called directly in those places where memory can still be released even after an error has occurred, for example, buffer memory allocation (memory is released by BufCloseBuf function).
Predefined Memory Handling
There are two types of predefined memory handling functions which can be used to perform allocation and freeing of memory:
- SDL Suite memory handlers XALLOC and XFREE, see Functions for Allocation and Deallocation, and
- standard malloc and free functions from <stdlib.h> which are the default memory handlers.
Users can also define their own application specific memory handling functions, see User defined Memory Handling.
The type of memory handler to be invoked for allocation and freeing of memory is configured when compiling coder library by compile switches, see Memory management configuration.
User defined Memory Handling
The coder library can be configured to work with user-specific memory handling procedures. It is possible to write your own allocate and free procedures, which will be invoked instead of the default memory handlers.
If you want to use your own memory handler, perform the following steps:
- Create file mms_user.h and insert definitions of macros USER_ALLOC_FUNC(size, type) and USER_FREE_FUNC(ptr, size, type) to point to the user defined memory handling procedures. These macros will be used for redefining pure memory handling procedures.
- Compile the coder library with the CODER_MMS_USER compile switch, see Memory management configuration.
- Add include path for the file mms_user.h to the compilation settings.
- Compile and link the user memory handler function to the application.
#ifndef mms_user_h#define mms_user_h#define USER_ALLOC_FUNC(size,type) UserAlloc(size)#define USER_FREE_FUNC(ptr,size,type) UserFree(ptr)extern void* UserAlloc(size_t size);extern void UserFree(void* ptr);#endifThere are also two BMS user-specific memory handling procedures that are referenced through macros defined in the mms_user.h:
#define USER_BMS_ALLOC_FUNC(buffer, size, type) UserBmsAlloc(buffer, size)#define USER_BMS_FREE_FUNC(coder, ptr, size, type) UserBmsFree(buffer, ptr, size)These interfaces are used only in the buffer functions (see Buffer Management Functions). By default (without this definition) USER_ALLOC_FUNC and USER_FREE_FUNC are used inside the buffer functions.
The BMS user-specific memory handlers use buffer access types tCoder* or tBuffer as the first argument. This argument opens the access to the tUserData (see User Data) inside the BMS memory handling:
void* UserBmsAlloc(tCoder* Coder, size_t Size){tUserData* data = BufGetUserData(Coder);... /* user implementation */}void UserBmsFree(tCoder* Coder, void* Ptr, size_t Size){tUserData* data = BufGetUserData(Coder);... /* user implementation */}
http://www.ibm.com/rational |
![]() |
![]() |
![]() |
![]() |