text
stringlengths
0
2.2M
// bdlma_heapbypassallocator.cpp -*-C++-*-
// ----------------------------------------------------------------------------
// NOTICE
//
// This component is not up to date with current BDE coding standards, and
// should not be used as an example for new development.
// ----------------------------------------------------------------------------
#include <bdlma_heapbypassallocator.h>
#include <bsls_ident.h>
BSLS_IDENT_RCSID(bdlma_heapbypassallocator_cpp,"$Id$ $CSID$")
#include <bsls_assert.h>
#include <bsls_alignmentutil.h>
#include <bsls_platform.h>
#include <bsl_algorithm.h>
#ifdef BSLS_PLATFORM_OS_WINDOWS
#include <windows.h>
#elif defined(BSLS_PLATFORM_OS_UNIX)
#include <unistd.h>
#include <sys/mman.h>
#endif
namespace BloombergLP {
namespace bdlma {
// ========================================
// struct HeapBypassAllocator::BufferHeader
// ========================================
struct HeapBypassAllocator::BufferHeader {
// This struct defines a link in a linked list of buffers allocated by
// 'class HeapBypassAllocator'. Each buffer header is located at the
// beginning of the buffer it describes, and contains the size (in bytes)
// of the buffer.
BufferHeader *d_nextBuffer; // pointer to linked list of buffers
// allocated after this one
bsls::Types::size_type d_size; // size (in bytes) of this buffer
};
} // close package namespace
// --------------------------
// bdlma::HeapBypassAllocator
// --------------------------
// PRIVATE CLASS METHODS
#if defined(BSLS_PLATFORM_OS_UNIX)
namespace bdlma {
char *HeapBypassAllocator::map(bsls::Types::size_type size)
{
// Note that passing 'MAP_ANONYMOUS' and a null file descriptor tells
// 'mmap' to use a special system file to map to.
char *address = (char *)mmap(0, // 'mmap' chooses what address to which
// to map the memory
size,
PROT_READ | PROT_WRITE,
#ifdef BSLS_PLATFORM_OS_DARWIN
MAP_ANON | MAP_PRIVATE,
#else
MAP_ANONYMOUS | MAP_PRIVATE,
#endif
-1, // null file descriptor
0);
return (MAP_FAILED == address ? 0 : address);
}
void HeapBypassAllocator::unmap(void *address, bsls::Types::size_type size) {
// On some platforms, munmap takes a 'char *', on others, a 'void *'.
munmap((char *)address, size);
}
} // close package namespace
#elif defined(BSLS_PLATFORM_OS_WINDOWS)
namespace bdlma {
char *HeapBypassAllocator::map(bsls::Types::size_type size)
{
char *address =
(char *)VirtualAlloc(0, // 'VirtualAlloc' chooses what address to
// which to map the memory
size,
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE);
return NULL == address ? 0 : address;
}