nlib
NMalloc.h File Reference

File that defines functions including nmalloc and nfree. More...

#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <new>
#include "nn/nlib/Config.h"

Go to the source code of this file.

Classes

struct  HeapHash
 Structure that contains a summary of the memory usage status of the heap used by the user application. More...
 
struct  NMallocSettings
 Declares parameters that are initialized by nmalloc. Set by defining nmalloc_get_settings. More...
 

Namespaces

 nn::nlib
 Implements stream-related classes usually commonly used, various containers, and other gadget classes. nlib_ns is an alias.
 
 nn::nlib::heap
 Namespace for the heap library. Functions such as nmalloc and nfree are defined in the global namespace.
 

Macros

#define NMALLOC_HEAPOPTION_ENABLE_ENV   (0x00000001)
 Enables the setting to be overwritten through the environment variable.
 
#define NMALLOC_HEAPOPTION_CACHE_DISABLE   (0x00000004)
 Disables caching with CachedHeap. More...
 
#define NLIB_REPLACE_MALLOC
 A macro that defines functions like nlib_malloc to use nmalloc.
 
#define NLIB_REPLACE_MALLOC_NEW
 A macro that defines functions like nlib_malloc to use nmalloc, and also defines new and delete that way.
 

Typedefs

typedef int(* nmalloc_heapwalk_callback) (void *allocated_ptr, size_t size, void *user_ptr)
 User-defined callback function that is called from nmalloc_walk_allocated_ptrs. More...
 
Errors

Utilities related to error values.

typedef int errno_t
 Indicates with an int-type typedef that a POSIX error value is returned as the return value.
 

Enumerations

enum  NMallocDumpMode {
  kNmallocDumpBasic = 0,
  kNmallocDumpSpans = 1,
  kNmallocDumpPointers = 2,
  kNmallocDumpPageSummary = 4,
  kNmallocDumpAll = kNmallocDumpSpans | kNmallocDumpPointers | kNmallocDumpPageSummary
}
 The type of the value that you specify for the second argument when specifying kNmallocQueryDump in the nmalloc_query() function. More...
 

Functions

NLIB_CHECK_RESULT void * nrealloc (void *ptr, size_t size)
 Changes the memory allocation. Corresponds to the standard C function realloc. More...
 
NLIB_CHECK_RESULT void * nmalloc (size_t size)
 Allocates a memory region of the specified size (in bytes). This corresponds to the standard C function, malloc. More...
 
NLIB_CHECK_RESULT void * ncalloc (size_t nmemb, size_t size)
 Allocates an array of memory and elements initialized to 0. More...
 
NLIB_CHECK_RESULT void * nmalloc_aligned (size_t size, size_t algn)
 Allocates memory with a specific alignment. More...
 
size_t nmalloc_size (const void *ptr)
 Returns the amount of memory actually allocated at the ptr parameter. More...
 
void nfree (void *p)
 Frees a memory region. Corresponds to the standard C function free. More...
 
void nfree_size (void *p, size_t size)
 Frees a memory region. Using information about memory sizes makes it possible to free memory quickly. More...
 
errno_t nmalloc_walk_allocated_ptrs (nmalloc_heapwalk_callback func, void *user_ptr)
 The callback function func is called for each region allocated in the heap. More...
 
bool operator== (const HeapHash &rhs, const HeapHash &lhs)
 Returns true if the two compared summaries are equal.
 
bool operator!= (const HeapHash &rhs, const HeapHash &lhs)
 Returns true if the two compared summaries are not equal.
 
nmalloc Initialization and Finalization
void nmalloc_get_settings (NMallocSettings *settings)
 User applications can define this function to control the initialization settings of nmalloc. More...
 
Error-Checking and Debugging Functions
errno_t nmalloc_query (int query,...)
 Gets and operates detailed data on the heap. More...
 

Detailed Description

File that defines functions including nmalloc and nfree.

Description
nmalloc and nfree have the following characteristics.
  • Thread cache. Using thread-local storage reduces the need for locks by caching allocatable memory without locking.
  • Low-fragmentation heaps. By grouping packets for each allocation size into memory blocks, packets can be allocated such that the size matches the requested allocation size, which in turn reduces memory fragmentation.
  • Multiple levels of free lists. Maintaining free lists for each size enables high-speed, best-fit allocation.

Definition in file NMalloc.h.