re-org platform APIs, simplify porting process (#201)

Co-authored-by: Xu Jun <jun1.xu@intel.com>
This commit is contained in:
Xu Jun
2020-03-16 16:43:57 +08:00
committed by GitHub
parent ef5ceffe71
commit f1a0e75ab7
177 changed files with 2954 additions and 7904 deletions

View File

@ -0,0 +1,53 @@
#include "bh_read_file.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
char*
bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
{
char *buffer;
int file;
uint32 file_size, read_size;
struct stat stat_buf;
if (!filename || !ret_size) {
printf("Read file to buffer failed: invalid filename or ret size.\n");
return NULL;
}
if ((file = open(filename, O_RDONLY, 0)) == -1) {
printf("Read file to buffer failed: open file %s failed.\n",
filename);
return NULL;
}
if (fstat(file, &stat_buf) != 0) {
printf("Read file to buffer failed: fstat file %s failed.\n",
filename);
close(file);
return NULL;
}
file_size = (uint32)stat_buf.st_size;
if (!(buffer = BH_MALLOC(file_size))) {
printf("Read file to buffer failed: alloc memory failed.\n");
close(file);
return NULL;
}
read_size = (uint32)read(file, buffer, file_size);
close(file);
if (read_size < file_size) {
printf("Read file to buffer failed: read file content failed.\n");
BH_FREE(buffer);
return NULL;
}
*ret_size = file_size;
return buffer;
}

View File

@ -0,0 +1,23 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _BH_FILE_H
#define _BH_FILE_H
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
char *
bh_read_file_to_buffer(const char *filename, uint32 *ret_size);
#ifdef __cplusplus
}
#endif
#endif /* end of _BH_FILE_H */

View File

@ -0,0 +1,11 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
set (UNCOMMON_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${UNCOMMON_SHARED_DIR})
file (GLOB_RECURSE source_all ${UNCOMMON_SHARED_DIR}/*.c)
set (UNCOMMON_SHARED_SOURCE ${source_all})