nlib
heap/nmalloc_withstl/nmalloc_withstl.cpp

STLでnmallocを利用するにはNMallocStlAllocator<T>を利用します。 std::vectorを使った場合のヒープの利用状況を、nmalloc_query(kNmallocQueryDump, ...)を用いて表示しています。

/*--------------------------------------------------------------------------------*
Project: CrossRoad
Copyright (C)Nintendo All rights reserved.
These coded instructions, statements, and computer programs contain proprietary
information of Nintendo and/or its licensed developers and are protected by
national and international copyright laws. They may not be disclosed to third
parties or copied or duplicated in any form, in whole or in part, without the
prior written consent of Nintendo.
The content herein is highly confidential and should be handled accordingly.
*--------------------------------------------------------------------------------*/
#include <vector>
#include "nn/nlib/Config.h"
using ::nlib_ns::heap::NMallocStlAllocator;
// vector with a non-default allocator
typedef std::vector<int, NMallocStlAllocator<int> > MyVec;
static bool VectorPushback() {
MyVec vec;
for (int i = 0; i < 10000; ++i) {
vec.push_back(i);
if (i == 5000) {
// You can dump the current status of the heap in XML.
nlib_printf("#\n# heap status: i == 5000\n#\n");
// Note that it shows the cached memory allocated.
// You have to return the memory to the central heap,
// or set NMALLOC_HEAPOPTION_CACHE_DISABLE to heap_option.
nmalloc_query(kNmallocQueryClearCache);
nmalloc_query(kNmallocQueryDump, kNmallocDumpAll, 1);
}
}
// Verifies here
for (int i = 0; i < 10000; ++i) {
if (vec[i] != i) return false;
}
// Dump again.
nlib_printf("#\n# heap status: i == 10000\n#\n");
nmalloc_query(kNmallocQueryClearCache);
nmalloc_query(kNmallocQueryDump, kNmallocDumpAll, 1);
// vec.clear() and Dumps again.
vec.clear();
nlib_printf("#\n# heap status: vec.clear()\n#\n");
nmalloc_query(kNmallocQueryClearCache);
nmalloc_query(kNmallocQueryDump, kNmallocDumpAll, 1); // the vector is free, but memory is not freed.
// vec.clear() does not free the memory for the vector.
MyVec(vec).swap(vec);
nlib_printf("#\n# heap status: fitting\n#\n");
nmalloc_query(kNmallocQueryClearCache);
nmalloc_query(kNmallocQueryDump, kNmallocDumpAll, 1); // memory is freed...
return true;
}
static bool VectorPushbackWithReserve() {
MyVec vec;
vec.reserve(10000);
for (int i = 0; i < 10000; ++i) {
vec.push_back(i);
}
// Verifies here
for (int i = 0; i < 10000; ++i) {
if (vec[i] != i) return false;
}
nlib_printf("#\n# heap status: i == 10000(with reserve)\n#\n");
nmalloc_query(kNmallocQueryClearCache);
nmalloc_query(kNmallocQueryDump, kNmallocDumpAll, 1);
return true;
}
#ifdef NLIB_HAS_VIRTUALMEMORY
extern "C" void nmalloc_get_settings(NMallocSettings* settings) {
settings->addr = NULL;
settings->size = 1024 * 512;
settings->heap_option = 0;
}
#else
const size_t heapmem_size = 1024 * 512;
NLIB_ALIGNAS(4096) static char heapmem[heapmem_size];
extern "C" void nmalloc_get_settings(NMallocSettings* settings) {
settings->addr = heapmem;
settings->size = heapmem_size;
settings->heap_option = 0;
}
#endif
static bool SampleMain(int, char**) { return VectorPushback() && VectorPushbackWithReserve(); }
NLIB_MAINFUNC