Choco OS  V.0.16.9.0
Join to the chocolate world
oc_memman.c File Reference

The file with sources for memory manager. More...

#include <oc_memman.h>
#include <oc_intman.h>
#include <oc_mem_lld.h>
#include <oc_sys_lld.h>
#include <oc_bits.h>
#include <oc_memory_cfg.h>
#include <oc_threadman.h>
#include <oc_array.h>
#include <oc_null.h>
#include <oc_ktime.h>
#include <oc_ifnot.h>
#include <oc_debug.h>
#include <string.h>

Go to the source code of this file.

Macros

#define OVERFLOW_PROTECTION_MAGIC_NUMBER
 
#define foreach_block(block)
 
#define LAST_WORD_OF_BUFFER(Buffer, Size)
 
#define ALIGN_SIZE(SIZE, ALIGNMENT)   ((((oC_UInt_t)SIZE) + ALIGNMENT - 1) & ~(ALIGNMENT-1))
 

Typedefs

typedef struct _Block_t Block_t
 
typedef struct _HeapMap_t HeapMap_t
 

Enumerations

Functions

static bool BlockModule (AllocationFlags_t Flags)
 
static bool UnblockModule (void)
 
static bool WaitForMemory (HeapMap_t *Map, oC_UInt_t Size, oC_UInt_t Alignment, AllocationFlags_t Flags)
 
static oC_ErrorCode_t InitializePointers (bool FillZero)
 
static oC_ErrorCode_t InitializeHeapMap (HeapMap_t *Map, void *HeapStartPointer, void *HeapEndPointer, oC_UInt_t HeapSize, bool FillZero)
 
static Block_tFindBlockOfAddress (const void *Address)
 
static Block_tFindBlockContainsAddress (const void *Address)
 
static oC_UInt_t GetIndexOfHeapPointer (HeapMap_t *Map, oC_UInt_t *HeapPointer)
 
static bool IsAddressOfHeapMap (HeapMap_t *Map, const void *Address)
 
static bool IsBlockCorrect (Block_t *Block)
 
static bool IsHeapUsed (HeapMap_t *Map, oC_UInt_t *HeapPointer)
 
static void DumpHeapMap (HeapMap_t *Map, oC_MemMan_DumpFunction_t DumpFunction)
 
static bool IsHeapMapInitialized (HeapMap_t *Map)
 
static void SetHeapState (HeapMap_t *Map, oC_UInt_t *HeapPointer, HeapState_t HeapState)
 
static bool CheckIfHeapInRangeIsInState (HeapMap_t *Map, oC_UInt_t *HeapFrom, oC_UInt_t *HeapTo, HeapState_t HeapState)
 
static void SetHeapStateInRange (HeapMap_t *Map, oC_UInt_t *HeapFrom, oC_UInt_t *HeapTo, HeapState_t HeapState)
 
static oC_UInt_t * FindNextHeapPointerInState (HeapMap_t *Map, oC_UInt_t *HeapPointer, oC_UInt_t *HeapEndLimit, HeapState_t HeapState)
 
static oC_UInt_t GetSizeOfHeapInState (HeapMap_t *Map, HeapState_t HeapState)
 
static oC_UInt_t GetSizeOfHeap (HeapMap_t *Map)
 
static void * FindFreeHeap (HeapMap_t *Map, oC_UInt_t Size, oC_UInt_t Alignment)
 
static void * RawAlloc (HeapMap_t *Map, oC_UInt_t Size, oC_UInt_t Alignment)
 
static bool RawFree (HeapMap_t *Map, void *Address, oC_UInt_t Size)
 
static bool IsMemoryValid (void *Start, oC_UInt_t Size)
 tests memory for the heap usage
 
static void CallAllocatorEvent (Allocator_t Allocator, void *Address, MemoryEventFlags_t EventFlags, const char *Function, uint32_t LineNumber)
 
static void CallEvent (void *Address, MemoryEventFlags_t EventFlags, const char *Function, uint32_t LineNumber)
 
static void MemoryFaultInterrupt (void)
 
static void BusFaultInterrupt (void)
 
static bool CanReleaseMemory (Block_t *Block)
 
oC_ErrorCode_t oC_MemMan_TurnOn (void)
 turns on memory manager More...
 
oC_ErrorCode_t oC_MemMan_TurnOff (void)
 turns off the module More...
 
void oC_MemMan_CheckOverflow (void)
 scan each block to check if the overflow event not occurs. More...
 
