Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
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.
 
void * allocate (const std::size_t block_size)
 Allocate memory.
 
void free (void *p, const std::size_t block_size)
 Deallocate memory.
 

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

◆ JBlock_t

Definition at line 28 of file JAllocator.hh.

Constructor & Destructor Documentation

◆ JAllocatorBuffer()

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 }
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
unsigned char * buffer
unsigned short JBlock_t
Definition JAllocator.hh:28

Member Function Documentation

◆ allocate()

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
70
72
73 return p;
74 }

◆ free()

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
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 }

Member Data Documentation

◆ buffer

unsigned char* JLANG::JAllocatorBuffer::buffer

Definition at line 103 of file JAllocator.hh.

◆ firstAvailableBlock

JBlock_t JLANG::JAllocatorBuffer::firstAvailableBlock

Definition at line 104 of file JAllocator.hh.

◆ numberOfFreeBlocks

JBlock_t JLANG::JAllocatorBuffer::numberOfFreeBlocks

Definition at line 105 of file JAllocator.hh.


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