GL CHOCO ENGINE
Loading...
Searching...
No Matches
linear_allocator.h File Reference

サブシステム等、ライフサイクルが固定で、個別のメモリ開放が不要なメモリ確保に対応するリニアアロケータモジュールの定義 More...

#include <stddef.h>
Include dependency graph for linear_allocator.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct linear_alloc linear_alloc_t
 linear_allocator構造体前方宣言
 

Enumerations

enum  linear_allocator_result_t { LINEAR_ALLOC_SUCCESS = 0 , LINEAR_ALLOC_NO_MEMORY , LINEAR_ALLOC_INVALID_ARGUMENT }
 linear_allocator実行結果コードリスト More...
 

Functions

void linear_allocator_preinit (size_t *memory_requirement_, size_t *align_requirement_)
 linear_alloc_t構造体インスタンスの生成に必要なメモリ使用量とメモリアライメント要件を取得する
 
linear_allocator_result_t linear_allocator_init (linear_alloc_t *allocator_, size_t capacity_, void *memory_pool_)
 linear_alloc_t構造体インスタンスをメモリ容量size_、メモリプール先頭アドレスmemory_pool_で初期化する
 
linear_allocator_result_t linear_allocator_allocate (linear_alloc_t *allocator_, size_t req_size_, size_t req_align_, void **out_ptr_)
 linear_allocatorを使用してメモリを割り当てる
 

Detailed Description

サブシステム等、ライフサイクルが固定で、個別のメモリ開放が不要なメモリ確保に対応するリニアアロケータモジュールの定義

Author
chocolate-pie24

メモリアロケータの一つであるLinearAllocatorを実装。特徴は、

  • 各リソースの個別解放はできず、アロケータが管理するメモリ領域の一括解放しかできない
  • 個別解放をしないことで各リソースごとの割り当て領域管理が不要で高速な割り当てが可能

gl_choco_engineでは、起動時の各サブシステム用メモリの取得に使用する

Note
linear_alloc_t構造体は、内部データを隠蔽している
このため、linear_alloc_t型で変数を宣言することはできない
使用の際は、linear_alloc_t*型で宣言すること
Todo:
linear_allocator_reset追加
Version
0.1
Date
2025-09-16
License
MIT License. See LICENSE file in the project root for full license text.

Typedef Documentation

◆ linear_alloc_t

typedef struct linear_alloc linear_alloc_t

linear_allocator構造体前方宣言

Note
内部データ構造はlinear_allocator.cで定義し、外部からは隠蔽する

Enumeration Type Documentation

◆ linear_allocator_result_t

linear_allocator実行結果コードリスト

Enumerator
LINEAR_ALLOC_SUCCESS 

処理成功

LINEAR_ALLOC_NO_MEMORY 

メモリ不足

LINEAR_ALLOC_INVALID_ARGUMENT 

無効な引数

Function Documentation

◆ linear_allocator_allocate()

linear_allocator_result_t linear_allocator_allocate ( linear_alloc_t allocator_,
size_t  req_size_,
size_t  req_align_,
void **  out_ptr_ 
)

linear_allocatorを使用してメモリを割り当てる

Note
下記の場合は何もしない
  • req_size_ == 0 または req_align_ == 0(結果はLINEAR_ALLOC_SUCCESSでワーニングメッセージを出力)
Warning
  • req_align_ == 0 または req_size_ == 0でワーニング出力し何もしない

使用例:

linear_alloc_t* linear_alloc = NULL; // リニアアロケータ
void* linear_alloc_pool = NULL; // リニアアロケータメモリプール先頭アドレス
size_t linear_alloc_pool_size = 1 * KIB; // リニアアロケータメモリプール容量(1kib)
size_t linear_alloc_mem_req = 0;
size_t linear_alloc_align_req = 0;
linear_allocator_preinit(&linear_alloc_mem_req, &linear_alloc_align_req);
// リニアアロケータの自体のメモリを確保
ret_mem = memory_system_allocate(linear_alloc_mem_req, MEMORY_TAG_SYSTEM, (void**)&linear_alloc);
// リニアアロケータのメモリプールメモリを確保
ret_mem = memory_system_allocate(linear_alloc_pool_size, MEMORY_TAG_SYSTEM, &linear_alloc_pool);
// リニアアロケータ初期化
ret_linear = linear_allocator_init(linear_alloc, linear_alloc_pool_size, linear_alloc_pool);
// メモリ割り当て
void* ptr = NULL;
ret_linear = linear_allocator_allocate(linear_alloc, 128, 8, &ptr); // ptrにアライメント8バイト, 容量128バイトでメモリ割り当て
#define KIB
KiB定義(=1024)
Definition: choco_macros.h:41
memory_system_result_t memory_system_allocate(size_t size_, memory_tag_t mem_tag_, void **out_ptr_)
容量size_のメモリを確保し、mem_tag_で指定されたメモリタグのメモリ使用量を更新する
Definition: choco_memory.c:132
memory_system_result_t
メモリシステム実行結果コードリスト
Definition: choco_memory.h:56
@ MEMORY_SYSTEM_INVALID_ARGUMENT
Definition: choco_memory.h:58
@ MEMORY_TAG_SYSTEM
Definition: choco_memory.h:44
void linear_allocator_preinit(size_t *memory_requirement_, size_t *align_requirement_)
linear_alloc_t構造体インスタンスの生成に必要なメモリ使用量とメモリアライメント要件を取得する
Definition: linear_allocator.c:65
linear_allocator_result_t
linear_allocator実行結果コードリスト
Definition: linear_allocator.h:50
@ LINEAR_ALLOC_INVALID_ARGUMENT
Definition: linear_allocator.h:53
linear_allocator_result_t linear_allocator_init(linear_alloc_t *allocator_, size_t capacity_, void *memory_pool_)
linear_alloc_t構造体インスタンスをメモリ容量size_、メモリプール先頭アドレスmemory_pool_で初期化する
Definition: linear_allocator.c:73
linear_allocator_result_t linear_allocator_allocate(linear_alloc_t *allocator_, size_t req_size_, size_t req_align_, void **out_ptr_)
linear_allocatorを使用してメモリを割り当てる
Definition: linear_allocator.c:88
linear_alloc_t内部データ構造
Definition: linear_allocator.c:32
Parameters
[in]allocator_linear_alloc_t型構造体インスタンスへのポインタ
[in]req_size_割り当て容量(byte)
[in]req_align_割り当てる構造体インスタンスのアライメント要件
[out]out_ptr_割り当てたアドレスを格納する
Return values
LINEAR_ALLOC_INVALID_ARGUMENT以下のいずれか
  • allocator_ == NULL
  • out_ptr_ == NULL
  • *out_ptr_ != NULL
  • req_align_が2の冪乗ではない
  • メモリを割り当てた場合、割り当て開始アドレスの値がオーバーフロー
  • メモリ割り当て先頭アドレス+割り当てサイズの値がオーバーフロー