void oC_MemMan_CheckMemoryExhausted (void)
 checks if memory exhausted event not occurs. More...
 
void oC_MemMan_CheckMemoryLeak (void)
 checks if memory is not leaking More...
 
oC_ErrorCode_t oC_MemMan_TestAndRepairMainHeap (oC_MemMan_DumpFunction_t DumpFunction)
 diagnoses main heap More...
 
oC_HeapMap_t oC_MemMan_AllocateHeapMap (oC_UInt_t Size, Allocator_t Allocator, const char *Function, uint32_t LineNumber, AllocationFlags_t Flags)
 allocates memory for new heap map More...
 
bool oC_MemMan_FreeHeapMap (oC_HeapMap_t *MapPointer, AllocationFlags_t Flags)
 release heap map More...
 
void * oC_MemMan_RawAllocate (oC_HeapMap_t Map, oC_UInt_t Size, const char *Function, uint32_t LineNumber, AllocationFlags_t Flags)
 allow to allocate memory in heap map More...
 
bool oC_MemMan_RawFree (oC_HeapMap_t Map, void *Address, oC_UInt_t Size)
 release memory in heap map More...
 
void * oC_MemMan_Allocate (oC_UInt_t Size, Allocator_t Allocator, const char *Function, uint32_t LineNumber, AllocationFlags_t Flags, oC_UInt_t Alignment)
 allocates memory on heap More...
 
bool oC_MemMan_Free (void *Address, AllocationFlags_t Flags)
 release allocated memory More...
 
bool oC_MemMan_FreeAllMemoryOfAllocator (Allocator_t Allocator)
 release all memory of allocator More...
 
oC_ErrorCode_t oC_MemMan_SetEventHandler (MemoryEventHandler_t EventHandler)
 sets event handler function More...
 
Allocator_t oC_MemMan_GetAllocatorOfAddress (const void *Address)
 returns allocator of address More...
 
oC_UInt_t oC_MemMan_GetSizeOfAllocation (const void *Address)
 returns size of allocation More...
 
void * oC_MemMan_AlignAddress (const void *Address)
 returns address aligned to the machine alignment. More...
 
void * oC_MemMan_AlignAddressTo (const void *Address, oC_UInt_t Alignment)
 returns address aligned to the given alignment. More...
 
bool oC_MemMan_IsAddressAligned (const void *Address)
 checks if address is aligned More...
 
bool oC_MemMan_IsAddressAlignedTo (const void *Address, oC_UInt_t Alignment)
 checks if address is aligned to given alignment More...
 
oC_UInt_t oC_MemMan_AlignSize (oC_UInt_t Size)
 returns size aligned to the machine alignment More...
 
oC_UInt_t oC_MemMan_AlignSizeTo (oC_UInt_t Size, oC_UInt_t Alignment)
 returns size aligned to the given alignment More...
 
bool oC_MemMan_IsSizeAligned (oC_UInt_t Size)
 checks if a size is aligned More...
 
oC_UInt_t oC_MemMan_GetMaximumAllocationSize (void)
 returns maximum size of allocation More...
 
oC_UInt_t oC_MemMan_GetFlashSize (void)
 returns size of the machine flash More...
 
oC_UInt_t oC_MemMan_GetRamSize (void)
 returns size of the machine ram More...
 
oC_UInt_t oC_MemMan_GetFreeFlashSize (void)
 returns size of the machine not used flash More...
 
oC_UInt_t oC_MemMan_GetFreeRamSize (void)
 returns size of free ram More...
 
oC_UInt_t oC_MemMan_GetExternalHeapSize (void)
 returns size of external ram More...
 
oC_UInt_t oC_MemMan_GetDmaRamHeapSize (void)
 returns size of external ram More...
 
oC_UInt_t oC_MemMan_GetMemoryOfAllocatorSize (Allocator_t Allocator)
 returns size of allocations per allocator More...
 
oC_UInt_t oC_MemMan_GetAllocationBlockSize (void)
 returns size of block needed for allocation More...
 
oC_UInt_t oC_MemMan_GetHeapMapSize (oC_HeapMap_t Map)
 returns size of heap map More...
 
oC_UInt_t oC_MemMan_GetFreeHeapMapSize (oC_HeapMap_t Map)
 returns size of free memory in heap map More...
 
bool oC_MemMan_IsFlashAddress (const void *Address)
 checks if address is placed in the flash section
 
bool oC_MemMan_IsUsedFlashAddress (const void *Address)
 checks if address is in used flash section
 
