picox  0.1
xstack_allocator.h ファイル

Stack memory allocator. [詳解]

#include <picox/core/xcore.h>
xstack_allocator.h の依存先関係図:
被依存関係図:

[ソースコード]

データ構造

struct  XStackAllocator
 

関数

static size_t xsalloc_alignment (const XStackAllocator *self)
 
void * xsalloc_allocate (XStackAllocator *self, size_t size)
 
static uint8_t * xsalloc_bedin (const XStackAllocator *self)
 
static size_t xsalloc_capacity (const XStackAllocator *self)
 
void xsalloc_clear (XStackAllocator *self)
 
static uint8_t * xsalloc_end (const XStackAllocator *self)
 
static bool xsalloc_growth_direction (XStackAllocator *self)
 
static uint8_t * xsalloc_heap (const XStackAllocator *self)
 
void xsalloc_init (XStackAllocator *self, void *heap, size_t size, size_t alignment)
 
static size_t xsalloc_reserve (const XStackAllocator *self)
 
void xsalloc_rewind (XStackAllocator *self, void *begin, void *end)
 
static void xsalloc_set_growth_direction (XStackAllocator *self, bool growth_upward)
 

詳解

Stack memory allocator.

ヒープ領域をスタックの用にポインタをずらしながらメモリアロケーションを行い ます。 通常のmallocを使用した場合との主な違いは以下の通りです。


[利点]

  1. 超高速

ポインタをずらすだけなので超高速です。

  1. 余分なヘッダ領域を一切使用しない

個別の解放ができないのでヘッダ領域を使用しません。

  1. 絶対に断片化しない

個別の解放ができないので断片化しようがありません。


[欠点]

  1. 個別のメモリ解放ができない

常に直線的にポインタ位置が移動するだけなので、個別にメモリの解放をするとい う概念がありません。


[利用例]

  • 同一タイミングでメモリ破棄が行える時、あるいは行えるようにした時
  • ゲームの1フレーム用スクラッチメモリ

    著者
    MaskedW

関数詳解

static size_t xsalloc_alignment ( const XStackAllocator self)
inlinestatic

初期化時に指定したアラインメントを返します

void* xsalloc_allocate ( XStackAllocator self,
size_t  size 
)

ヒープからsizeバイトのメモリを切り出して返します

覚え書き
確保するメモリサイズはalignmentに切り上げられます。
static uint8_t* xsalloc_bedin ( const XStackAllocator self)
inlinestatic

先頭スタックポインタを返します

static size_t xsalloc_capacity ( const XStackAllocator self)
inlinestatic

ヒープのサイズを返します

void xsalloc_clear ( XStackAllocator self)

ヒープを初期状態に戻します

static uint8_t* xsalloc_end ( const XStackAllocator self)
inlinestatic

終端スタックポインタを返します

static bool xsalloc_growth_direction ( XStackAllocator self)
inlinestatic

スタック伸長方向を返します

static uint8_t* xsalloc_heap ( const XStackAllocator self)
inlinestatic

ヒープメモリを返します

void xsalloc_init ( XStackAllocator self,
void *  heap,
size_t  size,
size_t  alignment 
)

メモリブロックを初期化します

引数
heapスタック管理するメモリ領域
sizeheap領域のサイズ
alignmentメモリ確保のアライメント
事前条件
  • heap != NULL
  • size >= alignment
  • alignment == 2のべき乗
覚え書き
heap領域はalignmentに切り上げられ、さらに、sizeは、alignmentに切り下げられま す。1バイトも無駄にしたくない場合は、heapをあらかじめアラインして確保し、 sizeはalignmentの倍数にしてください。
static size_t xsalloc_reserve ( const XStackAllocator self)
inlinestatic

空きメモリバイト数を返します

void xsalloc_rewind ( XStackAllocator self,
void *  begin,
void *  end 
)

begin, endの位置にスタックポインタを移動します

事前条件
  • end >= begin
  • begin, endはこのオブジェクトのheap領域を指していること。
  • begin, endはalignmentの倍数であること。
static void xsalloc_set_growth_direction ( XStackAllocator self,
bool  growth_upward 
)
inlinestatic

スタック伸長方向を設定します