I don't find any info about when to use the <wasm-rt.h>
header and how, in the GitHub readme of wabt
(WebAssembly binary toolkit).
I was originally thinking and trying out ways to compile a C file containing pthreads
utilities used, into a wasm
file which I can import in a JavaScript file.
I don't know if wabt
will be useful, so I installed wasmtime
, wabt
and wasi-sdk
all of these. And now I am confused what to use. I seem to have wasmtime
CLI installed but not the C APIs. The <wasmtime.h>, <wasm.h> and <wasi.h> headers are not found.
So, I have actually two questions, -
- When and how to use <wasm-rt.h> and wabt?
- Is it possible to compile my C code into WASM and use that WASM in js files in browsers and node?
I am here showing portions of my code so that you have an idea if this is viable or not. My header file using pthreads -
/**
* SQL_MAP is an experimental custom data structure implemented by me.
* (currently for using in my personal projects)
* The name SQL_MAP comes from the inspiration from the power of foreign keys in a table can access primary key of some other table (power of RDBMS).
* Initially tried to do something similar.
*/
#ifndef SQL_MAP_H
#define SQL_MAP_H
#include <stddef.h>
#if defined(__GNUC__)
#define USE_THREAD_SAFETY
#include <pthread.h>
#else
#warning "Thread safety is disabled. Compile with gcc and pthreads to enable thread safety."
#endif
// -------------------------
// Structures and Typedefs
// -------------------------
typedef struct IndexNode {
char *key; // Interned key string
int *dataIndex; // Pointer to index in dataNodes array
struct IndexNode *next; // For chaining within buckets
} IndexNode;
typedef struct DataNode {
void *data;
} DataNode;
typedef struct SQLMap {
IndexNode **buckets; // Array of bucket pointers (chaining)
size_t bucketCount; // Number of buckets (capacity)
size_t entryCount; // Number of key-value entries
DataNode *dataNodes; // Dynamic array of data nodes
size_t dataCapacity; // Capacity of dataNodes
size_t dataCount; // Number of stored data nodes
#ifdef USE_THREAD_SAFETY
pthread_mutex_t mutex; // Mutex for thread safety
#endif
} SQLMap;
// -------------------------
// Function Prototypes
// -------------------------
// Create and initialize a new SQLMap instance.
SQLMap* sql_map_create(void);
// Insert or update a key-value pair in the SQLMap.
void sql_map_put(SQLMap *map, const char *key, void *value);
// Retrieve the value associated with the given key. Returns NULL if not found.
void* sql_map_get(SQLMap *map, const char *key);
// Remove a key-value pair from the SQLMap. Returns 1 if removed, 0 if not found.
int sql_map_remove(SQLMap *map, const char *key);
// Free all memory associated with the SQLMap.
// Note: The stored data (void*) is not freed; freeing it is the caller's responsibility.
void sql_map_free(SQLMap *map);
#endif // SQL_MAP_H
Portions of my C code using pthreads -
static void lock_map(SQLMap *map) {
#ifdef USE_THREAD_SAFETY
pthread_mutex_lock(&map->mutex);
#endif
}
static void unlock_map(SQLMap *map) {
#ifdef USE_THREAD_SAFETY
pthread_mutex_unlock(&map->mutex);
#endif
}
SQLMap* sql_map_create(void) {
// some code
// some code
#ifdef USE_THREAD_SAFETY
if (pthread_mutex_init(&map->mutex, NULL) != 0) {
perror("Failed to initialize mutex");
exit(EXIT_FAILURE);
}
#endif
return map;
}
void sql_map_free(SQLMap *map) {
lock_map(map);
// Free each chain in the buckets.
for (size_t i = 0; i < map->bucketCount; i++) {
IndexNode *node = map->buckets[i];
while (node) {
IndexNode *temp = node;
node = node->next;
free(temp->dataIndex);
free(temp);
}
}
free(map->buckets);
free(map->dataNodes);
#ifdef USE_THREAD_SAFETY
pthread_mutex_destroy(&map->mutex);
#endif
unlock_map(map);
free(map);
// Optionally free the intern pool at program end.
free_intern_pool();
}