LINEAR_ALLOC_NO_MEMORYメモリを割り当てた場合、メモリプール内に収まらない
LINEAR_ALLOC_SUCCESS以下のいずれか
  • req_align_ == 0 または req_size_ == 0でワーニング出力し何もしない
  • メモリ割り当てに成功し正常終了

◆ linear_allocator_init()

linear_allocator_result_t linear_allocator_init ( linear_alloc_t allocator_,
size_t  capacity_,
void *  memory_pool_ 
)

linear_alloc_t構造体インスタンスをメモリ容量size_、メモリプール先頭アドレスmemory_pool_で初期化する

Note
  • リニアアロケータ構造体インスタンスは自身のメモリを破棄するAPIを持たない
  • リニアアロケータ構造体インスタンス自体の破棄は上位層で行う

使用例:

linear_alloc_t* linear_alloc = NULL; // リニアアロケータ
void* linear_alloc_pool = NULL; // リニアアロケータメモリプール先頭アドレス
size_t linear_alloc_pool_size = 1 * KIB; // リニアアロケータメモリプール容量(1KiB)
size_t linear_alloc_mem_req = 0;
size_t linear_alloc_align_req = 0;
linear_allocator_preinit(&linear_alloc_mem_req, &linear_alloc_align_req);
// リニアアロケータの自体のメモリを確保
ret_mem = memory_system_allocate(linear_alloc_mem_req, MEMORY_TAG_SYSTEM, (void**)&linear_alloc);
// リニアアロケータのメモリプールメモリを確保
ret_mem = memory_system_allocate(linear_alloc_pool_size, MEMORY_TAG_SYSTEM, &linear_alloc_pool);
// リニアアロケータ初期化
ret_linear = linear_allocator_init(linear_alloc, linear_alloc_pool_size, linear_alloc_pool);
// リニアアロケータメモリプールメモリ破棄
memory_system_free(linear_alloc_pool, linear_alloc_pool_size, MEMORY_TAG_SYSTEM);
// リニアアロケータメモリ破棄
void memory_system_free(void *ptr_, size_t size_, memory_tag_t mem_tag_)
ptr_が保持する領域のメモリを解放し、mem_tag_で指定されたメモリタグのメモリ使用量を更新する
Definition: choco_memory.c:177
Parameters
[in,out]allocator_リニアアロケータ構造体インスタンスアドレス
[in]capacity_メモリプール容量(byte)
[in]memory_pool_メモリプールアドレス
Return values
LINEAR_ALLOC_INVALID_ARGUMENT以下のいずれか
  • 引数allocator_ == NULL
  • 引数memory_pool_ == NULL
  • 引数capacity_ == 0
LINEAR_ALLOC_SUCCESSリニアアロケータの初期化に成功し、正常終了
See also
linear_allocator_preinit
memory_system_allocate
memory_system_free

◆ linear_allocator_preinit()

void linear_allocator_preinit ( size_t *  memory_requirement_,
size_t *  align_requirement_ 
)

linear_alloc_t構造体インスタンスの生成に必要なメモリ使用量とメモリアライメント要件を取得する

Note
  • リニアアロケータのメモリ確保には、メモリシステムを使用する
  • リニアアロケータモジュールがメモリシステムに依存しないよう、上位層にてリニアアロケータのメモリを確保するため本APIを使用する
  • memory_requirement_ == NULLまたは、align_requirement_ == NULLの場合は何もしない
  • 本APIを使用した後、リニアアロケータのメモリを確保し、linear_allocator_initを使用して初期化する

使用例:

size_t memory_requirement = 0; // メモリ使用量格納先
size_t align_requirement = 0; // メモリアライメント要件格納先
linear_allocator_preinit(&memory_requirement, &align_requirement);
Parameters
[out]memory_requirement_メモリ使用量格納先
[out]align_requirement_メモリアライメント要件格納先
See also
memory_system_allocate
linear_allocator_init