概要 †
- メモリ操作のためのAPI
- メモリはチャンクと呼ばれる塊単位で扱われ,コンテキスト(メモリチャンクを管理)に紐づくようになっている。
メモリはブロック単位で確保され,メモリチャンクはそのブロック領域から割り当てられる(詳細は,参考リンクの資料を参照)。 - コンテキストは木構造で管理されており,上位のコンテキストを破棄することで,下位のコンテキストの領域もまとめて解放することができる。
メモリコンテキストについては関連ページを参照の事。
関連 PostgreSQL/解析/MemoryContext
定義 †
マクロ †
MemoryContextAllocExtendedのフラグ †
マクロ名 | 説明 |
---|---|
MCXT_ALLOC_HUGE | 1GB以上のアロケーションを許容 |
MCXT_ALLOC_NO_OOM | OutOfMemoryでもエラーにしない |
MCXT_ALLOC_ZERO | 0クリアする |
palloc0fast †
// 引数1:アロケーションサイズ palloc0fast(sz)
- アライメントされていれば,アライメントチェックをスキップする。
- 実際には,MemoryContextAllocZeroAligned()とMemoryContextAllocZero()が呼び分けられる。
参考 palloc0fast - on doxygen.postgresql.org
列挙型 †
構造体 †
MemoryContextCallback †
番号 | データ型 | フィールド | 説明 |
---|---|---|---|
1 | MemoryContextCallbackFunction | func | コールバック関数 |
2 | void* | arg | コールバック関数に渡される引数 |
3 | struct MemoryContextCallback | next | コールバック関数リストの次ノード。 コールバック関数はリスト構造で管理されており複数のコールバック関数を登録できる。 |
MemoryContext †
MemoryContextDataポインタの別名
typedef struct MemoryContextData *MemoryContext;
参考 struct MemoryContextData - on doxygen.postgresql.org
関数 †
MemoryContextAlloc †
// 引数1:アロケーション対象のコンテキスト // 引数2:割当てサイズ(byte) // 戻り値:メモリチャンクのポインタ extern void *MemoryContextAlloc(MemoryContext context, Size size);
- アロケーション処理は,AllocSetAlloc関数で行われる。
- 引数で指定されたメモリコンテキストに指定されたサイズのメモリ領域を確保する。
- チャンクリミットを超える場合は,新規にブロックが作成される。そうでない場合は,フリーリストから検索される。
- アクティブなアロケーションブロックにチャンクスペースがあれば,チャンクをブロックに確保しチャンクのポインタを返す。チャンクスペースがなければ,ブロックの残りの領域を整備しチャンクをフリーリストに置く。
- ブロックがなければ,新規にブロックを作成してチャンクのポインタを返す。
- マクロ定数MaxAllocSizeを超えるサイズはエラーになる。
参考
- AllocSetAlloc() - on doxygen.postgresql.org
- MemoryContextAlloc() - on doxygen.postgresql.org
サンプル
void memory_sample() { // 専用のコンテキストを作成 MemoryContext context = AllocSetContextCreate(CurrentMemoryContext, "mycontext", 0, 8 * 1024, 8 * 1024); MemoryContext oldcontext = NULL; // カレントコンテキストのスイッチ oldcontext = MemoryContextSwitchTo(context); // メモリチャンクが返される MyExtensionOptions *opt = (MyExtensionOptions*) MemoryContextAlloc(context, sizeof(MyExtensionOptions)); // pallocは,内部でCurrentMemoryContextを使う // MyExtensionOptions *opt = (MyExtensionOptions*)palloc(sizeof(MyExtensionOptions)); opt->optcontext = ForeignTableRelationId; opt->optname = pstrdup("option1"); MemoryContextSwitchTo(oldcontext); MemoryContextDelete(context); }
MemoryContextAllocZero †
// 引数1:アロケーション対象のコンテキスト // 引数2:アロケーションサイズ(byte) // 戻り値:メモリチャンクのポインタ extern void *MemoryContextAllocZero(MemoryContext context, Size size);
- MemoryContextAlloc()と同様にメモリ領域をコンキストに確保する。
- 確保されたチャンクを返す前に0初期化される。
- アライメントされているかがチェックされ,memsetをするかどうかが判定される。
MemoryContextAllocZeroAligned †
// 引数1:アロケーション対象のコンテキスト // 引数2:アロケーションサイズ(byte) // 戻り値:メモリチャンクのポインタ extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);
- MemoryContextAlloc()と同様にメモリ領域をコンキストに確保する。
- MemoryContextAllocZero()との違いは,アライメントのチェックがない点。
MemoryContextAllocExtended †
// 引数1:アロケーション対象のコンテキスト // 引数2:アロケーションサイズ(byte) // 引数3:メモリアロケーション動作を決めるフラグ // 戻り値:メモリチャンクのポインタ extern void *MemoryContextAllocExtended(MemoryContext context, Size size, int flags);
- 基本的には,MemoryContextAlloc()と同様にメモリ領域をコンキストに確保する。
- 違いは,フラグによって細かな動作を指定できる点である。
参考 MemoryContextAllocExtended() - on doxygen.postgresql.org
サンプル
typedef struct MyExtensionOptions { const char *optname; Oid optcontext; } MyExtensionOptions; void memory_sample() { MemoryContext context = AllocSetContextCreate(CurrentMemoryContext, "mycontext", 0, 8 * 1024, 8 * 1024); MemoryContext oldcontext = NULL; oldcontext = MemoryContextSwitchTo(context); // 0クリア and メモリ確保失敗時にエラー報告 MyExtensionOptions *opt = (MyExtensionOptions*) MemoryContextAllocExtended(context, sizeof(MyExtensionOptions), MCXT_ALLOC_ZERO | MCXT_ALLOC_NO_OOM); opt->optcontext = ForeignTableRelationId; opt->optname = pstrdup("option1"); MemoryContextSwitchTo(oldcontext); MemoryContextDelete(context); }
palloc †
// 引数1:アロケーションサイズ(byte) // 戻り値:メモリチャンクのポインタ extern void *palloc(Size size);
- MemoryContextAlloc()と同様,現在のコンテキスト(CurrentMemoryContext)にメモリ領域を確保する。
参考 palloc() - on doxygen.postgresql.org
サンプル
MyExtensionOptions *opt = (MyExtensionOptions*)palloc(sizeof(MyExtensionOptions)); // 以下と同様である // MyExtensionOptions * opt = MemoryContextAlloc(CurrentMemoryContext, sizeof(MyExtensionOptions));
palloc0 †
// 引数1:アロケーションサイズ(byte) // 戻り値:メモリチャンクのポインタ extern void *palloc0(Size size);
- MemoryContextAllocZero()と同様,メモリ領域を確保し0クリアして返す。
palloc_extended †
// 引数1:アロケーションサイズ(byte) // 引数2:メモリアロケーション動作を決めるフラグ // 戻り値:メモリチャンクのポインタ extern void *palloc_extended(Size size, int flags);
- MemoryContextAllocExtended()と同様,メモリ領域を確保し0クリアして返す。
repalloc †
// 引数1:メモリチャンクのポインタ // 引数2:再割り当てするサイズ(byte) // 戻り値:新しいメモリチャンクのポインタ extern void *repalloc(void *pointer, Size size);
- reallocの処理のメインは,AllocSetRealloc関数が行なう。
- 既存のメモリチャンクの領域を再割り当てする。
- 既存のメモリチャンクよりsizeが小さい場合は,そのメモリチャンクのrequested_sizeを更新してポインタを返す。
- 既存のメモリチャンクがメモリチャンクリミットより大きかった場合は,チャンクを拡張する。
そうでない場合は,新しくメモリチャンクを確保し,既存のメモリチャンクは破棄する。
参考 AllocSetRealloc() - on doxygen.postgresql.org
サンプル
char *optname = palloc(1 << 4); strcpy(optname, "hello world"); char *newoptname = repalloc(optname, 1 << 5); strcpy(newoptname, "hello world, hello postgres");
pfree †
// 引数1:メモリチャンクのポインタ extern void pfree(void *pointer);
- 処理のメインはAllocSetFree関数で行われる。
- メモリチャンクを解放する。
- メモリチャンクリミットより大きかった場合は,メモリチャンクが属するブロックが解放される。
そうでない場合は,フリーリストに置かれる。
参考 AllocSetFree() - on doxygen.postgresql.org
MemoryContextAllocHuge †
// 引数1:アロケーション対象のコンテキスト // 引数2:アロケーションサイズ(byte) // 戻り値:メモリチャンクのポインタ extern void *MemoryContextAllocHuge(MemoryContext context, Size size);
- MemoryContextAlloc関数と同様だが,MemoryContextAllocよりも大きな上限sizeを指定できる。
- マクロ定数MaxAllocHugeSizeの値までsizeを指定することができる。
MaxAllocHugeSizeは,「-1 >> 1」( SIZE_MAX / 2)となっている。
repalloc_huge †
// 引数1:メモリチャンクのポインタ // 引数2:再割り当てするサイズ(byte) // 戻り値:新しいメモリチャンクのポインタ extern void *repalloc_huge(void *pointer, Size size);
- repalloc関数のHuge版。
MemoryContextSwitchTo †
// 引数1:スイッチするメモリコンテキスト // 戻り値:古い(スイッチする前の)メモリコンテキスト MemoryContext MemoryContextSwitchTo(MemoryContext context)
- メモリコンテキストを引数で指定されたメモリコンテキストにスイッチする。
サンプル
void memory_sample() { MemoryContext context = AllocSetContextCreate(CurrentMemoryContext, "mycontext", 0, 8 * 1024, 8 * 1024); MemoryContext oldcontext = MemoryContextSwitchTo(context); StringInfo strinfo = makeStringInfo(); appendStringInfo(strinfo, "%s", "hoge"); elog(NOTICE, "%s", strinfo->data); MemoryContextSwitchTo(oldcontext); MemoryContextDelete(context); }
MemoryContextRegisterResetCallback †
// 引数1:対象のメモリコンテキスト // 引数2:reset/delete時に呼ばれるコールバック関数 extern void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb);
- MemoryContextDelete関数やMemoryContextReset関数呼び出し時に実行されるコールバック関数をメモリコンテキストに登録する。
コールバック関数はリスト構造となっており,複数のコールバック関数を登録できる。
参考 struct MemoryContextCallback - on doxygen.postgresql.org
サンプル
// callback function void report_context_delete(void *arg) { MemoryContext context = (MemoryContext)arg; elog(NOTICE, "%s will be deleted", context->name); } void memory_sample() { MemoryContext context = AllocSetContextCreate(CurrentMemoryContext, "mycontext", 0, 8 * 1024, 8 * 1024); // コールバック関数の宣言と初期化 MemoryContextCallback callback = { report_context_delete, // Function Pointer context, // Argument }; // コールバック関数の登録 MemoryContextRegisterResetCallback(context, &callback); MemoryContext oldcontext = MemoryContextSwitchTo(context); char *optname = palloc(16); strcpy(optname, "hello world"); MemoryContextSwitchTo(oldcontext); // report_context_delete(context) is called MemoryContextDelete(context); }
MemoryContextStrdup †
// 引数1:対象のメモリコンキスト // 引数2:複製するデータ // 戻り値:複製されたデータのポインタ extern char *MemoryContextStrdup(MemoryContext context, const char *string);
- 引数で指定されたデータの複製を指定されたメモリコンテキストに割り当てる。
具体的には,stringサイズを指定してMemoryContextAlloc関数を呼び,返されたメモリチャンクにデータstringを複製する。
参考 MemoryContextStrdup() - on doxygen.postgresql.org
pstrdup †
// 引数1:複製するデータ // 戻り値:複製されたデータのポインタ extern char *pstrdup(const char *string);
- MemoryContextStrdup関数と同様で,現在のメモリコンテキストに対して行われる。
pnstrdup †
// 引数1:複製するデータ // 引数2:複製するデータの長さ // 戻り値:複製されたデータのポインタ extern char *pnstrdup(const char *in, Size len);
- pstrdup関数と同様だが,終端のヌルバイト ('\0') が追加される。
サンプル
void sample() { const char *string = "hello world"; size_t len = strlen(string); // pstrdup char *str = pstrdup(string); str[len] = '\0'; elog(NOTICE, "str = %s", str); // pnstrdup char *str2 = pnstrdup(string, strlen(string)); elog(NOTICE, "str2 = %s", str2); }
psprintf †
// 引数:format書式定文字列,文字列 // 戻り値:フォーマットされた文字列のポインタ extern char *psprintf(const char *fmt,...) pg_attribute_printf(1, 2);
- sprintfタイプの文字列フォーマットを行なう。
- 格納領域に必要なサイズは内部で計算される。初期サイズは,128byteで計算される。
参考 psprintf() - on doxygen.postgresql.org
サンプル
char *formatted_str = psprintf("Hello %s", "Mike"); elog(NOTICE, "%s", formatted_str); // Hello Mike
pvsnprintf †
// 引数1:データを格納する領域のポインタ // 引数2:書き込みされる最大サイズ // 引数3:フォーマット書式指定文字列 // 引数4:書き込みするデータ // 戻り値:書き込み長さ(nprinted)が, // ・len(NULL終端含む)より小さい場合は,nprinted // ・lenより大きい場合且つMaxAllocSize -2以下の場合は,nprinted+2 // ・lenがMaxAllocSize / 2以上の場合は,MaxAllocSize // ・それ以外は,nprinted * 2 extern size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3, 0);
- vsnprintfのラッパーでC99非対応環境を考慮した実装。
- エラー発生時(nprintedが負数)は,elog(ERROR)となる(FRONTENDはexit())。
参考 pvsnprintf() - on doxygen.postgresql.org
サンプルプログラム †
// オプション用の構造体 typedef struct MyExtensionOptions { const char *optname; Oid optcontext; } MyExtensionOptions; // delete時に呼ばれるコールバック関数 void report_context_delete(void *arg) { MemoryContext context = (MemoryContext)arg; elog(NOTICE, "%s will be deleted", context->name); } // サンプル void memory_sample2() { MemoryContext oldcontext = NULL; // 独自のコンテキスト作成 MemoryContext context = AllocSetContextCreate(CurrentMemoryContext, "mycontext", 0, 8 * 1024, 8 * 1024); // delete時のコールバック関数を登録 MemoryContextCallback callback = { report_context_delete, context }; MemoryContextRegisterResetCallback(context, &callback); // カレントコンテキストを作成したメモリコンテキストに切り替え oldcontext = MemoryContextSwitchTo(context); // カレントコンテキストで色々と操作 StringInfo strinfo = makeStringInfo(); appendStringInfo(strinfo, "%s", "hoge"); MyExtensionOptions *opt = (MyExtensionOptions*)palloc(sizeof(MyExtensionOptions)); opt->optcontext = ForeignTableRelationId; opt->optname = psprintf("format_%s", "csv"); // カレントコンテキストを切り替え前に戻す MemoryContextSwitchTo(oldcontext); // 作成したメモリコンテキストを破棄する MemoryContextDelete(context); }
参考・関連 †
- palloc.h - on doxygen.postgresql.org
- memnodes.h - on doxygen.postgresql.org
- mcxt.c - on doxygen.postgresql.org
- aset.c - on doxygen.postgresql.org
関連