Default Memory Manager
The default memory manager is a general-purpose memory manager which attempts to balance performance and memory requirements. It provides adequate performance for most applications while attempting to minimize the amount of additional memory needed.
The memory manager maintains the free space in the heap as nodes in a Cartesian binary search tree. This data structure imposes no limitation on the number of block sizes supported by the tree, allowing a wide range of potential block sizes.
Allocation
size = ROUND (n+16, 16)
For example, an allocation of size 37 would require a size of ROUND(37+16, 16), which is equal to 64 bytes.
A node of the tree that is greater than or equal to the size required is removed from the tree. If the block found is larger than the needed size, the block is divided into two blocks: one of the needed size, and the second a remainder. The second block is returned to the free tree for future allocation. The first block is returned to the caller.
If a block of sufficient size is not found in the free tree, the following processing occurs:
- The heap is expanded.
- A block the size of the acquired extension is added to the free tree.
- Allocation continues as previously described.
Deallocation
Memory blocks deallocated with the free
operation
are returned to the tree, at the root. Each node along the path to
the insertion point for the new node is examined to see if it adjoins
the node being inserted. If it does, the two nodes are merged and
the newly merged node is relocated in the tree. If no adjoining block
is found, the node is inserted at the appropriate place in the tree.
Merging adjacent blocks is done to reduce heap fragmentation.
Reallocation
- A new block of the requested size is allocated.
- The data is moved from the original block to the new block.
- The original block is returned to the free tree with the
free
operation. - The new block is returned to the caller.
If the size of the reallocated block is smaller than the original block, and the difference in size is small, the original block is returned. Otherwise, if the size of the reallocated block is smaller than the original block, the block is split and the remaining portion is returned to the free tree.
Enabling the default memory manager
QIBM_MALLOC_TYPE=DEFAULT
QIBM_MALLOC_DEFAULT_OPTIONS=options
To specify user-specified configuration options for the default memory manager, set QIBM_MALLOC_DEFAULT_OPTIONS=options, where options is a blank delimited list of one or more configuration options.
If the QIBM_MALLOC_TYPE=DEFAULT environment variable is specified
and the _C_Quickpool_Init()
function is called, the
environment variable settings take precedence over the _C_Quickpool_Init()
function
and the _C_Quickpool_Init()
function returns a -1
value indicating that an alternate heap manager has already been enabled.
Configuration Options
The following configuration options are available:
MALLOC_INIT:N
This option can be used to specify that each byte of allocated memory is initialized to the given value. The value N represents an integer in the range of 0 to 255.
This option is not enabled by default.
FREE_INIT:N
This option can be used to specify that each byte of freed memory is initialized to the given value. The value N represents an integer in the range of 0 to 255.
This option is not enabled by default.
Any number of options can be specified and they can be specified in any order. Blanks are the only valid delimiter for separating configuration options. Each configuration option can only be specified once. If a configuration option is specified more than once, only the final instance applies. If a configuration option is specified with an invalid value, the configuration option is ignored.
ADDENVVAR ENVVAR(QIBM_MALLOC_DEFAULT_OPTIONS) LEVEL(*JOB) REPLACE(*YES) VALUE('')
ADDENVVAR ENVVAR(QIBM_MALLOC_DEFAULT_OPTIONS) LEVEL(*JOB) REPLACE(*YES)
VALUE('MALLOC_INIT:255 FREE_INIT:0')
The first example represents the default configuration values. The second example illustrates all options being specified.
Related functions
There are no functions available to enable or specify configuration options for the default memory manager. The environment variable support must be used.
Related Information
- calloc() — Reserve and Initialize Storage
- free() — Release Storage Blocks
- malloc() — Reserve Storage Block
- realloc() — Change Reserved Storage Block Size
- _C_TS_malloc_debug() — Determine amount of teraspace memory used (with optional dumps and verification)
- _C_TS_malloc_info() — Determine amount of teraspace memory used
- _C_Quickpool_Init() — Initialize Quick Pool Memory Manager