Allocates and frees memory by explicitly using nn::nlib::CachedHeap
and nn::nlib::CentralHeap
.
These classes are used implicitly when using nmalloc
and nfree
, but you can also use them explicitly. This sample demonstrates how to use this method.
By using them explicitly instead of via nmalloc
, you can use multiple CentralHeap
objects (one for each module) instead of having a shared CentralHeap
for all processes. This approach also makes it possible to use multiple CachedHeap
objects within a single thread.
using nlib_ns::threading::Thread;
using nlib_ns::heap::CentralHeap;
using nlib_ns::heap::CachedHeap;
const size_t heapmem_size = 1024 * 512;
CentralHeap g_CentralHeap;
class HeapSetup {
public:
HeapSetup() {
g_CentralHeap.Init(&heapmem[0], heapmem_size, 0);
}
~HeapSetup() {
g_CentralHeap.Finalize();
}
};
volatile bool g_Success = true;
static void ThreadFunc(void* ptr) {
NLIB_UNUSED(ptr);
CachedHeap heap;
g_Success = false;
return;
}
const int n = 1000;
void* p[n];
for (int j = 0; j < 1000; ++j) {
for (int i = 0; i < n; ++i) {
p[i] = heap.Alloc(8);
if (!p[i]) g_Success = false;
}
for (int i = 0; i < n; ++i) {
heap.Free(p[i]);
}
}
}
const int kNumThread = 10;
Thread th[kNumThread];
static bool SampleMain(int, char**) {
HeapSetup obj;
{
uint64_t from, to;
for (int i = 0; i < kNumThread; ++i) {
th[i].StartRaw(ThreadFunc, NULL);
}
for (int i = 0; i < kNumThread; ++i) {
th[i].Join();
}
nlib_printf(
"Small: using CachedHeap: %" PRIu64
" msec\n", to - from);
}
return g_Success;
}
NLIB_MAINFUNC