Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix][mm] fix get mem size api
Browse files Browse the repository at this point in the history
sakumisue committed Mar 21, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 8db4f82 commit 772512b
Showing 3 changed files with 31 additions and 87 deletions.
43 changes: 4 additions & 39 deletions components/mm/mem.c
Original file line number Diff line number Diff line change
@@ -236,7 +236,6 @@ void free(void *addr)
bflb_free(PMEM_HEAP, addr);
}


/****************************************************************************
* Name: kfree_size
*
@@ -247,21 +246,13 @@ void free(void *addr)

uint32_t kfree_size(void)
{
struct meminfo info;

bflb_mem_usage(KMEM_HEAP, &info);

return info.free_size;
return g_kmemheap.free_bytes;
}

uint32_t pfree_size(void)
{
#if defined(CONFIG_PSRAM) && defined(BL616) // only for bl618
struct meminfo info;

bflb_mem_usage(PMEM_HEAP, &info);

return info.free_size;
return g_pmemheap.free_bytes;
#else
return 0;
#endif
@@ -272,34 +263,8 @@ uint32_t pfree_size(void)

int cmd_free(int argc, char **argv)
{
const char *Header = "total free alloc mxblk frnode alnode \r\n";
struct meminfo info;
char *mem;

mem = malloc(64);
bflb_mem_usage(KMEM_HEAP, &info);

snprintf(mem, 64, "%-8d%-8d%-8d%-8d%-8d%-8d\r\n", info.total_size, info.free_size, info.used_size, info.max_free_size,
info.free_node, info.used_node);

printf(Header);
printf(mem);

free(mem);

#if defined(CONFIG_PSRAM) && defined(BL616) // only for bl618
mem = malloc(64);
bflb_mem_usage(PMEM_HEAP, &info);

snprintf(mem, 64, "%-8d%-8d%-8d%-8d%-8d%-8d\r\n", info.total_size, info.free_size, info.used_size, info.max_free_size,
info.free_node, info.used_node);

printf(Header);
printf(mem);

free(mem);
#endif

printf("sram free size:%d\r\n", kfree_size());
printf("psram free size:%d\r\n", pfree_size());
return 0;
}
SHELL_CMD_EXPORT_ALIAS(cmd_free, free, show memory usage);
15 changes: 1 addition & 14 deletions components/mm/mem.h
Original file line number Diff line number Diff line change
@@ -62,18 +62,7 @@ struct mem_heap_s {
void *priv;
void *heapstart;
size_t heapsize;
};

struct meminfo {
int total_size; /* This is the total size of memory allocated
* for use by malloc in bytes. */
int free_node; /* This is the number of free (not in use) chunks */
int used_node; /* This is the number of allocated (in use) chunks */
int max_free_size; /* Size of the largest free (not in use) chunk */
int used_size; /* This is the total size of memory occupied by
* chunks handed out by malloc. */
int free_size; /* This is the total size of memory occupied
* by free (not in use) chunks. */
size_t free_bytes;
};

/****************************************************************************
@@ -118,8 +107,6 @@ void *bflb_calloc(struct mem_heap_s *heap, size_t count, size_t size);

void *bflb_malloc_align(struct mem_heap_s *heap, size_t align, size_t size);

void bflb_mem_usage(struct mem_heap_s *heap, struct meminfo *info);

#undef EXTERN
#ifdef __cplusplus
}
60 changes: 26 additions & 34 deletions components/mm/tlsf/bflb_tlsf.c
Original file line number Diff line number Diff line change
@@ -46,24 +46,6 @@
} \
}

/****************************************************************************
* Name: mem_tlfsinfo_walker
****************************************************************************/

static void mem_tlfsinfo_walker(void *ptr, size_t size, int used,
void *user)
{
struct meminfo *info = user;

if (!used) {
info->free_node++;
info->free_size += size;
if (size > info->max_free_size) {
info->max_free_size = size;
}
}
}

/****************************************************************************
* Functions
****************************************************************************/
@@ -73,6 +55,7 @@ void bflb_mem_init(struct mem_heap_s *heap, void *heapstart, size_t heapsize)
heap->heapstart = heapstart + tlsf_size();
heap->heapsize = heapsize - tlsf_size();
heap->priv = tlsf_create_with_pool(heapstart, heapsize);
heap->free_bytes = heap->heapsize;
}

void *bflb_malloc(struct mem_heap_s *heap, size_t nbytes)
@@ -84,6 +67,10 @@ void *bflb_malloc(struct mem_heap_s *heap, size_t nbytes)

ret = tlsf_memalign(heap->priv, 32, nbytes);
TLSF_MALLOC_ASSERT(heap, ret != NULL, nbytes);
if (ret) {
heap->free_bytes -= tlsf_block_size(ret);
heap->free_bytes -= tlsf_alloc_overhead();
}

bflb_irq_restore(flag);

@@ -95,6 +82,10 @@ void bflb_free(struct mem_heap_s *heap, void *ptr)
uintptr_t flag;

flag = bflb_irq_save();
if (ptr) {
heap->free_bytes += tlsf_block_size(ptr);
heap->free_bytes += tlsf_alloc_overhead();
}

tlsf_free(heap->priv, ptr);

@@ -108,8 +99,14 @@ void *bflb_realloc(struct mem_heap_s *heap, void *ptr, size_t nbytes)

flag = bflb_irq_save();

size_t previous_block_size = tlsf_block_size(ptr);

ret = tlsf_realloc(heap->priv, ptr, nbytes);
TLSF_MALLOC_ASSERT(heap, ret != NULL, nbytes);
if (ret) {
heap->free_bytes += previous_block_size;
heap->free_bytes -= tlsf_block_size(ptr);
}

bflb_irq_restore(flag);

@@ -126,7 +123,13 @@ void *bflb_calloc(struct mem_heap_s *heap, size_t count, size_t size)

if (count > 0 && size > 0) {
if (count <= (SIZE_MAX / size)) {
ptr = tlsf_malloc(heap->priv, total);
ptr = tlsf_memalign(heap->priv, 32, total);
TLSF_MALLOC_ASSERT(heap, ptr != NULL, total);
if (ptr) {
heap->free_bytes -= tlsf_block_size(ptr);
heap->free_bytes -= tlsf_alloc_overhead();
}

if (ptr) {
memset(ptr, 0, total);
}
@@ -147,23 +150,12 @@ void *bflb_malloc_align(struct mem_heap_s *heap, size_t align, size_t size)

ret = tlsf_memalign(heap->priv, align, size);
TLSF_MALLOC_ASSERT(heap, ret != NULL, size);
if (ret) {
heap->free_bytes -= tlsf_block_size(ret);
heap->free_bytes -= tlsf_alloc_overhead();
}

bflb_irq_restore(flag);

return ret;
}

void bflb_mem_usage(struct mem_heap_s *heap, struct meminfo *info)
{
uintptr_t flag;

memset(info, 0, sizeof(struct meminfo));

flag = bflb_irq_save();
tlsf_walk_pool(heap->heapstart,
mem_tlfsinfo_walker, info);
bflb_irq_restore(flag);

info->total_size = heap->heapsize;
info->used_size = info->total_size - info->free_size;
}

0 comments on commit 772512b

Please sign in to comment.