bool oC_MemMan_IsRamAddress (const void *Address)
 checks if address is in ram section
 
bool oC_MemMan_IsDynamicAllocatedAddress (const void *Address)
 checks if address is in dynamic allocated section
 
bool oC_MemMan_IsStaticRamAddress (const void *Address)
 checks if address is in static used ram section (data section)
 
bool oC_MemMan_IsAddressCorrect (const void *Address)
 checks if address is correct - in RAM or in FLASH
 
bool oC_MemMan_IsAllocatorCorrect (Allocator_t Allocator)
 checks if allocator is correct
 
oC_ErrorCode_t oC_MemMan_ReadAllocatorsStats (oC_MemMan_AllocatorsStats_t *outAllocatorsArray, oC_UInt_t *Size)
 Reads allocators statistics array. More...
 
oC_ErrorCode_t oC_MemMan_ReadAllocationsStats (Allocator_t Allocator, oC_MemMan_AllocationStats_t *outAllocationsArray, oC_UInt_t *Size, bool JoinSimilar)
 Reads allocations statistics of the allocator. More...
 
bool oC_MemMan_FindOverflowedAllocation (Allocator_t Allocator, oC_MemMan_AllocationStats_t *outAllocationStat)
 searches for a overflowed buffer More...
 
oC_ErrorCode_t oC_MemMan_ConfigureExternalHeapMap (void *StartAddress, oC_UInt_t Size)
 prepares HeapMap stored in external RAM More...
 
oC_ErrorCode_t oC_MemMan_UnconfigureExternalHeapMap (void)
 unconfigures external heap More...
 
float oC_MemMan_GetMemoryExhaustedLimit (void)
 
float oC_MemMan_GetPanicMemoryExhaustedLimit (void)
 
oC_ErrorCode_t oC_MemMan_SetMemoryExhaustedLimit (float LimitPercent)
 
oC_ErrorCode_t oC_MemMan_SetPanicMemoryExhaustedLimit (float LimitPercent)
 

Variables

static bool ModuleTestModeEnabledFlag = false
 
static bool ModuleEnabledFlag = false
 
static uint32_t ModuleBusyFlag = false
 
static bool FirstPowerOn = false
 
static uint32_t * DataOverflowProtection = NULL
 
static Block_tBlockListHead = NULL
 
static Block_tBlockListTail = NULL
 
static MemoryEventHandler_t ModuleEventHandler = NULL
 
static HeapMap_t HeapMap
 
static HeapMap_t ExternalHeapMap
 
static HeapMap_t DmaHeapMap
 

Detailed Description


Author
Patryk Kubiak - (Created on: 6 06 2015 12:15:56)
Note
Copyright (C) 2015 Patryk Kubiak patry.nosp@m.k.ku.nosp@m.biak9.nosp@m.0@gm.nosp@m.ail.c.nosp@m.om
           This program is free software; you can redistribute it and/or modify
           it under the terms of the GNU General Public License as published by
           the Free Software Foundation; either version 2 of the License, or
           (at your option) any later version.

           This program is distributed in the hope that it will be useful,
           but WITHOUT ANY WARRANTY; without even the implied warranty of
           MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
           GNU General Public License for more details.

           You should have received a copy of the GNU General Public License
           along with this program; if not, write to the Free Software
           Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

Definition in file oc_memman.c.

Macro Definition Documentation

#define ALIGN_SIZE (   SIZE,
  ALIGNMENT 
)    ((((oC_UInt_t)SIZE) + ALIGNMENT - 1) & ~(ALIGNMENT-1))

The macro returns size aligned to the current memory alignment

Definition at line 102 of file oc_memman.c.

#define foreach_block (   block)

This is the macro that helps to prepare loop for each block in the global block list.

Parameters
blockname of the block variable, that will be defined for each iteration

Example of usage:

{
if(block->Allocator == Allocator)
{
found = true;
break;
}
}

Definition at line 76 of file oc_memman.c.

#define LAST_WORD_OF_BUFFER (   Buffer,
  Size 
)

Reference to the last word of buffer on heap.

Parameters
BufferBuffer pointer
SizeSize of the buffer in bytes

Example of usage:

uint8_t Buffer[10];
LAST_WORD_OF_BUFFER(Buffer,10) = 0xFF;

Definition at line 95 of file oc_memman.c.

#define OVERFLOW_PROTECTION_MAGIC_NUMBER

This is the special number, that helps to protect allocated memory before overflow.

