STLでnmallocを利用するにはNMallocStlAllocator<T>
を利用します。 std::vector
を使った場合のヒープの利用状況を、nmalloc_dumpex()
を用いて表示しています。
#include <vector>
using ::nlib_ns::heap::NMallocStlAllocator;
typedef std::vector<int, NMallocStlAllocator<int> > MyVec;
static bool vector_pushback() {
MyVec vec;
for (int i = 0; i < 10000; ++i) {
vec.push_back(i);
if (i == 5000) {
}
}
for (int i = 0; i < 10000; ++i) {
if (vec[i] != i) return false;
}
vec.clear();
MyVec(vec).swap(vec);
return true;
}
static bool vector_pushback_with_reserve() {
MyVec vec;
vec.reserve(10000);
for (int i = 0; i < 10000; ++i) {
vec.push_back(i);
}
for (int i = 0; i < 10000; ++i) {
if (vec[i] != i) return false;
}
nlib_printf(
"#\n# heap status: i == 10000(with reserve)\n#\n");
return true;
}
#ifdef NLIB_HAS_VIRTUALMEMORY
settings->
size = 1024 * 512;
}
#else
const size_t heapmem_size = 1024 * 512;
settings->addr = heapmem;
settings->size = heapmem_size;
settings->heap_option = 0;
}
#endif
static bool SampleMain(int, char**) { return vector_pushback() && vector_pushback_with_reserve(); }
NLIB_MAINFUNC