enhance ordq

This commit is contained in:
Rongsong Shen 2025-12-16 17:04:04 +08:00
parent c478ea8516
commit 05abed1964
4 changed files with 18 additions and 3 deletions

View file

@ -14,7 +14,7 @@ typedef struct _ordq_item {
void* data; void* data;
} ordq_item_t; } ordq_item_t;
typedef int (*ordq_cmp_fn_t)(ordkey_t, ordkey_t); typedef int (*ordq_cmp_fn_t)(void*, ordkey_t, ordkey_t);
typedef void (*ordq_data_killer_t)(void*); typedef void (*ordq_data_killer_t)(void*);
typedef struct { typedef struct {
@ -22,6 +22,7 @@ typedef struct {
ordq_item_t* head; ordq_item_t* head;
ordq_data_killer_t killer; ordq_data_killer_t killer;
ordq_cmp_fn_t cmp; ordq_cmp_fn_t cmp;
void* ctx;
} ordq_t; } ordq_t;
#define mk_ordkey(k) (ordkey_t)((long)(k)) #define mk_ordkey(k) (ordkey_t)((long)(k))
@ -31,5 +32,6 @@ extern void ordq_fini(ordq_t* q);
extern int ordq_put(ordq_t*, ordkey_t, void*); extern int ordq_put(ordq_t*, ordkey_t, void*);
extern ordq_item_t* ordq_get(ordq_t*); extern ordq_item_t* ordq_get(ordq_t*);
extern ordq_item_t* ordq_peek(ordq_t*); extern ordq_item_t* ordq_peek(ordq_t*);
extern void ordq_set_ctx(ordq_t*, void*);
#endif #endif

View file

@ -16,6 +16,7 @@ int ordq_init(ordq_t* q, ordq_data_killer_t killer, ordq_cmp_fn_t cmp)
q->head = NULL; q->head = NULL;
q->killer = killer; q->killer = killer;
q->cmp = cmp; q->cmp = cmp;
q->ctx = NULL;
return 0; return 0;
} }
@ -25,7 +26,7 @@ static int ordq_put_(ordq_t* q, ordq_item_t* it)
ordq_item_t** p = &(q->head); ordq_item_t** p = &(q->head);
while ((*p) != NULL) { while ((*p) != NULL) {
if (q->cmp((*p)->key, it->key) > 0) { if (q->cmp(q->ctx, (*p)->key, it->key) > 0) {
/* we found the location to insert */ /* we found the location to insert */
break; break;
} }
@ -105,3 +106,14 @@ ordq_item_t* ordq_peek(ordq_t* q)
return it; return it;
} }
void ordq_set_ctx(ordq_t* q, void* ctx)
{
if (q == NULL) {
return;
}
mylock_lock(&(q->lock));
q->ctx = ctx;
mylock_unlock(&(q->lock));
}

View file

@ -7,7 +7,7 @@
#include "unity.h" #include "unity.h"
#include "utils.h" #include "utils.h"
static int long_cmp(ordkey_t k1, ordkey_t k2) static int long_cmp(void* ctx, ordkey_t k1, ordkey_t k2)
{ {
if ((long)k1 < (long)k2) { if ((long)k1 < (long)k2) {
return -1; return -1;

View file

@ -6,6 +6,7 @@
#include "client.h" #include "client.h"
#include "xce/types.h" #include "xce/types.h"
#include "mylock.h"
#include "xlist.h" #include "xlist.h"
typedef enum { typedef enum {