アロケータの作り方
STL標準のstd::allocatorには、アラインメントを操作する機能がないので
確保したいメモリのアイランメントを設定したい場合、自前でアロケータを作る必要がある
#include <malloc.h> typedef unsigned long uint32; // デフォで4バイトアラインメント template <class T, uint32 N = 4> class CAllocator : public std::allocator<T> { public: typedef uint32 size_type; typedef T* pointer; typedef const T* const_pointer; typedef T& reference; typedef const T& const_reference; typedef T value_type; // メモリを割り当てる pointer allocate(size_type num, const void* hint = 0) { return (pointer)memalign(N, num * sizeof(T)); } // メモリを解放する void deallocate(pointer p, size_type num) { free(p); } // 割当てることができる最大の要素数を返す size_type max_size() const { struct mallinfo memInfo = mallinfo(); return memInfo.fordblks; } };
max_sizeの処理が正しくないけど、スルーでお願いします^^
mallinfoは、構造体と関数の両方があるので、構造体として使用する場合、C++でもstructを付ける必要があります。