Skip to content

Commit e7d48df

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

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

mc_util.c

Lines changed: 19 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,23 @@ 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+
}
113+
uint32_t rvec_cnt = 0;
110114
list->pool = (void*)pool;
111115
list->head = pool->head;
112116
list->tail = list->head;
113117
list->blck_cnt = 1;
114-
while (list->blck_cnt < blck_cnt) {
118+
while (list->blck_cnt + rvec_cnt < blck_cnt) {
115119
list->tail = list->tail->next;
116-
list->blck_cnt += 1;
120+
list->rvec[rvec_cnt] = &list->tail->data;
121+
list->rvec[rvec_cnt]->len = nitems_per_blck * item_len;
122+
rvec_cnt += 1;
117123
}
118124
pool->head = list->tail->next;
119125
list->tail->next = NULL;
126+
list->blck_cnt += rvec_cnt;
120127

121128
if (pool->head == NULL) {
122129
pool->tail = NULL;
@@ -141,7 +148,11 @@ void mblck_list_merge(mblck_list_t *pri_list, mblck_list_t *add_list)
141148
add_list->head = NULL;
142149
add_list->tail = NULL;
143150
add_list->blck_cnt = 0;
144-
/* FIXME: item_cnt and item_len: how to merge them ? */
151+
if (add_list->rvec != NULL) {
152+
free(add_list->rvec);
153+
add_list->rvec = NULL;
154+
}
155+
/* FIXME: item_cnt, item_len and rvec: how to merge them ? */
145156
}
146157

147158
void mblck_list_free(mblck_pool_t *pool, mblck_list_t *list)
@@ -160,6 +171,10 @@ void mblck_list_free(mblck_pool_t *pool, mblck_list_t *list)
160171
list->head = NULL;
161172
list->tail = NULL;
162173
list->blck_cnt = 0;
174+
if (list->rvec != NULL) {
175+
free(list->rvec);
176+
list->rvec = NULL;
177+
}
163178
}
164179
list->pool = NULL;
165180
}

mc_util.h

Lines changed: 3 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 **rvec;
4748
uint32_t blck_cnt;
4849
uint32_t body_len;
4950
uint32_t item_cnt;
@@ -69,7 +70,7 @@ typedef struct _mblck_pool {
6970
#define MBLCK_GET_ITEMCNT(l) ((l)->item_cnt)
7071
#define MBLCK_GET_ITEMLEN(l) ((l)->item_len)
7172
#define MBLCK_GET_NEXTBLK(b) ((b)->next)
72-
#define MBLCK_GET_BODYPTR(b) ((b)->data)
73+
#define MBLCK_GET_BODYPTR(b) ((b)->data.ptr)
7374

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

memcached.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,7 @@ static void ritem_set_first(conn *c, int rtype, int vleng)
10221022
c->rlbytes = vleng < MBLCK_GET_BODYLEN(&c->memblist)
10231023
? vleng : MBLCK_GET_BODYLEN(&c->memblist);
10241024
c->rltotal = vleng;
1025+
c->rindex = 0;
10251026
}
10261027
else if (c->rtype == CONN_RTYPE_HINFO) {
10271028
if (c->hinfo.naddnl == 0) {
@@ -1070,10 +1071,10 @@ static void ritem_set_next(conn *c)
10701071
assert(c->rltotal > 0);
10711072

10721073
if (c->rtype == CONN_RTYPE_MBLCK) {
1073-
c->membk = MBLCK_GET_NEXTBLK(c->membk);
1074-
c->ritem = MBLCK_GET_BODYPTR(c->membk);
1074+
c->ritem = c->memblist.rvec[c->rindex]->ptr;
10751075
c->rlbytes = c->rltotal < MBLCK_GET_BODYLEN(&c->memblist)
10761076
? c->rltotal : MBLCK_GET_BODYLEN(&c->memblist);
1077+
c->rindex += 1;
10771078
}
10781079
else if (c->rtype == CONN_RTYPE_HINFO) {
10791080
c->ritem = c->hinfo.addnl[c->rindex]->ptr;

0 commit comments

Comments
 (0)