CHECK_PTR macro to check pointer

This commit is contained in:
c9s 2014-06-01 21:58:29 +08:00
parent 70693ea1f2
commit da2f2311c9

View file

@ -12,6 +12,9 @@
#include "r3_str.h" #include "r3_str.h"
#include "zmalloc.h" #include "zmalloc.h"
#define CHECK_PTR(ptr) if (ptr == NULL) return NULL;
// String value as the index http://judy.sourceforge.net/doc/JudySL_3x.htm // String value as the index http://judy.sourceforge.net/doc/JudySL_3x.htm
@ -39,8 +42,7 @@ static int strdiff(char * d1, char * d2) {
*/ */
node * r3_tree_create(int cap) { node * r3_tree_create(int cap) {
node * n = (node*) zmalloc( sizeof(node) ); node * n = (node*) zmalloc( sizeof(node) );
if (!n) CHECK_PTR(n);
return NULL;
n->edges = (edge**) zmalloc( sizeof(edge*) * cap ); n->edges = (edge**) zmalloc( sizeof(edge*) * cap );
n->edge_len = 0; n->edge_len = 0;
@ -92,8 +94,7 @@ edge * r3_node_connectl(node * n, const char * pat, int len, int dupl, node *chi
pat = zstrndup(pat, len); pat = zstrndup(pat, len);
} }
e = r3_edge_create(pat, len, child); e = r3_edge_create(pat, len, child);
if (!e) CHECK_PTR(e);
return NULL;
r3_node_append_edge(n, e); r3_node_append_edge(n, e);
return e; return e;
} }
@ -407,9 +408,7 @@ inline edge * r3_node_find_edge_str(const node * n, const char * str, int str_le
node * r3_node_create() { node * r3_node_create() {
node * n = (node*) zmalloc( sizeof(node) ); node * n = (node*) zmalloc( sizeof(node) );
if (!n) { CHECK_PTR(n);
return NULL;
}
n->edges = NULL; n->edges = NULL;
n->edge_len = 0; n->edge_len = 0;
n->edge_cap = 0; n->edge_cap = 0;
@ -435,8 +434,7 @@ void r3_route_free(route * route) {
route * r3_route_createl(const 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));
if (!info) CHECK_PTR(info);
return NULL;
info->path = (char*) 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)
@ -454,8 +452,7 @@ route * r3_route_createl(const char * path, int path_len) {
route * r3_tree_insert_routel(node *tree, int method, const char *path, int path_len, void *data) { route * r3_tree_insert_routel(node *tree, int method, const char *path, int path_len, void *data) {
route *r = r3_route_createl(path, path_len); route *r = r3_route_createl(path, path_len);
if(!r) CHECK_PTR(r);
return NULL;
r->request_method = method; // ALLOW GET OR POST METHOD r->request_method = method; // ALLOW GET OR POST METHOD
r3_tree_insert_pathl_(tree, path, path_len, r, data); r3_tree_insert_pathl_(tree, path, path_len, r, data);
return r; return r;
@ -524,8 +521,8 @@ node * r3_tree_insert_pathl_(node *tree, const char *path, int path_len, route *
// insert the first one edge, and break at "p" // insert the first one edge, and break at "p"
node * child = r3_tree_create(3); node * child = r3_tree_create(3);
if (!child) CHECK_PTR(child);
return NULL;
r3_node_connect(n, zstrndup(path, (int)(p - path)), child); r3_node_connect(n, zstrndup(path, (int)(p - path)), child);
// and insert the rest part to the child // and insert the rest part to the child
@ -553,16 +550,16 @@ node * r3_tree_insert_pathl_(node *tree, const char *path, int path_len, route *
node *c1; node *c1;
if (slug_p > path) { if (slug_p > path) {
c1 = r3_tree_create(3); c1 = r3_tree_create(3);
if (!c1) CHECK_PTR(c1);
return NULL;
r3_node_connectl(n, path, slug_p - path, 1, c1); // duplicate r3_node_connectl(n, path, slug_p - path, 1, c1); // duplicate
} else { } else {
c1 = n; c1 = n;
} }
node * c2 = r3_tree_create(3); node * c2 = r3_tree_create(3);
if (!c2) CHECK_PTR(c2);
return NULL;
edge * op_edge = r3_node_connectl(c1, slug_p, slug_len , 1, c2); edge * op_edge = r3_node_connectl(c1, slug_p, slug_len , 1, c2);
op_edge->opcode = opcode; op_edge->opcode = opcode;
@ -583,8 +580,8 @@ node * r3_tree_insert_pathl_(node *tree, const char *path, int path_len, route *
} }
// only one slug // only one slug
node * child = r3_tree_create(3); node * child = r3_tree_create(3);
if (!child) CHECK_PTR(child);
return NULL;
r3_node_connect(n, zstrndup(path, path_len) , child); r3_node_connect(n, zstrndup(path, path_len) , child);
child->data = data; child->data = data;
child->endpoint++; child->endpoint++;