Skip to content

Commit

Permalink
INTERNAL: Refactor the structure of additional item in mblck
Browse files Browse the repository at this point in the history
  • Loading branch information
ing-eoking committed Apr 26, 2024
1 parent c1539bd commit 47c3805
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
23 changes: 19 additions & 4 deletions mc_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void do_mblck_pool_init(mblck_pool_t *pool, uint32_t blck_len)
pool->tail = NULL;
pool->head = NULL;
pool->blck_len = blck_len;
pool->body_len = blck_len - sizeof(void *);
pool->body_len = blck_len - (sizeof(void *) + sizeof(uint32_t));
pool->used_cnt = 0;
pool->free_cnt = 0;
}
Expand Down Expand Up @@ -107,16 +107,23 @@ int mblck_list_alloc(mblck_pool_t *pool, uint32_t item_len, uint32_t item_cnt,
}
assert(pool->free_cnt >= blck_cnt);

if (blck_cnt > 1) {
list->rvec = (value_item **)malloc(sizeof(value_item *) * (blck_cnt - 1));
}
uint32_t rvec_cnt = 0;
list->pool = (void*)pool;
list->head = pool->head;
list->tail = list->head;
list->blck_cnt = 1;
while (list->blck_cnt < blck_cnt) {
while (list->blck_cnt + rvec_cnt < blck_cnt) {
list->tail = list->tail->next;
list->blck_cnt += 1;
list->rvec[rvec_cnt] = &list->tail->data;
list->rvec[rvec_cnt]->len = nitems_per_blck * item_len;
rvec_cnt += 1;
}
pool->head = list->tail->next;
list->tail->next = NULL;
list->blck_cnt += rvec_cnt;

if (pool->head == NULL) {
pool->tail = NULL;
Expand All @@ -141,7 +148,11 @@ void mblck_list_merge(mblck_list_t *pri_list, mblck_list_t *add_list)
add_list->head = NULL;
add_list->tail = NULL;
add_list->blck_cnt = 0;
/* FIXME: item_cnt and item_len: how to merge them ? */
if (add_list->rvec != NULL) {
free(add_list->rvec);
add_list->rvec = NULL;
}
/* FIXME: item_cnt, item_len and rvec: how to merge them ? */
}

void mblck_list_free(mblck_pool_t *pool, mblck_list_t *list)
Expand All @@ -160,6 +171,10 @@ void mblck_list_free(mblck_pool_t *pool, mblck_list_t *list)
list->head = NULL;
list->tail = NULL;
list->blck_cnt = 0;
if (list->rvec != NULL) {
free(list->rvec);
list->rvec = NULL;
}
}
list->pool = NULL;
}
Expand Down
5 changes: 3 additions & 2 deletions mc_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ typedef struct _token_buff {
*/
typedef struct _mblck_node {
struct _mblck_node *next;
char data[1];
value_item data;
} mblck_node_t;

typedef struct _mblck_list {
void *pool;
mblck_node_t *head;
mblck_node_t *tail;
value_item **rvec;
uint32_t blck_cnt;
uint32_t body_len;
uint32_t item_cnt;
Expand All @@ -69,7 +70,7 @@ typedef struct _mblck_pool {
#define MBLCK_GET_ITEMCNT(l) ((l)->item_cnt)
#define MBLCK_GET_ITEMLEN(l) ((l)->item_len)
#define MBLCK_GET_NEXTBLK(b) ((b)->next)
#define MBLCK_GET_BODYPTR(b) ((b)->data)
#define MBLCK_GET_BODYPTR(b) ((b)->data.ptr)

/* memory block functions */
int mblck_pool_create(mblck_pool_t *pool, uint32_t blck_len, uint32_t blck_cnt);
Expand Down
5 changes: 3 additions & 2 deletions memcached.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@ static void ritem_set_first(conn *c, int rtype, int vleng)
c->rlbytes = vleng < MBLCK_GET_BODYLEN(&c->memblist)
? vleng : MBLCK_GET_BODYLEN(&c->memblist);
c->rltotal = vleng;
c->rindex = 0;
}
else if (c->rtype == CONN_RTYPE_HINFO) {
if (c->hinfo.naddnl == 0) {
Expand Down Expand Up @@ -1070,10 +1071,10 @@ static void ritem_set_next(conn *c)
assert(c->rltotal > 0);

if (c->rtype == CONN_RTYPE_MBLCK) {
c->membk = MBLCK_GET_NEXTBLK(c->membk);
c->ritem = MBLCK_GET_BODYPTR(c->membk);
c->ritem = c->memblist.rvec[c->rindex]->ptr;
c->rlbytes = c->rltotal < MBLCK_GET_BODYLEN(&c->memblist)
? c->rltotal : MBLCK_GET_BODYLEN(&c->memblist);
c->rindex += 1;
}
else if (c->rtype == CONN_RTYPE_HINFO) {
c->ritem = c->hinfo.addnl[c->rindex]->ptr;
Expand Down

0 comments on commit 47c3805

Please sign in to comment.