- 追加された行はこの色です。
- 削除された行はこの色です。
#author("2017-05-22T22:37:28+09:00","default:haikikyou","haikikyou")
#author("2017-05-23T22:36:13+09:00","default:haikikyou","haikikyou")
[[PostgreSQL/開発]]
#contents
* 概要 [#d39eb51e]
- 概要説明
- メモリアロケーションのユーティリティ機能
* 定義 [#x1a86688]
** マクロ [#qcffa5c6]
*** MaxAllocSize, MaxAllocHugeSize [#r5e25b72]
|~マクロ名|~説明|~値|h
|MaxAllocSize||0|
|MaxAllocHugeSize||(8 * 1024 * 1024)|
|MaxAllocSize|アロケーションの最大サイズ(1G-1)。&br;pallocでは,このサイズがリミットである。&br;|(Size) 0x3fffffff|
|MaxAllocHugeSize|アロケーションの最大サイズ(SIZE_MAX/2)。&br;XXXHogeやXXX_huge系の関数ではこのリミットとなる。&br;|(Size) -1 >> 1|
*** AllocSizeIsValid, AllocHugeSizeIsValid [#o711b550]
*** STANDARDCHUNKHEADERSIZE [#l8764aec]
*** MemoryContextResetAndDeleteChildren [#u74855f2]
*** ALLOCSET_DEFAULT_XXXX [#i7676be1]
|~定数|~説明|~値|h
|ALLOCSET_DEFAULT_MINSIZE||0|
|ALLOCSET_DEFAULT_INITSIZE||(8 * 1024)|
|ALLOCSET_DEFAULT_MAXSIZE||(8 * 1024 * 1024)|
*** ALLOCSET_SMALL_XXXX [#x8b6a559]
|~定数|~説明|~値|h
|ALLOCSET_SMALL_MINSIZE||0|
|ALLOCSET_SMALL_INITSIZE||(1 * 1024)|
|ALLOCSET_SMALL_MAXSIZE||(8 * 1024)|
*** ALLOCSET_SEPARATE_THRESHOLD [#v16bd8d2]
** 列挙型 [#a91733b0]
** 構造体 [#j3ab1cd0]
*** Struct [#pd8e61e3]
|~番号|~データ型|~フィールド|~説明|h
** 関数 [#u75fdaa2]
*** MemoryContextInit [#zfc402dc]
#geshi(c){{
extern void MemoryContextInit(void);
}}
- メモリコンテスト管理システムの初期化を行なう。
- postmasterのスタートアップ時に呼ばれるので,拡張機能などでこの関数を呼ぶケースはない(すでにメモリコンテキスト管理が利用できる状態になっているため)。
- この処理の中で,TopMemoryContextというメモリコンテキストツリーのトープレベルのメモリコンテキストとErrorContextが作成される。
&size(12){&color(white,orange){ 参考 };}; [[MemoryContextInit()>https://doxygen.postgresql.org/mcxt_8c.html#a90e9ce77120b687f9197ec3f42556e7b]] - on doxygen.postgresql.org
*** MemoryContextReset [#r2f7ee24]
#geshi(c){{
// 引数1:対象のメモリコンテキスト
extern void MemoryContextReset(MemoryContext context);
}}
- 指定されたメモリコンテキスト及び子階層に割り当てられたメモリ領域を解放する。
- 子階層のメモリコンテキストに対しては,MemoryContextDelete()(AllocSetDelete())を再帰的に呼ぶ。
- 対象のメモリコンテキスト(context)については,MemoryContextResetOnly()(AllocSetReset())が呼ばれる。contextの最初のブロック(存在すれば)は残され,メモリチャンクは消去される(freeptrは先頭にポイントされる)。
&size(12){&color(white,orange){ 参考 };};
- [[AllocSetDelete()>https://doxygen.postgresql.org/aset_8c.html#a800b4ddef4d9f1bc24252d34af22971c]] - on doxygen.postgresql.org
- [[AllocSetReset()>https://doxygen.postgresql.org/aset_8c.html#a4184c05efdf010f2c86bb057f4bc41a9]] - on doxygen.postgresql.org
*** MemoryContextDelete [#j584a692]
#geshi(c){{
// 引数1:対象のメモリコンテキスト
extern void MemoryContextDelete(MemoryContext context);
}}
- 指定されたメモリコンテキストのメモリ領域を解放する。
- 子階層のメモリコンテキストも再帰的に処理される。
&size(12){&color(white,orange){ 参考 };}; [[MemoryContextDelete()>https://doxygen.postgresql.org/mcxt_8c.html#adabf8bdc427b4e273e7445b4701f51f2]] - on doxygen.postgresql.org
*** MemoryContextResetOnly [#x6978405]
#geshi(c){{
// 引数1:対象のメモリコンテキスト
extern void MemoryContextResetOnly(MemoryContext context);
}}
- 指定されたメモリコンテキストをリセットする(AllocSetReset()が呼ばれる)。
- 子階層のメモリコンテキストに対しては何もしない。
*** MemoryContextResetChildren [#g76807a0]
#geshi(c){{
// 引数1:対象のメモリコンテキスト
extern void MemoryContextResetChildren(MemoryContext context);
}}
- 指定されたメモリコンテキストの子階層のメモリコンテキストに対して,再帰的にMemoryContextResetOnly関数を呼ぶ。
*** MemoryContextDeleteChildren [#q7ca2c17]
#geshi(c){{
// 引数1:対象のメモリコンテキスト
extern void MemoryContextDeleteChildren(MemoryContext context);
}}
- 指定されたメモリコンテキストの子階層のメモリコンテキストに対して,再帰的にMemoryContextDelete関数を呼ぶ。
&size(12){&color(white,#00afcc){ サンプル };};
#geshi(c){{{
#define BLOCK_SIZE 1024 * 8
void reset_callback(void *arg)
{
MemoryContext context = (MemoryContext)arg;
elog(NOTICE, "%s will be deleted.", context->name);
}
void sample()
{
MemoryContext oldcontext = NULL;
MemoryContextCallback callback;
MemoryContextCallback callback2;
// init parent context
MemoryContext parent_context = AllocSetContextCreate(CurrentMemoryContext,
"parent context",
0,
BLOCK_SIZE,
BLOCK_SIZE);
callback.func = reset_callback;
callback.arg = parent_context;
MemoryContextRegisterResetCallback(parent_context, &callback);
oldcontext = MemoryContextSwitchTo(parent_context);
// parent context from here
StringInfo strinfo = makeStringInfo();
appendStringInfo(strinfo, "Hello %s", "Duke");
elog(NOTICE, "%s", strinfo->data);
// init child context
MemoryContext child_context = AllocSetContextCreate(parent_context,
"child context",
0,
BLOCK_SIZE,
BLOCK_SIZE);
callback2.func = reset_callback;
callback2.arg = child_context;
MemoryContextRegisterResetCallback(child_context, &callback2);
MemoryContextSwitchTo(child_context);
// child context from here
StringInfo strinfo2 = makeStringInfo();
appendStringInfo(strinfo2, "Hello %s", "Togo");
elog(NOTICE, "%s", strinfo2->data);
// switch back to original context.
MemoryContextSwitchTo(oldcontext);
// delete parent_context and child_context
//
// CurrentMemoryContext
// |- parent_context <--- delete
// |- child_context <--- delete
MemoryContextDeleteChildren(CurrentMemoryContext);
}
}}}
*** MemoryContextSetParent [#b9de69d5]
#geshi(c){{
// 引数1:
// 引数1:対象のメモリコンテキスト
// 引数2:新しい親となるメモリコンテキスト
extern void MemoryContextSetParent(MemoryContext context, MemoryContext new_parent);
/* Change a context to belong a new parent.
== Before ==
new_parent current_parent
|- context1 |- context <-- move to (o)
|- context2 |- next_context
== After ==
new_parent current_parent
|- context (o) |- next_context
|- context1
|- context2
*/
}}
- 説明
- 指定されたメモリコンテキスト(context)を新しいメモリコンテスト(new_parent)に付け替える。
&size(12){&color(white,orange){ 参考 };}; [[MemoryContextSetParent()>https://doxygen.postgresql.org/mcxt_8c.html#a38bd1f1dd1d024e312d5e5dabd68831d]] - on doxygen.postgresql.org
*** GetMemoryChunkSpace [#h49cf6cc]
#geshi(c){{
// 引数1:
extern Size GetMemoryChunkSpace(void *pointer);
}}
- 説明
*** GetMemoryChunkContext [#s051b011]
#geshi(c){{
// 引数1:
extern MemoryContext GetMemoryChunkContext(void *pointer);
}}
- 説明
*** MemoryContextGetParent [#o8a8b1a8]
#geshi(c){{
// 引数1:
extern MemoryContext MemoryContextGetParent(MemoryContext context);
}}
- 説明
*** MemoryContextIsEmpty [#lac3b573]
#geshi(c){{
// 引数1:
extern bool MemoryContextIsEmpty(MemoryContext context);
}}
- 説明
*** MemoryContextStats [#p2c2599b]
#geshi(c){{
// 引数1:
extern void MemoryContextStats(MemoryContext context);
}}
- 説明
*** MemoryContextAllowInCriticalSection [#l9fe4341]
#geshi(c){{
// 引数1:
extern void MemoryContextAllowInCriticalSection(MemoryContext context, bool allow);
}}
- 説明
*** MemoryContextCheck [#ka9bb5bd]
#geshi(c){{
// 引数1:
extern void MemoryContextCheck(MemoryContext context);
}}
- 説明
*** MemoryContextContains [#n111b2d3]
#geshi(c){{
// 引数1:
extern bool MemoryContextContains(MemoryContext context, void *pointer);
}}
- 説明
*** MemoryContextCreate [#a79de8dc]
#geshi(c){{
// 引数1:
extern MemoryContext MemoryContextCreate(NodeTag tag, Size size,
MemoryContextMethods *methods,
MemoryContext parent,
const char *name);
}}
- 説明
*** AllocSetContextCreate [#lc8a5772]
#geshi(c){{
// 引数1:
extern MemoryContext AllocSetContextCreate(MemoryContext parent,
const char *name,
Size minContextSize,
Size initBlockSize,
Size maxBlockSize);
}}
* サンプルプログラム [#j0b18196]
#geshi(c){{
// sample
}}
* 参考・関連 [#vceb7bae]
- [[aset.c>https://doxygen.postgresql.org/aset_8c.html]] - on doxygen.postgresql.org
- [[mcxt.c>https://doxygen.postgresql.org/mcxt_8c.html]] - on doxygen.postgresql.org
- [[palloc.h>PostgreSQL/開発/include/utils/palloc.h]]
&size(12){&color(white,orange){ 関連 };};
#related
* コメント [#m0e0ed05]
#comment