Definition at line 54 of file oc_memman.c.

Typedef Documentation

typedef struct _Block_t Block_t

The structure that is created for each allocation.

typedef struct _HeapMap_t HeapMap_t

Structure that stores pointers for handling heap

Real start of machine heap _ _ +--------------------------------—+ | | Bit map - each bit in this section is reserved for one word (oC_UInt_t) | BIT MAP | in HEAP section. If bit is set, then the word is used (allocated) | | +--------------------------------—+ | | Heap - memory for allocations. | HEAP | | | Real end of machine heap _ _ +--------------------------------—+

Enumeration Type Documentation

Variable that stores information about usage of heap

Enumerator
HeapState_NotUsed 

heap is not used

HeapState_Used 

heap is used

Definition at line 134 of file oc_memman.c.

Function Documentation

static bool BlockModule ( AllocationFlags_t  Flags)
static

Blocks module to be unavailable for the others threads (mutex lock)

Returns
true if block is blocked for current thread (if success)

Definition at line 1923 of file oc_memman.c.

static void BusFaultInterrupt ( void  )
static

Interrupt that is called when the bus fault occurs

Definition at line 2607 of file oc_memman.c.

static void CallAllocatorEvent ( Allocator_t  Allocator,
void *  Address,
MemoryEventFlags_t  EventFlags,
const char *  Function,
uint32_t  LineNumber 
)
static

Calls event of the allocator.

Definition at line 2567 of file oc_memman.c.

static void CallEvent ( void *  Address,
MemoryEventFlags_t  EventFlags,
const char *  Function,
uint32_t  LineNumber 
)
static

Calls event of the module.

Definition at line 2584 of file oc_memman.c.

static bool CanReleaseMemory ( Block_t Block)
static

Checks if current thread is allowed for releasing the memory

Definition at line 2617 of file oc_memman.c.

static bool CheckIfHeapInRangeIsInState ( HeapMap_t Map,
oC_UInt_t *  HeapFrom,
oC_UInt_t *  HeapTo,
HeapState_t  HeapState 
)
static

Checks if heap in range is in state

Definition at line 2333 of file oc_memman.c.

static void DumpHeapMap ( HeapMap_t Map,
oC_MemMan_DumpFunction_t  DumpFunction 
)
static

The function is for dumping heap map using function that is given as argument.

Definition at line 2296 of file oc_memman.c.

static Block_t * FindBlockContainsAddress ( const void *  Address)
static

Find block on the list, that contains this address (the address must not be start of the allocated buffer)

Parameters
AddressAddress on heap (must not be at the start of allocated buffer)
Returns
block if found, NULL otherwise

Definition at line 2209 of file oc_memman.c.

static Block_t * FindBlockOfAddress ( const void *  Address)
static

Find block on the list, that was created for the address during allocation

Parameters
AddressAddress from allocation (start of a buffer)
Returns
pointer reference or NULL if not found

Definition at line 2180 of file oc_memman.c.

static void * FindFreeHeap ( HeapMap_t Map,
oC_UInt_t  Size,
oC_UInt_t  Alignment 
)
static

Searches for not used heap in size

Definition at line 2417 of file oc_memman.c.

static oC_UInt_t * FindNextHeapPointerInState ( HeapMap_t Map,
oC_UInt_t *  HeapPointer,
oC_UInt_t *  HeapEndLimit,
HeapState_t  HeapState 
)
static

Finds next heap pointer in the heap map that is in the given state, starting from the heap pointer, that is given as the argument

Definition at line 2367 of file oc_memman.c.

static oC_UInt_t GetIndexOfHeapPointer ( HeapMap_t Map,
oC_UInt_t *  HeapPointer 
)
static

Returns index of heap word in the heap section in the heap map.

Parameters
MapReference to the heap map
HeapPointerPointer to the word from heap section in map
Returns
index

Definition at line 2235 of file oc_memman.c.

static oC_UInt_t GetSizeOfHeap ( HeapMap_t Map)
static

Returns size of heap map

Definition at line 2407 of file oc_memman.c.

static oC_UInt_t GetSizeOfHeapInState ( HeapMap_t Map,
HeapState_t  HeapState 
)
static

Returns size of memory that is in the given heap state in the heap map.

Definition at line 2387 of file oc_memman.c.

static oC_ErrorCode_t InitializeHeapMap ( HeapMap_t Map,
void *  HeapStartPointer,
void *  HeapEndPointer,
oC_UInt_t  HeapSize,
bool  FillZero 
)
static

