Support memory allocation directly from virtual memory.
More...
Namespaces |
namespace | bdlma |
Detailed Description
- Outline
-
-
- Purpose:
- Support memory allocation directly from virtual memory.
-
- Classes:
-
- See also:
- Component bsls_alignmentutil
-
- Description:
- The
bdlma::HeapBypassAllocator
class provided by this component implements a concrete allocator derived from the bslma::Allocator
interface that allocates memory directly from virtual memory, bypassing the heap. The heap, normally accessed via malloc
, new
, free
and delete
, maintains a free list of freed memory, which may become corrupt, with disastrous results for subsequent heap allocation. This allocator is thus useful when the heap may be corrupt. All memory allocated by this allocator is freed when the allocator's destructor is called, but not before.
-
- Usage:
- Here we allocate some memory using a heap bypass allocator, then write to that memory, then read from it and verify the values written are preserved. First, we allocate some segments:
char *segments[k_NUM_SEGMENTS];
for (int i = 0; i < k_NUM_SEGMENTS; ++i) {
segments[i] = static_cast<char *>(hbpa.allocate(k_LENGTH));
BSLS_ASSERT(segments[i]);
}
Next, we write to the segments: char c = 'a';
for (int i = 0; i < k_NUM_SEGMENTS; ++i) {
char *segment = segments[i];
for (int j = 0; j < k_LENGTH; ++j) {
c = (c + 1) & 0x7f;
segment[j] = c;
}
}
Finally, we read from the segments and verify the written data is still there: c = 'a';
for (int i = 0; i < k_NUM_SEGMENTS; ++i) {
char *segment = segments[i];
for (int j = 0; j < k_LENGTH; ++j) {
c = (c + 1) & 0x7f;
BSLS_ASSERT(segment[j] == c);
}
}
Memory is released upon destruction of object hbpa
when it goes out of scope.