Dynamic memory management. More...
Defines | |
#define | NUTMEM_GUARD_BYTES 0 |
#define | NUT_HEAP_OVERHEAD (sizeof(HEAPNODE) - sizeof(HEAPNODE *)) |
Number of bytes used for management information. | |
#define | NUTMEM_HEAPNODE_MIN (sizeof(HEAPNODE) + (2 * NUTMEM_GUARD_BYTES)) |
Minimum size of a node. | |
Functions | |
void * | malloc (size_t len) |
Allocate a block from heap memory. | |
void | free (void *p) |
Return a block to heap memory. | |
void * | realloc (void *ptr, size_t len) |
Reallocate a block from heap memory. | |
void * | NutHeapRootAlloc (HEAPNODE **root, size_t size) |
Allocate a block from heap memory. | |
void * | NutHeapRootAllocClear (HEAPNODE **root, size_t size) |
Allocate an initialized block from heap memory. | |
int | NutHeapRootFree (HEAPNODE **root, void *block) |
Return a block to heap memory. | |
void | NutHeapRootAdd (HEAPNODE **root, void *addr, size_t size) |
Add a new memory region to the heap. | |
size_t | NutHeapRootAvailable (HEAPNODE **root) |
Return the total number of bytes available. | |
size_t | NutHeapRootRegionAvailable (HEAPNODE **root) |
Return the size of the largest block available. | |
void * | NutHeapRootRealloc (HEAPNODE **root, void *block, size_t size) |
Change the size of an allocated memory block. | |
int | NutHeapCheck (void) |
Check consistency of heap. | |
void | NutHeapDump (void *stream) |
Dump heap memory to a given stream. | |
Variables | |
HEAPNODE * | heapFreeList |
List of free nodes in normal memory. |
Dynamic memory management.
Dynamic memory allocations are made from the heap. The heap is a global resource containing all of the free memory in the system. The heap is handled as a linked list of unused blocks of memory, the so called free-list.
The heap manager uses best fit, address ordered algorithm to keep the free-list as unfragmented as possible. This strategy is intended to ensure that more useful allocations can be made. We end up with relatively few large free blocks rather than lots of small ones.
#define NUTMEM_GUARD_BYTES 0 |
Referenced by NutHeapRootAlloc(), NutHeapRootFree(), and NutHeapRootRealloc().
Number of bytes used for management information.
Referenced by NutHeapRootAlloc(), NutHeapRootAvailable(), NutHeapRootFree(), NutHeapRootRealloc(), and NutHeapRootRegionAvailable().
#define NUTMEM_HEAPNODE_MIN (sizeof(HEAPNODE) + (2 * NUTMEM_GUARD_BYTES)) |
Minimum size of a node.
Referenced by NutHeapRootAlloc(), and NutHeapRootRealloc().
void* malloc | ( | size_t | len | ) |
Allocate a block from heap memory.
This function simply calls NutHeapAlloc(). It overrides the function of the runtime library, when the application is linked with nutcrt or nutcrtf.
len | Size of the requested memory block. |
References ENOMEM, errno, NULL, and NutHeapAlloc.
Referenced by _fdopen(), _putf(), AllocConcatStringLen(), AllocConcatStrings(), AsnOidBuild(), At45dbPageWrite(), At45dbParamRead(), At45dbParamWrite(), At49bvParamWrite(), At91EfcParamWrite(), At91SpiBusNodeInit(), Avr32FlashcParamWrite(), Avr32SpiBusNodeInit(), AvrSpiBus0NodeInit(), basename(), calloc(), CreateFilePath(), CreateFullPathName(), dirname(), DiscoveryResponder(), DS1307RamWrite(), EditHistoryCreate(), FeederThread(), findenv(), fopen(), freopen(), GetParentPath(), GpioRegisterIrqHandler(), HttpArgParseNext(), HttpArgReadNext(), HttpdClientHandler(), HttpParseHeader(), HttpParseMultipartHeader(), HttpSendRedirection(), HttpSsiExecHandler(), HttpSsiProcessFile(), IOExpInit(), LiLiInsertItemAfterNode(), LiLiInsertItemBeforeNode(), Lpc17xxIapParamWrite(), MediaTypeHandlerBinary(), MibRegisterIfVars(), NotifyTask(), NutBlockDeviceOpen(), NutChat_P(), NutChatCreate(), NutChatSend(), NutDhcpClient(), NutDnsGetResource(), NutFtpOpenSession(), NutFtpServerSession(), NutFtpTransferDirectoryOptions(), NutFtpTransferFile(), NutHttpProcessAsp(), NutHttpProcessPostQuery(), NutHttpProcessQueryString(), NutHttpProcessRequest(), NutHttpURLEncode(), NutIpRouteAdd(), NutIpRouteList(), NutRegisterAuth(), NutRegisterCgi(), NutRegisterFtpRoot(), NutRegisterHttpRoot(), NutRegisterKey(), NutRegisterLed(), NutSmtpLogin(), NutSNTPStartThread(), NutTcpDeviceWrite(), opendir(), openlog(), PerCiAttachReader(), PerCiDump(), PerCiWriteVarList(), PhatDirCreate(), PhatDirDelEntry(), PhatDirEntryFind(), PhatDirEntryStatus(), PhatDirOpen(), PhatDirRead(), PhatDirRemove(), PhatDirRenameEntry(), PhatFileOpen(), PhatVolMount(), RawFsFileOpen(), RawFsMount(), s_printf(), s_vputs(), setenv(), SnmpAgent(), SnmpCommunityCreate(), SnmpMibFind(), SnmpMibRegister(), SnmpPduAddVariable(), SnmpPduCreate(), SnmpSessionSendPdu(), SnmpViewCreate(), Spi7segInit(), Spi7SegOpen(), SpiAt45dConfigRead(), SpiAt45dConfigWrite(), SSDPTask(), Stm32UsartBusNodeInit(), strdup(), StreamClientAccept(), StreamCopy(), StreamInfo(), TermInit(), TermOpen(), time(), Tlv320DacWrite(), UsartOpen(), UxmlNodeAddAttrib(), UxmlNodeCreate(), UxmlParseStream(), VsCodecOpen(), and X12EepromWrite().
void free | ( | void * | p | ) |
Return a block to heap memory.
This function simply calls NutHeapFree(). It overrides the function of the runtime library, when the application is linked with nutcrt or nutcrtf.
p | Points to a memory block previously allocated through a call to malloc(). |
References NutHeapFree.
Referenced by _putf(), AsnOidBuild(), At45dbPageWrite(), At45dbParamRead(), At45dbParamWrite(), At49bvParamWrite(), At91EfcParamWrite(), Avr32FlashcParamWrite(), closedir(), closelog(), DestroyRequestInfo(), DS1307RamWrite(), EditHistoryCreate(), EditHistoryDestroy(), EditHistoryInsert(), EditHistorySet(), EdLineClose(), EnvRegisterVariable(), fclose(), FeederThread(), findenv(), fopen(), freopen(), HttpArgParseNext(), HttpArgReadNext(), HttpdClientHandler(), HttpParseHeader(), HttpParseMultipartHeader(), HttpRegisterAuthBasic(), HttpRegisterCgiFunction(), HttpRegisterRedir(), HttpRegisterRootPath(), HttpSendRedirection(), HttpSessionInfo(), HttpSsiExecHandler(), HttpSsiIncludeHandler(), HttpSsiProcessFile(), ISC_LIST(), LiLiClean(), LiLiDestroy(), LiLiDestroyStringItemCopy(), LiLiRemoveNode(), Lpc17xxIapParamWrite(), MediaTypeHandlerBinary(), MmCardMount(), MmCardUnmount(), NutBlockDeviceClose(), NutBlockDeviceOpen(), NutChat(), NutChat_P(), NutChatDestroy(), NutClearAuth(), NutDhcpClient(), NutDnsConfig2(), NutDnsGetResource(), NutFtpCloseSession(), NutFtpOpenSession(), NutFtpProcessCwd(), NutFtpProcessRequest(), NutFtpRenameAction(), NutFtpRenamePrepare(), NutFtpServerSession(), NutFtpTransferDirectoryOptions(), NutFtpTransferFile(), NutHttpProcessAsp(), NutHttpProcessPostQuery(), NutHttpProcessRequest(), NutIpRouteDel(), NutIpRouteDelAll(), NutRegisterAuth(), NutRegisterCgiBinPath(), NutRegisterFtpRoot(), NutRegisterFtpUser(), NutRegisterHttpRoot(), NutRegisterOwiBus_BB(), NutRegisterTwiBus(), NutSmtpDisconnect(), NutSNTPGetTime(), NutSNTPStartThread(), NutTcpDestroySocket(), NutTcpDeviceWrite(), NutUdpDestroySocket(), NutWinsNameQuery(), opendir(), PerCiClose(), PerCiDetachReader(), PerCiDump(), PerCiInit(), PerCiOpen(), PerCiWriteVarList(), PhatDirCreate(), PhatDirDelEntry(), PhatDirEntryFind(), PhatDirEntryStatus(), PhatDirOpen(), PhatDirOpenParent(), PhatDirRead(), PhatDirRemove(), PhatDirRenameEntry(), PhatFileClose(), PhatFileOpen(), PhatVolMount(), PhatVolUnmount(), putenv(), RawFsFileClose(), RawFsFileOpen(), RawFsMount(), RawFsUnmount(), s_printf(), s_vputs(), setenv(), SnmpAgent(), SnmpMibFind(), SnmpPduAddVariable(), SnmpPduCreate(), SnmpPduDestroy(), SnmpSessionClose(), SnmpSessionOpen(), SnmpSessionSendPdu(), SNTP_resync(), SpiAt45dConfigRead(), SpiAt45dConfigWrite(), SpiMmcMount(), SpiMmcUnmount(), StreamClientAccept(), StreamClientThread(), StreamCopy(), StreamInfo(), TapClose(), TermClose(), time(), Tlv320DacWrite(), unsetenv(), UsartClose(), UsartOpen(), UxmlNodeAddAttrib(), UxmlParseStream(), VsCodecClose(), and X12EepromWrite().
void* realloc | ( | void * | ptr, |
size_t | len | ||
) |
Reallocate a block from heap memory.
This function simply calls NutHeapRealloc(). It overrides the function of the runtime library, when the application is linked with nutcrt or nutcrtf.
ptr | Pointer to memory block previously allocated with malloc to be reallocated. If this is NULL, a new block is allocated. |
len | Size of the requested memory block. |
References ENOMEM, errno, NULL, and NutHeapRealloc.
Referenced by HttpSsiEchoHandler().
void* NutHeapRootAlloc | ( | HEAPNODE ** | root, |
size_t | size | ||
) |
Allocate a block from heap memory.
This functions allocates a memory block of the specified size and returns a pointer to that block.
The actual size of the allocated block is larger than the requested size because of space required for maintenance information. This additional information is invisible to the application.
The routine looks for the smallest block that will meet the required size and releases it to the caller. If the block being requested is usefully smaller than the smallest free block then the block from which the request is being met is split in two. The unused portion is put back into the free-list.
The contents of the allocated block is unspecified. To allocate a block with all bytes set to zero use NutHeapAllocClear().
root | Points to the linked list of free nodes. |
size | Size of the requested memory block. |
References _HEAPNODE::hn_next, _HEAPNODE::hn_size, NULL, NUT_HEAP_OVERHEAD, NUTMEM_GUARD_BYTES, NUTMEM_HEAPNODE_MIN, and NUTMEM_TOP_ALIGN.
Referenced by NutHeapRootAllocClear(), and NutHeapRootRealloc().
void* NutHeapRootAllocClear | ( | HEAPNODE ** | root, |
size_t | size | ||
) |
Allocate an initialized block from heap memory.
This functions allocates a memory block of the specified size with all bytes initialized to zero and returns a pointer to that block.
root | Points to the linked list of free nodes. |
size | Size of the requested memory block. |
References memset(), and NutHeapRootAlloc().
int NutHeapRootFree | ( | HEAPNODE ** | root, |
void * | block | ||
) |
Return a block to heap memory.
An application calls this function, when a previously allocated memory block is no longer needed.
The heap manager checks, if the released block adjoins any other free regions. If it does, then the adjacent free regions are joined together to form one larger region.
root | Points to the linked list of free nodes. |
block | Points to a memory block previously allocated. |
References _HEAPNODE::hn_next, _HEAPNODE::hn_size, NULL, NUT_HEAP_OVERHEAD, NUTMEM_GUARD_BYTES, and NUTPANIC().
Referenced by NutHeapRootAdd(), and NutHeapRootRealloc().
void NutHeapRootAdd | ( | HEAPNODE ** | root, |
void * | addr, | ||
size_t | size | ||
) |
Add a new memory region to the heap.
This function can be called more than once to manage non-continous memory regions. It is automatically called by Nut/OS during initialization.
addr | Start address of the memory region. |
size | Number of bytes of the memory region. |
References _HEAPNODE::hn_size, NULL, NutHeapRootFree(), NUTMEM_BOTTOM_ALIGN, and NUTMEM_TOP_ALIGN.
size_t NutHeapRootAvailable | ( | HEAPNODE ** | root | ) |
Return the total number of bytes available.
References _HEAPNODE::hn_next, _HEAPNODE::hn_size, NUT_HEAP_OVERHEAD, and rc.
size_t NutHeapRootRegionAvailable | ( | HEAPNODE ** | root | ) |
Return the size of the largest block available.
References _HEAPNODE::hn_next, _HEAPNODE::hn_size, NUT_HEAP_OVERHEAD, and rc.
void* NutHeapRootRealloc | ( | HEAPNODE ** | root, |
void * | block, | ||
size_t | size | ||
) |
Change the size of an allocated memory block.
If more memory is requested than available at that block the data is copied to a new, bigger block.
block | Points to a previously allocated memory block. If NULL, then this call is equivalent to NutHeapRootAlloc(). |
size | The requested new size. If 0, then this call is equivalent to NutHeapRootFree(). |
References _HEAPNODE::hn_next, _HEAPNODE::hn_size, memcpy(), NULL, NUT_HEAP_OVERHEAD, NutHeapRootAlloc(), NutHeapRootFree(), NUTMEM_GUARD_BYTES, NUTMEM_HEAPNODE_MIN, and NUTMEM_TOP_ALIGN.
int NutHeapCheck | ( | void | ) |
Check consistency of heap.
Right now this function will just return 0 unless NUTDEBUG_HEAP is defined.
void NutHeapDump | ( | void * | stream | ) |
Dump heap memory to a given stream.
References fprintf(), _HEAPNODE::hn_next, and _HEAPNODE::hn_size.
List of free nodes in normal memory.
Referenced by NutDumpHeap().