Initializes heap map

Parameters
MapPointer to the heap map to initialize
HeapStartPointerStart of the heap, where heap map should be placed
HeapEndPointerEnd of the heap map, where heap map should be placed
HeapSizeHeap size (HeapEndPointer - HeapStartPointer)
FillZeroFills memory with 0
Returns
code of error

Definition at line 2135 of file oc_memman.c.

static oC_ErrorCode_t InitializePointers ( bool  FillZero)
static

Initialize pointers for the module

Parameters
FillZerotrue if heap map should be filled with 0 after initialize
Returns
code of error

Definition at line 2084 of file oc_memman.c.

static bool IsAddressOfHeapMap ( HeapMap_t Map,
const void *  Address 
)
inlinestatic

Checks if address is on heap of map.

Parameters
MapReference to the heap map
AddressAddress to check
Returns
true if address belong to the heap map

Definition at line 2251 of file oc_memman.c.

static bool IsBlockCorrect ( Block_t Block)
inlinestatic

Checks if block is correct

Definition at line 2261 of file oc_memman.c.

static bool IsHeapMapInitialized ( HeapMap_t Map)
inlinestatic

Checks if heap map is initialized

Definition at line 2286 of file oc_memman.c.

static bool IsHeapUsed ( HeapMap_t Map,
oC_UInt_t *  HeapPointer 
)
inlinestatic

Checks if heap word is used

Definition at line 2274 of file oc_memman.c.

static void MemoryFaultInterrupt ( void  )
static

Interrupt that is called when the memory fault occurs

Definition at line 2597 of file oc_memman.c.

static void * RawAlloc ( HeapMap_t Map,
oC_UInt_t  Size,
oC_UInt_t  Alignment 
)
static

Allow to allocate memory in the heap map in the raw way.

Definition at line 2466 of file oc_memman.c.

static bool RawFree ( HeapMap_t Map,
void *  Address,
oC_UInt_t  Size 
)
static

Allow to release memory in the heap map in the raw way.

Definition at line 2485 of file oc_memman.c.

static void SetHeapState ( HeapMap_t Map,
oC_UInt_t *  HeapPointer,
HeapState_t  HeapState 
)
inlinestatic

Checks if heap word is in state

Definition at line 2312 of file oc_memman.c.

static void SetHeapStateInRange ( HeapMap_t Map,
oC_UInt_t *  HeapFrom,
oC_UInt_t *  HeapTo,
HeapState_t  HeapState 
)
inlinestatic

Sets heap in range to state

Definition at line 2354 of file oc_memman.c.

static bool UnblockModule ( void  )
static

Unblocks module to be available for the other threads (mutex unlock)

Returns
true if success

Definition at line 2002 of file oc_memman.c.

static bool WaitForMemory ( HeapMap_t Map,
oC_UInt_t  Size,
oC_UInt_t  Alignment,
AllocationFlags_t  Flags 
)
static

Sleep current thread during there is no memory to allocate (or it is to small)

Definition at line 2022 of file oc_memman.c.

Variable Documentation

Block_t* BlockListHead = NULL
static

Head element of the block list

Definition at line 215 of file oc_memman.c.

Block_t* BlockListTail = NULL
static

Tail element of the block list

Definition at line 216 of file oc_memman.c.

uint32_t* DataOverflowProtection = NULL
static

Stores magic number at the start of machine heap. If it is overwritten, then it means, that data section was overwritten

Definition at line 214 of file oc_memman.c.

HeapMap_t DmaHeapMap
static

DMA heap map

Definition at line 220 of file oc_memman.c.

HeapMap_t ExternalHeapMap
static

External heap map

Definition at line 219 of file oc_memman.c.

bool FirstPowerOn = false
static

The module is turned on the first time after power on, so the memory is filled with 0

Definition at line 213 of file oc_memman.c.

HeapMap_t HeapMap
static

Main heap map

Definition at line 218 of file oc_memman.c.

uint32_t ModuleBusyFlag = false
static

Module is already busy, no operation is possible

Definition at line 212 of file oc_memman.c.

bool ModuleEnabledFlag = false
static

This flag stores information if the module is turned on.

Definition at line 211 of file oc_memman.c.

MemoryEventHandler_t ModuleEventHandler = NULL
static

Pointer to the function that is handling module events

Definition at line 217 of file oc_memman.c.

bool ModuleTestModeEnabledFlag = false
static

The flag that is set, when test mode is active

Definition at line 210 of file oc_memman.c.