add skiplist wrapper

pull/5/head
yuqing 2015-12-30 11:16:06 +08:00
parent a1fc869430
commit 66e2db7b6f
9 changed files with 276 additions and 89 deletions

View File

@ -1,5 +1,5 @@
Version 1.24 2015-12-29 Version 1.24 2015-12-30
* php extension compiled on PHP 7 * php extension compiled on PHP 7
* add skiplist which support stable sort * add skiplist which support stable sort
* make.sh: use sed to replace perl * make.sh: use sed to replace perl

View File

@ -10,7 +10,7 @@ FAST_SHARED_OBJS = hash.lo chain.lo shared_func.lo ini_file_reader.lo \
avl_tree.lo ioevent.lo ioevent_loop.lo fast_task_queue.lo \ avl_tree.lo ioevent.lo ioevent_loop.lo fast_task_queue.lo \
fast_timer.lo process_ctrl.lo fast_mblock.lo \ fast_timer.lo process_ctrl.lo fast_mblock.lo \
connection_pool.lo fast_mpool.lo fast_allocator.lo \ connection_pool.lo fast_mpool.lo fast_allocator.lo \
fast_buffer.lo skiplist.lo multi_skiplist.lo fast_buffer.lo multi_skiplist.lo flat_skiplist.lo
FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \ FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \
logger.o sockopt.o base64.o sched_thread.o \ logger.o sockopt.o base64.o sched_thread.o \
@ -18,7 +18,7 @@ FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \
avl_tree.o ioevent.o ioevent_loop.o fast_task_queue.o \ avl_tree.o ioevent.o ioevent_loop.o fast_task_queue.o \
fast_timer.o process_ctrl.o fast_mblock.o \ fast_timer.o process_ctrl.o fast_mblock.o \
connection_pool.o fast_mpool.o fast_allocator.o \ connection_pool.o fast_mpool.o fast_allocator.o \
fast_buffer.o skiplist.o multi_skiplist.o fast_buffer.o multi_skiplist.o flat_skiplist.o
HEADER_FILES = common_define.h hash.h chain.h logger.h base64.h \ HEADER_FILES = common_define.h hash.h chain.h logger.h base64.h \
shared_func.h pthread_func.h ini_file_reader.h _os_define.h \ shared_func.h pthread_func.h ini_file_reader.h _os_define.h \
@ -26,7 +26,8 @@ HEADER_FILES = common_define.h hash.h chain.h logger.h base64.h \
avl_tree.h ioevent.h ioevent_loop.h fast_task_queue.h \ avl_tree.h ioevent.h ioevent_loop.h fast_task_queue.h \
fast_timer.h process_ctrl.h fast_mblock.h \ fast_timer.h process_ctrl.h fast_mblock.h \
connection_pool.h fast_mpool.h fast_allocator.h \ connection_pool.h fast_mpool.h fast_allocator.h \
fast_buffer.h skiplist.h multi_skiplist.h fast_buffer.h skiplist.h multi_skiplist.h flat_skiplist.h \
skiplist_common.h
ALL_OBJS = $(FAST_STATIC_OBJS) $(FAST_SHARED_OBJS) ALL_OBJS = $(FAST_STATIC_OBJS) $(FAST_SHARED_OBJS)

View File

