Przeglądaj źródła

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

Huang Qi 3 lat temu
rodzic
commit
77c516ac80

+ 9 - 0
core/iwasm/common/wasm_memory.c

@@ -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;
+}

+ 13 - 0
core/iwasm/include/wasm_export.h

@@ -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.
  *

+ 7 - 0
core/shared/mem-alloc/mem_alloc.c

@@ -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"

+ 3 - 0
core/shared/mem-alloc/mem_alloc.h

@@ -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