Loading...
Searching...
No Matches
by::chunk Class Reference

Memory chunk for block-based allocation. More...

#include <chunk.hpp>

Inheritance diagram for by::chunk:
by::allocator by::memoryHaver by::watcher

Public Member Functions

 chunk (ncnt blockSz=1, ncnt sz=MIN_SZ)
 
voidoperator[] (nidx n)
 
const voidoperator[] (nidx n) const BY_CONST_FUNC(operator[](n))
 
voidget (nidx n)
 
voidnew1 () override
 
nbool del (void *used, ncnt) override
 
ncnt len () const override
 
ncnt size () const override
 
nbool rel () override
 
nbool has (const instance &it) const override
 Checks if this chunk contains the memory allocated for the given instance.
 
- Public Member Functions inherited from by::allocator
 allocator (ncnt blksize=1)
 
ncnt getBlkSize () const
 
- Public Member Functions inherited from by::memoryHaver
nbool has (const instance *it) const BY_SIDE_FUNC(has)
 
nbool isFull () const
 
nbool isCapable () const
 

Static Public Attributes

static constexpr ncnt MIN_SZ = 20
 

Protected Member Functions

void_get (nidx n) override
 
nuchar_getHeap ()
 
const nuchar_getHeap () const BY_CONST_FUNC(_getHeap()) nbool _resize(ncnt new Sz)
 

Detailed Description

Memory chunk for block-based allocation.

The minimum unit class that can actually allocate memory in memlite. All memory management is performed by linking chunks together.

Remarks
Fixed memory size chunk has a _resize() function for flexible memory growth, but following memlite's concept, this is not exposed publicly. Consequently, chunk memory is fixed at object creation. If additional memory is needed, create and operate more chunk objects.

Block size

chunk receives blockSize and size at creation. blockSize is the minimum unit size one instance occupies in memory. size defines how many such instances can fit. For example, to create a chunk holding 100 int64 values:

new chunk(sizeof(int64), 100)
Memory chunk for block-based allocation.
Definition chunk.hpp:57
Rich logging support with polymorphic type conversion.
Definition richLog.hpp:34

Real block size

Actual memory allocation uses real block size instead of block size for optimization. Rather than allocating in small 1 or 2 byte units during CPU operations, it's more efficient to allocate minimum bytes appropriate for the CPU level, like 4 or 8 bytes.

ArrayList implementation

chunk is implemented as an ArrayList. Size is fixed, but addition and deletion are free within size limits, with very fast random access.

Algorithm: 0. Precondition: Each element's byte size must be >= 4. All elements have identical byte sizes. Since chunk handles everything as void*, elements without values are treated as int type.

  1. On array initialization with size, place integer value n+1 before the bytes occupied by nth element. Example: size=4 results in [1, 2, 3, 4]
  2. _head represents index of most recently added element, initialized to 0. _heap points to heap-allocated memory.
  3. When external new1 requests memory allocation, _head is assigned the int value at current _head-th element. Example: first new1() assigns _head the value 1 from _heap[0], meaning next new1() treats _heap[1] as available empty element.
  4. Return address of allocated memory for just-retrieved element. _head = 1, [in-use, 2, 3, 4]
  5. On memory release, receive memory address to release as void* parameter. Example: del(used = _heap[0])
  6. Assign _head value to that memory (assuming destructor already called). *used = _head // [1, 2, 3, 4]
  7. Assign _head to index of currently releasing memory. Calculate index via pointer arithmetic for _heap distance from _head. _head = _heap - used // _heap - _heap --> 0 _head = 0, [1, 2, 3, 4]

Member Function Documentation

◆ _get()

void * by::chunk::_get ( nidx n)
overrideprotectedvirtual
Returns
returns regarding current size, not length. can return garbage if size is bigger than n.

Implements by::memoryHaver.

◆ del()

nbool by::chunk::del ( void * used,
ncnt  )
overridevirtual

Implements by::allocator.

Reimplemented in by::watcher.

◆ has()

nbool by::chunk::has ( const instance & it) const
overridevirtual

Checks if this chunk contains the memory allocated for the given instance.

Parameters
itThe instance whose memory location is to be checked.
Returns
true if the instance's memory is within this chunk, false otherwise.

Implements by::memoryHaver.

◆ len()

ncnt by::chunk::len ( ) const
overridevirtual

Implements by::memoryHaver.

◆ new1()

void * by::chunk::new1 ( )
overridevirtual

Implements by::allocator.

◆ rel()

nbool by::chunk::rel ( )
overridevirtual

Implements by::memoryHaver.

◆ size()

ncnt by::chunk::size ( ) const
overridevirtual

Implements by::memoryHaver.


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