add more comments

This commit is contained in:
vms 2019-08-16 12:58:24 +03:00
parent 485b69a948
commit 2ca09f634a
3 changed files with 59 additions and 62 deletions

View File

@ -1,13 +0,0 @@
#include "allocator.h"
#include <stdlib.h>
#define UNUSED(x) (void)(x)
void *allocate(size_t size) {
return malloc(size);
}
void deallocate(void *ptr, size_t size) {
UNUSED(size);
free(ptr);
}

View File

@ -1,27 +0,0 @@
#ifndef C_SDK_ALLOCATOR_H
#define C_SDK_ALLOCATOR_H
#include <stddef.h> // for size_t
/**
* Allocates a memory region of given size.
*
* Used by Wasm VM for byte array passing. Should be exported from module.
*
* @param size a size of needed memory region.
* @return a pointer to allocated memory region.
*/
void *allocate(size_t size);
/**
* Frees a memory region.
*
* Used by Wasm VM for freeing previous memory allocated by `allocate` function.
* Should be exported from module.
*
* @param ptr the pointer to the previously allocated memory region.
* @param size the size of the previously allocated memory region.
*/
void deallocate(void *ptr, size_t size);
#endif //C_SDK_ALLOCATOR_H

View File

@ -15,38 +15,48 @@ int init() {
int g_isInited = 0; int g_isInited = 0;
/**
* Stores one byte by a given address in the module memory.
*
* @param ptr a address where byte should be stored
* @param value a byte to be stored
*/
void store(char *ptr, unsigned char byte) { void store(char *ptr, unsigned char byte) {
*ptr = byte; *ptr = byte;
} }
void sqlite_store(char *ptr, unsigned char byte) { /**
store(ptr, byte); * Returns one byte by a given address in the module memory.
} *
* @param ptr a address at which the needed byte is located
unsigned char load(const unsigned char *ptr) { * @return the byte at the given address
*/
unsigned char load(const unsigned char *ptr) {
return *ptr; return *ptr;
} }
unsigned char sqlite_load(const unsigned char *ptr) { /**
return load(ptr); * Allocates a memory region of a given size.
} * Could be used by Wasm execution environments for byte array passing.
*
void* allocate(size_t size) { * @param size a size of allocated memory region
* @return a pointer to the allocated memory region
*/
void* allocate(size_t size) {
return malloc(size + 1); return malloc(size + 1);
} }
void* sqlite_allocate(size_t size) { /**
return allocate(size); * Frees a memory region.
} * Could be used by Wasm execution environments for freeing previous memory allocated by `allocate` function.
*
void deallocate(void *ptr, int size) { * @param ptr a pointer to the previously allocated memory region
* @param size a size of the previously allocated memory region
*/
void deallocate(void *ptr, int size) {
free(ptr); free(ptr);
} }
void sqlite_deallocate(void *ptr, int size) {
deallocate(ptr, size);
}
char *write_response(char *response, int response_size) { char *write_response(char *response, int response_size) {
char *result_response = allocate(response_size + 4); char *result_response = allocate(response_size + 4);
@ -130,7 +140,15 @@ static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
return 0; return 0;
} }
const char *invoke(char *request, int request_size) { /**
* Executes given SQL request and returns result in as a pointer to the following structure:
* | result size (4 bytes, le)| result (size bytes) |.
*
* @param sql a pointer to the supplied sql request
* @param length a size of the supplied sql request
* @return a pointer to the struct contains result_size and result
*/
const char *invoke(char *request, int request_size) {
if(g_isInited == 0) { if(g_isInited == 0) {
// TODO: check the return code // TODO: check the return code
init(); init();
@ -143,12 +161,13 @@ const char *invoke(char *request, int request_size) {
g_isInited = 1; g_isInited = 1;
} }
request[request_size] = 0; request[request_size] = '\n';
#ifdef ENABLE_LOG #ifdef ENABLE_LOG
wasm_log(request, request_size); wasm_log(request, request_size);
wasm_log("\n", 1);
#endif #endif
request[request_size] = 0;
ShellText str; ShellText str;
initText(&str); initText(&str);
char *errorMessage = 0; char *errorMessage = 0;
@ -175,6 +194,24 @@ const char *invoke(char *request, int request_size) {
return response; return response;
} }
// these functions are needed to support Rust backends
// (https://github.com/rust-lang/rust/issues/63562)
const char *sqlite_invoke(char *request, int request_size) { const char *sqlite_invoke(char *request, int request_size) {
return invoke(request, request_size); return invoke(request, request_size);
} }
void* sqlite_allocate(size_t size) {
return allocate(size);
}
void sqlite_deallocate(void *ptr, int size) {
deallocate(ptr, size);
}
void sqlite_store(char *ptr, unsigned char byte) {
store(ptr, byte);
}
unsigned char sqlite_load(const unsigned char *ptr) {
return load(ptr);
}