te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - What is the use case of <wasm-rt.h> header file installed with wabt? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the use case of <wasm-rt.h> header file installed with wabt? - Stack Overflow

programmeradmin4浏览0评论

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, -

  1. When and how to use <wasm-rt.h> and wabt?
  2. 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();
}
发布评论

评论列表(0)

  1. 暂无评论