PostgreSQL/開発

概要

定義

マクロ

MemoryContextAllocExtendedのフラグ

マクロ名説明
MCXT_ALLOC_HUGE
MCXT_ALLOC_NO_OOM
MCXT_ALLOC_ZERO

列挙型

構造体

MemoryContextCallback

番号データ型フィールド説明
1MemoryContextCallbackFunctionfuncコールバック関数
2void*argコールバック関数に渡される引数
3struct MemoryContextCallbacknextコールバック関数リストの次ノード。
コールバック関数はリスト構造で管理されており複数のコールバック関数を登録できる。

MemoryContext

MemoryContextDataポインタの別名

// 引数1:アロケーションサイズ
palloc0fast(sz)

 参考  struct MemoryContextData - on doxygen.postgresql.org

関数

MemoryContextAlloc

typedef struct MemoryContextData *MemoryContext;

 参考  AllocSetAlloc() - on doxygen.postgresql.org

 サンプル 

// 引数1:アロケーション対象のコンテキスト
// 引数2:割当てサイズ(byte)
// 戻り値:メモリチャンクのポインタ
extern void *MemoryContextAlloc(MemoryContext context, Size size);

MemoryContextAllocZero

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);
}

MemoryContextAllocZeroAligned

// 引数1:アロケーション対象のコンテキスト
// 引数2:アロケーションサイズ(byte)
// 戻り値:メモリチャンクのポインタ
extern void *MemoryContextAllocZero(MemoryContext context, Size size);

MemoryContextAllocExtended

// 引数1:アロケーション対象のコンテキスト
// 引数2:アロケーションサイズ(byte)
// 戻り値:メモリチャンクのポインタ
extern void *MemoryContextAllocZeroAligned(MemoryContext context, Size size);

 参考  MemoryContextAllocExtended () - on doxygen.postgresql.org

 サンプル 

// 引数1:アロケーション対象のコンテキスト
// 引数2:アロケーションサイズ(byte)
// 引数3:メモリアロケーション動作を決めるフラグ
// 戻り値:メモリチャンクのポインタ
extern void *MemoryContextAllocExtended(MemoryContext context, Size size, int flags);

palloc

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() - on doxygen.postgresql.org

 サンプル 

// 引数1:アロケーションサイズ(byte)
// 戻り値:メモリチャンクのポインタ
extern void *palloc(Size size);

palloc0

MyExtensionOptions *opt  = (MyExtensionOptions*)palloc(sizeof(MyExtensionOptions));
// 以下と同様である
// MyExtensionOptions  * opt = MemoryContextAlloc(CurrentMemoryContext, sizeof(MyExtensionOptions));

palloc_extended

// 引数1:アロケーションサイズ(byte)
// 戻り値:メモリチャンクのポインタ
extern void *palloc0(Size size);

repalloc

// 引数1:アロケーションサイズ(byte)
// 引数2:メモリアロケーション動作を決めるフラグ
// 戻り値:メモリチャンクのポインタ
extern void *palloc_extended(Size size, int flags);

 参考  AllocSetRealloc() - on doxygen.postgresql.org

 サンプル 

// 引数1:メモリチャンクのポインタ
// 引数2:再割り当てするサイズ(byte)
// 戻り値:新しいメモリチャンクのポインタ
extern void *repalloc(void *pointer, Size size);

pfree

char *optname = palloc(1 << 4);
strcpy(optname, "hello world");
char *newoptname = repalloc(optname, 1 << 5);
strcpy(newoptname, "hello world, hello postgres");

 参考  AllocSetFree () - on doxygen.postgresql.org

MemoryContextAllocHuge

// 引数1:メモリチャンクのポインタ
extern void pfree(void *pointer);

repalloc_huge

// 引数1:アロケーション対象のコンテキスト
// 引数2:アロケーションサイズ(byte)
// 戻り値:メモリチャンクのポインタ
extern void *MemoryContextAllocHuge(MemoryContext context, Size size);

MemoryContextSwitchTo

// 引数1:メモリチャンクのポインタ
// 引数2:再割り当てするサイズ(byte)
// 戻り値:新しいメモリチャンクのポインタ
extern void *repalloc_huge(void *pointer, Size size);

MemoryContextRegisterResetCallback

// 引数1:スイッチするメモリコンテキスト
// 戻り値:古い(スイッチする前の)メモリコンテキスト
MemoryContext MemoryContextSwitchTo(MemoryContext context)

MemoryContextStrdup

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);
}

pstrdup

// 引数1:対象のメモリコンテキスト
// 引数2:reset/delete時に呼ばれるコールバック関数
extern void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb);

pnstrdup

// 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);
}

psprintf

// 引数1:対象のメモリコンキスト
// 引数2:複製するデータ
// 戻り値:複製されたデータのポインタ
extern char *MemoryContextStrdup(MemoryContext context, const char *string);

pvsnprintf

// 引数1:複製するデータ
// 戻り値:複製されたデータのポインタ
extern char *pstrdup(const char *string);

サンプルプログラム

// 引数1:複製するデータ
// 引数2:複製するデータの長さ
// 戻り値:複製されたデータのポインタ
extern char *pnstrdup(const char *in, Size len);

参考・関連

 関連 

コメント



トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
目次
ダブルクリックで閉じるTOP | 閉じる
GO TO TOP