nlib
nn::nlib::heap Namespace Reference

Namespace for the heap library. Functions such as nmalloc and nfree are defined in the global namespace. More...

Classes

class  CachedHeap
 Thread-specific cache class whose use is paired with CentralHeap. More...
 
class  CentralHeap
 Central heap class whose use is paired with CachedHeap. It is used to implement nmalloc. More...
 
class  FrameHeap
 Allows memory to be allocated from both sides of the heap. More...
 
class  StackHeap
 Allocator that allocates memory from the top like a stack. More...
 
class  UnitHeap
 Heap for allocating and freeing fixed-size regions of memory. This is a so-called "pool-allocator.". More...
 

Detailed Description

Namespace for the heap library. Functions such as nmalloc and nfree are defined in the global namespace.

About nmalloc() and nfree()

nmalloc and nfree both have the following characteristics.
  • Equipped with multi-level free lists that keep in check drops in speed during actual usage.
  • Efficiently allocate and free small regions of memory.
    • Using per-thread memory caching speeds up memory allocation and free operations.
    • Fragmentation is mitigated by grouping regions by memory size.
    • Overhead of header information has been reduced to improve space efficiency.
    • Implements a mechanism that reduces the risk of different cores accessing the same cache line.
  • Provides a check feature for double free detection, write detection after calling free, and metadata overwrites in heap.
    • It is difficult to mangle and illegally use pointers when memory regions are linked to the free list after calling free.
  • Using nmalloc_aligned enables allocation of aligned memory regions.
  • nrealloc is an implementation of realloc that operates with reasonable efficiency.
  • Specify nmalloc_query(NMALLOC_QUERY_HEAP_HASH, &hash) to easily get a hash of the overall heap status.
  • Traverse all of the pointers that were allocated using nmalloc by calling nmalloc_walk_allocated_ptrs. It is possible to deal with leaked memory by forcibly freeing it, for example.

About CentralHeap and CachedHeap

The nmalloc and nfree functions are implemented using nn::nlib::heap::CentralHeap and nn::nlib::heap::CachedHeap. There is one CentralHeap object per process and one CachedHeap object per thread, which correspond to nmalloc and nfree, and code within a process shares these objects. Alternatively, developers can create these objects and heaps directly on their own, and use these heaps to allocate and free memory.

About other heaps

Several special-purpose heaps have also been provided.
These heaps are intended to be used to improve speed; they are not completely intended for general-purpose use. For example, to use from multiple threads, memory allocation and deallocation processes must each be locked. If CachedHeap is used, this type of lock may not be necessary. In addition, when a program is first created, even if memory was intended to be used as single-thread-specific memory, the ownership of the allocated memory may have to be passed to another thread. Note that for this reason, using CentralHeap and CachedHeap may be more immediate and faster.
Also refer to the heap/nmalloc_simple/nmalloc_simple.cpp sample.