184 lines
5.6 KiB
C
Executable File
184 lines
5.6 KiB
C
Executable File
/****************************************************************************
|
|
|
|
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
|
|
|
|
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
|
|
be copied by any method or incorporated into another program without
|
|
the express written consent of Aerospace C.Power. This Information or any portion
|
|
thereof remains the property of Aerospace C.Power. The Information contained herein
|
|
is believed to be accurate and Aerospace C.Power assumes no responsibility or
|
|
liability for its use in any way and conveys no license or title under
|
|
any patent or copyright and makes no representation or warranty that this
|
|
Information is free from patent or copyright infringement.
|
|
|
|
****************************************************************************/
|
|
|
|
/* Free RTOS includes */
|
|
#include "FreeRTOS.h"
|
|
|
|
/* os shim includes */
|
|
#include "os_types.h"
|
|
#include "os_mem.h"
|
|
|
|
/* common includes */
|
|
#include "iot_config.h"
|
|
#include "iot_utils.h"
|
|
#include "iot_dbglog_api.h"
|
|
#include "iot_io_api.h"
|
|
#include "iot_dbglog_parser.h"
|
|
#include "iot_module.h"
|
|
|
|
/* statistics related variables */
|
|
#if (PLC_STATISTICS_ENABLE)
|
|
uint32_t mid_mem[MAX_MID_NUM] = {0};
|
|
#endif
|
|
|
|
uint32_t total_mem = 0;
|
|
|
|
/* Define the linked list structure. This is used to link malloc blocks
|
|
* in order of their memory address. */
|
|
typedef struct _aligned_malloc_block
|
|
{
|
|
uint32_t aligned_bytes; /*<< The size of the aligned memory. */
|
|
uint32_t malloc_size; /*<< The size of malloc memory block. */
|
|
} aligned_malloc_block_t;
|
|
|
|
#if IOT_OS_MALLOC_DEBUG
|
|
void *os_mem_malloc_debug(uint8_t *file, uint32_t line,
|
|
module_id_t module_id, size_t size)
|
|
#else
|
|
void *os_mem_malloc(module_id_t module_id, size_t size)
|
|
#endif
|
|
{
|
|
(void)module_id; // avoid warning. to be fixed.
|
|
void *buf = pvPortMalloc(size);
|
|
#if IOT_OS_MALLOC_DEBUG
|
|
iot_printf("%s:%d malloc.\n", file, line);
|
|
#endif
|
|
if (buf) {
|
|
os_mem_set(buf, 0, size);
|
|
total_mem += size;
|
|
#if (PLC_STATISTICS_ENABLE)
|
|
mid_mem[module_id] += size;
|
|
iot_printf("ststic:alloc misc- mid= [%d][%s], size = %d, "
|
|
"mtotal = %d, p = [%08X]\n", module_id,
|
|
iot_get_mid_str(module_id), size,
|
|
mid_mem[module_id], buf);
|
|
iot_dbglog_input(IOT_STATISTICS_MID, DBGLOG_ERR, OS_MEM_MISC_USAGE1,
|
|
4, module_id, size, mid_mem[module_id], buf);
|
|
#endif // PLC_STATISTICS_ENABLE
|
|
} else {
|
|
#if IOT_OS_MALLOC_DEBUG
|
|
iot_printf("%s:%d malloc failed\n", file, line);
|
|
IOT_ASSERT_FILE(0, file, line);
|
|
#else
|
|
IOT_ASSERT(0);
|
|
#endif
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
#if IOT_OS_MALLOC_DEBUG
|
|
void *os_mem_malloc_tag_debug(uint8_t *file, uint32_t line,
|
|
module_id_t module_id, size_t size,uint32_t mode)
|
|
#else
|
|
void *os_mem_malloc_tag(module_id_t module_id, size_t size,uint32_t mode)
|
|
#endif
|
|
{
|
|
(void)module_id; // avoid warning. to be fixed.
|
|
void *buf = pvPortMallocCaps(size,mode);
|
|
#if IOT_OS_MALLOC_DEBUG
|
|
iot_printf("%s:%d malloc.\n", file, line);
|
|
#endif
|
|
if (buf) {
|
|
os_mem_set(buf, 0, size);
|
|
total_mem += size;
|
|
} else {
|
|
#if IOT_OS_MALLOC_DEBUG
|
|
iot_printf("%s:%d malloc failed\n", file, line);
|
|
IOT_ASSERT_FILE(0, file, line);
|
|
#else
|
|
IOT_ASSERT(0);
|
|
#endif
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
void os_mem_free(void *ptr)
|
|
{
|
|
vPortFree(ptr);
|
|
}
|
|
|
|
void* os_mem_aligned_malloc(module_id_t module_id,
|
|
size_t bytes, size_t alignment)
|
|
{
|
|
void *aligned_address = NULL;
|
|
void *malloc_address = NULL;
|
|
size_t address,want_size,byte_aligned,byte_aligned_mask,aligned_ahead;
|
|
aligned_malloc_block_t *malloc_block = NULL;
|
|
|
|
IOT_ASSERT( alignment != 0 );
|
|
IOT_ASSERT( (alignment&(alignment-1)) == 0 );
|
|
|
|
byte_aligned = alignment;
|
|
byte_aligned_mask = alignment - 1;
|
|
want_size = bytes;
|
|
|
|
/* alignment is bigger than 4*/
|
|
if( byte_aligned > portBYTE_ALIGNMENT_MASK+1){
|
|
want_size += byte_aligned + sizeof(aligned_malloc_block_t);
|
|
malloc_address = os_mem_malloc(module_id, want_size);
|
|
if( NULL == malloc_address ){
|
|
return NULL;
|
|
}
|
|
address = ( size_t )malloc_address;
|
|
aligned_ahead = address;
|
|
|
|
/* Ensure the malloc memory starts on a correctly aligned boundary. */
|
|
if( ( address & byte_aligned_mask) != 0 ){
|
|
address += ( byte_aligned - 1 );
|
|
address &= ~( byte_aligned_mask );
|
|
}
|
|
if (address - aligned_ahead < sizeof(aligned_malloc_block_t)) {
|
|
address += byte_aligned;
|
|
}
|
|
} else {
|
|
want_size += sizeof(aligned_malloc_block_t);
|
|
malloc_address = os_mem_malloc(module_id, want_size);
|
|
if (NULL == malloc_address) {
|
|
return NULL;
|
|
}
|
|
address = ( size_t )malloc_address;
|
|
aligned_ahead = address;
|
|
|
|
/* malloc memory starts on a correctly aligned boundary. */
|
|
address += sizeof(aligned_malloc_block_t);
|
|
}
|
|
/* set the block aligned bytes value*/
|
|
malloc_block = ( aligned_malloc_block_t * )
|
|
( address - sizeof(aligned_malloc_block_t));
|
|
malloc_block->aligned_bytes = address - aligned_ahead;
|
|
malloc_block->malloc_size = bytes;
|
|
|
|
/* Return the memory space pointed*/
|
|
aligned_address = ( void * ) address;
|
|
return aligned_address;
|
|
}
|
|
|
|
void os_mem_aligned_free(void* p)
|
|
{
|
|
aligned_malloc_block_t *puc = NULL;
|
|
void *pv = NULL;
|
|
if( NULL != p ){
|
|
puc = (aligned_malloc_block_t *)
|
|
((size_t)p - sizeof(aligned_malloc_block_t));
|
|
pv = (void *)( (size_t)p - puc->aligned_bytes );
|
|
os_mem_free(pv);
|
|
}
|
|
}
|
|
|
|
size_t os_mem_free_get(void)
|
|
{
|
|
return xPortGetFreeHeapSize();
|
|
}
|