Skip to content

Commit

Permalink
first runnable version
Browse files Browse the repository at this point in the history
  • Loading branch information
buf1024 committed May 29, 2014
1 parent 1017c4c commit a3d1ce0
Show file tree
Hide file tree
Showing 24 changed files with 2,685 additions and 620 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
CC = cc
CCFLAGS = -Wall -g3
CCFLAGS = -Wall -g

MYSCH=mysch

MYSCH_MY_OBJ=mysch.o

MYSCH_OBJ=dict.o iniconf.o log.o myqueue.o util.o \
MYSCH_OBJ=dict.o dlist.o iniconf.o log.o util.o \
$(MYSCH_MY_OBJ)

EXT_CCFLAGS =
Expand Down
12 changes: 1 addition & 11 deletions dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ static unsigned long _dict_next_power(unsigned long size)
return i;
i *= 2;
}
return 0;
}

/* Returns the index of a free slot that can be populated with
Expand Down Expand Up @@ -752,14 +753,3 @@ void dict_disable_resize(void)
{
dict_can_resize = 0;
}

void *alloc_key(dict *d, int size)
{
return malloc(size);
}

void *alloc_val(dict *d, int size)
{
return malloc(size);
}

3 changes: 0 additions & 3 deletions dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ typedef struct dict_iterator {
#define dict_is_rehashing(ht) ((ht)->rehashidx != -1)

/* API */
void *alloc_key(dict *d, int size);
void *alloc_val(dict *d, int size);

dict *dict_create(void *priv_data_ptr);
int dict_expand(dict *d, unsigned long size);
int dict_add(dict *d, void *key, int keylen, void *val, int vallen);
Expand Down
345 changes: 345 additions & 0 deletions dlist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,345 @@
/* adlist.c - A generic doubly linked list implementation
*
* Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/


#include <stdlib.h>
#include <string.h>
#include "dlist.h"

/* Create a new list. The created list can be freed with
* AlFreeList(), but private value of every node need to be freed
* by the user before to call AlFreeList().
*
* On error, NULL is returned. Otherwise the pointer to the new list. */
list *list_create(void)
{
struct list *list;

if ((list = malloc(sizeof(*list))) == NULL)
return NULL;
list->head = list->tail = NULL;
list->len = 0;
/* list->dup = NULL;
list->free = NULL;
list->match = NULL;*/
return list;
}

/* Free the whole list.
*
* This function can't fail. */
void list_release(list *list, int nofree)
{
unsigned long len;
list_node *current, *next;

current = list->head;
len = list->len;
while(len--) {
next = current->next;
if (AL_FREE == nofree)
free(current->value);
free(current);
current = next;
}
free(list);
}

/* Add a new node to the list, to head, contaning the specified 'value'
* pointer as value.
*
* On error, NULL is returned and no operation is performed (i.e. the
* list remains unaltered).
* On success the 'list' pointer you pass to the function is returned. */
list *list_add_node_head(list *list, void *value)
{
list_node *node;

if ((node = malloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
if (list->len == 0) {
list->head = list->tail = node;
node->prev = node->next = NULL;
} else {
node->prev = NULL;
node->next = list->head;
list->head->prev = node;
list->head = node;
}
list->len++;
return list;
}

/* Add a new node to the list, to tail, containing the specified 'value'
* pointer as value.
*
* On error, NULL is returned and no operation is performed (i.e. the
* list remains unaltered).
* On success the 'list' pointer you pass to the function is returned. */
list *list_add_node_tail(list *list, void *value)
{
list_node *node;

if ((node = malloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
if (list->len == 0) {
list->head = list->tail = node;
node->prev = node->next = NULL;
} else {
node->prev = list->tail;
node->next = NULL;
list->tail->next = node;
list->tail = node;
}
list->len++;
return list;
}

list *list_insert_node(list *list, list_node *old_node, void *value, int after) {
list_node *node;

if ((node = malloc(sizeof(*node))) == NULL)
return NULL;
node->value = value;
if (after) {
node->prev = old_node;
node->next = old_node->next;
if (list->tail == old_node) {
list->tail = node;
}
} else {
node->next = old_node;
node->prev = old_node->prev;
if (list->head == old_node) {
list->head = node;
}
}
if (node->prev != NULL) {
node->prev->next = node;
}
if (node->next != NULL) {
node->next->prev = node;
}
list->len++;
return list;
}

/* Remove the specified node from the specified list.
* It's up to the caller to free the private value of the node.
*
* This function can't fail. */
void list_del_node(list *list, list_node *node, int nofree)
{
if (node->prev)
node->prev->next = node->next;
else
list->head = node->next;
if (node->next)
node->next->prev = node->prev;
else
list->tail = node->prev;
if(AL_FREE == nofree)
free(node->value);
free(node);
list->len--;
}

/* Returns a list iterator 'iter'. After the initialization every
* call to list_next() will return the next element of the list.
*
* This function can't fail. */
list_iter *list_get_iterator(list *list, int direction)
{
list_iter *iter;

if ((iter = malloc(sizeof(*iter))) == NULL) return NULL;
if (direction == AL_START_HEAD)
iter->next = list->head;
else
iter->next = list->tail;
iter->direction = direction;
return iter;
}

/* Release the iterator memory */
void list_release_iterator(list_iter *iter) {
free(iter);
}

/* Create an iterator in the list private iterator structure */
void list_rewind(list *list, list_iter *li) {
li->next = list->head;
li->direction = AL_START_HEAD;
}

void list_rewind_tail(list *list, list_iter *li) {
li->next = list->tail;
li->direction = AL_START_TAIL;
}

/* Return the next element of an iterator.
* It's valid to remove the currently returned element using
* list_del_node(), but not to remove other elements.
*
* The function returns a pointer to the next element of the list,
* or NULL if there are no more elements, so the classical usage patter
* is:
*
* iter = list_get_iterator(list,<direction>);
* while ((node = list_next(iter)) != NULL) {
* doSomethingWith(list_node_value(node));
* }
*
* */
list_node *list_next(list_iter *iter)
{
list_node *current = iter->next;

if (current != NULL) {
if (iter->direction == AL_START_HEAD)
iter->next = current->next;
else
iter->next = current->prev;
}
return current;
}

/* Duplicate the whole list. On out of memory NULL is returned.
* On success a copy of the original list is returned.
*
* The 'Dup' method set with listSetDupMethod() function is used
* to copy the node value. Otherwise the same pointer value of
* the original node is used as value of the copied node.
*
* The original list both on success or error is never modified. */
/*
list *list_dup(list *orig)
{
list *copy;
list_iter *iter;
list_node *node;
if ((copy = list_create()) == NULL)
return NULL;
copy->dup = orig->dup;
copy->free = orig->free;
copy->match = orig->match;
iter = list_get_iterator(orig, AL_START_HEAD);
while((node = list_next(iter)) != NULL) {
void *value;
if (copy->dup) {
value = copy->dup(node->value);
if (value == NULL) {
list_release(copy);
list_release_iterator(iter);
return NULL;
}
} else
value = node->value;
if (list_add_node_tail(copy, value) == NULL) {
list_release(copy);
list_release_iterator(iter);
return NULL;
}
}
list_release_iterator(iter);
return copy;
}
*/

/* Search the list for a node matching a given key.
* The match is performed using the 'match' method
* set with listSetMatchMethod(). If no 'match' method
* is set, the 'value' pointer of every node is directly
* compared with the 'key' pointer.
*
* On success the first matching node pointer is returned
* (search starts from head). If no matching node exists
* NULL is returned. */
list_node *list_search_key(list *list, void *key, int len)
{
list_iter *iter;
list_node *node;

iter = list_get_iterator(list, AL_START_HEAD);
while((node = list_next(iter)) != NULL) {
if (len > 0) {
if (memcmp(node->value, key, len) == 0) {
list_release_iterator(iter);
return node;
}
} else {
if (key == node->value) {
list_release_iterator(iter);
return node;
}
}
}
list_release_iterator(iter);
return NULL;
}

/* Return the element at the specified zero-based index
* where 0 is the head, 1 is the element next to head
* and so on. Negative integers are used in order to count
* from the tail, -1 is the last element, -2 the penultimate
* and so on. If the index is out of range NULL is returned. */
list_node *list_index(list *list, long index) {
list_node *n;

if (index < 0) {
index = (-index)-1;
n = list->tail;
while(index-- && n) n = n->prev;
} else {
n = list->head;
while(index-- && n) n = n->next;
}
return n;
}

/* Rotate the list removing the tail node and inserting it to the head. */
void list_rotate(list *list) {
list_node *tail = list->tail;

if (list_length(list) <= 1) return;

/* Detach current tail */
list->tail = tail->prev;
list->tail->next = NULL;
/* Move it as head */
list->head->prev = tail;
tail->prev = NULL;
tail->next = list->head;
list->head = tail;
}
Loading

0 comments on commit a3d1ce0

Please sign in to comment.