IBM
Contents Index Previous Next



Memory Management System


Figure 510 : Memory management

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:

Figure 511 : Memory management functions

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:

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.

Note: Performance optimization

Fast memory handling, if applicable, can help optimize the performance of the executable quite a lot. One of the examples of fast memory handling is working with statically allocated memory instead of dynamically allocated in alloc and free procedures.

If you want to use your own memory handler, perform the following steps:

  1. 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.
  2. Compile the coder library with the CODER_MMS_USER compile switch, see Memory management configuration.
  3. Add include path for the file mms_user.h to the compilation settings.
  4. Compile and link the user memory handler function to the application.

Example 481 : File mms_user.h

#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);

#endif

There 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
Contents Index Previous Next