Re-org memory allocation interfaces, add --stack-size and --heap-size option (#193)

This commit is contained in:
wenyongh
2020-03-10 19:54:44 +08:00
committed by GitHub
parent 381859d530
commit 0fdd49ea31
110 changed files with 1264 additions and 2125 deletions

View File

@ -16,10 +16,14 @@ typedef union jvalue {
double d;
} jvalue;
#ifndef bh_memcpy_s
int b_memcpy_s(void * s1, unsigned int s1max,
const void * s2, unsigned int n);
#define bh_memcpy_s(dest, dlen, src, slen) do { \
int _ret = slen == 0 ? 0 : b_memcpy_s (dest, dlen, src, slen); \
(void)_ret; \
} while (0)
#endif
static inline int16_t get_int16(const char *buf)
{

View File

@ -406,11 +406,11 @@ void
attr_container_dump(const attr_container_t *attr_cont);
#ifndef attr_container_malloc
#define attr_container_malloc wa_malloc
#define attr_container_malloc WA_MALLOC
#endif
#ifndef attr_container_free
#define attr_container_free wa_free
#define attr_container_free WA_FREE
#endif
#ifndef attr_container_printf

View File

@ -145,6 +145,8 @@ unpack_response(char * packet, int size, response_t * response);
void
free_req_resp_packet(char * packet);
char *
wa_strdup(const char *str);
#ifdef __cplusplus

View File

@ -43,7 +43,7 @@ char * pack_request(request_t *request, int * size)
{
int url_len = strlen(request->url) + 1;
int len = REQUEST_PACKET_FIX_PART_LEN + url_len + request->payload_len;
char * packet = (char*) wa_malloc(len);
char * packet = (char*) WA_MALLOC(len);
if (packet == NULL)
return NULL;
@ -65,7 +65,7 @@ char * pack_request(request_t *request, int * size)
void free_req_resp_packet(char * packet)
{
wa_free(packet);
WA_FREE(packet);
}
request_t * unpack_request(char * packet, int size, request_t * request)
@ -108,7 +108,7 @@ request_t * unpack_request(char * packet, int size, request_t * request)
char * pack_response(response_t *response, int * size)
{
int len = RESPONSE_PACKET_FIX_PART_LEN + response->payload_len;
char * packet = (char*) wa_malloc(len);
char * packet = (char*) WA_MALLOC(len);
if (packet == NULL)
return NULL;
@ -152,7 +152,7 @@ response_t * unpack_response(char * packet, int size, response_t * response)
request_t *clone_request(request_t *request)
{
/* deep clone */
request_t *req = (request_t *) wa_malloc(sizeof(request_t));
request_t *req = (request_t *) WA_MALLOC(sizeof(request_t));
if (req == NULL)
return NULL;
@ -169,7 +169,7 @@ request_t *clone_request(request_t *request)
req->payload_len = request->payload_len;
if (request->payload_len) {
req->payload = (char *) wa_malloc(request->payload_len);
req->payload = (char *) WA_MALLOC(request->payload_len);
if (!req->payload)
goto fail;
memcpy(req->payload, request->payload, request->payload_len);
@ -188,24 +188,24 @@ fail:
void request_cleaner(request_t *request)
{
if (request->url != NULL)
wa_free(request->url);
WA_FREE(request->url);
if (request->payload != NULL && request->payload_len > 0)
wa_free(request->payload);
WA_FREE(request->payload);
wa_free(request);
WA_FREE(request);
}
void response_cleaner(response_t * response)
{
if (response->payload != NULL && response->payload_len > 0)
wa_free(response->payload);
WA_FREE(response->payload);
wa_free(response);
WA_FREE(response);
}
response_t * clone_response(response_t * response)
{
response_t *clone = (response_t *) wa_malloc(sizeof(response_t));
response_t *clone = (response_t *) WA_MALLOC(sizeof(response_t));
if (clone == NULL)
return NULL;
@ -216,7 +216,7 @@ response_t * clone_response(response_t * response)
clone->reciever = response->reciever;
clone->payload_len = response->payload_len;
if (clone->payload_len) {
clone->payload = (char *) wa_malloc(response->payload_len);
clone->payload = (char *) WA_MALLOC(response->payload_len);
if (!clone->payload)
goto fail;
memcpy(clone->payload, response->payload, response->payload_len);

View File

@ -75,7 +75,7 @@ uint16 ntohs(uint16 value)
char *wa_strdup(const char *s)
{
char *s1 = NULL;
if (s && (s1 = wa_malloc(strlen(s) + 1)))
if (s && (s1 = WA_MALLOC(strlen(s) + 1)))
memcpy(s1, s, strlen(s) + 1);
return s1;
}

View File

@ -25,14 +25,14 @@ typedef int int32;
#define inline __inline
#endif
// all wasm-app<->native shared source files should use wa_malloc/wa_free.
// all wasm-app<->native shared source files should use WA_MALLOC/WA_FREE.
// they will be mapped to different implementations in each side
#ifndef wa_malloc
#define wa_malloc malloc
#ifndef WA_MALLOC
#define WA_MALLOC malloc
#endif
#ifndef wa_free
#define wa_free free
#ifndef WA_FREE
#define WA_FREE free
#endif
char *wa_strdup(const char *s);

View File

@ -107,7 +107,7 @@ timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
return NULL;
timer_ctx_node_t * node = (timer_ctx_node_t*)
bh_malloc(sizeof(timer_ctx_node_t));
wasm_runtime_malloc(sizeof(timer_ctx_node_t));
if (node == NULL) {
destroy_timer_ctx(ctx);
return NULL;
@ -131,7 +131,7 @@ void destroy_module_timer_ctx(unsigned int module_id)
if (timer_ctx_get_owner(elem->timer_ctx) == module_id) {
bh_list_remove(&g_timer_ctx_list, elem);
destroy_timer_ctx(elem->timer_ctx);
bh_free(elem);
wasm_runtime_free(elem);
break;
}

View File

@ -115,8 +115,8 @@ static void add_connection(sys_connection_t *conn)
#define FREE_CONNECTION(conn) do { \
if (conn->arg) \
bh_free(conn->arg); \
bh_free(conn); \
wasm_runtime_free(conn->arg); \
wasm_runtime_free(conn); \
} while (0)
static int get_app_conns_num(uint32 module_id)
@ -223,7 +223,7 @@ static uint32 _conn_open(wasm_module_inst_t module_inst,
if (get_app_conns_num(module_id) >= MAX_CONNECTION_PER_APP)
return -1;
conn = (sys_connection_t *)bh_malloc(sizeof(*conn));
conn = (sys_connection_t *)wasm_runtime_malloc(sizeof(*conn));
if (conn == NULL)
return -1;
@ -296,7 +296,7 @@ static uint32 _conn_open(wasm_module_inst_t module_inst,
fail:
find_connection(conn->handle, true);
bh_free(conn);
wasm_runtime_free(conn);
return -1;
}
@ -356,7 +356,7 @@ static bool _conn_config(uint32 handle, attr_container_t *cfg)
port = attr_container_get_as_uint16(cfg, "port");
if (conn->arg == NULL) {
addr = (struct sockaddr_in *)bh_malloc(sizeof(*addr));
addr = (struct sockaddr_in *)wasm_runtime_malloc(sizeof(*addr));
if (addr == NULL)
return false;
@ -390,8 +390,8 @@ typedef struct connection_event {
static void connection_event_cleaner(connection_event_t *conn_event)
{
if (conn_event->data != NULL)
bh_free(conn_event->data);
bh_free(conn_event);
wasm_runtime_free(conn_event->data);
wasm_runtime_free(conn_event);
}
static void post_msg_to_module(sys_connection_t *conn,
@ -406,14 +406,14 @@ static void post_msg_to_module(sys_connection_t *conn,
if (module == NULL)
return;
conn_data_event = (connection_event_t *)bh_malloc(sizeof(*conn_data_event));
conn_data_event = (connection_event_t *)wasm_runtime_malloc(sizeof(*conn_data_event));
if (conn_data_event == NULL)
return;
if (len > 0) {
data_copy = (char *)bh_malloc(len);
data_copy = (char *)wasm_runtime_malloc(len);
if (data_copy == NULL) {
bh_free(conn_data_event);
wasm_runtime_free(conn_data_event);
return;
}
bh_memcpy_s(data_copy, len, data, len);

View File

@ -32,10 +32,10 @@ sensor_event_cleaner(sensor_event_data_t *sensor_event)
if (sensor_event->data_fmt == FMT_ATTR_CONTAINER)
attr_container_destroy(sensor_event->data);
else
bh_free(sensor_event->data);
wasm_runtime_free(sensor_event->data);
}
bh_free(sensor_event);
wasm_runtime_free(sensor_event);
}
static void
@ -56,7 +56,7 @@ wasm_sensor_callback(void *client, uint32 sensor_id, void *user_data)
return;
sensor_data_len = attr_container_get_serialize_length(sensor_data);
sensor_data_clone = (attr_container_t *)bh_malloc(sensor_data_len);
sensor_data_clone = (attr_container_t *)wasm_runtime_malloc(sensor_data_len);
if (sensor_data_clone == NULL)
return;
@ -64,9 +64,9 @@ wasm_sensor_callback(void *client, uint32 sensor_id, void *user_data)
bh_memcpy_s(sensor_data_clone, sensor_data_len,
sensor_data, sensor_data_len);
sensor_event = (sensor_event_data_t *)bh_malloc(sizeof(*sensor_event));
sensor_event = (sensor_event_data_t *)wasm_runtime_malloc(sizeof(*sensor_event));
if (sensor_event == NULL) {
bh_free(sensor_data_clone);
wasm_runtime_free(sensor_data_clone);
return;
}
@ -158,7 +158,7 @@ wasm_sensor_open(wasm_exec_env_t exec_env,
return -1;
}
sensor_client_t * client = (sensor_client_t*) bh_malloc(
sensor_client_t * client = (sensor_client_t*) wasm_runtime_malloc(
sizeof(sensor_client_t));
if (client == NULL) {
vm_mutex_unlock(&s->lock);
@ -220,7 +220,7 @@ wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor)
vm_mutex_lock(&s->lock);
if ((c = find_sensor_client(s, client_id, true)) != NULL)
bh_free(c);
wasm_runtime_free(c);
vm_mutex_unlock(&s->lock);
refresh_read_interval(s);
@ -272,7 +272,7 @@ sensor_obj_t
add_sys_sensor(char * name, char * description, int instance,
uint32 default_interval, void * read_func, void * config_func)
{
sys_sensor_t * s = (sys_sensor_t *) bh_malloc(sizeof(sys_sensor_t));
sys_sensor_t * s = (sys_sensor_t *) wasm_runtime_malloc(sizeof(sys_sensor_t));
if (s == NULL)
return NULL;
@ -282,15 +282,15 @@ add_sys_sensor(char * name, char * description, int instance,
s->default_interval = default_interval;
if (!s->name) {
bh_free(s);
wasm_runtime_free(s);
return NULL;
}
if (description) {
s->description = bh_strdup(description);
if (!s->description) {
bh_free(s->name);
bh_free(s);
wasm_runtime_free(s->name);
wasm_runtime_free(s);
return NULL;
}
}
@ -414,7 +414,7 @@ void sensor_cleanup_callback(uint32 module_id)
sensor_client_t *c;
vm_mutex_lock(&s->lock);
if ((c = find_sensor_client(s, module_id, true)) != NULL) {
bh_free(c);
wasm_runtime_free(c);
}
vm_mutex_unlock(&s->lock);
s = s->next;

View File

@ -124,7 +124,7 @@ void wgl_native_func_call(wasm_module_inst_t module_inst,
argc1++; /* module_inst */
argc1 += func_def->arg_num;
if (argc1 > 16) {
argv_copy = (intptr_t *)bh_malloc(func_def->arg_num *
argv_copy = (intptr_t *)wasm_runtime_malloc(func_def->arg_num *
sizeof(intptr_t));
if (argv_copy == NULL)
return;
@ -190,14 +190,14 @@ void wgl_native_func_call(wasm_module_inst_t module_inst,
}
if (argv_copy != argv_copy_buf)
bh_free(argv_copy);
wasm_runtime_free(argv_copy);
/* success return */
return;
fail:
if (argv_copy != argv_copy_buf)
bh_free(argv_copy);
wasm_runtime_free(argv_copy);
return;
}

View File

@ -93,7 +93,7 @@ static void cleanup_object_list(uint32 module_id)
found = true;
lv_obj_del(elem->obj);
bh_list_remove(&g_object_list, elem);
bh_free(elem);
wasm_runtime_free(elem);
elem = next;
} else {
elem = (object_node_t *)bh_list_elem_next(elem);
@ -150,7 +150,7 @@ bool wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id)
{
object_node_t *node;
node = (object_node_t *) bh_malloc(sizeof(object_node_t));
node = (object_node_t *) wasm_runtime_malloc(sizeof(object_node_t));
if (node == NULL)
return false;
@ -200,7 +200,7 @@ static void _obj_del_recursive(lv_obj_t *obj)
while (elem) {
if (obj == elem->obj) {
bh_list_remove(&g_object_list, elem);
bh_free(elem);
wasm_runtime_free(elem);
vm_mutex_unlock(&g_object_list_mutex);
return;
}
@ -237,7 +237,7 @@ static void post_widget_msg_to_module(object_node_t *object_node, lv_event_t eve
if (module == NULL)
return;
object_event = (object_event_t *)bh_malloc(sizeof(*object_event));
object_event = (object_event_t *)wasm_runtime_malloc(sizeof(*object_event));
if (object_event == NULL)
return;