text
stringlengths 0
2.2M
|
---|
void HeapBypassAllocator::unmap(void *address, bsls::Types::size_type size)
|
{
|
VirtualFree(address, 0, MEM_RELEASE);
|
}
|
} // close package namespace
|
#else
|
#error unsupported platform
|
#endif
|
namespace bdlma {
|
// PRIVATE MANIPULATORS
|
int HeapBypassAllocator::replenish(bsls::Types::size_type size)
|
{
|
// round size up to a multiple of page size
|
const bsls::Types::size_type pageMask = d_pageSize - 1;
|
// '%' can be very slow -- if 'd_pageSize' is a power of 2, use '&'
|
const bsls::Types::size_type mod =
|
!(d_pageSize & pageMask) ? (size & pageMask)
|
: size % d_pageSize;
|
if (0 != mod) {
|
size += d_pageSize - mod;
|
}
|
BufferHeader *newBuffer = (BufferHeader *)(void *)map(size);
|
if (0 == newBuffer) {
|
return -1; // RETURN
|
}
|
if (d_currentBuffer_p) {
|
d_currentBuffer_p->d_nextBuffer = newBuffer;
|
}
|
else {
|
d_firstBuffer_p = newBuffer;
|
}
|
d_currentBuffer_p = newBuffer;
|
d_endOfBuffer_p = (char *)newBuffer + size;
|
newBuffer->d_nextBuffer = 0;
|
newBuffer->d_size = size;
|
d_cursor_p = (char *)(newBuffer + 1);
|
return 0;
|
}
|
// CREATORS
|
HeapBypassAllocator::HeapBypassAllocator()
|
: d_firstBuffer_p(0)
|
, d_currentBuffer_p(0)
|
, d_cursor_p(0)
|
, d_endOfBuffer_p(0)
|
, d_alignment(bsls::AlignmentUtil::BSLS_MAX_ALIGNMENT)
|
{
|
#if defined(BSLS_PLATFORM_OS_UNIX)
|
d_pageSize = ::sysconf(_SC_PAGESIZE);
|
#else // Windows
|
SYSTEM_INFO si;
|
GetSystemInfo(&si);
|
d_pageSize = si.dwAllocationGranularity;
|
#endif
|
#ifdef BSLS_PLATFORM_OS_HPUX
|
// 128-bit alignment is required on HP-UX for bdesu_stacktrace,
|
// 'BSLS_MAX_ALIGNMENT' at the time of this writing is only 64 bits on
|
// HP-UX, which causes bus traps on alignment errors.
|
struct {
|
char d_dummyChar;
|
__float80 d_dummyFloat80;
|
} s;
|
d_alignment = (char *)&s.d_dummyFloat80 - &s.d_dummyChar;
|
#endif
|
BSLS_ASSERT(d_alignment >= bsls::AlignmentUtil::BSLS_MAX_ALIGNMENT);
|
BSLS_ASSERT(0 == (d_alignment & (d_alignment - 1))); // is power of 2
|
}
|
HeapBypassAllocator::~HeapBypassAllocator()
|
{
|
BufferHeader *buffer = d_firstBuffer_p;
|
while (buffer) {
|
BufferHeader *nextBuffer = buffer->d_nextBuffer;
|
unmap(buffer, buffer->d_size);
|
buffer = nextBuffer;
|
}
|
}
|
// MANIPULATORS
|
void *HeapBypassAllocator::allocate(bsls::Types::size_type size)
|
{
|
d_cursor_p = d_cursor_p + bsls::AlignmentUtil::calculateAlignmentOffset(
|
d_cursor_p, static_cast<int>(d_alignment));
|
if (d_endOfBuffer_p < d_cursor_p + size) {
|
bsls::Types::size_type blockSize =
|
size + d_alignment + sizeof(BufferHeader);
|
int sts = replenish(blockSize); // 'replenish' will round up to
|
// multiple of 'd_pageSize'
|