Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions core/shared/platform/esp-idf/espidf_malloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

#include "platform_api_vmcore.h"
#include "platform_api_extension.h"

void *
os_malloc(unsigned size)
{
void *buf_origin;
void *buf_fixed;
uintptr_t *addr_field;

buf_origin = malloc(size + 8 + sizeof(uintptr_t));
buf_fixed = buf_origin + sizeof(void *);
if ((uintptr_t)buf_fixed & (uintptr_t)0x7) {
buf_fixed = (void *)((uintptr_t)(buf_fixed + 8) & (~(uintptr_t)7));
}

addr_field = buf_fixed - sizeof(uintptr_t);
*addr_field = (uintptr_t)buf_origin;

return buf_fixed;
}

void *
os_realloc(void *ptr, unsigned size)
{
void *mem_origin;
void *mem_new;
void *mem_new_fixed;
uintptr_t *addr_field;

if (!ptr) {
return NULL;
}

addr_field = ptr - sizeof(uintptr_t);
mem_origin = (void *)(*addr_field);
mem_new = realloc(mem_origin, size + 8 + sizeof(uintptr_t));

if (mem_origin != mem_new) {
mem_new_fixed = mem_new + sizeof(uintptr_t);
if ((uint32)mem_new_fixed & 0x7) {
mem_new_fixed =
(void *)((uintptr_t)(mem_new + 8) & (~(uintptr_t)7));
}

addr_field = mem_new_fixed - sizeof(uintptr_t);
*addr_field = (uintptr_t)mem_new;

return mem_new_fixed;
}

return ptr;
}

void
os_free(void *ptr)
{
void *mem_origin;
uintptr *addr_field;

if (ptr) {
addr_field = ptr - sizeof(uintptr_t);
mem_origin = (void *)(*addr_field);

free(mem_origin);
}
}
29 changes: 29 additions & 0 deletions core/shared/platform/esp-idf/espidf_memmap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

#include "platform_api_vmcore.h"
#include "platform_api_extension.h"

void *
os_mmap(void *hint, size_t size, int prot, int flags)
{
return os_malloc((int)size);
}

void
os_munmap(void *addr, size_t size)
{
return os_free(addr);
}

int
os_mprotect(void *addr, size_t size, int prot)
{
return 0;
}

void
os_dcache_flush()
{}
86 changes: 10 additions & 76 deletions core/shared/platform/esp-idf/espidf_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,15 @@
#include "platform_api_vmcore.h"
#include "platform_api_extension.h"

int errno = 0;

int
os_thread_sys_init();

void
os_thread_sys_destroy();

int
bh_platform_init()
{
return os_thread_sys_init();
return 0;
}

void
bh_platform_destroy()
{
os_thread_sys_destroy();
}
{}

int
os_printf(const char *format, ...)
Expand All @@ -45,76 +35,20 @@ os_vprintf(const char *format, va_list ap)
return vprintf(format, ap);
}

void *
os_mmap(void *hint, size_t size, int prot, int flags)
uint64
os_time_get_boot_microsecond(void)
{

return BH_MALLOC(size);
return (uint64)esp_timer_get_time();
}

void
os_munmap(void *addr, size_t size)
uint8 *
os_thread_get_stack_boundary(void)
{
BH_FREE(addr);
return NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there API to get the stack boundary? If not, runtime might not be able to check the native stack overflow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to look it up, but I didn't find one before. Although I am quite sure that it should exist somewhere!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, so let's merge the patch firstly, and fix it in the future.

}

int
os_mprotect(void *addr, size_t size, int prot)
os_usleep(uint32 usec)
{
return 0;
}

void
os_dcache_flush()
{}

int
atoi(const char *nptr)
{
bool is_negative = false;
int total = 0;
const char *p = nptr;
char temp = '0';

if (NULL == p) {
os_printf("invlaid atoi input\n");
return 0;
}

if (*p == '-') {
is_negative = true;
p++;
}

while ((temp = *p++) != '\0') {
if (temp > '9' || temp < '0') {
continue;
}

total = total * 10 + (int)(temp - '0');
}

if (is_negative)
total = 0 - total;

return total;
}

void *
memmove(void *dest, const void *src, size_t n)
{
char *d = dest;
const char *s = src;

if (d < s) {
while (n--)
*d++ = *s++;
}
else {
const char *lasts = s + (n - 1);
char *lastd = d + (n - 1);
while (n--)
*lastd-- = *lasts--;
}
return dest;
return usleep(usec);
}
Loading