Provide a namespace for memory block growth strategies.
More...
Detailed Description
- Outline
-
-
- Purpose:
- Provide a namespace for memory block growth strategies.
-
- Classes:
-
- See also:
- Component bsls_alignment
-
- Description:
- This component provides a namespace for enumerating memory block growth strategies, and provides a function that converts each of these enumerators to its corresponding string representation.
-
- Block Growth Strategy:
- This component supports two memory block growth strategies:
GEOMETRIC GROWTH: A container, pool or allocator that employs this strategy, as indicated by the enumerator BSLS_GEOMETRIC
, grows its buffer geometrically.
CONSTANT GROWTH: A container, pool or allocator that employs this strategy, as indicated by the enumerator BSLS_CONSTANT
, locks the buffer growth. The new buffer is always the same size as the current buffer.
-
- Usage:
- Memory block growth strategies are often used in memory managers and containers to control memory usage. First of all, suppose we have a
my_BlockList
class that manages a link list of memory blocks: We can then create a memory manager class my_SequentialPool
that manages a pool of memory: class my_SequentialPool {
char *d_currentBuffer_p;
int d_currentBufferSize;
bsls::BlockGrowth::Strategy
d_growthStrategy;
my_BlockList d_blockList;
my_SequentialPool(const my_SequentialPool&);
my_SequentialPool& operator=(const my_SequentialPool&);
private:
int calculateNextSize(int size);
public:
my_SequentialPool(bsls::BlockGrowth::Strategy strategy);
void *allocate(int size);
};
The implementation for the rest of the class is elided as the function calculateNextSize
alone is sufficient to illustrate the use of this component: Note that, if the growth strategy in effect is constant growth (BSLS_CONSTANT
), the size of the internal buffers will always be the same. If size
is greater than the buffer size, the implementation of allocate
will return a block having the exact size
from the internal block list: int nextSize = d_currentBufferSize;
do {
nextSize *= 2;
} while (nextSize < size);
Note that, if the growth strategy in effect is geometric growth (BSLS_GEOMETRIC
), the size of the internal buffer grows geometrically by a factor of 2:
Typedef Documentation