Jpp  16.0.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | Public Attributes | List of all members
JLANG::JAllocatorBuffer Struct Reference

Low-level memory management. More...

#include <JAllocator.hh>

Public Types

typedef unsigned short JBlock_t
 

Public Member Functions

 JAllocatorBuffer (const std::size_t block_size, const JBlock_t number_of_blocks)
 Constructor. More...
 
void * allocate (const std::size_t block_size)
 Allocate memory. More...
 
void free (void *p, const std::size_t block_size)
 Deallocate memory. More...
 

Public Attributes

unsigned char * buffer
 
JBlock_t firstAvailableBlock
 
JBlock_t numberOfFreeBlocks
 

Detailed Description

Low-level memory management.

Source code is taken from reference: A. Alexandrescu, Modern C++ Design, Addison Wesley.

Definition at line 26 of file JAllocator.hh.

Member Typedef Documentation

typedef unsigned short JLANG::JAllocatorBuffer::JBlock_t

Definition at line 28 of file JAllocator.hh.

Constructor & Destructor Documentation

JLANG::JAllocatorBuffer::JAllocatorBuffer ( const std::size_t  block_size,
const JBlock_t  number_of_blocks 
)
inline

Constructor.

Parameters
block_sizenumber of bytes
number_of_blocksnumber of blocks

Definition at line 37 of file JAllocator.hh.

39  {
40  buffer = new unsigned char[block_size * number_of_blocks];
41 
42  if (buffer == NULL) {
43  throw JException("JAllocatorBuffer::init(): not enough space in memory.");
44  }
45 
46  // initialise memory as linked list
47 
48  unsigned char* p = buffer;
49 
50  for (JBlock_t i = 0; i != number_of_blocks; p += block_size) {
51  ((JBlock_t&) *p) = ++i;
52  }
53 
55  numberOfFreeBlocks = number_of_blocks;
56  }
General exception.
Definition: JException.hh:23
unsigned short JBlock_t
Definition: JAllocator.hh:28
unsigned char * buffer
Definition: JAllocator.hh:103

Member Function Documentation

void* JLANG::JAllocatorBuffer::allocate ( const std::size_t  block_size)
inline

Allocate memory.

Parameters
block_sizenumber of bytes
Returns
pointer to available memory

Definition at line 65 of file JAllocator.hh.

66  {
67  unsigned char* p = buffer + firstAvailableBlock * block_size;
68 
69  firstAvailableBlock = ((JBlock_t&) *p);
70 
72 
73  return p;
74  }
unsigned short JBlock_t
Definition: JAllocator.hh:28
unsigned char * buffer
Definition: JAllocator.hh:103
void JLANG::JAllocatorBuffer::free ( void *  p,
const std::size_t  block_size 
)
inline

Deallocate memory.

Parameters
ppointer to memory to be freed
block_sizenumber of bytes

Definition at line 83 of file JAllocator.hh.

84  {
85  unsigned char* q = static_cast<unsigned char*>(p);
86 
87  if (p < buffer || (q - buffer) % block_size != 0) {
88  throw JException("JAllocatorBuffer::free(): inconsistent pointer.");
89  }
90 
91  ((JBlock_t&) *q) = firstAvailableBlock;
92 
93  firstAvailableBlock = static_cast<JBlock_t>((q - buffer) / block_size);
94 
95  if (firstAvailableBlock != (q - buffer) / block_size) {
96  throw JException("JAllocatorBuffer::free(): failed truncation check.");
97  }
98 
100  }
General exception.
Definition: JException.hh:23
unsigned short JBlock_t
Definition: JAllocator.hh:28
unsigned char * buffer
Definition: JAllocator.hh:103

Member Data Documentation

unsigned char* JLANG::JAllocatorBuffer::buffer

Definition at line 103 of file JAllocator.hh.

JBlock_t JLANG::JAllocatorBuffer::firstAvailableBlock

Definition at line 104 of file JAllocator.hh.

JBlock_t JLANG::JAllocatorBuffer::numberOfFreeBlocks

Definition at line 105 of file JAllocator.hh.


The documentation for this struct was generated from the following file: