フィールドのポインタから、オブジェクト全体のポインタを取得するためのマクロ
構造体やクラスのインスタンスの特定のフィールドのアドレス(ポインタ)だけ分かっている場合に、そこからオブジェクト全体のポインタを取得したい場合に使うマクロ。
やっていることは単にフィールドのアドレスから、そのフィールドの(クラスor構造体の)先頭からのオフセットを引いているだけ。
// type.fieldが、typeの先頭の何バイト目に配置されているかを求める #define field_offset(type, field) \ ((char*)&(((type*)NULL)->field) - (char*)NULL) // type.filedのアドレス(field_ptr)から、fieldのオフセット分マイナスし、typeのアドレスを求める #define obj_ptr(type, field, field_ptr) \ ((type*)((char*)field_ptr - field_offset(type, field)))
メモリアロケータを自作する際に、割当メモリ領域にタグ付けするのに利用できそうだと思い作成。
/* main.cc */ #include <iostream> // タグ付きメモリチャンク struct chunk { struct { // タグ情報: const char* type; // - 型名 int size; // - サイズ } tag; void* buf; }; // メモリ割り当て inline void* mem_allocate(const char* type, int size) { chunk* c = (chunk*)new char[sizeof(chunk::tag)+size]; // タグ情報を保存する c->tag.type = type; c->tag.size = size; std::cout << "[" << c->tag.type << "] allocate: " << c->tag.size << " bytes" << std::endl; // 割り当てアドレスを返す return &c->buf; } // メモリ解放 void mem_free(void* p) { // 解放するアドレス(chunk::buf)から、タグ情報(chunk)を取得する chunk* c = obj_ptr(chunk, buf, p); std::cout << "[" << c->tag.type << "] free: " << c->tag.size << " bytes" << std::endl; delete c; } struct abc { int a; char b; long c; }; // main関数 int main() { void* ptrs[3]; std::cout << "# allocate:" << std::endl; ptrs[0] = new (mem_allocate("int", sizeof(int))) int; ptrs[1] = new (mem_allocate("pointer", sizeof(void*))) void*; ptrs[2] = new (mem_allocate("struct", sizeof(abc))) abc; std::cout << std::endl; std::cout << "# free:" << std::endl; for(int i=2; i >=0; i--) mem_free(ptrs[i]); return 0; }
$ g++ -o main main.cc $ ./main # allocate: [int] allocate: 4 bytes [pointer] allocate: 8 bytes [struct] allocate: 16 bytes # free: [struct] free: 16 bytes [pointer] free: 8 bytes [int] free: 4 bytes