BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bdls_memoryutil

Detailed Description

Outline

Purpose

Provide a set of portable utilities for memory manipulation.

Classes

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

This section illustrates intended use of this component.

Example 1: Basic Usage

First, allocate one page of memory.

int pageSize = bdls::MemoryUtil::pageSize();
char *data = (char*)bdls::MemoryUtil::allocate(pageSize);
static void * allocate(int numBytes)
static int pageSize()
Return the memory page size of the platform.

Write into the allocated buffer.

data[0] = '1';

Make the memory write protected

@ k_ACCESS_READ
Definition bdls_memoryutil.h:112
static int protect(void *address, int numBytes, int mode)

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::free(data);
@ k_ACCESS_READ_WRITE
Definition bdls_memoryutil.h:115