Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component bdls_memoryutil
[Package bdls]

Provide a set of portable utilities for memory manipulation. More...

Namespaces

namespace  bdls

Detailed Description

Outline
Purpose:
Provide a set of portable utilities for memory manipulation.
Classes:
bdls::MemoryUtil struct which scopes memory system utilities.
Description:
This component, bdls::MemoryUtil, defines a platform-independent interface for memory manipulation, providing utilities for querying page size, allocating/deallocating page-aligned memory, and utility to change memory protection.
Usage:
First, allocate one page of memory.
  int pageSize = bdls::MemoryUtil::pageSize();
  char *data = (char*)bdls::MemoryUtil::allocate(pageSize);
Write into the allocated buffer.
  data[0] = '1';
Make the memory write protected
Verify that data still could be read.
  assert('1' == data[0]);
Once again, try writing into the buffer. This should crash our process.
  data[0] = '2';
Restore read/write access and free the allocated memory. Actually, this will never be executed, as the process has already crashed.
  bdls::MemoryUtil::protect(data, pageSize,
                            bdls::MemoryUtil::k_ACCESS_READ_WRITE);
  bdls::MemoryUtil::free(data);