add skiplist
parent
eec032be76
commit
74fe4c8889
3
HISTORY
3
HISTORY
|
|
@ -1,6 +1,7 @@
|
|||
|
||||
Version 1.24 2015-11-30
|
||||
Version 1.24 2015-12-24
|
||||
* php extension compiled on PHP 7
|
||||
* add skiplist
|
||||
|
||||
Version 1.23 2015-11-16
|
||||
* sched_thread.c: task can execute in a new thread
|
||||
|
|
|
|||
|
|
@ -11,7 +11,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 \
|
||||
fast_timer.lo process_ctrl.lo fast_mblock.lo \
|
||||
connection_pool.lo fast_mpool.lo fast_allocator.lo \
|
||||
fast_buffer.lo
|
||||
fast_buffer.lo skiplist.lo
|
||||
|
||||
FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \
|
||||
logger.o sockopt.o base64.o sched_thread.o \
|
||||
|
|
@ -19,7 +19,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 \
|
||||
fast_timer.o process_ctrl.o fast_mblock.o \
|
||||
connection_pool.o fast_mpool.o fast_allocator.o \
|
||||
fast_buffer.o
|
||||
fast_buffer.o skiplist.o
|
||||
|
||||
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 \
|
||||
|
|
@ -27,7 +27,7 @@ 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 \
|
||||
fast_timer.h process_ctrl.h fast_mblock.h \
|
||||
connection_pool.h fast_mpool.h fast_allocator.h \
|
||||
fast_buffer.h
|
||||
fast_buffer.h skiplist.h
|
||||
|
||||
ALL_OBJS = $(FAST_STATIC_OBJS) $(FAST_SHARED_OBJS)
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,214 @@
|
|||
/**
|
||||
* 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.c
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include "logger.h"
|
||||
#include "skiplist.h"
|
||||
|
||||
int skiplist_init_ex(Skiplist *sl, const int level_count,
|
||||
skiplist_compare_func compare_func, const int alloc_elements_once)
|
||||
{
|
||||
int bytes;
|
||||
int element_size;
|
||||
int i;
|
||||
int result;
|
||||
struct fast_mblock_man *top_mblock;
|
||||
|
||||
if (level_count <= 0) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"invalid level count: %d",
|
||||
__LINE__, level_count);
|
||||
return EINVAL;
|
||||
}
|
||||
|
||||
if (level_count > 32) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"level count: %d is too large",
|
||||
__LINE__, level_count);
|
||||
return E2BIG;
|
||||
}
|
||||
|
||||
bytes = sizeof(struct fast_mblock_man) * level_count;
|
||||
sl->mblocks = (struct fast_mblock_man *)malloc(bytes);
|
||||
if (sl->mblocks == NULL) {
|
||||
logError("file: "__FILE__", line: %d, "
|
||||
"malloc %d bytes fail, errno: %d, error info: %s",
|
||||
__LINE__, bytes, errno, STRERROR(errno));
|
||||
return errno != 0 ? errno : ENOMEM;
|
||||
}
|
||||
memset(sl->mblocks, 0, bytes);
|
||||
|
||||
for (i=0; i<level_count; i++) {
|
||||
element_size = sizeof(SkiplistNode) + sizeof(SkiplistNode *) * (i + 1);
|
||||
if ((result=fast_mblock_init_ex(sl->mblocks + i,
|
||||
element_size, alloc_elements_once, NULL, false)) != 0)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
sl->top_level_index = level_count - 1;
|
||||
top_mblock = sl->mblocks + sl->top_level_index;
|
||||
sl->top = (SkiplistNode *)fast_mblock_alloc_object(top_mblock);
|
||||
if (sl->top == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
memset(sl->top, 0, top_mblock->info.element_size);
|
||||
|
||||
sl->level_count = level_count;
|
||||
sl->compare_func = compare_func;
|
||||
|
||||
srand(time(NULL));
|
||||
return 0;
|
||||
}
|
||||
|
||||
void skiplist_destroy(Skiplist *sl)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (sl->mblocks == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (i=0; i<sl->level_count; i++) {
|
||||
fast_mblock_destroy(sl->mblocks + i);
|
||||
}
|
||||
|
||||
free(sl->mblocks);
|
||||
sl->mblocks = NULL;
|
||||
}
|
||||
|
||||
static inline int skiplist_get_previous_level_index(Skiplist *sl)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i=0; i<sl->level_count; i++) {
|
||||
if (rand() < RAND_MAX / 2) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
int skiplist_insert(Skiplist *sl, void *data)
|
||||
{
|
||||
int i;
|
||||
int level_index;
|
||||
SkiplistNode *node;
|
||||
SkiplistNode *previous;
|
||||
SkiplistNode *current;
|
||||
|
||||
level_index = skiplist_get_previous_level_index(sl);
|
||||
node = (SkiplistNode *)fast_mblock_alloc_object(sl->mblocks + level_index);
|
||||
if (node == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
previous = sl->top;
|
||||
for (i=sl->top_level_index; i>level_index; i--) {
|
||||
while (previous->links[i] != NULL && sl->compare_func(data,
|
||||
previous->links[i]->data) > 0)
|
||||
{
|
||||
previous = previous->links[i];
|
||||
}
|
||||
}
|
||||
|
||||
while (i >= 0) {
|
||||
while (previous->links[i] != NULL && sl->compare_func(data,
|
||||
previous->links[i]->data) > 0)
|
||||
{
|
||||
previous = previous->links[i];
|
||||
}
|
||||
|
||||
current = previous->links[i];
|
||||
previous->links[i] = node;
|
||||
node->links[i] = current;
|
||||
|
||||
i--;
|
||||
}
|
||||
|
||||
node->data = data;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static SkiplistNode *skiplist_get_previous(Skiplist *sl, void *data,
|
||||
int *level_index)
|
||||
{
|
||||
int i;
|
||||
int cmp;
|
||||
SkiplistNode *previous;
|
||||
SkiplistNode *found;
|
||||
|
||||
found = NULL;
|
||||
previous = sl->top;
|
||||
for (i=sl->top_level_index; i>=0; i--) {
|
||||
while (previous->links[i] != NULL) {
|
||||
cmp = sl->compare_func(data, previous->links[i]->data);
|
||||
if (cmp < 0) {
|
||||
break;
|
||||
}
|
||||
else if (cmp == 0) {
|
||||
found = previous;
|
||||
*level_index = i;
|
||||
goto DONE;
|
||||
}
|
||||
|
||||
previous = previous->links[i];
|
||||
}
|
||||
}
|
||||
|
||||
DONE:
|
||||
return found;
|
||||
}
|
||||
|
||||
int skiplist_delete(Skiplist *sl, void *data)
|
||||
{
|
||||
int i;
|
||||
int level_index;
|
||||
SkiplistNode *previous;
|
||||
SkiplistNode *deleted;
|
||||
|
||||
previous = skiplist_get_previous(sl, data, &level_index);
|
||||
if (previous == NULL) {
|
||||
return ENOENT;
|
||||
}
|
||||
|
||||
deleted = previous->links[level_index];
|
||||
for (i=level_index; i>=0; i--) {
|
||||
while (previous->links[i] != NULL && sl->compare_func(data,
|
||||
previous->links[i]->data) > 0)
|
||||
{
|
||||
previous = previous->links[i];
|
||||
}
|
||||
|
||||
assert(sl->compare_func(data, previous->links[i]->data) == 0);
|
||||
previous->links[i] = previous->links[i]->links[i];
|
||||
}
|
||||
|
||||
fast_mblock_free_object(sl->mblocks + level_index, deleted);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *skiplist_find(Skiplist *sl, void *data)
|
||||
{
|
||||
int level_index;
|
||||
SkiplistNode *previous;
|
||||
|
||||
previous = skiplist_get_previous(sl, data, &level_index);
|
||||
return (previous != NULL) ? previous->links[level_index]->data : NULL;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
/**
|
||||
* 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.h
|
||||
#ifndef _SKIPLIST_H
|
||||
#define _SKIPLIST_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "common_define.h"
|
||||
#include "fast_mblock.h"
|
||||
|
||||
typedef int (*skiplist_compare_func)(const void *p1, const void *p2);
|
||||
|
||||
typedef struct skiplist_node
|
||||
{
|
||||
void *data;
|
||||
struct skiplist_node *links[0];
|
||||
} SkiplistNode;
|
||||
|
||||
typedef struct skiplist
|
||||
{
|
||||
int level_count;
|
||||
int top_level_index;
|
||||
skiplist_compare_func compare_func;
|
||||
struct fast_mblock_man *mblocks; //node allocators
|
||||
SkiplistNode *top; //the top node
|
||||
} Skiplist;
|
||||
|
||||
typedef struct skiplist_iterator {
|
||||
SkiplistNode *current;
|
||||
} SkiplistIterator;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define skiplist_init(sl, level_count, compare_func) \
|
||||
skiplist_init_ex(sl, level_count, compare_func, 1024)
|
||||
|
||||
int skiplist_init_ex(Skiplist *sl, const int level_count,
|
||||
skiplist_compare_func compare_func, const int alloc_elements_once);
|
||||
|
||||
void skiplist_destroy(Skiplist *sl);
|
||||
|
||||
int skiplist_insert(Skiplist *sl, void *data);
|
||||
int skiplist_delete(Skiplist *sl, void *data);
|
||||
void *skiplist_find(Skiplist *sl, void *data);
|
||||
|
||||
static inline void skiplist_iterator(Skiplist *sl, SkiplistIterator *iterator)
|
||||
{
|
||||
iterator->current = sl->top->links[0];
|
||||
}
|
||||
|
||||
static inline void *skiplist_next(SkiplistIterator *iterator)
|
||||
{
|
||||
void *data;
|
||||
|
||||
if (iterator->current == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
data = iterator->current->data;
|
||||
iterator->current = iterator->current->links[0];
|
||||
return data;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
.SUFFIXES: .c .o
|
||||
|
||||
COMPILE = $(CC) -Wall -D_FILE_OFFSET_BITS=64 -g -DDEBUG_FLAG
|
||||
INC_PATH = -I/usr/include/fastcommon
|
||||
LIB_PATH = -lfastcommon
|
||||
|
||||
ALL_PRGS = test_allocator test_skiplist
|
||||
|
||||
all: $(ALL_PRGS)
|
||||
.c:
|
||||
$(COMPILE) -o $@ $< $(LIB_PATH) $(INC_PATH)
|
||||
.c.o:
|
||||
$(COMPILE) -c -o $@ $< $(INC_PATH)
|
||||
clean:
|
||||
rm -f $(ALL_PRGS)
|
||||
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <inttypes.h>
|
||||
#include <sys/time.h>
|
||||
#include "logger.h"
|
||||
#include "shared_func.h"
|
||||
#include "sched_thread.h"
|
||||
#include "ini_file_reader.h"
|
||||
#include "fast_allocator.h"
|
||||
|
||||
#define OUTER_LOOP_COUNT 128
|
||||
#define INNER_LOOP_COUNT 1024 * 64
|
||||
|
||||
#define USE_ALLOCATOR 1
|
||||
|
||||
#if USE_ALLOCATOR == 1
|
||||
#define MALLOC(bytes) fast_allocator_alloc(&acontext, bytes)
|
||||
#define FREE(ptr) fast_allocator_free(&acontext, ptr)
|
||||
#else
|
||||
#define MALLOC(bytes) malloc(bytes)
|
||||
#define FREE(ptr) free(ptr)
|
||||
#endif
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int result;
|
||||
struct fast_allocator_context acontext;
|
||||
void *ptrs[INNER_LOOP_COUNT];
|
||||
int bytes;
|
||||
int i;
|
||||
int k;
|
||||
int64_t start_time;
|
||||
|
||||
printf("use allocator: %d\n", USE_ALLOCATOR);
|
||||
start_time = get_current_time_ms();
|
||||
|
||||
log_init();
|
||||
srand(time(NULL));
|
||||
g_log_context.log_level = LOG_DEBUG;
|
||||
|
||||
fast_mblock_manager_init();
|
||||
if ((result=fast_allocator_init(&acontext, 0, 0.00, 0, true)) != 0)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
fast_mblock_manager_stat_print(true);
|
||||
for (k=0; k<OUTER_LOOP_COUNT; k++) {
|
||||
for (i=0; i<INNER_LOOP_COUNT; i++) {
|
||||
bytes = 65536L * rand() / RAND_MAX;
|
||||
|
||||
ptrs[i] = MALLOC(bytes);
|
||||
}
|
||||
|
||||
printf("after alloc, bytes: %"PRId64"\n", acontext.alloc_bytes);
|
||||
//fast_mblock_manager_stat_print(true);
|
||||
|
||||
for (i=0; i<INNER_LOOP_COUNT; i++) {
|
||||
FREE(ptrs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
fast_mblock_manager_stat_print(true);
|
||||
printf("after free, bytes: %"PRId64"\n", acontext.alloc_bytes);
|
||||
printf("time used: %"PRId64" ms\n", get_current_time_ms() - start_time);
|
||||
|
||||
fast_allocator_destroy(&acontext);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <assert.h>
|
||||
#include <inttypes.h>
|
||||
#include <sys/time.h>
|
||||
#include "skiplist.h"
|
||||
|
||||
static int compare_func(const void *p1, const void *p2)
|
||||
{
|
||||
return *((int *)p1) - *((int *)p2);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
#define COUNT 1280
|
||||
#define LAST_INDEX (COUNT - 1)
|
||||
|
||||
Skiplist sl;
|
||||
SkiplistIterator iterator;
|
||||
int *numbers;
|
||||
int i;
|
||||
int tmp;
|
||||
int index;
|
||||
int result;
|
||||
void *value;
|
||||
|
||||
numbers = (int *)malloc(sizeof(int) * COUNT);
|
||||
srand(time(NULL));
|
||||
for (i=0; i<COUNT; i++) {
|
||||
numbers[i] = i + 1;
|
||||
}
|
||||
|
||||
for (i=0; i<COUNT; i++) {
|
||||
index = LAST_INDEX * (int64_t)rand() / (int64_t)RAND_MAX;
|
||||
tmp = numbers[index];
|
||||
numbers[index] = numbers[LAST_INDEX - index];
|
||||
numbers[LAST_INDEX - index] = tmp;
|
||||
}
|
||||
|
||||
result = skiplist_init_ex(&sl, 12, compare_func, 128);
|
||||
if (result != 0) {
|
||||
return result;
|
||||
}
|
||||
|
||||
for (i=0; i<COUNT; i++) {
|
||||
if ((result=skiplist_insert(&sl, numbers + i)) != 0) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
for (i=1; i<=COUNT; i++) {
|
||||
value = skiplist_find(&sl, &i);
|
||||
assert(value != NULL && *((int *)value) == i);
|
||||
}
|
||||
|
||||
i = 0;
|
||||
skiplist_iterator(&sl, &iterator);
|
||||
while ((value=skiplist_next(&iterator)) != NULL) {
|
||||
i++;
|
||||
assert(i == *((int *)value));
|
||||
}
|
||||
assert(i==COUNT);
|
||||
skiplist_destroy(&sl);
|
||||
|
||||
printf("pass OK\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue