Skip to content

Commit 1208009

Browse files
committed
INTERNAL: Refactor the structure of additional item in mblck
1 parent c1539bd commit 1208009

File tree

4 files changed

+45
-25
lines changed

4 files changed

+45
-25
lines changed

mc_util.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static void do_mblck_pool_init(mblck_pool_t *pool, uint32_t blck_len)
3131
pool->tail = NULL;
3232
pool->head = NULL;
3333
pool->blck_len = blck_len;
34-
pool->body_len = blck_len - sizeof(void *);
34+
pool->body_len = blck_len - (sizeof(void *) + sizeof(uint32_t));
3535
pool->used_cnt = 0;
3636
pool->free_cnt = 0;
3737
}
@@ -107,16 +107,29 @@ int mblck_list_alloc(mblck_pool_t *pool, uint32_t item_len, uint32_t item_cnt,
107107
}
108108
assert(pool->free_cnt >= blck_cnt);
109109

110+
if (blck_cnt > 1) {
111+
list->rvec = (value_item **)malloc(sizeof(value_item *) * (blck_cnt - 1));
112+
if (list->rvec == NULL) {
113+
return -1;
114+
}
115+
} else {
116+
list->rvec = NULL;
117+
}
118+
119+
uint32_t cnt = 0;
110120
list->pool = (void*)pool;
111121
list->head = pool->head;
112122
list->tail = list->head;
113123
list->blck_cnt = 1;
114-
while (list->blck_cnt < blck_cnt) {
124+
while (list->blck_cnt + cnt < blck_cnt) {
115125
list->tail = list->tail->next;
116-
list->blck_cnt += 1;
126+
list->rvec[cnt] = &list->tail->data;
127+
list->rvec[cnt]->len = nitems_per_blck * item_len;
128+
cnt += 1;
117129
}
118130
pool->head = list->tail->next;
119131
list->tail->next = NULL;
132+
list->blck_cnt += cnt;
120133

121134
if (pool->head == NULL) {
122135
pool->tail = NULL;
@@ -141,7 +154,11 @@ void mblck_list_merge(mblck_list_t *pri_list, mblck_list_t *add_list)
141154
add_list->head = NULL;
142155
add_list->tail = NULL;
143156
add_list->blck_cnt = 0;
144-
/* FIXME: item_cnt and item_len: how to merge them ? */
157+
if (add_list->rvec != NULL) {
158+
free(add_list->rvec);
159+
add_list->rvec = NULL;
160+
}
161+
/* FIXME: item_cnt, item_len and rvec: how to merge them ? */
145162
}
146163

147164
void mblck_list_free(mblck_pool_t *pool, mblck_list_t *list)
@@ -160,6 +177,10 @@ void mblck_list_free(mblck_pool_t *pool, mblck_list_t *list)
160177
list->head = NULL;
161178
list->tail = NULL;
162179
list->blck_cnt = 0;
180+
if (list->rvec != NULL) {
181+
free(list->rvec);
182+
list->rvec = NULL;
183+
}
163184
}
164185
list->pool = NULL;
165186
}

