Marmos
v juptune
v asn1
v lang
v ast
v Classes
v Enums
v ir
v parser
v Enums
v Structs
v core
v crypto
v keyexchange
v memory
v data
v alphabet
v buffer
v json
v builder
v parser
v Enums
v Structs
v serialise
v Structs
v Templates
v pem
v Enums
v Structs

Structs MemoryBlockPool

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module juptune.core.ds.block;

struct MemoryBlockPool
{
    /++ ALIASES ++/
    
    ///
    alias __xdtor = __MARMOS_BUG__;
    ///
    alias __xpostblit = __MARMOS_BUG__;
    
    /++ FUNCTIONS ++/
    
    /// Allocates a certain amount of blocks from the pool, at a specific power of two size. 
    Result allocate(ulong powerOfTwo, ulong blockCount, MemoryBlockAllocation allocation);
    ///
    MemoryBlockPool opAssign(MemoryBlockPool p);
    /// Preallocates a certain amount of blocks from the system, at a specific power of two size. 
    Result preallocateBlocks(ulong powerOfTwo, ulong blockCount);
    
    /++ MANIFEST CONSTANTS ++/
    
    ///
    enum int BLOCK_BUCKETS = 15;
    /// Maximum block size, in power of two 
    enum int MAX_BLOCK_POWER = 22;
    /// Maximum block size, in bytes 
    enum int MAX_BLOCK_SIZE = 4194304;
    /// Minimum block size, in power of two 
    enum int MIN_BLOCK_POWER = 8;
    /// Minimum block size, in bytes 
    enum int MIN_BLOCK_SIZE = 256;
    
    /++ ENUMS ++/
    
    /// Result ` error enum 
    enum Errors : int
    {
        ///
        none = 0, 
        /// Returned whenever preallocateBlocks fails due to lack of system resources. 
        outOfMemory = 1, 
        /// Returned whenever allocate fails due to lack of free blocks. 
        notEnoughBlocks = 2
    }
    
}

Documentation

A pool used to manage blocks of memory of varying sizes.

Notes

This pool is not thread safe.

It is not safe to move this pool once any memory has been allocated from it.

This pool is not garbage collected. Once this pool's dtor is called, all memory allocated by this pool is freed. This also means that any allocations will become unsafe to use after the pool's dtor is called.

Additionally memory blocks are not made aware to the GC, so please do not store GC pointers into the blocks.

This pool is weakly safe in the sense that the user's management of memory is sane, and also trusted in the sense that calculations within the pool are correct.

Block sizes are powers of two, and are in the range [256, 1024 1024 4] inclusive.

Usage

First you must preallocate blocks into the pool using preallocateBlocks.

You can then allocate blocks from the pool using allocate, and free the blocks by calling .freeor .__xdtoron the resulting allocation object.

Due to the nature of the pool's memory model, it may be more efficient to allocate a larger number of blocks at once rather than allocating them one at a time.

Currently there's not really any way to free data once preallocated beyond calling the destructor but this of course frees the entire set of data. This is a TODO since it hurts my brain when thinking about the overall flow of a program.

Memory model

"Preallocation" of blocks simply means that the pool will allocate a large chunk of memory from the system, where the size is dependent on the amount of blocks requested, and then split that memory up to form the resulting memory blocks.

Each preallocation contains blocks for a single power of two.

The layout of the memory is as follows, where the top is the start of the allocation:

[AllocationMetadata (internal struct)] [n MemoryBlocks (internal struct)] [n*(1 << powerOfTwo) bytes]

The AllocationMetadatastruct is used to form a linked list of preallocation so that the pool can free all of its data when it is destroyed.

The MemoryBlockstruct is used to form a linked list of free blocks; store the underlying ubyte slice from the preallocation, and other misc internal metadata.

The n*(1 << powerOfTwo) bytesis the unstructured chunk of memory that is split equally amongst each MemoryBlock. This is where your user data lives.

Additionally this pool only contains a linked list of free blocks, and does not keep track of allocated blocks.

Free block lists are stored within a static array of buckets, where each bucket represents a power of two, for easy and fast lookup.

Aliases

Name Description
__xdtor
__xpostblit

Constants

Name Description
BLOCK_BUCKETS
MAX_BLOCK_POWER

Maximum block size, in power of two

MAX_BLOCK_SIZE

Maximum block size, in bytes

MIN_BLOCK_POWER

Minimum block size, in power of two

MIN_BLOCK_SIZE

Minimum block size, in bytes

Enums

Name Description
Errors

Result` error enum

Functions

Name Description
allocate

Allocates a certain amount of blocks from the pool, at a specific power of two size.

opAssign
preallocateBlocks

Preallocates a certain amount of blocks from the system, at a specific power of two size.