nlib
heap/object_tracking/object_tracking.cpp

A sample that associates a 24-bit integer value (color) and name with memory allocated using nmalloc.

You can now associate the following data with any allocated memory of 4096 bytes or more.
  • 24-bit integer value. This value allows a memory region to support RGB color.
  • String of up to 16 bytes, including a null terminating character. This string can contain data including a module name.
Since this function uses an empty region within the metadata for nmalloc, using the function will cause no side effect that memory arrangement on the heap will change.
/*--------------------------------------------------------------------------------*
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.
*--------------------------------------------------------------------------------*/
void* red_ptr[100];
void* green_ptr[100];
void* blue_ptr[100];
static void RedThread(void*) {
for (int i = 0; i < 100; ++i) {
red_ptr[i] = nmalloc(4096);
nmalloc_query(kNmallocQuerySetColor, red_ptr[i], 0xFF0000);
char name[16];
nlib_snprintf(NULL, name, sizeof(name), "red%d", i);
nmalloc_query(kNmallocQuerySetName, red_ptr[i], name);
}
}
static void GreenThread(void*) {
for (int i = 0; i < 100; ++i) {
green_ptr[i] = nmalloc(4096);
nmalloc_query(kNmallocQuerySetColor, green_ptr[i], 0x00FF00);
char name[16];
nlib_snprintf(NULL, name, sizeof(name), "green%d", i);
nmalloc_query(kNmallocQuerySetName, green_ptr[i], name);
}
}
static void BlueThread(void*) {
for (int i = 0; i < 100; ++i) {
blue_ptr[i] = nmalloc(4096);
nmalloc_query(kNmallocQuerySetColor, blue_ptr[i], 0x00FF00);
char name[16];
nlib_snprintf(NULL, name, sizeof(name), "blue%d", i);
nmalloc_query(kNmallocQuerySetName, blue_ptr[i], name);
}
}
static bool SampleMain(int, char**) {
nlib_thread red, green, blue;
e = nlib_thread_create(&red, NULL, RedThread, NULL);
if (e != 0) return false; // no cleanup for simplicity
e = nlib_thread_create(&green, NULL, GreenThread, NULL);
if (e != 0) return false; // no cleanup for simplicity
e = nlib_thread_create(&blue, NULL, BlueThread, NULL);
if (e != 0) return false; // no cleanup for simplicity
for (int i = 0; i < 100; ++i) {
nfree(red_ptr[i]);
nfree(green_ptr[i]);
nfree(blue_ptr[i]);
}
return true;
}
NLIB_MAINFUNC