Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
WuJin committed Jul 19, 2024
1 parent a7d9761 commit b19b25a
Show file tree
Hide file tree
Showing 16 changed files with 223 additions and 223 deletions.
6 changes: 3 additions & 3 deletions examples/example-rbtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ int main(void) {
userdata_t* ud = malloc(sizeof(userdata_t));
if (ud) {
ud->value = i;
ud->node.rb_key.i32 = i;
ud->node.key.i32 = i;
cdk_rbtree_insert(&tree, &ud->node);
}
}
cdk_rbtree_node_key_t key;
key.i32 = 3;
userdata_t* ud = cdk_rbtree_data(cdk_rbtree_find(&tree, key), userdata_t, node);
if (ud) {
printf("key: %d, value: %d\n", ud->node.rb_key.i32, ud->value);
printf("key: %d, value: %d\n", ud->node.key.i32, ud->value);
cdk_rbtree_erase(&tree, &ud->node);
free(ud);
ud = NULL;
}
while(!cdk_rbtree_empty(&tree)) {
userdata_t* ud = cdk_rbtree_data(cdk_rbtree_first(&tree), userdata_t, node);
if (ud) {
printf("key: %d, value: %d\n", ud->node.rb_key.i32, ud->value);
printf("key: %d, value: %d\n", ud->node.key.i32, ud->value);
cdk_rbtree_erase(&tree, &ud->node);
free(ud);
ud = NULL;
Expand Down
6 changes: 3 additions & 3 deletions include/cdk/cdk-loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

_Pragma("once")

extern void* cdk_loader_create(char* m);
extern void* cdk_loader_load(void* m, const char* restrict f);
extern void cdk_loader_destroy(void* m);
extern void* cdk_loader_create(char* module);
extern void* cdk_loader_load(void* module, const char* restrict func);
extern void cdk_loader_destroy(void* module);

16 changes: 8 additions & 8 deletions include/cdk/cdk-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ union cdk_rbtree_node_key_u {

struct cdk_rbtree_node_s
{
cdk_rbtree_node_key_t rb_key;
struct cdk_rbtree_node_s* rb_parent;
struct cdk_rbtree_node_s* rb_right;
struct cdk_rbtree_node_s* rb_left;
char rb_color;
cdk_rbtree_node_key_t key;
struct cdk_rbtree_node_s* parent;
struct cdk_rbtree_node_s* right;
struct cdk_rbtree_node_s* left;
char color;
};

struct cdk_rwlock_s {
Expand All @@ -173,8 +173,8 @@ struct cdk_spinlock_s {

struct cdk_rbtree_s
{
cdk_rbtree_node_t* rb_root;
int(*rb_keycmp)(cdk_rbtree_node_key_t*, cdk_rbtree_node_key_t*);
cdk_rbtree_node_t* root;
int(*compare)(cdk_rbtree_node_key_t*, cdk_rbtree_node_key_t*);
};

struct cdk_list_node_s {
Expand All @@ -193,7 +193,7 @@ struct cdk_heap_s {
size_t heap_nelts;
/* a < b return positive that means min-heap */
/* a > b return positive, means max-heap */
int (*heap_cmp)(cdk_heap_node_t* a, cdk_heap_node_t* b);
int (*compare)(cdk_heap_node_t* a, cdk_heap_node_t* b);
};

struct cdk_thrdpool_s {
Expand Down
12 changes: 6 additions & 6 deletions src/cdk-loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@

#include "platform/platform-loader.h"

void* cdk_loader_create(char* m) {
return platform_loader_create(m);
void* cdk_loader_create(char* module) {
return platform_loader_create(module);
}

void* cdk_loader_load(void* m, const char* restrict f) {
return platform_loader_load(m, f);
void* cdk_loader_load(void* module, const char* restrict func) {
return platform_loader_load(module, func);
}

void cdk_loader_destroy(void* m) {
platform_loader_destroy(m);
void cdk_loader_destroy(void* module) {
platform_loader_destroy(module);
}
16 changes: 8 additions & 8 deletions src/cdk-logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ static const char* levels[] = {
static logger_t logger;
static atomic_flag initialized = ATOMIC_FLAG_INIT;

static inline void _logger_printer(void* arg) {
static inline void _printer(void* arg) {
fprintf(logger.file, "%s", (char*)arg);
fflush(logger.file);
free(arg);
arg = NULL;
}

static inline void _logger_syncbase(int level, const char* restrict file, int line) {
static inline void _syncbase(int level, const char* restrict file, int line) {
struct timespec tsc;
struct tm tm;
if (!timespec_get(&tsc, TIME_UTC)) { return; }
Expand All @@ -78,7 +78,7 @@ static inline void _logger_syncbase(int level, const char* restrict file, int li
);
}

static inline void _logger_asyncbase(int level, const char* restrict file, int line, const char* restrict fmt, va_list v) {
static inline void _asyncbase(int level, const char* restrict file, int line, const char* restrict fmt, va_list v) {
struct timespec tsc;
struct tm tm;
char buf[BUFSIZE];
Expand Down Expand Up @@ -108,7 +108,7 @@ static inline void _logger_asyncbase(int level, const char* restrict file, int l
char* arg = malloc(ret);
if (arg) {
memcpy(arg, buf, ret);
cdk_thrdpool_post(&logger.pool, _logger_printer, arg);
cdk_thrdpool_post(&logger.pool, _printer, arg);
}
}

Expand Down Expand Up @@ -145,10 +145,10 @@ void cdk_logger_log(int level, const char* restrict file, int line, const char*
char* p = strrchr(file, S);
if (!logger.async) {
if (!p) {
_logger_syncbase(level, file, line);
_syncbase(level, file, line);
}
if (p) {
_logger_syncbase(level, ++p, line);
_syncbase(level, ++p, line);
}
va_list v;
va_start(v, fmt);
Expand All @@ -161,10 +161,10 @@ void cdk_logger_log(int level, const char* restrict file, int line, const char*
va_list v;
va_start(v, fmt);
if (!p) {
_logger_asyncbase(level, file, line, fmt, v);
_asyncbase(level, file, line, fmt, v);
}
if (p) {
_logger_asyncbase(level, ++p, line, fmt, v);
_asyncbase(level, ++p, line, fmt, v);
}
va_end(v);
}
Expand Down
8 changes: 4 additions & 4 deletions src/cdk-threadpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ typedef struct thrdpool_job_s {
cdk_queue_node_t n;
}thrdpool_job_t;

static int _thrdpool_thrdfunc(void* arg) {
static int _thrdfunc(void* arg) {
cdk_thrdpool_t* pool = arg;
while (pool->status) {
mtx_lock(&pool->qmtx);
Expand All @@ -52,7 +52,7 @@ static int _thrdpool_thrdfunc(void* arg) {
return 0;
}

static void _thrdpool_createthread(cdk_thrdpool_t* pool) {
static void _createthread(cdk_thrdpool_t* pool) {
void* thrds;
mtx_lock(&pool->tmtx);
thrds = realloc(pool->thrds, (pool->thrdcnt + 1) * sizeof(thrd_t));
Expand All @@ -61,7 +61,7 @@ static void _thrdpool_createthread(cdk_thrdpool_t* pool) {
return;
}
pool->thrds = thrds;
thrd_create(pool->thrds + pool->thrdcnt, _thrdpool_thrdfunc, pool);
thrd_create(pool->thrds + pool->thrdcnt, _thrdfunc, pool);

pool->thrdcnt++;
mtx_unlock(&pool->tmtx);
Expand All @@ -78,7 +78,7 @@ void cdk_thrdpool_create(cdk_thrdpool_t* pool, int nthrds) {
pool->status = true;
pool->thrds = NULL;
for (int i = 0; i < nthrds; i++) {
_thrdpool_createthread(pool);
_createthread(pool);
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/container/cdk-heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "cdk/container/cdk-heap.h"

/* swap parent with child. child moves closer to the root, parent moves away. */
static void _heap_node_swap(cdk_heap_t* heap, cdk_heap_node_t* parent, cdk_heap_node_t* child) {
static void _node_swap(cdk_heap_t* heap, cdk_heap_node_t* parent, cdk_heap_node_t* child) {
cdk_heap_node_t* sibling;
cdk_heap_node_t t;

Expand Down Expand Up @@ -59,7 +59,7 @@ static void _heap_node_swap(cdk_heap_t* heap, cdk_heap_node_t* parent, cdk_heap_
void cdk_heap_init(cdk_heap_t* heap, int (*cmp)(cdk_heap_node_t* a, cdk_heap_node_t* b)) {
heap->heap_min = NULL;
heap->heap_nelts = 0;
heap->heap_cmp = cmp;
heap->compare = cmp;
}

void cdk_heap_insert(cdk_heap_t* heap, cdk_heap_node_t* node) {
Expand Down Expand Up @@ -99,8 +99,8 @@ void cdk_heap_insert(cdk_heap_t* heap, cdk_heap_node_t* node) {
/* walk up the tree and check at each node if the heap property holds.
* it's a min heap so parent < child must be true.
*/
while (node->parent != NULL && heap->heap_cmp(node, node->parent)) {
_heap_node_swap(heap, node->parent, node);
while (node->parent != NULL && heap->compare(node, node->parent)) {
_node_swap(heap, node->parent, node);
}
}

Expand Down Expand Up @@ -170,23 +170,23 @@ void cdk_heap_remove(cdk_heap_t* heap, cdk_heap_node_t* node) {
*/
for (;;) {
smallest = child;
if (child->left != NULL && heap->heap_cmp(child->left, smallest)) {
if (child->left != NULL && heap->compare(child->left, smallest)) {
smallest = child->left;
}
if (child->right != NULL && heap->heap_cmp(child->right, smallest)) {
if (child->right != NULL && heap->compare(child->right, smallest)) {
smallest = child->right;
}
if (smallest == child) {
break;
}
_heap_node_swap(heap, child, smallest);
_node_swap(heap, child, smallest);
}
/* walk up the subtree and check that each parent is less than the node
* this is required, because `max` node is not guaranteed to be the
* actual maximum in tree
*/
while (child->parent != NULL && heap->heap_cmp(child, child->parent)) {
_heap_node_swap(heap, child->parent, child);
while (child->parent != NULL && heap->compare(child, child->parent)) {
_node_swap(heap, child->parent, child);
}
}

Expand Down
Loading

0 comments on commit b19b25a

Please sign in to comment.