Re-org memory allocation interfaces, add --stack-size and --heap-size option (#193)
This commit is contained in:
@ -1,4 +0,0 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
obj-y += bh_assert.o bh_definition.o bh_memory.o bh_platform_log.o bh_thread.o bh_time.o
|
||||
@ -4,7 +4,6 @@
|
||||
*/
|
||||
|
||||
#include "bh_platform.h"
|
||||
#include "bh_memory.h"
|
||||
#include "bh_common.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -58,16 +57,33 @@ bh_platform_init()
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *
|
||||
os_malloc(unsigned size)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *
|
||||
os_realloc(void *ptr, unsigned size)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
os_free(void *ptr)
|
||||
{
|
||||
}
|
||||
|
||||
void *
|
||||
bh_mmap(void *hint, unsigned int size, int prot, int flags)
|
||||
{
|
||||
return bh_malloc(size);
|
||||
return BH_MALLOC(size);
|
||||
}
|
||||
|
||||
void
|
||||
bh_munmap(void *addr, uint32 size)
|
||||
{
|
||||
return bh_free(addr);
|
||||
return BH_FREE(addr);
|
||||
}
|
||||
|
||||
int
|
||||
|
||||
@ -8,7 +8,6 @@
|
||||
|
||||
#include "bh_config.h"
|
||||
#include "bh_types.h"
|
||||
#include "bh_memory.h"
|
||||
#include <autoconf.h>
|
||||
#include <zephyr.h>
|
||||
#include <kernel.h>
|
||||
@ -55,10 +54,6 @@ typedef korp_thread *korp_tid;
|
||||
typedef struct k_mutex korp_mutex;
|
||||
typedef struct k_sem korp_sem;
|
||||
|
||||
#define wa_malloc bh_malloc
|
||||
#define wa_free bh_free
|
||||
#define wa_strdup bh_strdup
|
||||
|
||||
struct bh_thread_wait_node;
|
||||
typedef struct bh_thread_wait_node *bh_thread_wait_list;
|
||||
typedef struct korp_cond {
|
||||
@ -68,8 +63,9 @@ typedef struct korp_cond {
|
||||
|
||||
typedef void* (*thread_start_routine_t)(void*);
|
||||
|
||||
#define wa_malloc bh_malloc
|
||||
#define wa_free bh_free
|
||||
void *os_malloc(unsigned size);
|
||||
void *os_realloc(void *ptr, unsigned size);
|
||||
void os_free(void *ptr);
|
||||
|
||||
#define bh_printf printf
|
||||
|
||||
@ -102,11 +98,6 @@ int snprintf(char *buffer, size_t count, const char *format, ...);
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2,
|
||||
unsigned int n);
|
||||
int b_strcat_s(char * s1, size_t s1max, const char * s2);
|
||||
int b_strcpy_s(char * s1, size_t s1max, const char * s2);
|
||||
|
||||
/* math functions */
|
||||
double sqrt(double x);
|
||||
double floor(double x);
|
||||
|
||||
@ -6,9 +6,6 @@
|
||||
#include "bh_thread.h"
|
||||
#include "bh_assert.h"
|
||||
#include "bh_log.h"
|
||||
#include "bh_memory.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct bh_thread_wait_node {
|
||||
struct k_sem sem;
|
||||
@ -140,11 +137,11 @@ static void thread_obj_list_reclaim()
|
||||
if (p->to_be_freed) {
|
||||
if (p_prev == NULL) { /* p is the head of list */
|
||||
thread_obj_list = p->next;
|
||||
bh_free(p);
|
||||
BH_FREE(p);
|
||||
p = thread_obj_list;
|
||||
} else { /* p is not the head of list */
|
||||
p_prev->next = p->next;
|
||||
bh_free(p);
|
||||
BH_FREE(p);
|
||||
p = p_prev->next;
|
||||
}
|
||||
} else {
|
||||
@ -210,7 +207,7 @@ static void vm_thread_cleanup(void)
|
||||
/* Set flag to true for the next thread creating to
|
||||
free the thread object */
|
||||
((bh_thread_obj*) thread_data->tid)->to_be_freed = true;
|
||||
bh_free(thread_data);
|
||||
BH_FREE(thread_data);
|
||||
}
|
||||
|
||||
static void vm_thread_wrapper(void *start, void *arg, void *thread_data)
|
||||
@ -244,15 +241,15 @@ int _vm_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
|
||||
thread_obj_list_reclaim();
|
||||
|
||||
/* Create and initialize thread object */
|
||||
if (!(tid = bh_malloc(sizeof(bh_thread_obj))))
|
||||
if (!(tid = BH_MALLOC(sizeof(bh_thread_obj))))
|
||||
return BHT_ERROR;
|
||||
|
||||
memset(tid, 0, sizeof(bh_thread_obj));
|
||||
|
||||
/* Create and initialize thread data */
|
||||
thread_data_size = offsetof(bh_thread_data, stack) + stack_size;
|
||||
if (!(thread_data = bh_malloc(thread_data_size))) {
|
||||
bh_free(tid);
|
||||
if (!(thread_data = BH_MALLOC(thread_data_size))) {
|
||||
BH_FREE(tid);
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
@ -265,8 +262,8 @@ int _vm_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
|
||||
if (!((tid = k_thread_create(tid, (k_thread_stack_t *) thread_data->stack,
|
||||
stack_size, vm_thread_wrapper, start, arg, thread_data, prio, 0,
|
||||
K_NO_WAIT)))) {
|
||||
bh_free(tid);
|
||||
bh_free(thread_data);
|
||||
BH_FREE(tid);
|
||||
BH_FREE(thread_data);
|
||||
return BHT_ERROR;
|
||||
}
|
||||
|
||||
@ -305,7 +302,7 @@ int _vm_thread_join(korp_tid thread, void **value_ptr, int mills)
|
||||
bh_thread_wait_node *node;
|
||||
|
||||
/* Create wait node and append it to wait list */
|
||||
if (!(node = bh_malloc(sizeof(bh_thread_wait_node))))
|
||||
if (!(node = BH_MALLOC(sizeof(bh_thread_wait_node))))
|
||||
return BHT_ERROR;
|
||||
|
||||
k_sem_init(&node->sem, 0, 1);
|
||||
@ -334,7 +331,7 @@ int _vm_thread_join(korp_tid thread, void **value_ptr, int mills)
|
||||
k_sleep(100);
|
||||
|
||||
/* Destroy resource */
|
||||
bh_free(node);
|
||||
BH_FREE(node);
|
||||
return BHT_OK;
|
||||
}
|
||||
|
||||
@ -449,7 +446,7 @@ static int _vm_cond_wait_internal(korp_cond *cond, korp_mutex *mutex,
|
||||
bh_thread_wait_node *node;
|
||||
|
||||
/* Create wait node and append it to wait list */
|
||||
if (!(node = bh_malloc(sizeof(bh_thread_wait_node))))
|
||||
if (!(node = BH_MALLOC(sizeof(bh_thread_wait_node))))
|
||||
return BHT_ERROR;
|
||||
|
||||
k_sem_init(&node->sem, 0, 1);
|
||||
@ -483,7 +480,7 @@ static int _vm_cond_wait_internal(korp_cond *cond, korp_mutex *mutex,
|
||||
p = p->next;
|
||||
p->next = node->next;
|
||||
}
|
||||
bh_free(node);
|
||||
BH_FREE(node);
|
||||
k_mutex_unlock(&cond->wait_list_lock);
|
||||
|
||||
return BHT_OK;
|
||||
|
||||
Reference in New Issue
Block a user