Add const

This commit is contained in:
c9s 2014-05-27 01:07:33 +08:00
parent 533eb6b9c3
commit 54397987bd
5 changed files with 99 additions and 106 deletions

View file

@ -24,7 +24,7 @@ typedef struct _str_array {
str_array * str_array_create(int cap); str_array * str_array_create(int cap);
bool str_array_is_full(str_array * l); bool str_array_is_full(const str_array * l);
bool str_array_resize(str_array *l, int new_cap); bool str_array_resize(str_array *l, int new_cap);
@ -32,7 +32,7 @@ bool str_array_append(str_array * list, char * token);
void str_array_free(str_array *l); void str_array_free(str_array *l);
void str_array_dump(str_array *l); void str_array_dump(const str_array *l);
str_array * split_route_pattern(char *pattern, int pattern_len); str_array * split_route_pattern(char *pattern, int pattern_len);
@ -136,7 +136,7 @@ edge * r3_node_connectl(node * n, char * pat, int len, int strdup, node *child);
#define r3_node_connect(n, pat, child) r3_node_connectl(n, pat, strlen(pat), 0, child) #define r3_node_connect(n, pat, child) r3_node_connectl(n, pat, strlen(pat), 0, child)
edge * r3_node_find_edge(node * n, char * pat); edge * r3_node_find_edge(const node * n, char * pat);
void r3_node_append_edge(node *n, edge *child); void r3_node_append_edge(node *n, edge *child);
@ -151,7 +151,7 @@ node * r3_tree_insert_pathl(node *tree, char *path, int path_len, void * data);
*/ */
node * r3_tree_insert_pathl_(node *tree, char *path, int path_len, route * route, void * data); node * r3_tree_insert_pathl_(node *tree, char *path, int path_len, route * route, void * data);
void r3_tree_dump(node * n, int level); void r3_tree_dump(const node * n, int level);
int r3_tree_render_file(node * tree, char * format, char * filename); int r3_tree_render_file(node * tree, char * format, char * filename);
@ -171,9 +171,9 @@ node * r3_tree_matchl(const node * n, char * path, int path_len, match_entry * e
// node * r3_tree_match_entry(node * n, match_entry * entry); // node * r3_tree_match_entry(node * n, match_entry * entry);
#define r3_tree_match_entry(n, entry) r3_tree_matchl(n, entry->path, entry->path_len, entry) #define r3_tree_match_entry(n, entry) r3_tree_matchl(n, entry->path, entry->path_len, entry)
bool r3_node_has_slug_edges(node *n); bool r3_node_has_slug_edges(const node *n);
edge * r3_edge_create(char * pattern, int pattern_len, node * child); edge * r3_edge_create(const char * pattern, int pattern_len, node * child);
node * r3_edge_branch(edge *e, int dl); node * r3_edge_branch(edge *e, int dl);
@ -188,9 +188,9 @@ match_entry * match_entry_createl(char * path, int path_len);
void match_entry_free(match_entry * entry); void match_entry_free(match_entry * entry);
route * r3_route_create(char * path); route * r3_route_create(const char * path);
route * r3_route_createl(char * path, int path_len); route * r3_route_createl(const char * path, int path_len);
int r3_route_cmp(route *r1, match_entry *r2); int r3_route_cmp(route *r1, match_entry *r2);

View file

@ -23,7 +23,7 @@
#include "r3_str.h" #include "r3_str.h"
#include "zmalloc.h" #include "zmalloc.h"
edge * r3_edge_create(char * pattern, int pattern_len, node * child) { edge * r3_edge_create(const char * pattern, int pattern_len, node * child) {
edge * e = (edge*) zmalloc( sizeof(edge) ); edge * e = (edge*) zmalloc( sizeof(edge) );
e->pattern = pattern; e->pattern = pattern;
e->pattern_len = pattern_len; e->pattern_len = pattern_len;

View file

@ -1,7 +1,6 @@
/* /*
* list.c * list.c Copyright (C) 2014 c9s <c9s@c9smba.local>
* Copyright (C) 2014 c9s <c9s@c9smba.local> *
*
* Distributed under terms of the MIT license. * Distributed under terms of the MIT license.
*/ */
#include <stdlib.h> #include <stdlib.h>
@ -10,121 +9,114 @@
/* Naive linked list implementation */ /* Naive linked list implementation */
list * list *
list_create() list_create()
{ {
list *l = (list *) zmalloc(sizeof(list)); list *l = (list *) zmalloc(sizeof(list));
l->count = 0; l->count = 0;
l->head = NULL; l->head = NULL;
l->tail = NULL; l->tail = NULL;
pthread_mutex_init(&(l->mutex), NULL); pthread_mutex_init(&(l->mutex), NULL);
return l; return l;
} }
void void
list_free(l) list_free(l)
list *l; list *l;
{ {
if (l) { if (l) {
list_item *li, *tmp; list_item *li, *tmp;
pthread_mutex_lock(&(l->mutex));
if (l != NULL) {
li = l->head;
while (li != NULL) {
tmp = li->next;
li = tmp;
}
}
pthread_mutex_unlock(&(l->mutex));
pthread_mutex_destroy(&(l->mutex));
zfree(l);
}
}
list_item * list_add_element(list * l, void * ptr)
{
list_item *li;
pthread_mutex_lock(&(l->mutex)); pthread_mutex_lock(&(l->mutex));
if (l != NULL) { li = (list_item *) zmalloc(sizeof(list_item));
li = l->head; li->value = ptr;
while (li != NULL) { li->next = NULL;
tmp = li->next; li->prev = l->tail;
li = tmp;
} if (l->tail == NULL) {
l->head = l->tail = li;
} else {
l->tail = li;
} }
l->count++;
pthread_mutex_unlock(&(l->mutex)); pthread_mutex_unlock(&(l->mutex));
pthread_mutex_destroy(&(l->mutex));
zfree(l);
}
}
list_item * return li;
list_add_element(l, ptr)
list *l;
void *ptr;
{
list_item *li;
pthread_mutex_lock(&(l->mutex));
li = (list_item *) zmalloc(sizeof(list_item));
li->value = ptr;
li->next = NULL;
li->prev = l->tail;
if (l->tail == NULL) {
l->head = l->tail = li;
}
else {
l->tail = li;
}
l->count++;
pthread_mutex_unlock(&(l->mutex));
return li;
} }
int int
list_remove_element(l, ptr) list_remove_element(l, ptr)
list *l; list *l;
void *ptr; void *ptr;
{ {
int result = 0; int result = 0;
list_item *li = l->head; list_item *li = l->head;
pthread_mutex_lock(&(l->mutex)); pthread_mutex_lock(&(l->mutex));
while (li != NULL) { while (li != NULL) {
if (li->value == ptr) { if (li->value == ptr) {
if (li->prev == NULL) { if (li->prev == NULL) {
l->head = li->next; l->head = li->next;
} } else {
else { li->prev->next = li->next;
li->prev->next = li->next; }
}
if (li->next == NULL) { if (li->next == NULL) {
l->tail = li->prev; l->tail = li->prev;
} } else {
else { li->next->prev = li->prev;
li->next->prev = li->prev; }
} l->count--;
l->count--; zfree(li);
zfree(li); result = 1;
result = 1; break;
break; }
li = li->next;
} }
li = li->next;
}
pthread_mutex_unlock(&(l->mutex)); pthread_mutex_unlock(&(l->mutex));
return result; return result;
} }
void void
list_each_element(l, func) list_each_element(l, func)
list *l; list *l;
int (*func)(list_item *); int (*func) (list_item *);
{ {
list_item *li; list_item *li;
pthread_mutex_lock(&(l->mutex)); pthread_mutex_lock(&(l->mutex));
li = l->head; li = l->head;
while (li != NULL) { while (li != NULL) {
if (func(li) == 1) { if (func(li) == 1) {
break; break;
}
li = li->next;
} }
li = li->next;
}
pthread_mutex_unlock(&(l->mutex)); pthread_mutex_unlock(&(l->mutex));
} }

View file

@ -109,7 +109,7 @@ void r3_node_append_edge(node *n, edge *e) {
n->edges[ n->edge_len++ ] = e; n->edges[ n->edge_len++ ] = e;
} }
edge * r3_node_find_edge(node * n, char * pat) { edge * r3_node_find_edge(const node * n, char * pat) {
edge * e; edge * e;
for (int i = 0 ; i < n->edge_len ; i++ ) { for (int i = 0 ; i < n->edge_len ; i++ ) {
e = n->edges[i]; e = n->edges[i];
@ -420,7 +420,7 @@ node * r3_node_create() {
} }
route * r3_route_create(char * path) { route * r3_route_create(const char * path) {
return r3_route_createl(path, strlen(path)); return r3_route_createl(path, strlen(path));
} }
@ -428,9 +428,9 @@ void r3_route_free(route * route) {
zfree(route); zfree(route);
} }
route * r3_route_createl(char * path, int path_len) { route * r3_route_createl(const char * path, int path_len) {
route * info = zmalloc(sizeof(route)); route * info = zmalloc(sizeof(route));
info->path = path; info->path = (char*) path;
info->path_len = path_len; info->path_len = path_len;
info->request_method = 0; // can be (GET || POST) info->request_method = 0; // can be (GET || POST)
@ -602,7 +602,7 @@ node * r3_tree_insert_pathl_(node *tree, char *path, int path_len, route * route
return n; return n;
} }
bool r3_node_has_slug_edges(node *n) { bool r3_node_has_slug_edges(const node *n) {
bool found = FALSE; bool found = FALSE;
edge *e; edge *e;
for ( int i = 0 ; i < n->edge_len ; i++ ) { for ( int i = 0 ; i < n->edge_len ; i++ ) {
@ -616,7 +616,7 @@ bool r3_node_has_slug_edges(node *n) {
void r3_tree_dump(node * n, int level) { void r3_tree_dump(const node * n, int level) {
print_indent(level); print_indent(level);
printf("(o)"); printf("(o)");

View file

@ -22,17 +22,18 @@ str_array * str_array_create(int cap) {
void str_array_free(str_array *l) { void str_array_free(str_array *l) {
for ( int i = 0; i < l->len ; i++ ) { for ( int i = 0; i < l->len ; i++ ) {
char * t = l->tokens[ i ]; if (l->tokens[ i ]) {
zfree(t); zfree(l->tokens[i]);
}
} }
zfree(l); zfree(l);
} }
bool str_array_is_full(str_array * l) { bool str_array_is_full(const str_array * l) {
return l->len >= l->cap; return l->len >= l->cap;
} }
bool str_array_resize(str_array *l, int new_cap) { bool str_array_resize(str_array * l, int new_cap) {
l->tokens = zrealloc(l->tokens, sizeof(char**) * new_cap); l->tokens = zrealloc(l->tokens, sizeof(char**) * new_cap);
l->cap = new_cap; l->cap = new_cap;
return l->tokens != NULL; return l->tokens != NULL;
@ -49,7 +50,7 @@ bool str_array_append(str_array * l, char * token) {
return TRUE; return TRUE;
} }
void str_array_dump(str_array *l) { void str_array_dump(const str_array *l) {
printf("["); printf("[");
for ( int i = 0; i < l->len ; i++ ) { for ( int i = 0; i < l->len ; i++ ) {
printf("\"%s\"", l->tokens[i] ); printf("\"%s\"", l->tokens[i] );