Add a new API to get free memory in memory pool (#1430)

This commit is contained in:
Huang Qi 2022-08-31 16:39:25 +08:00 committed by GitHub
parent e0c2acd178
commit 77c516ac80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 0 deletions

View File

@ -167,3 +167,12 @@ wasm_runtime_free(void *ptr)
{
wasm_runtime_free_internal(ptr);
}
bool
wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info)
{
if (memory_mode == MEMORY_MODE_POOL) {
return mem_allocator_get_alloc_info(pool_allocator, mem_alloc_info);
}
return false;
}

View File

@ -121,6 +121,13 @@ typedef union MemAllocOption {
} MemAllocOption;
#endif
/* Memory pool info */
typedef struct mem_alloc_info_t {
uint32_t total_size;
uint32_t total_free_size;
uint32_t highmark_size;
} mem_alloc_info_t;
/* WASM runtime initialize arguments */
typedef struct RuntimeInitArgs {
mem_alloc_type_t mem_alloc_type;
@ -229,6 +236,12 @@ wasm_runtime_realloc(void *ptr, unsigned int size);
WASM_RUNTIME_API_EXTERN void
wasm_runtime_free(void *ptr);
/*
* Get memory info, only pool mode is supported now.
*/
WASM_RUNTIME_API_EXTERN bool
wasm_runtime_get_mem_alloc_info(mem_alloc_info_t *mem_alloc_info);
/**
* Get the package type of a buffer.
*

View File

@ -69,6 +69,13 @@ mem_allocator_is_heap_corrupted(mem_allocator_t allocator)
return gc_is_heap_corrupted((gc_handle_t)allocator);
}
bool
mem_allocator_get_alloc_info(mem_allocator_t allocator, void *mem_alloc_info)
{
gc_heap_stats((gc_handle_t)allocator, mem_alloc_info, 3);
return true;
}
#else /* else of DEFAULT_MEM_ALLOCATOR */
#include "tlsf/tlsf.h"

View File

@ -45,6 +45,9 @@ mem_allocator_migrate(mem_allocator_t allocator, char *pool_buf_new,
bool
mem_allocator_is_heap_corrupted(mem_allocator_t allocator);
bool
mem_allocator_get_alloc_info(mem_allocator_t allocator, void *mem_alloc_info);
#ifdef __cplusplus
}
#endif