Skip to content

Commit b273038

Browse files
jooho812MinWooJin
authored andcommitted
FEATURE : Add block allocator for more efficient memory management
- This feature is applied only on list collection
1 parent 581b213 commit b273038

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-0
lines changed

engines/default/default_engine.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,23 @@ default_list_elem_alloc(ENGINE_HANDLE* handle, const void* cookie,
396396
return ret;
397397
}
398398

399+
#ifdef USE_EBLOCK_RESULT
400+
static void
401+
default_list_elem_release(ENGINE_HANDLE* handle, const void *cookie,
402+
eitem *eitem, EITEM_TYPE type)
403+
{
404+
struct default_engine *engine = get_handle(handle);
405+
list_elem_release(engine, eitem, type);
406+
}
407+
#else
399408
static void
400409
default_list_elem_release(ENGINE_HANDLE* handle, const void *cookie,
401410
eitem **eitem_array, const int eitem_count)
402411
{
403412
struct default_engine *engine = get_handle(handle);
404413
list_elem_release(engine, (list_elem_item**)eitem_array, eitem_count);
405414
}
415+
#endif
406416

407417
static ENGINE_ERROR_CODE
408418
default_list_elem_insert(ENGINE_HANDLE* handle, const void* cookie,
@@ -443,7 +453,11 @@ default_list_elem_get(ENGINE_HANDLE* handle, const void* cookie,
443453
const void* key, const int nkey,
444454
int from_index, int to_index,
445455
const bool delete, const bool drop_if_empty,
456+
#ifdef USE_EBLOCK_RESULT
457+
eblock_result_t *eblk_ret,
458+
#else
446459
eitem** eitem_array, uint32_t* eitem_count,
460+
#endif
447461
uint32_t* flags, bool* dropped, uint16_t vbucket)
448462
{
449463
struct default_engine *engine = get_handle(handle);
@@ -453,7 +467,11 @@ default_list_elem_get(ENGINE_HANDLE* handle, const void* cookie,
453467
if (delete) ACTION_BEFORE_WRITE(cookie, key, nkey);
454468
ret = list_elem_get(engine, key, nkey, from_index, to_index,
455469
delete, drop_if_empty,
470+
#ifdef USE_EBLOCK_RESULT
471+
eblk_ret,
472+
#else
456473
(list_elem_item**)eitem_array, eitem_count,
474+
#endif
457475
flags, dropped);
458476
if (delete) ACTION_AFTER_WRITE(cookie, ret);
459477
return ret;

engines/default/items.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,18 @@ static void do_list_elem_free(struct default_engine *engine, list_elem_item *ele
15291529
do_mem_slot_free(engine, elem, ntotal);
15301530
}
15311531

1532+
#ifdef USE_EBLOCK_RESULT
1533+
static void do_list_elem_release(struct default_engine *engine, eitem *eitem)
1534+
{
1535+
list_elem_item *elem = (list_elem_item *)eitem;
1536+
if (elem->refcount != 0) {
1537+
elem->refcount--;
1538+
}
1539+
if (elem->refcount == 0 && elem->next == (list_elem_item *)ADDR_MEANS_UNLINKED) {
1540+
do_list_elem_free(engine, elem);
1541+
}
1542+
}
1543+
#else
15321544
static void do_list_elem_release(struct default_engine *engine, list_elem_item *elem)
15331545
{
15341546
if (elem->refcount != 0) {
@@ -1538,6 +1550,7 @@ static void do_list_elem_release(struct default_engine *engine, list_elem_item *
15381550
do_list_elem_free(engine, elem);
15391551
}
15401552
}
1553+
#endif
15411554

15421555
static list_elem_item *do_list_elem_find(list_meta_info *info, int index)
15431556
{
@@ -1643,23 +1656,40 @@ static ENGINE_ERROR_CODE do_list_elem_get(struct default_engine *engine,
16431656
list_meta_info *info,
16441657
const int index, const uint32_t count,
16451658
const bool forward, const bool delete,
1659+
#ifdef USE_EBLOCK_RESULT
1660+
eblock_result_t *eblk_ret)
1661+
#else
16461662
list_elem_item **elem_array, uint32_t *elem_count)
1663+
#endif
16471664
{
16481665
uint32_t fcnt = 0; /* found count */
16491666
list_elem_item *tobe;
16501667
list_elem_item *elem;
16511668

1669+
#ifdef USE_EBLOCK_RESULT
1670+
if (!eblk_prepare(eblk_ret, (count == 0 || count > info->ccnt) ? info->ccnt : count))
1671+
return ENGINE_ENOMEM;
1672+
#endif
16521673
elem = do_list_elem_find(info, index);
16531674
while (elem != NULL) {
16541675
tobe = (forward ? elem->next : elem->prev);
16551676
elem->refcount++;
1677+
#ifdef USE_EBLOCK_RESULT
1678+
eblk_add_elem(eblk_ret, elem);
1679+
fcnt++;
1680+
#else
16561681
elem_array[fcnt++] = elem;
1682+
#endif
16571683
if (delete) do_list_elem_unlink(engine, info, elem, ELEM_DELETE_NORMAL);
16581684
if (count > 0 && fcnt >= count) break;
16591685
elem = tobe;
16601686
}
16611687

1688+
#ifdef USE_EBLOCK_RESULT
1689+
eblk_truncate(eblk_ret);
1690+
#else
16621691
*elem_count = fcnt;
1692+
#endif
16631693
if (fcnt > 0) {
16641694
return ENGINE_SUCCESS;
16651695
} else {
@@ -6574,6 +6604,13 @@ list_elem_item *list_elem_alloc(struct default_engine *engine,
65746604
return elem;
65756605
}
65766606

6607+
#ifdef USE_EBLOCK_RESULT
6608+
void list_elem_release(struct default_engine *engine,
6609+
eitem *eitem, EITEM_TYPE type)
6610+
{
6611+
do_coll_elem_release(engine, eitem, type, do_list_elem_release);
6612+
}
6613+
#else
65776614
void list_elem_release(struct default_engine *engine,
65786615
list_elem_item **elem_array, const int elem_count)
65796616
{
@@ -6588,6 +6625,7 @@ void list_elem_release(struct default_engine *engine,
65886625
}
65896626
pthread_mutex_unlock(&engine->cache_lock);
65906627
}
6628+
#endif
65916629

65926630
ENGINE_ERROR_CODE list_elem_insert(struct default_engine *engine,
65936631
const char *key, const size_t nkey,
@@ -6700,7 +6738,11 @@ ENGINE_ERROR_CODE list_elem_get(struct default_engine *engine,
67006738
const char *key, const size_t nkey,
67016739
int from_index, int to_index,
67026740
const bool delete, const bool drop_if_empty,
6741+
#ifdef USE_EBLOCK_RESULT
6742+
eblock_result_t *eblk_ret,
6743+
#else
67036744
list_elem_item **elem_array, uint32_t *elem_count,
6745+
#endif
67046746
uint32_t *flags, bool *dropped)
67056747
{
67066748
hash_item *it;
@@ -6722,7 +6764,11 @@ ENGINE_ERROR_CODE list_elem_get(struct default_engine *engine,
67226764
int index = from_index;
67236765
uint32_t count = (forward ? (to_index - from_index + 1)
67246766
: (from_index - to_index + 1));
6767+
#ifdef USE_EBLOCK_RESULT
6768+
ret = do_list_elem_get(engine, info, index, count, forward, delete, eblk_ret);
6769+
#else
67256770
ret = do_list_elem_get(engine, info, index, count, forward, delete, elem_array, elem_count);
6771+
#endif
67266772
if (ret == ENGINE_SUCCESS) {
67276773
if (info->ccnt == 0 && drop_if_empty) {
67286774
assert(delete == true);
@@ -6732,7 +6778,11 @@ ENGINE_ERROR_CODE list_elem_get(struct default_engine *engine,
67326778
*dropped = false;
67336779
}
67346780
*flags = it->flags;
6781+
#ifdef USE_EBLOCK_RESULT
6782+
} /* ret = ENGINE_ENOMEM or ENGINE_ELEM_ENOENT */
6783+
#else
67356784
} /* ret = ENGINE_ELEM_ENOENT */
6785+
#endif
67366786
}
67376787
} while (0);
67386788
do_item_release(engine, it);

engines/default/items.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,11 @@ list_elem_item *list_elem_alloc(struct default_engine *engine,
408408
const int nbytes, const void *cookie);
409409

410410
void list_elem_release(struct default_engine *engine,
411+
#ifdef USE_EBLOCK_RESULT
412+
eitem *eitem, EITEM_TYPE type);
413+
#else
411414
list_elem_item **elem_array, const int elem_count);
415+
#endif
412416

413417
ENGINE_ERROR_CODE list_elem_insert(struct default_engine *engine,
414418
const char *key, const size_t nkey,
@@ -426,7 +430,11 @@ ENGINE_ERROR_CODE list_elem_get(struct default_engine *engine,
426430
const char *key, const size_t nkey,
427431
int from_index, int to_index,
428432
const bool delete, const bool drop_if_empty,
433+
#ifdef USE_EBLOCK_RESULT
434+
eblock_result_t *eblk_ret,
435+
#else
429436
list_elem_item **elem_array, uint32_t *elem_count,
437+
#endif
430438
uint32_t *flags, bool *dropped);
431439

432440
ENGINE_ERROR_CODE set_struct_create(struct default_engine *engine,

engines/demo/demo_engine.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,21 @@ Demo_list_elem_alloc(ENGINE_HANDLE* handle, const void* cookie,
289289
return ENGINE_ENOTSUP;
290290
}
291291

292+
#ifdef USE_EBLOCK_RESULT
293+
static void
294+
Demo_list_elem_release(ENGINE_HANDLE* handle, const void *cookie,
295+
eitem *eitem, EITEM_TYPE type)
296+
{
297+
return;
298+
}
299+
#else
292300
static void
293301
Demo_list_elem_release(ENGINE_HANDLE* handle, const void *cookie,
294302
eitem **eitem_array, const int eitem_count)
295303
{
296304
return;
297305
}
306+
#endif
298307

299308
static ENGINE_ERROR_CODE
300309
Demo_list_elem_insert(ENGINE_HANDLE* handle, const void* cookie,
@@ -319,7 +328,11 @@ Demo_list_elem_get(ENGINE_HANDLE* handle, const void* cookie,
319328
const void* key, const int nkey,
320329
int from_index, int to_index,
321330
const bool delete, const bool drop_if_empty,
331+
#ifdef USE_EBLOCK_RESULT
332+
eblock_result_t *eblk_ret,
333+
#else
322334
eitem** eitem_array, uint32_t* eitem_count,
335+
#endif
323336
uint32_t* flags, bool* dropped, uint16_t vbucket)
324337
{
325338
return ENGINE_ENOTSUP;

include/memcached/engine.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,11 @@ extern "C" {
351351
const size_t nbytes, eitem** eitem);
352352

353353
void (*list_elem_release)(ENGINE_HANDLE* handle, const void *cookie,
354+
#ifdef USE_EBLOCK_RESULT
355+
eitem *eitem, EITEM_TYPE type);
356+
#else
354357
eitem **eitem_array, const int eitem_count);
358+
#endif
355359

356360
ENGINE_ERROR_CODE (*list_elem_insert)(ENGINE_HANDLE* handle, const void* cookie,
357361
const void* key, const int nkey,
@@ -370,7 +374,11 @@ extern "C" {
370374
const void* key, const int nkey,
371375
int from_index, int to_index,
372376
const bool delete, const bool drop_if_empty,
377+
#ifdef USE_EBLOCK_RESULT
378+
eblock_result_t *eblk_ret,
379+
#else
373380
eitem** eitem_array, uint32_t* eitem_count,
381+
#endif
374382
uint32_t* flags, bool* dropped,
375383
uint16_t vbucket);
376384

0 commit comments

Comments
 (0)