Re-org memory allocation interfaces, add --stack-size and --heap-size option (#193)
This commit is contained in:
@ -1,99 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef _BH_MEMORY_H
|
||||
#define _BH_MEMORY_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define BH_KB (1024)
|
||||
#define BH_MB ((BH_KB)*1024)
|
||||
#define BH_GB ((BH_MB)*1024)
|
||||
|
||||
/**
|
||||
* Initialize memory allocator with a pool, the bh_malloc/bh_free function
|
||||
* will malloc/free memory from the pool
|
||||
*
|
||||
* @param mem the pool buffer
|
||||
* @param bytes the size bytes of the buffer
|
||||
*
|
||||
* @return 0 if success, -1 otherwise
|
||||
*/
|
||||
int bh_memory_init_with_pool(void *mem, unsigned int bytes);
|
||||
|
||||
/**
|
||||
* Initialize memory allocator with memory allocator, the bh_malloc/bh_free
|
||||
* function will malloc/free memory with the allocator passed
|
||||
*
|
||||
* @param malloc_func the malloc function
|
||||
* @param free_func the free function
|
||||
*
|
||||
* @return 0 if success, -1 otherwise
|
||||
*/
|
||||
int bh_memory_init_with_allocator(void *malloc_func, void *free_func);
|
||||
|
||||
/**
|
||||
* Destroy memory
|
||||
*/
|
||||
void bh_memory_destroy();
|
||||
|
||||
/**
|
||||
* Get the pool size of memory, if memory is initialized with allocator,
|
||||
* return 1GB by default.
|
||||
*/
|
||||
unsigned bh_memory_pool_size();
|
||||
|
||||
#if BEIHAI_ENABLE_MEMORY_PROFILING == 0
|
||||
|
||||
/**
|
||||
* This function allocates a memory chunk from system
|
||||
*
|
||||
* @param size bytes need allocate
|
||||
*
|
||||
* @return the pointer to memory allocated
|
||||
*/
|
||||
void* bh_malloc(unsigned int size);
|
||||
|
||||
/**
|
||||
* This function frees memory chunk
|
||||
*
|
||||
* @param ptr the pointer to memory need free
|
||||
*/
|
||||
void bh_free(void *ptr);
|
||||
|
||||
#else
|
||||
|
||||
void* bh_malloc_profile(const char *file, int line, const char *func, unsigned int size);
|
||||
void bh_free_profile(const char *file, int line, const char *func, void *ptr);
|
||||
|
||||
#define bh_malloc(size) bh_malloc_profile(__FILE__, __LINE__, __func__, size)
|
||||
#define bh_free(ptr) bh_free_profile(__FILE__, __LINE__, __func__, ptr)
|
||||
|
||||
/**
|
||||
* Print current memory profiling data
|
||||
*
|
||||
* @param file file name of the caller
|
||||
* @param line line of the file of the caller
|
||||
* @param func function name of the caller
|
||||
*/
|
||||
void memory_profile_print(const char *file, int line, const char *func, int alloc);
|
||||
|
||||
/**
|
||||
* Summarize memory usage and print it out
|
||||
* Can use awk to analyze the output like below:
|
||||
* awk -F: '{print $2,$4,$6,$8,$9}' OFS="\t" ./out.txt | sort -n -r -k 1
|
||||
*/
|
||||
void memory_usage_summarize();
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* #ifndef _BH_MEMORY_H */
|
||||
|
||||
@ -9,7 +9,6 @@
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include "lib_export.h"
|
||||
#include "bh_memory.h"
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
@ -53,24 +52,33 @@ typedef enum {
|
||||
|
||||
/* Memory allocator type */
|
||||
typedef enum {
|
||||
/* pool mode, allocate memory from user defined heap buffer */
|
||||
Alloc_With_Pool = 0,
|
||||
Alloc_With_Allocator
|
||||
/* user allocator mode, allocate memory from user defined
|
||||
malloc function */
|
||||
Alloc_With_Allocator,
|
||||
/* system allocator mode, allocate memory from system allocator,
|
||||
or, platform's os_malloc function */
|
||||
Alloc_With_System_Allocator,
|
||||
} mem_alloc_type_t;
|
||||
|
||||
/* Memory allocator option */
|
||||
typedef union MemAllocOption {
|
||||
struct {
|
||||
void *heap_buf;
|
||||
uint32_t heap_size;
|
||||
} pool;
|
||||
struct {
|
||||
void *malloc_func;
|
||||
void *realloc_func;
|
||||
void *free_func;
|
||||
} allocator;
|
||||
} MemAllocOption;
|
||||
|
||||
/* WASM runtime initialize arguments */
|
||||
typedef struct RuntimeInitArgs {
|
||||
mem_alloc_type_t mem_alloc_type;
|
||||
union {
|
||||
struct {
|
||||
void *heap_buf;
|
||||
uint32_t heap_size;
|
||||
} pool;
|
||||
struct {
|
||||
void *malloc_func;
|
||||
void *realloc_func;
|
||||
void *free_func;
|
||||
} allocator;
|
||||
} mem_alloc;
|
||||
MemAllocOption mem_alloc_option;
|
||||
|
||||
const char *native_module_name;
|
||||
NativeSymbol *native_symbols;
|
||||
@ -78,19 +86,15 @@ typedef struct RuntimeInitArgs {
|
||||
} RuntimeInitArgs;
|
||||
|
||||
/**
|
||||
* Initialize the WASM runtime environment.
|
||||
* Initialize the WASM runtime environment, and also initialize
|
||||
* the memory allocator with system allocator, which calls os_malloc
|
||||
* to allocate memory
|
||||
*
|
||||
* @return true if success, false otherwise
|
||||
*/
|
||||
bool
|
||||
wasm_runtime_init();
|
||||
|
||||
/**
|
||||
* Destroy the WASM runtime environment.
|
||||
*/
|
||||
void
|
||||
wasm_runtime_destroy();
|
||||
|
||||
/**
|
||||
* Initialize the WASM runtime environment, and also initialize
|
||||
* the memory allocator and register native symbols, which are specified
|
||||
@ -104,11 +108,36 @@ bool
|
||||
wasm_runtime_full_init(RuntimeInitArgs *init_args);
|
||||
|
||||
/**
|
||||
* Destroy the wasm runtime environment, and also destroy
|
||||
* the memory allocator and registered native symbols
|
||||
* Destroy the WASM runtime environment.
|
||||
*/
|
||||
void
|
||||
wasm_runtime_full_destroy();
|
||||
wasm_runtime_destroy();
|
||||
|
||||
/**
|
||||
* Allocate memory from runtime memory environment.
|
||||
*
|
||||
* @param size bytes need to allocate
|
||||
*
|
||||
* @return the pointer to memory allocated
|
||||
*/
|
||||
void *
|
||||
wasm_runtime_malloc(unsigned int size);
|
||||
|
||||
/**
|
||||
* Reallocate memory from runtime memory environment
|
||||
*
|
||||
* @param ptr the original memory
|
||||
* @param size bytes need to reallocate
|
||||
*
|
||||
* @return the pointer to memory reallocated
|
||||
*/
|
||||
void *
|
||||
wasm_runtime_realloc(void *ptr, unsigned int size);
|
||||
|
||||
/*
|
||||
* Free memory to runtime memory environment.
|
||||
*/
|
||||
void wasm_runtime_free(void *ptr);
|
||||
|
||||
/**
|
||||
* Get the package type of a buffer.
|
||||
|
||||
Reference in New Issue
Block a user