@ -6,7 +6,7 @@
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. * Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
**/ **/
//skiplist.c //flat_skiplist.c
#include <ctype.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
@ -15,9 +15,9 @@
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include "logger.h" #include "logger.h"
#include "skiplist.h" #include "flat_skiplist.h"
int skiplist_init_ex(Skiplist *sl, const int level_count, int flat_skiplist_init_ex(FlatSkiplist *sl, const int level_count,
skiplist_compare_func compare_func, skiplist_free_func free_func, skiplist_compare_func compare_func, skiplist_free_func free_func,
const int min_alloc_elements_once) const int min_alloc_elements_once)
{ {
@ -61,7 +61,7 @@ int skiplist_init_ex(Skiplist *sl, const int level_count,
} }
for (i=level_count-1; i>=0; i--) { for (i=level_count-1; i>=0; i--) {
element_size = sizeof(SkiplistNode) + sizeof(SkiplistNode *) * (i + 1); element_size = sizeof(FlatSkiplistNode) + sizeof(FlatSkiplistNode *) * (i + 1);
if ((result=fast_mblock_init_ex(sl->mblocks + i, if ((result=fast_mblock_init_ex(sl->mblocks + i,
element_size, alloc_elements_once, NULL, false)) != 0) element_size, alloc_elements_once, NULL, false)) != 0)
{ {
@ -74,13 +74,13 @@ int skiplist_init_ex(Skiplist *sl, const int level_count,
sl->top_level_index = level_count - 1; sl->top_level_index = level_count - 1;
top_mblock = sl->mblocks + sl->top_level_index; top_mblock = sl->mblocks + sl->top_level_index;
sl->top = (SkiplistNode *)fast_mblock_alloc_object(top_mblock); sl->top = (FlatSkiplistNode *)fast_mblock_alloc_object(top_mblock);
if (sl->top == NULL) { if (sl->top == NULL) {
return ENOMEM; return ENOMEM;
} }
memset(sl->top, 0, top_mblock->info.element_size); memset(sl->top, 0, top_mblock->info.element_size);
sl->tail = (SkiplistNode *)fast_mblock_alloc_object(sl->mblocks + 0); sl->tail = (FlatSkiplistNode *)fast_mblock_alloc_object(sl->mblocks + 0);
if (sl->tail == NULL) { if (sl->tail == NULL) {
return ENOMEM; return ENOMEM;
} }
@ -99,11 +99,11 @@ int skiplist_init_ex(Skiplist *sl, const int level_count,
return 0; return 0;
} }
void skiplist_destroy(Skiplist *sl) void flat_skiplist_destroy(FlatSkiplist *sl)
{ {
int i; int i;
SkiplistNode *node; FlatSkiplistNode *node;
SkiplistNode *deleted; FlatSkiplistNode *deleted;
if (sl->mblocks == NULL) { if (sl->mblocks == NULL) {
return; return;
@ -126,7 +126,7 @@ void skiplist_destroy(Skiplist *sl)
sl->mblocks = NULL; sl->mblocks = NULL;
} }
static inline int skiplist_get_level_index(Skiplist *sl) static inline int flat_skiplist_get_level_index(FlatSkiplist *sl)
{ {
int i; int i;
@ -139,16 +139,16 @@ static inline int skiplist_get_level_index(Skiplist *sl)
return i; return i;
} }
int skiplist_insert(Skiplist *sl, void *data) int flat_skiplist_insert(FlatSkiplist *sl, void *data)
{ {
int i; int i;
int level_index; int level_index;
SkiplistNode *node; FlatSkiplistNode *node;
SkiplistNode *previous; FlatSkiplistNode *previous;
SkiplistNode *current = NULL; FlatSkiplistNode *current = NULL;
level_index = skiplist_get_level_index(sl); level_index = flat_skiplist_get_level_index(sl);
node = (SkiplistNode *)fast_mblock_alloc_object(sl->mblocks + level_index); node = (FlatSkiplistNode *)fast_mblock_alloc_object(sl->mblocks + level_index);
if (node == NULL) { if (node == NULL) {
return ENOMEM; return ENOMEM;
} }
@ -182,13 +182,13 @@ int skiplist_insert(Skiplist *sl, void *data)
return 0; return 0;
} }
static SkiplistNode *skiplist_get_previous(Skiplist *sl, void *data, static FlatSkiplistNode *flat_skiplist_get_previous(FlatSkiplist *sl, void *data,
int *level_index) int *level_index)
{ {
int i; int i;
int cmp; int cmp;
SkiplistNode *previous; FlatSkiplistNode *previous;
SkiplistNode *found; FlatSkiplistNode *found;
found = NULL; found = NULL;
previous = sl->top; previous = sl->top;
@ -212,14 +212,14 @@ DONE:
return found; return found;
} }
int skiplist_delete(Skiplist *sl, void *data) int flat_skiplist_delete(FlatSkiplist *sl, void *data)
{ {
int i; int i;
int level_index; int level_index;
SkiplistNode *previous; FlatSkiplistNode *previous;
SkiplistNode *deleted; FlatSkiplistNode *deleted;
previous = skiplist_get_previous(sl, data, &level_index); previous = flat_skiplist_get_previous(sl, data, &level_index);
if (previous == NULL) { if (previous == NULL) {
return ENOENT; return ENOENT;
} }
@ -245,32 +245,32 @@ int skiplist_delete(Skiplist *sl, void *data)
return 0; return 0;
} }
int skiplist_delete_all(Skiplist *sl, void *data, int *delete_count) int flat_skiplist_delete_all(FlatSkiplist *sl, void *data, int *delete_count)
{ {
*delete_count = 0; *delete_count = 0;
while (skiplist_delete(sl, data) == 0) { while (flat_skiplist_delete(sl, data) == 0) {
(*delete_count)++; (*delete_count)++;
} }
return *delete_count > 0 ? 0 : ENOENT; return *delete_count > 0 ? 0 : ENOENT;
} }
void *skiplist_find(Skiplist *sl, void *data) void *flat_skiplist_find(FlatSkiplist *sl, void *data)
{ {
int level_index; int level_index;
SkiplistNode *previous; FlatSkiplistNode *previous;
previous = skiplist_get_previous(sl, data, &level_index); previous = flat_skiplist_get_previous(sl, data, &level_index);
return (previous != NULL) ? previous->links[level_index]->data : NULL; return (previous != NULL) ? previous->links[level_index]->data : NULL;
} }
int skiplist_find_all(Skiplist *sl, void *data, SkiplistIterator *iterator) int flat_skiplist_find_all(FlatSkiplist *sl, void *data, FlatSkiplistIterator *iterator)
{ {
int level_index; int level_index;
SkiplistNode *previous; FlatSkiplistNode *previous;
SkiplistNode *last; FlatSkiplistNode *last;
previous = skiplist_get_previous(sl, data, &level_index); previous = flat_skiplist_get_previous(sl, data, &level_index);
if (previous == NULL) { if (previous == NULL) {
iterator->top = sl->top; iterator->top = sl->top;
iterator->current = sl->top; iterator->current = sl->top;

87
src/flat_skiplist.h Normal file
View File

@ -0,0 +1,87 @@
/**
* Copyright (C) 2015 Happy Fish / YuQing
*
* libfastcommon may be copied only under the terms of the GNU General
* Public License V3, which may be found in the FastDFS source kit.
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
**/
//flat_skiplist.h, support stable sort :)
#ifndef _FLAT_SKIPLIST_H
#define _FLAT_SKIPLIST_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common_define.h"
#include "skiplist_common.h"
#include "fast_mblock.h"
typedef struct flat_skiplist_node
{
void *data;
struct flat_skiplist_node *prev; //for stable sort
struct flat_skiplist_node *links[0];
} FlatSkiplistNode;
typedef struct flat_skiplist
{
int level_count;
int top_level_index;
skiplist_compare_func compare_func;
skiplist_free_func free_func;
struct fast_mblock_man *mblocks; //node allocators
FlatSkiplistNode *top; //the top node
FlatSkiplistNode *tail; //the tail node for interator
} FlatSkiplist;
typedef struct flat_skiplist_iterator {
FlatSkiplistNode *top;
FlatSkiplistNode *current;
} FlatSkiplistIterator;
#ifdef __cplusplus
extern "C" {
#endif
#define flat_skiplist_init(sl, level_count, compare_func, free_func) \
flat_skiplist_init_ex(sl, level_count, compare_func, free_func, \
SKIPLIST_DEFAULT_MIN_ALLOC_ELEMENTS_ONCE)
int flat_skiplist_init_ex(FlatSkiplist *sl, const int level_count,
skiplist_compare_func compare_func, skiplist_free_func free_func,
const int min_alloc_elements_once);
void flat_skiplist_destroy(FlatSkiplist *sl);
int flat_skiplist_insert(FlatSkiplist *sl, void *data);
int flat_skiplist_delete(FlatSkiplist *sl, void *data);
int flat_skiplist_delete_all(FlatSkiplist *sl, void *data, int *delete_count);
void *flat_skiplist_find(FlatSkiplist *sl, void *data);
int flat_skiplist_find_all(FlatSkiplist *sl, void *data, FlatSkiplistIterator *iterator);
static inline void flat_skiplist_iterator(FlatSkiplist *sl, FlatSkiplistIterator *iterator)
{
iterator->top = sl->top;
iterator->current = sl->tail->prev;
}
static inline void *flat_skiplist_next(FlatSkiplistIterator *iterator)
{
void *data;
if (iterator->current == iterator->top) {
return NULL;
}
data = iterator->current->data;
iterator->current = iterator->current->prev;
return data;
}
#ifdef __cplusplus
}
#endif
#endif

View File

@ -18,8 +18,8 @@
#include "multi_skiplist.h" #include "multi_skiplist.h"
int multi_skiplist_init_ex(MultiSkiplist *sl, const int level_count, int multi_skiplist_init_ex(MultiSkiplist *sl, const int level_count,
multi_skiplist_compare_func compare_func, skiplist_compare_func compare_func,
multi_skiplist_free_func free_func, skiplist_free_func free_func,
const int min_alloc_elements_once) const int min_alloc_elements_once)
{ {
int bytes; int bytes;

View File

@ -15,9 +15,7 @@
#include <string.h> #include <string.h>
#include "common_define.h" #include "common_define.h"
#include "fast_mblock.h" #include "fast_mblock.h"
#include "skiplist_common.h"
typedef int (*multi_skiplist_compare_func)(const void *p1, const void *p2);
typedef void (*multi_skiplist_free_func)(void *ptr);
typedef struct multi_skiplist_data typedef struct multi_skiplist_data
{ {
@ -36,8 +34,8 @@ typedef struct multi_skiplist
{ {
int level_count; int level_count;
int top_level_index; int top_level_index;
multi_skiplist_compare_func compare_func; skiplist_compare_func compare_func;
multi_skiplist_free_func free_func; skiplist_free_func free_func;
struct fast_mblock_man data_mblock; //data node allocators struct fast_mblock_man data_mblock; //data node allocators
struct fast_mblock_man *mblocks; //node allocators struct fast_mblock_man *mblocks; //node allocators
MultiSkiplistNode *top; //the top node MultiSkiplistNode *top; //the top node
@ -56,15 +54,13 @@ typedef struct multi_skiplist_iterator {
extern "C" { extern "C" {
#endif #endif
#define SKIPLIST_DEFAULT_MIN_ALLOC_ELEMENTS_ONCE 128
#define multi_skiplist_init(sl, level_count, compare_func, free_func) \ #define multi_skiplist_init(sl, level_count, compare_func, free_func) \
multi_skiplist_init_ex(sl, level_count, compare_func, free_func, \ multi_skiplist_init_ex(sl, level_count, compare_func, free_func, \
SKIPLIST_DEFAULT_MIN_ALLOC_ELEMENTS_ONCE) SKIPLIST_DEFAULT_MIN_ALLOC_ELEMENTS_ONCE)
int multi_skiplist_init_ex(MultiSkiplist *sl, const int level_count, int multi_skiplist_init_ex(MultiSkiplist *sl, const int level_count,
multi_skiplist_compare_func compare_func, skiplist_compare_func compare_func,
multi_skiplist_free_func free_func, skiplist_free_func free_func,
const int min_alloc_elements_once); const int min_alloc_elements_once);
void multi_skiplist_destroy(MultiSkiplist *sl); void multi_skiplist_destroy(MultiSkiplist *sl);

View File

@ -14,73 +14,133 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "common_define.h" #include "common_define.h"
#include "fast_mblock.h" #include "skiplist_common.h"
#include "flat_skiplist.h"
#include "multi_skiplist.h"
typedef int (*skiplist_compare_func)(const void *p1, const void *p2); #define SKIPLIST_TYPE_FLAT 0
typedef void (*skiplist_free_func)(void *ptr); #define SKIPLIST_TYPE_MULTI 1
typedef struct skiplist_node
{
void *data;
struct skiplist_node *prev; //for stable sort
struct skiplist_node *links[0];
} SkiplistNode;
typedef struct skiplist typedef struct skiplist
{ {
int level_count; int type;
int top_level_index; union {
skiplist_compare_func compare_func; FlatSkiplist flat;
skiplist_free_func free_func; MultiSkiplist multi;
struct fast_mblock_man *mblocks; //node allocators } u;
SkiplistNode *top; //the top node
SkiplistNode *tail; //the tail node for interator
} Skiplist; } Skiplist;
typedef struct skiplist_iterator { typedef struct skiplist_iterator {
SkiplistNode *top; int type;
SkiplistNode *current; union {
FlatSkiplistIterator flat;
MultiSkiplistIterator multi;
} u;
} SkiplistIterator; } SkiplistIterator;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#define SKIPLIST_DEFAULT_MIN_ALLOC_ELEMENTS_ONCE 128
#define skiplist_init(sl, level_count, compare_func, free_func) \ #define skiplist_init(sl, level_count, compare_func, free_func) \
skiplist_init_ex(sl, level_count, compare_func, free_func, \ skiplist_init_ex(sl, level_count, compare_func, free_func, \
SKIPLIST_DEFAULT_MIN_ALLOC_ELEMENTS_ONCE) SKIPLIST_DEFAULT_MIN_ALLOC_ELEMENTS_ONCE, SKIPLIST_TYPE_FLAT)
int skiplist_init_ex(Skiplist *sl, const int level_count, static inline int skiplist_init_ex(Skiplist *sl, const int level_count,
skiplist_compare_func compare_func, skiplist_free_func free_func, skiplist_compare_func compare_func, skiplist_free_func free_func,
const int min_alloc_elements_once); const int min_alloc_elements_once, const int type)
{
sl->type = type;
if (type == SKIPLIST_TYPE_FLAT) {
return flat_skiplist_init_ex(&sl->u.flat, level_count,
compare_func, free_func, min_alloc_elements_once);
}
else {
return multi_skiplist_init_ex(&sl->u.multi, level_count,
compare_func, free_func, min_alloc_elements_once);
}
}
void skiplist_destroy(Skiplist *sl); static inline void skiplist_destroy(Skiplist *sl)
{
if (sl->type == SKIPLIST_TYPE_FLAT) {
flat_skiplist_destroy(&sl->u.flat);
}
else {
multi_skiplist_destroy(&sl->u.multi);
}
}
int skiplist_insert(Skiplist *sl, void *data); static inline int skiplist_insert(Skiplist *sl, void *data)
int skiplist_delete(Skiplist *sl, void *data); {
int skiplist_delete_all(Skiplist *sl, void *data, int *delete_count); if (sl->type == SKIPLIST_TYPE_FLAT) {
void *skiplist_find(Skiplist *sl, void *data); return flat_skiplist_insert(&sl->u.flat, data);
int skiplist_find_all(Skiplist *sl, void *data, SkiplistIterator *iterator); }
else {
return multi_skiplist_insert(&sl->u.multi, data);
}
}
static inline int skiplist_delete(Skiplist *sl, void *data)
{
if (sl->type == SKIPLIST_TYPE_FLAT) {
return flat_skiplist_delete(&sl->u.flat, data);
}
else {
return multi_skiplist_delete(&sl->u.multi, data);
}
}
static inline int skiplist_delete_all(Skiplist *sl, void *data, int *delete_count)
{
if (sl->type == SKIPLIST_TYPE_FLAT) {
return flat_skiplist_delete_all(&sl->u.flat, data, delete_count);
}
else {
return multi_skiplist_delete_all(&sl->u.multi, data, delete_count);
}
}
static inline void *skiplist_find(Skiplist *sl, void *data)
{
if (sl->type == SKIPLIST_TYPE_FLAT) {
return flat_skiplist_find(&sl->u.flat, data);
}
else {
return multi_skiplist_find(&sl->u.multi, data);
}
}
static inline int skiplist_find_all(Skiplist *sl, void *data, SkiplistIterator *iterator)
{
iterator->type = sl->type;
if (sl->type == SKIPLIST_TYPE_FLAT) {
return flat_skiplist_find_all(&sl->u.flat, data, &iterator->u.flat);
}
else {
return multi_skiplist_find_all(&sl->u.multi, data, &iterator->u.multi);
}
}
static inline void skiplist_iterator(Skiplist *sl, SkiplistIterator *iterator) static inline void skiplist_iterator(Skiplist *sl, SkiplistIterator *iterator)
{ {
iterator->top = sl->top; iterator->type = sl->type;
iterator->current = sl->tail->prev; if (sl->type == SKIPLIST_TYPE_FLAT) {
return flat_skiplist_iterator(&sl->u.flat, &iterator->u.flat);
}
else {
return multi_skiplist_iterator(&sl->u.multi, &iterator->u.multi);
}
} }
static inline void *skiplist_next(SkiplistIterator *iterator) static inline void *skiplist_next(SkiplistIterator *iterator)
{ {
void *data; if (iterator->type == SKIPLIST_TYPE_FLAT) {
return flat_skiplist_next(&iterator->u.flat);
if (iterator->current == iterator->top) { }
return NULL; else {
return multi_skiplist_next(&iterator->u.multi);
} }
data = iterator->current->data;
iterator->current = iterator->current->prev;
return data;
} }
#ifdef __cplusplus #ifdef __cplusplus

32
src/skiplist_common.h Normal file
View File

@ -0,0 +1,32 @@
/**
* Copyright (C) 2015 Happy Fish / YuQing
*
* libfastcommon may be copied only under the terms of the GNU General
* Public License V3, which may be found in the FastDFS source kit.
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
**/
//skiplist_common.h
#ifndef _SKIPLIST_COMMON_H
#define _SKIPLIST_COMMON_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common_define.h"
#define SKIPLIST_DEFAULT_MIN_ALLOC_ELEMENTS_ONCE 128
typedef int (*skiplist_compare_func)(const void *p1, const void *p2);
typedef void (*skiplist_free_func)(void *ptr);
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@ -18,6 +18,7 @@
static int *numbers; static int *numbers;
static Skiplist sl; static Skiplist sl;
static SkiplistIterator iterator; static SkiplistIterator iterator;
static int skiplist_type = SKIPLIST_TYPE_FLAT;
static int instance_count = 0; static int instance_count = 0;
@ -135,7 +136,7 @@ static int test_stable_sort()
instance_count = 0; instance_count = 0;
result = skiplist_init_ex(&sl, 12, compare_record, result = skiplist_init_ex(&sl, 12, compare_record,
free_test_func, 128); free_test_func, 128, skiplist_type);
if (result != 0) { if (result != 0) {
return result; return result;
} }
@ -222,7 +223,17 @@ int main(int argc, char *argv[])
int index2; int index2;
int result; int result;
log_init(); log_init();
if (argc > 1) {
if (strcasecmp(argv[1], "multi") == 0 || strcmp(argv[1], "1") == 0) {
skiplist_type = SKIPLIST_TYPE_MULTI;
}
}
printf("skiplist type: %s\n",
skiplist_type == SKIPLIST_TYPE_FLAT ? "flat" : "multi");
numbers = (int *)malloc(sizeof(int) * COUNT); numbers = (int *)malloc(sizeof(int) * COUNT);
srand(time(NULL)); srand(time(NULL));
for (i=0; i<COUNT; i++) { for (i=0; i<COUNT; i++) {
@ -242,7 +253,7 @@ int main(int argc, char *argv[])
fast_mblock_manager_init(); fast_mblock_manager_init();
result = skiplist_init_ex(&sl, LEVEL_COUNT, compare_func, result = skiplist_init_ex(&sl, LEVEL_COUNT, compare_func,
free_test_func, MIN_ALLOC_ONCE); free_test_func, MIN_ALLOC_ONCE, skiplist_type);
if (result != 0) { if (result != 0) {
return result; return result;
} }