Implemented zstrndup.
This commit is contained in:
parent
e34759c1aa
commit
3ea9354e0b
5 changed files with 22 additions and 5 deletions
|
@ -70,6 +70,7 @@ void *zcalloc(size_t size);
|
||||||
void *zrealloc(void *ptr, size_t size);
|
void *zrealloc(void *ptr, size_t size);
|
||||||
void zfree(void *ptr);
|
void zfree(void *ptr);
|
||||||
char *zstrdup(const char *s);
|
char *zstrdup(const char *s);
|
||||||
|
char *zstrndup(const char *s, size_t n);
|
||||||
size_t zmalloc_used_memory(void);
|
size_t zmalloc_used_memory(void);
|
||||||
void zmalloc_enable_thread_safeness(void);
|
void zmalloc_enable_thread_safeness(void);
|
||||||
void zmalloc_set_oom_handler(void (*oom_handler)(size_t));
|
void zmalloc_set_oom_handler(void (*oom_handler)(size_t));
|
||||||
|
|
|
@ -55,7 +55,7 @@ node * r3_edge_branch(edge *e, int dl) {
|
||||||
// the suffix edge of the leaf
|
// the suffix edge of the leaf
|
||||||
new_child = r3_tree_create(3);
|
new_child = r3_tree_create(3);
|
||||||
s1_len = e->pattern_len - dl;
|
s1_len = e->pattern_len - dl;
|
||||||
e1 = r3_edge_create(strndup(s1, s1_len), s1_len, new_child);
|
e1 = r3_edge_create(zstrndup(s1, s1_len), s1_len, new_child);
|
||||||
|
|
||||||
// Migrate the child edges to the new edge we just created.
|
// Migrate the child edges to the new edge we just created.
|
||||||
for ( int i = 0 ; i < tmp_edge_len ; i++ ) {
|
for ( int i = 0 ; i < tmp_edge_len ; i++ ) {
|
||||||
|
|
|
@ -306,7 +306,7 @@ node * r3_tree_matchl(const node * n, char * path, int path_len, match_entry * e
|
||||||
|
|
||||||
if (entry && e->has_slug) {
|
if (entry && e->has_slug) {
|
||||||
// append captured token to entry
|
// append captured token to entry
|
||||||
str_array_append(entry->vars , strndup(substring_start, substring_length));
|
str_array_append(entry->vars , zstrndup(substring_start, substring_length));
|
||||||
}
|
}
|
||||||
if (restlen == 0 ) {
|
if (restlen == 0 ) {
|
||||||
return e->child && e->child->endpoint > 0 ? e->child : NULL;
|
return e->child && e->child->endpoint > 0 ? e->child : NULL;
|
||||||
|
@ -459,14 +459,14 @@ node * r3_tree_insert_pathl_(node *tree, char *path, int path_len, route * 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);
|
||||||
r3_node_add_child(n, strndup(path, (int)(p - path)), child);
|
r3_node_add_child(n, zstrndup(path, (int)(p - path)), child);
|
||||||
child->endpoint = 0;
|
child->endpoint = 0;
|
||||||
|
|
||||||
// and insert the rest part to the child
|
// and insert the rest part to the child
|
||||||
return r3_tree_insert_pathl_(child, p, path_len - (int)(p - path), route, data);
|
return r3_tree_insert_pathl_(child, p, path_len - (int)(p - path), route, data);
|
||||||
} else {
|
} else {
|
||||||
node * child = r3_tree_create(3);
|
node * child = r3_tree_create(3);
|
||||||
r3_node_add_child(n, strndup(path, path_len) , child);
|
r3_node_add_child(n, zstrndup(path, path_len) , child);
|
||||||
// info("edge not found, insert one: %s\n", path);
|
// info("edge not found, insert one: %s\n", path);
|
||||||
child->data = data;
|
child->data = data;
|
||||||
child->endpoint++;
|
child->endpoint++;
|
||||||
|
|
|
@ -204,7 +204,7 @@ char *zstrdup(const char *s) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef HAVE_STRNDUP
|
#ifndef HAVE_STRNDUP
|
||||||
char *strndup(const char *s, int n) {
|
char *zstrndup(const char *s, int n) {
|
||||||
char *out;
|
char *out;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
while( count < n && s[count] )
|
while( count < n && s[count] )
|
||||||
|
|
|
@ -215,6 +215,22 @@ char *zstrdup(const char *s) {
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char * zstrndup (const char *s, size_t n)
|
||||||
|
{
|
||||||
|
char *result;
|
||||||
|
size_t len = strlen (s);
|
||||||
|
|
||||||
|
if (n < len)
|
||||||
|
len = n;
|
||||||
|
|
||||||
|
result = (char *) zmalloc (len + 1);
|
||||||
|
if (!result)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
result[len] = '\0';
|
||||||
|
return (char *) memcpy (result, s, len);
|
||||||
|
}
|
||||||
|
|
||||||
size_t zmalloc_used_memory(void) {
|
size_t zmalloc_used_memory(void) {
|
||||||
size_t um;
|
size_t um;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue