Skip to content

Commit

Permalink
Clnag-format the entire codebase
Browse files Browse the repository at this point in the history
Signed-off-by: Ping Xie <pingxie@google.com>
  • Loading branch information
PingXie committed May 7, 2024
1 parent aa23f09 commit 7d728c7
Show file tree
Hide file tree
Showing 159 changed files with 40,548 additions and 32,694 deletions.
1,547 changes: 849 additions & 698 deletions src/acl.c

Large diffs are not rendered by default.

121 changes: 70 additions & 51 deletions src/adlist.c
Expand Up @@ -39,12 +39,12 @@
* listSetFreeMethod.
*
* On error, NULL is returned. Otherwise the pointer to the new list. */
list *listCreate(void)
{
list *listCreate(void) {
struct list *list;

if ((list = zmalloc(sizeof(*list))) == NULL)
if ((list = zmalloc(sizeof(*list))) == NULL) {
return NULL;
}
list->head = list->tail = NULL;
list->len = 0;
list->dup = NULL;
Expand All @@ -54,16 +54,17 @@ list *listCreate(void)
}

/* Remove all the elements from the list without destroying the list itself. */
void listEmpty(list *list)
{
void listEmpty(list *list) {
unsigned long len;
listNode *current, *next;

current = list->head;
len = list->len;
while(len--) {
while (len--) {
next = current->next;
if (list->free) list->free(current->value);
if (list->free) {
list->free(current->value);
}
zfree(current);
current = next;
}
Expand All @@ -74,10 +75,10 @@ void listEmpty(list *list)
/* Free the whole list.
*
* This function can't fail. */
void listRelease(list *list)
{
if (!list)
void listRelease(list *list) {
if (!list) {
return;
}
listEmpty(list);
zfree(list);
}
Expand All @@ -88,12 +89,12 @@ void listRelease(list *list)
* 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 *listAddNodeHead(list *list, void *value)
{
list *listAddNodeHead(list *list, void *value) {
listNode *node;

if ((node = zmalloc(sizeof(*node))) == NULL)
if ((node = zmalloc(sizeof(*node))) == NULL) {
return NULL;
}
node->value = value;
listLinkNodeHead(list, node);
return list;
Expand All @@ -102,7 +103,7 @@ list *listAddNodeHead(list *list, void *value)
/*
* Add a node that has already been allocated to the head of list
*/
void listLinkNodeHead(list* list, listNode *node) {
void listLinkNodeHead(list *list, listNode *node) {
if (list->len == 0) {
list->head = list->tail = node;
node->prev = node->next = NULL;
Expand All @@ -121,12 +122,12 @@ void listLinkNodeHead(list* list, listNode *node) {
* 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 *listAddNodeTail(list *list, void *value)
{
list *listAddNodeTail(list *list, void *value) {
listNode *node;

if ((node = zmalloc(sizeof(*node))) == NULL)
if ((node = zmalloc(sizeof(*node))) == NULL) {
return NULL;
}
node->value = value;
listLinkNodeTail(list, node);
return list;
Expand All @@ -151,8 +152,9 @@ void listLinkNodeTail(list *list, listNode *node) {
list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
listNode *node;

if ((node = zmalloc(sizeof(*node))) == NULL)
if ((node = zmalloc(sizeof(*node))) == NULL) {
return NULL;
}
node->value = value;
if (after) {
node->prev = old_node;
Expand Down Expand Up @@ -181,25 +183,28 @@ list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
* The node is freed. If free callback is provided the value is freed as well.
*
* This function can't fail. */
void listDelNode(list *list, listNode *node)
{
void listDelNode(list *list, listNode *node) {
listUnlinkNode(list, node);
if (list->free) list->free(node->value);
if (list->free) {
list->free(node->value);
}
zfree(node);
}

/*
* Remove the specified node from the list without freeing it.
*/
void listUnlinkNode(list *list, listNode *node) {
if (node->prev)
if (node->prev) {
node->prev->next = node->next;
else
} else {
list->head = node->next;
if (node->next)
}
if (node->next) {
node->next->prev = node->prev;
else
} else {
list->tail = node->prev;
}

node->next = NULL;
node->prev = NULL;
Expand All @@ -211,15 +216,17 @@ void listUnlinkNode(list *list, listNode *node) {
* call to listNext() will return the next element of the list.
*
* This function can't fail. */
listIter *listGetIterator(list *list, int direction)
{
listIter *listGetIterator(list *list, int direction) {
listIter *iter;

if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL;
if (direction == AL_START_HEAD)
if ((iter = zmalloc(sizeof(*iter))) == NULL) {
return NULL;
}
if (direction == AL_START_HEAD) {
iter->next = list->head;
else
} else {
iter->next = list->tail;
}
iter->direction = direction;
return iter;
}
Expand Down Expand Up @@ -254,15 +261,15 @@ void listRewindTail(list *list, listIter *li) {
* }
*
* */
listNode *listNext(listIter *iter)
{
listNode *listNext(listIter *iter) {
listNode *current = iter->next;

if (current != NULL) {
if (iter->direction == AL_START_HEAD)
if (iter->direction == AL_START_HEAD) {
iter->next = current->next;
else
} else {
iter->next = current->prev;
}
}
return current;
}
Expand All @@ -275,19 +282,19 @@ listNode *listNext(listIter *iter)
* the original node is used as value of the copied node.
*
* The original list both on success or error is never modified. */
list *listDup(list *orig)
{
list *listDup(list *orig) {
list *copy;
listIter iter;
listNode *node;

if ((copy = listCreate()) == NULL)
if ((copy = listCreate()) == NULL) {
return NULL;
}
copy->dup = orig->dup;
copy->free = orig->free;
copy->match = orig->match;
listRewind(orig, &iter);
while((node = listNext(&iter)) != NULL) {
while ((node = listNext(&iter)) != NULL) {
void *value;

if (copy->dup) {
Expand All @@ -299,10 +306,12 @@ list *listDup(list *orig)
} else {
value = node->value;
}

if (listAddNodeTail(copy, value) == NULL) {
/* Free value if dup succeed but listAddNodeTail failed. */
if (copy->free) copy->free(value);
if (copy->free) {
copy->free(value);
}

listRelease(copy);
return NULL;
Expand All @@ -320,13 +329,12 @@ list *listDup(list *orig)
* On success the first matching node pointer is returned
* (search starts from head). If no matching node exists
* NULL is returned. */
listNode *listSearchKey(list *list, void *key)
{
listNode *listSearchKey(list *list, void *key) {
listIter iter;
listNode *node;

listRewind(list, &iter);
while((node = listNext(&iter)) != NULL) {
while ((node = listNext(&iter)) != NULL) {
if (list->match) {
if (list->match(node->value, key)) {
return node;
Expand All @@ -349,19 +357,25 @@ listNode *listIndex(list *list, long index) {
listNode *n;

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

/* Rotate the list removing the tail node and inserting it to the head. */
void listRotateTailToHead(list *list) {
if (listLength(list) <= 1) return;
if (listLength(list) <= 1) {
return;
}

/* Detach current tail */
listNode *tail = list->tail;
Expand All @@ -376,7 +390,9 @@ void listRotateTailToHead(list *list) {

/* Rotate the list removing the head node and inserting it to the tail. */
void listRotateHeadToTail(list *list) {
if (listLength(list) <= 1) return;
if (listLength(list) <= 1) {
return;
}

listNode *head = list->head;
/* Detach current head */
Expand All @@ -392,14 +408,17 @@ void listRotateHeadToTail(list *list) {
/* Add all the elements of the list 'o' at the end of the
* list 'l'. The list 'other' remains empty but otherwise valid. */
void listJoin(list *l, list *o) {
if (o->len == 0) return;
if (o->len == 0) {
return;
}

o->head->prev = l->tail;

if (l->tail)
if (l->tail) {
l->tail->next = o->head;
else
} else {
l->head = o->head;
}

l->tail = o->tail;
l->len += o->len;
Expand Down
6 changes: 3 additions & 3 deletions src/adlist.h
Expand Up @@ -61,9 +61,9 @@ typedef struct list {
#define listNextNode(n) ((n)->next)
#define listNodeValue(n) ((n)->value)

#define listSetDupMethod(l,m) ((l)->dup = (m))
#define listSetFreeMethod(l,m) ((l)->free = (m))
#define listSetMatchMethod(l,m) ((l)->match = (m))
#define listSetDupMethod(l, m) ((l)->dup = (m))
#define listSetFreeMethod(l, m) ((l)->free = (m))
#define listSetMatchMethod(l, m) ((l)->match = (m))

#define listGetDupMethod(l) ((l)->dup)
#define listGetFreeMethod(l) ((l)->free)
Expand Down

0 comments on commit 7d728c7

Please sign in to comment.