SOC OV788开发:6.ov788 malloc 实现机制
文章目录
./ovipc/OV788_SDK_52786_1396/Firmware/OV788_SDK_src.52791.1396$ ls -l ./share/libsystem/r2/
总用量 240
-rw-rw-r-- 1 root linux 3847 12月 24 12:26 Makefile
-rw-rw-r-- 1 root linux 2302 12月 24 12:26 malloc.c
1
2 ARLIB = $(BLDDIR)/share/libsystem_chip.a
3
4 EXTRA_SDK_OBJS = kcmd.o \
5 hdmi_in_set.o \
6 mipi_set.o \
7 cif_set.o \
8 cif_set_internal.o \
9 sccb.o \
10 sysclk_get.o \
11 sif_base.o \
12 sif_fifo.o \
13 sif0_init.o \
* The definition of pool element, it jsut has a pointer points to the next pool element. This pointer reuses the data buffer for the pool element
*/
typedef struct _t_pool_element
{
struct _t_pool_element *next;
} t_pool_element;
/**
* The Pool structure definition. The main structure for the pool. If the application wants to create a pool, it will call pool_init() function, then it will get a pointer to this structure. The application will use this to indentify a pool. The property of the pool is in this structure: the *head pointer of the pool; the buffer list; the element_size etc.
*/
typedef struct _t_pool
{
t_pool_element *head; ///< the head pointer of the pool
u32 element_size; ///< the element size of the pool
u32 element_total_cnt; ///< the total element count of the pool
u32 element_ref_cnt; ///< the referenced element count of the pool, pool_element_alloc() is called, this value will be increased by 1; while pool_element_free() is called, this value will be decreased by 1.
} t_pool;
/** Get the overhead of the pool, it is used for static pool when external module calculate the memory requirment for the pool */
#define POOL_OVERHEAD_SIZE (sizeof(t_pool))
ovipc/OV788_SDK_52786_1396/Firmware/OV788_SDK_src.52791.1396$ cat ./share/libsystem/r2/malloc.c
#include “includes.h”
extern int newos_malloc_partcnt;
extern t_pool * newos_malloc_table[];
void *newos_malloc(u32 size)
{
int i;
for (i = 0; i < newos_malloc_partcnt; i++)
{
if (newos_malloc_table[i]->element_size >= size && newos_malloc_table[i]->head)
{
break;
}
}
if (i == newos_malloc_partcnt){
debug_printf(“ERROR: malloc failed:%d\n”, size);
return NULL;
}
#ifdef SYS_MALLOC_DEBUG
debug_printf(“malloc:%d\n”, size);
#endif
void * ret = pool_element_alloc(newos_malloc_table[i]);
if(ret == NULL){
debug_printf(“ERROR: malloc failed:%d\n”, size);
}
return ret;
}
void newos_free(void *addr)
{
int i;
#ifdef SYS_MALLOC_DEBUG
debug_printf(“free:%x\n”, addr);
#endif
for (i = 0; i < newos_malloc_partcnt; i++)
{
if ((u32)addr >= (u32)(newos_malloc_table[i]) &&
(u32)addr < ((u32)(newos_malloc_table[i]) + POOL_OVERHEAD_SIZE + newos_malloc_table[i]->element_size * newos_malloc_table[i]->element_total_cnt + 4 ) )
{
break;
}
}
if (i == newos_malloc_partcnt){
debug_printf(“ERROR: free non-malloc\n”);
return ;
}
pool_element_free(newos_malloc_table[i], addr);
return ;
}
void * newos_calloc(u32 cnt, u32 size)
{
void *ptr;
ptr = newos_malloc(cnt * size);
if (ptr)
{
memset(ptr, 0, cnt * size);
}
return ptr;
}
void *newos_realloc(void * addr, size_t size)
{
int i;
for (i = 0; i < newos_malloc_partcnt; i++)
{
if ((u32)addr >= (u32)(newos_malloc_table[i]) &&
(u32)addr < ((u32)(newos_malloc_table[i]) + POOL_OVERHEAD_SIZE + newos_malloc_table[i]->element_size * newos_malloc_table[i]->element_total_cnt + 4 ) )
{
break;
}
}
if (i == newos_malloc_partcnt){
debug_printf(“ERROR: newos_realloc failed:%d\n”, size);
return (void*)NULL;
}
if (newos_malloc_table[i]->element_size >= size){
return addr;
}
void * newaddr = newos_malloc(size);
if (newaddr == NULL){
return NULL;
}
memcpy(newaddr, addr, newos_malloc_table[i]->element_size);
newos_free(addr);
return newaddr;
return addr;
}
void * newaddr = newos_malloc(size);
if (newaddr == NULL){
return NULL;
}
memcpy(newaddr, addr, newos_malloc_table[i]->element_size);
newos_free(addr);
return newaddr;
}