Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions src/dice/loader.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) Huawei Technologies Co., Ltd. 2025. All rights reserved.
* Copyright (C) 2025 Huawei Technologies Co., Ltd.
* SPDX-License-Identifier: 0BSD
*/
#include <assert.h>
Expand All @@ -25,13 +25,13 @@ int ps_dispatch_max(void);
void ps_init_();

static DICE_CTOR void
_init()
init_()
{
ps_init_();
}

static void
_load_plugin(const char *path)
load_plugin_(const char *path)
{
log_debug("[%4d] LOAD: %s", DICE_MODULE_PRIO, path);
void *handle = dlopen(path, RTLD_GLOBAL | RTLD_LAZY);
Expand All @@ -41,7 +41,7 @@ _load_plugin(const char *path)
}

static char *
_strdup(const char *str)
strdup_(const char *str)
{
if (str == NULL)
return NULL;
Expand All @@ -60,15 +60,15 @@ PS_SUBSCRIBE(CHAIN_CONTROL, EVENT_DICE_INIT, {
log_debug("[%4d] LOAD: builtin modules: 0..%d", DICE_MODULE_PRIO,
ps_dispatch_max());
if (envvar != NULL) {
char *plugins = _strdup(envvar);
char *plugins = strdup_(envvar);
assert(plugins);

char *path = strtok(plugins, ":");
assert(path);
// skip first
path = strtok(NULL, ":");
while (path != NULL) {
_load_plugin(path);
load_plugin_(path);
path = strtok(NULL, ":");
}
mempool_free(plugins);
Expand Down
44 changes: 22 additions & 22 deletions src/dice/mempool.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) Huawei Technologies Co., Ltd. 2025. All rights reserved.
* Copyright (C) 2025 Huawei Technologies Co., Ltd.
* SPDX-License-Identifier: 0BSD
*/
#include <assert.h>
Expand All @@ -12,7 +12,7 @@
#include <dice/mempool.h>
#include <vsync/spinlock/caslock.h>

static size_t _sizes[] = {32,
static size_t sizes_[] = {32,
128,
512,
1024,
Expand All @@ -21,15 +21,15 @@ static size_t _sizes[] = {32,
1 * 1024 * 1024,
4 * 1024 * 1024,
8 * 1024 * 1024};
#define NSTACKS (sizeof(_sizes) / sizeof(size_t))
#define NSTACKS (sizeof(sizes_) / sizeof(size_t))

#define MEMPOOL_SIZE (1024 * 1024 * 200)

static unsigned int
_bucketize(size_t size)
bucketize_(size_t size)
{
unsigned int i = 0;
for (; i < NSTACKS && size > _sizes[i]; i++)
for (; i < NSTACKS && size > sizes_[i]; i++)
;
assert(i < NSTACKS);
return i;
Expand All @@ -54,46 +54,46 @@ typedef struct mempool {
entry_t *stack[NSTACKS];
} mempool_t;

static mempool_t _mp;
static mempool_t mp_;

/* bypass malloc interceptor */
REAL_DECL(void *, malloc, size_t n);

DICE_HIDE void
mempool_init(size_t cap)
{
memset(&_mp.stack, 0, sizeof(entry_t *) * NSTACKS);
_mp.allocated = 0;
_mp.pool.capacity = cap;
_mp.pool.next = 0;
_mp.pool.memory = REAL_CALL(malloc, 0, cap);
assert(_mp.pool.memory);
memset(_mp.pool.memory, 0, cap);
memset(&mp_.stack, 0, sizeof(entry_t *) * NSTACKS);
mp_.allocated = 0;
mp_.pool.capacity = cap;
mp_.pool.next = 0;
mp_.pool.memory = REAL_CALL(malloc, 0, cap);
assert(mp_.pool.memory);
memset(mp_.pool.memory, 0, cap);
// caslock already initialized with 0
}

static inline void
mempool_ensure_initd(void)
{
// assumes protected by lock
if (_mp.pool.memory == NULL)
if (mp_.pool.memory == NULL)
mempool_init(MEMPOOL_SIZE);
}

DICE_MODULE_INIT({
caslock_acquire(&_mp.lock);
caslock_acquire(&mp_.lock);
mempool_ensure_initd();
caslock_release(&_mp.lock);
caslock_release(&mp_.lock);
})

DICE_HIDE_IF void *
mempool_alloc(size_t n)
{
mempool_t *mp = &_mp;
mempool_t *mp = &mp_;
entry_t *e = NULL;
size_t size = n + sizeof(entry_t);
unsigned bucket = _bucketize(size);
size = _sizes[bucket];
unsigned bucket = bucketize_(size);
size = sizes_[bucket];
entry_t **stack = &mp->stack[bucket];
assert(stack);

Expand Down Expand Up @@ -138,12 +138,12 @@ mempool_realloc(void *ptr, size_t size)
DICE_HIDE_IF void
mempool_free(void *ptr)
{
mempool_t *mp = &_mp;
mempool_t *mp = &mp_;
assert(ptr);
entry_t *e = (entry_t *)ptr - 1;
size_t size = e->size + sizeof(entry_t);
unsigned bucket = _bucketize(size);
size = _sizes[bucket];
unsigned bucket = bucketize_(size);
size = sizes_[bucket];
entry_t **stack = &mp->stack[bucket];

// Mempool is used from rogue thread, serialization is necessary
Expand Down
32 changes: 16 additions & 16 deletions src/dice/pubsub.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) Huawei Technologies Co., Ltd. 2025. All rights reserved.
* Copyright (C) 2025 Huawei Technologies Co., Ltd.
* SPDX-License-Identifier: 0BSD
*/
#include <assert.h>
Expand Down Expand Up @@ -33,7 +33,7 @@ struct chain {
struct type types[MAX_TYPES];
};

static struct chain _chains[MAX_CHAINS];
static struct chain chains_[MAX_CHAINS];

// -----------------------------------------------------------------------------
// initializer
Expand All @@ -46,23 +46,23 @@ ps_initd_(void)
NONE,
START,
BLOCK,
} _state = NONE;
} state_ = NONE;
static bool ready = false;

if (likely(ready)) {
return true;
}

switch (_state) {
switch (state_) {
case NONE:
// This must be the main thread, at latest the thread creation.
_state = START;
state_ = START;
PS_PUBLISH(CHAIN_CONTROL, EVENT_DICE_INIT, 0, 0);
ready = true;
PS_PUBLISH(CHAIN_CONTROL, EVENT_DICE_READY, 0, 0);
return true;
case START:
_state = BLOCK;
state_ = BLOCK;
return true;
case BLOCK:
// The publication above is still running, drop nested publications.
Expand All @@ -87,7 +87,7 @@ DICE_MODULE_INIT()
// -----------------------------------------------------------------------------

static void
_ps_subscribe_sorted(struct sub **cur, chain_id chain, type_id type,
ps_subscribe_sorted_(struct sub **cur, chain_id chain, type_id type,
ps_callback_f cb, int prio, bool any_type)
{
// any_type is set if this subscription was from ANY_TYPE
Expand All @@ -101,7 +101,7 @@ _ps_subscribe_sorted(struct sub **cur, chain_id chain, type_id type,
next = *cur;
goto insert;
} else {
_ps_subscribe_sorted(&(*cur)->next, chain, type, cb, prio, any_type);
ps_subscribe_sorted_(&(*cur)->next, chain, type, cb, prio, any_type);
return;
}

Expand All @@ -117,20 +117,20 @@ _ps_subscribe_sorted(struct sub **cur, chain_id chain, type_id type,
}

static int
_ps_subscribe_type(chain_id chain, type_id type, ps_callback_f cb, int prio,
ps_subscribe_type_(chain_id chain, type_id type, ps_callback_f cb, int prio,
bool any_type)
{
(void)prio;
if (type > MAX_TYPES)
return PS_INVALID;

struct type *ev = &_chains[chain].types[type];
struct type *ev = &chains_[chain].types[type];

// increment the idx of next subscription
ev->count++;

// register subscription
_ps_subscribe_sorted(&ev->head, chain, type, cb, prio, any_type);
ps_subscribe_sorted_(&ev->head, chain, type, cb, prio, any_type);

return PS_OK;
}
Expand All @@ -150,11 +150,11 @@ ps_subscribe(chain_id chain, type_id type, ps_callback_f cb, int prio)
if (chain > MAX_CHAINS)
return PS_INVALID;
if (type != ANY_TYPE)
return _ps_subscribe_type(chain, type, cb, prio, false);
return ps_subscribe_type_(chain, type, cb, prio, false);

int err;
for (size_t i = 1; i < MAX_TYPES; i++)
if ((err = _ps_subscribe_type(chain, i, cb, prio, true)) != 0)
if ((err = ps_subscribe_type_(chain, i, cb, prio, true)) != 0)
return err;

return PS_OK;
Expand All @@ -165,15 +165,15 @@ ps_subscribe(chain_id chain, type_id type, ps_callback_f cb, int prio)
// -----------------------------------------------------------------------------

static enum ps_err
_ps_publish(const chain_id chain, const type_id type, void *event,
ps_publish_(const chain_id chain, const type_id type, void *event,
metadata_t *md)
{
if (unlikely(chain >= MAX_CHAINS))
return PS_INVALID;
if (unlikely(type == ANY_TYPE || type >= MAX_TYPES))
return PS_INVALID;

struct type *ev = &_chains[chain].types[type];
struct type *ev = &chains_[chain].types[type];
struct sub *cur = ev->head;
while (cur) {
// now we call the callback and abort the chain if the subscriber
Expand Down Expand Up @@ -215,5 +215,5 @@ ps_publish(const chain_id chain, const type_id type, void *event,
if (likely(err == PS_DROP_EVENT))
return PS_DROP_EVENT;

return _ps_publish(chain, type, event, md);
return ps_publish_(chain, type, event, md);
}
Loading