mc_util.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ typedef struct _token_buff {
3737
*/
3838
typedef struct _mblck_node {
3939
struct _mblck_node *next;
40-
char data[1];
40+
value_item data;
4141
} mblck_node_t;
4242

4343
typedef struct _mblck_list {
4444
void *pool;
4545
mblck_node_t *head;
4646
mblck_node_t *tail;
47+
value_item **addnl;
4748
uint32_t blck_cnt;
4849
uint32_t body_len;
4950
uint32_t item_cnt;
@@ -68,8 +69,9 @@ typedef struct _mblck_pool {
6869
#define MBLCK_GET_BODYLEN(l) ((l)->body_len)
6970
#define MBLCK_GET_ITEMCNT(l) ((l)->item_cnt)
7071
#define MBLCK_GET_ITEMLEN(l) ((l)->item_len)
72+
#define MBLCK_GET_ADDLIST(l) ((l)->addnl)
7173
#define MBLCK_GET_NEXTBLK(b) ((b)->next)
72-
#define MBLCK_GET_BODYPTR(b) ((b)->data)
74+
#define MBLCK_GET_BODYPTR(b) ((b)->data.ptr)
7375

7476
/* memory block functions */
7577
int mblck_pool_create(mblck_pool_t *pool, uint32_t blck_len, uint32_t blck_cnt);

memcached.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,11 +1017,11 @@ static void ritem_set_first(conn *c, int rtype, int vleng)
10171017
c->rtype = rtype;
10181018

10191019
if (c->rtype == CONN_RTYPE_MBLCK) {
1020-
c->membk = MBLCK_GET_HEADBLK(&c->memblist);
1021-
c->ritem = MBLCK_GET_BODYPTR(c->membk);
1020+
c->ritem = MBLCK_GET_BODYPTR(MBLCK_GET_HEADBLK(&c->memblist));
10221021
c->rlbytes = vleng < MBLCK_GET_BODYLEN(&c->memblist)
10231022
? vleng : MBLCK_GET_BODYLEN(&c->memblist);
10241023
c->rltotal = vleng;
1024+
c->rindex = 0;
10251025
}
10261026
else if (c->rtype == CONN_RTYPE_HINFO) {
10271027
if (c->hinfo.naddnl == 0) {
@@ -1069,24 +1069,22 @@ static void ritem_set_next(conn *c)
10691069
{
10701070
assert(c->rltotal > 0);
10711071

1072+
value_item *ritem = NULL;
1073+
10721074
if (c->rtype == CONN_RTYPE_MBLCK) {
1073-
c->membk = MBLCK_GET_NEXTBLK(c->membk);
1074-
c->ritem = MBLCK_GET_BODYPTR(c->membk);
1075-
c->rlbytes = c->rltotal < MBLCK_GET_BODYLEN(&c->memblist)
1076-
? c->rltotal : MBLCK_GET_BODYLEN(&c->memblist);
1077-
}
1078-
else if (c->rtype == CONN_RTYPE_HINFO) {
1079-
c->ritem = c->hinfo.addnl[c->rindex]->ptr;
1080-
c->rlbytes = c->rltotal < c->hinfo.addnl[c->rindex]->len
1081-
? c->rltotal : c->hinfo.addnl[c->rindex]->len;
1082-
c->rindex += 1;
1083-
}
1084-
else if (c->rtype == CONN_RTYPE_EINFO) {
1085-
c->ritem = c->einfo.addnl[c->rindex]->ptr;
1086-
c->rlbytes = c->rltotal < c->einfo.addnl[c->rindex]->len
1087-
? c->rltotal : c->einfo.addnl[c->rindex]->len;
1088-
c->rindex += 1;
1075+
ritem = MBLCK_GET_ADDLIST(&c->memblist)[c->rindex];
1076+
} else if (c->rtype == CONN_RTYPE_HINFO) {
1077+
ritem = c->hinfo.addnl[c->rindex];
1078+
} else if (c->rtype == CONN_RTYPE_EINFO) {
1079+
ritem = c->einfo.addnl[c->rindex];
1080+
} else {
1081+
/* Invalid rtype */
1082+
return;
10891083
}
1084+
1085+
c->ritem = ritem->ptr;
1086+
c->rlbytes = c->rltotal < ritem->len ? c->rltotal : ritem->len;
1087+
c->rindex += 1;
10901088
}
10911089

10921090
/**

memcached.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ struct conn {
296296
uint32_t rlbytes;
297297
/* use memory blocks */
298298
uint32_t rltotal; /* Used when read data with memory block */
299-
mblck_node_t *membk; /* current memory block pointer */
300299
mblck_list_t memblist; /* (key or field) string memory block list */
301300

302301
/* hash item and elem item info */

0 commit comments

Comments
 (0)