Remove zmalloc dependency

Zmalloc comes from Redis. It's purpose is to track total allocations so
that a maximum can be set. For more information see the following links:

https://groups.google.com/forum/#!topic/redis-db/dPRdpowqJsY
https://stackoverflow.com/questions/22729730/when-would-one-use-malloc-over-zmalloc

This allocator is slower, not needed, and conflicts with Zlib (when
linking statically). This patch removes Zmalloc in favor of the system
allocator. Application developers can still choose to override the
allocator in their applications, e.g. Jemalloc, TCMalloc, etc.

Fixes #104
This commit is contained in:
Michael Steinert 2018-07-01 09:41:26 -05:00
parent afd2c486c6
commit b3dbf75da5
22 changed files with 37 additions and 520 deletions

14
3rdparty/Makefile.am vendored
View file

@ -1,14 +0,0 @@
AM_CFLAGS=$(DEPS_CFLAGS) $(GVC_DEPS_CFLAGS) -I$(top_builddir) -I$(top_builddir)/include -I$(top_builddir)/3rdparty -Wall -std=c99
AM_LDFLAGS=$(DEPS_LIBS) $(GVC_DEPS_LIBS)
noinst_LTLIBRARIES = libr3ext.la
libr3ext_la_SOURCES = zmalloc.c
libr3ext_la_LIBADD=$(DEPS_LIBS)
# noinst_LIBRARIES = libr3ext.la
libr3ext_la_CFLAGS=$(DEPS_CFLAGS) -I$(top_builddir) -I$(top_builddir)/3rdparty -Wall -std=c99
noinst_HEADERS = \
zmalloc.h \
$(NULL)

368
3rdparty/zmalloc.c vendored
View file

@ -1,368 +0,0 @@
/* zmalloc - total amount of allocated memory aware version of malloc()
*
* Copyright (c) 2009-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 <stdio.h>
#include <stdlib.h>
/* This function provide us access to the original libc free(). This is useful
* for instance to free results obtained by backtrace_symbols(). We need
* to define this function before including zmalloc.h that may shadow the
* free implementation if we use jemalloc or another non standard allocator. */
void zlibc_free(void *ptr) {
free(ptr);
}
#include <string.h>
#include <pthread.h>
#include "config.h"
#include "zmalloc.h"
#ifdef HAVE_MALLOC_SIZE
#define PREFIX_SIZE (0)
#else
#if defined(__sun) || defined(__sparc) || defined(__sparc__)
#define PREFIX_SIZE (sizeof(long long))
#else
#define PREFIX_SIZE (sizeof(size_t))
#endif
#endif
/* Explicitly override malloc/free etc when using tcmalloc. */
#if defined(USE_TCMALLOC)
#define malloc(size) tc_malloc(size)
#define calloc(count,size) tc_calloc(count,size)
#define realloc(ptr,size) tc_realloc(ptr,size)
#define free(ptr) tc_free(ptr)
#elif defined(USE_JEMALLOC) && (JEMALLOC_VERSION_MAJOR > 2)
#include <jemalloc/jemalloc.h>
#define malloc(size) je_malloc(size)
#define calloc(count,size) je_calloc(count,size)
#define realloc(ptr,size) je_realloc(ptr,size)
#define free(ptr) je_free(ptr)
#endif
#ifdef HAVE_ATOMIC
#define update_zmalloc_stat_add(__n) __sync_add_and_fetch(&used_memory, (__n))
#define update_zmalloc_stat_sub(__n) __sync_sub_and_fetch(&used_memory, (__n))
#else
#define update_zmalloc_stat_add(__n) do { \
pthread_mutex_lock(&used_memory_mutex); \
used_memory += (__n); \
pthread_mutex_unlock(&used_memory_mutex); \
} while(0)
#define update_zmalloc_stat_sub(__n) do { \
pthread_mutex_lock(&used_memory_mutex); \
used_memory -= (__n); \
pthread_mutex_unlock(&used_memory_mutex); \
} while(0)
#endif
#define update_zmalloc_stat_alloc(__n) do { \
size_t _n = (__n); \
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
if (zmalloc_thread_safe) { \
update_zmalloc_stat_add(_n); \
} else { \
used_memory += _n; \
} \
} while(0)
#define update_zmalloc_stat_free(__n) do { \
size_t _n = (__n); \
if (_n&(sizeof(long)-1)) _n += sizeof(long)-(_n&(sizeof(long)-1)); \
if (zmalloc_thread_safe) { \
update_zmalloc_stat_sub(_n); \
} else { \
used_memory -= _n; \
} \
} while(0)
static size_t used_memory = 0;
static int zmalloc_thread_safe = 0;
pthread_mutex_t used_memory_mutex = PTHREAD_MUTEX_INITIALIZER;
static void zmalloc_default_oom(size_t size) {
fprintf(stderr, "zmalloc: Out of memory trying to allocate %zu bytes\n",
size);
fflush(stderr);
abort();
}
static void (*zmalloc_oom_handler)(size_t) = zmalloc_default_oom;
void *zmalloc(size_t size) {
void *ptr = malloc(size+PREFIX_SIZE);
if (!ptr) zmalloc_oom_handler(size);
#ifdef HAVE_MALLOC_SIZE
update_zmalloc_stat_alloc(zmalloc_size(ptr));
return ptr;
#else
*((size_t*)ptr) = size;
update_zmalloc_stat_alloc(size+PREFIX_SIZE);
return (char*)ptr+PREFIX_SIZE;
#endif
}
void *zcalloc(size_t size) {
void *ptr = calloc(1, size+PREFIX_SIZE);
if (!ptr) zmalloc_oom_handler(size);
#ifdef HAVE_MALLOC_SIZE
update_zmalloc_stat_alloc(zmalloc_size(ptr));
return ptr;
#else
*((size_t*)ptr) = size;
update_zmalloc_stat_alloc(size+PREFIX_SIZE);
return (char*)ptr+PREFIX_SIZE;
#endif
}
void *zrealloc(void *ptr, size_t size) {
#ifndef HAVE_MALLOC_SIZE
void *realptr;
#endif
size_t oldsize;
void *newptr;
if (ptr == NULL) return zmalloc(size);
#ifdef HAVE_MALLOC_SIZE
oldsize = zmalloc_size(ptr);
newptr = realloc(ptr,size);
if (!newptr) zmalloc_oom_handler(size);
update_zmalloc_stat_free(oldsize);
update_zmalloc_stat_alloc(zmalloc_size(newptr));
return newptr;
#else
realptr = (char*)ptr-PREFIX_SIZE;
oldsize = *((size_t*)realptr);
newptr = realloc(realptr,size+PREFIX_SIZE);
if (!newptr) zmalloc_oom_handler(size);
*((size_t*)newptr) = size;
update_zmalloc_stat_free(oldsize);
update_zmalloc_stat_alloc(size);
return (char*)newptr+PREFIX_SIZE;
#endif
}
/* Provide zmalloc_size() for systems where this function is not provided by
* malloc itself, given that in that case we store a header with this
* information as the first bytes of every allocation. */
#ifndef HAVE_MALLOC_SIZE
size_t zmalloc_size(void *ptr) {
void *realptr = (char*)ptr-PREFIX_SIZE;
size_t size = *((size_t*)realptr);
/* Assume at least that all the allocations are padded at sizeof(long) by
* the underlying allocator. */
if (size&(sizeof(long)-1)) size += sizeof(long)-(size&(sizeof(long)-1));
return size+PREFIX_SIZE;
}
#endif
void zfree(void *ptr) {
#ifndef HAVE_MALLOC_SIZE
void *realptr;
size_t oldsize;
#endif
if (ptr == NULL) return;
#ifdef HAVE_MALLOC_SIZE
update_zmalloc_stat_free(zmalloc_size(ptr));
free(ptr);
#else
realptr = (char*)ptr-PREFIX_SIZE;
oldsize = *((size_t*)realptr);
update_zmalloc_stat_free(oldsize+PREFIX_SIZE);
free(realptr);
#endif
}
char *zstrdup(const char *s) {
size_t l = strlen(s)+1;
char *p = zmalloc(l);
memcpy(p,s,l);
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 um;
if (zmalloc_thread_safe) {
#ifdef HAVE_ATOMIC
um = __sync_add_and_fetch(&used_memory, 0);
#else
pthread_mutex_lock(&used_memory_mutex);
um = used_memory;
pthread_mutex_unlock(&used_memory_mutex);
#endif
}
else {
um = used_memory;
}
return um;
}
void zmalloc_enable_thread_safeness(void) {
zmalloc_thread_safe = 1;
}
void zmalloc_set_oom_handler(void (*oom_handler)(size_t)) {
zmalloc_oom_handler = oom_handler;
}
/* Get the RSS information in an OS-specific way.
*
* WARNING: the function zmalloc_get_rss() is not designed to be fast
* and may not be called in the busy loops where Redis tries to release
* memory expiring or swapping out objects.
*
* For this kind of "fast RSS reporting" usages use instead the
* function RedisEstimateRSS() that is a much faster (and less precise)
* version of the function. */
#if defined(HAVE_PROC_STAT)
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
size_t zmalloc_get_rss(void) {
int page = sysconf(_SC_PAGESIZE);
size_t rss;
char buf[4096];
char filename[256];
int fd, count;
char *p, *x;
snprintf(filename,256,"/proc/%d/stat",getpid());
if ((fd = open(filename,O_RDONLY)) == -1) return 0;
if (read(fd,buf,4096) <= 0) {
close(fd);
return 0;
}
close(fd);
p = buf;
count = 23; /* RSS is the 24th field in /proc/<pid>/stat */
while(p && count--) {
p = strchr(p,' ');
if (p) p++;
}
if (!p) return 0;
x = strchr(p,' ');
if (!x) return 0;
*x = '\0';
rss = strtoll(p,NULL,10);
rss *= page;
return rss;
}
#elif defined(HAVE_TASKINFO)
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <mach/task.h>
#include <mach/mach_init.h>
size_t zmalloc_get_rss(void) {
task_t task = MACH_PORT_NULL;
struct task_basic_info t_info;
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
if (task_for_pid(current_task(), getpid(), &task) != KERN_SUCCESS)
return 0;
task_info(task, TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count);
return t_info.resident_size;
}
#else
size_t zmalloc_get_rss(void) {
/* If we can't get the RSS in an OS-specific way for this system just
* return the memory usage we estimated in zmalloc()..
*
* Fragmentation will appear to be always 1 (no fragmentation)
* of course... */
return zmalloc_used_memory();
}
#endif
/* Fragmentation = RSS / allocated-bytes */
float zmalloc_get_fragmentation_ratio(size_t rss) {
return (float)rss/zmalloc_used_memory();
}
#if defined(HAVE_PROC_SMAPS)
size_t zmalloc_get_private_dirty(void) {
char line[1024];
size_t pd = 0;
FILE *fp = fopen("/proc/self/smaps","r");
if (!fp) return 0;
while(fgets(line,sizeof(line),fp) != NULL) {
if (strncmp(line,"Private_Dirty:",14) == 0) {
char *p = strchr(line,'k');
if (p) {
*p = '\0';
pd += strtol(line+14,NULL,10) * 1024;
}
}
}
fclose(fp);
return pd;
}
#else
size_t zmalloc_get_private_dirty(void) {
return 0;
}
#endif

86
3rdparty/zmalloc.h vendored
View file

@ -1,86 +0,0 @@
#ifndef ZMALLOC_H
#define ZMALLOC_H
/* zmalloc - total amount of allocated memory aware version of malloc()
*
* Copyright (c) 2009-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.
*/
/* Double expansion needed for stringification of macro values. */
#define __xstr(s) __str(s)
#define __str(s) #s
#if defined(USE_TCMALLOC)
#define ZMALLOC_LIB ("tcmalloc-" __xstr(TC_VERSION_MAJOR) "." __xstr(TC_VERSION_MINOR))
#include <google/tcmalloc.h>
#if (TC_VERSION_MAJOR == 1 && TC_VERSION_MINOR >= 6) || (TC_VERSION_MAJOR > 1)
#define HAVE_MALLOC_SIZE 1
#define zmalloc_size(p) tc_malloc_size(p)
#else
#error "Newer version of tcmalloc required"
#endif
#elif defined(USE_JEMALLOC) && (JEMALLOC_VERSION_MAJOR > 2)
#define ZMALLOC_LIB ("jemalloc-" __xstr(JEMALLOC_VERSION_MAJOR) "." __xstr(JEMALLOC_VERSION_MINOR) "." __xstr(JEMALLOC_VERSION_BUGFIX))
#include <jemalloc/jemalloc.h>
#if (JEMALLOC_VERSION_MAJOR == 2 && JEMALLOC_VERSION_MINOR >= 1) || (JEMALLOC_VERSION_MAJOR > 2)
#define HAVE_MALLOC_SIZE 1
#define zmalloc_size(p) je_malloc_usable_size(p)
#else
#error "Newer version of jemalloc required"
#endif
#elif defined(__APPLE__)
#include <malloc/malloc.h>
#define HAVE_MALLOC_SIZE 1
#define zmalloc_size(p) malloc_size(p)
#endif
#ifndef ZMALLOC_LIB
#define ZMALLOC_LIB "libc"
#endif
void *zmalloc(size_t size);
void *zcalloc(size_t size);
void *zrealloc(void *ptr, size_t size);
void zfree(void *ptr);
char *zstrdup(const char *s);
char *zstrndup(const char *s, size_t n);
size_t zmalloc_used_memory(void);
void zmalloc_enable_thread_safeness(void);
void zmalloc_set_oom_handler(void (*oom_handler)(size_t));
float zmalloc_get_fragmentation_ratio(size_t rss);
size_t zmalloc_get_rss(void);
size_t zmalloc_get_private_dirty(void);
void zlibc_free(void *ptr);
#ifndef HAVE_MALLOC_SIZE
size_t zmalloc_size(void *ptr);
#endif
#endif // ZMALLOC_H

View file

@ -1,4 +1,4 @@
SUBDIRS=3rdparty src . examples
SUBDIRS=src . examples
if ENABLE_CHECK
SUBDIRS += tests
@ -6,10 +6,10 @@ endif
lib_LTLIBRARIES = libr3.la
libr3_la_SOURCES =
libr3_la_LIBADD = 3rdparty/libr3ext.la src/libr3core.la
libr3_la_LIBADD = src/libr3core.la
libr3_la_LDFLAGS =
AM_CFLAGS=$(DEPS_CFLAGS) $(GVC_DEPS_CFLAGS) -I$(top_builddir) -I$(top_builddir)/include -I$(top_builddir)/3rdparty -Wall -std=c99
AM_CFLAGS=$(DEPS_CFLAGS) $(GVC_DEPS_CFLAGS) -I$(top_builddir) -I$(top_builddir)/include -Wall -std=c99
AM_LDFLAGS=$(DEPS_LIBS) $(GVC_DEPS_LIBS)

View file

@ -131,7 +131,6 @@ AM_CONDITIONAL(ENABLE_CHECK, test "x$enable_check" = "xyes")
AC_CONFIG_FILES([
r3.pc
Makefile
3rdparty/Makefile
src/Makefile
tests/Makefile
examples/Makefile

View file

@ -5,5 +5,5 @@
"description": "high-performance path dispatching library",
"keywords": ["path", "dispatch", "performance", "r3", "c9s"],
"license": "MIT",
"src": ["3rdparty/zmalloc.c", "3rdparty/zmalloc.h", "include/memory.h", "include/r3.h", "include/r3.hpp", "include/r3_gvc.h", "include/r3_json.h", "include/r3_list.h", "include/r3_slug.h", "include/str_array.h", "src/edge.c", "src/gvc.c", "src/json.c", "src/list.c", "src/match_entry.c", "src/node.c", "src/slug.c", "src/slug.h", "src/str.c", "src/token.c"]
"src": ["include/memory.h", "include/r3.h", "include/r3.hpp", "include/r3_gvc.h", "include/r3_json.h", "include/r3_list.h", "include/r3_slug.h", "include/str_array.h", "src/edge.c", "src/gvc.c", "src/json.c", "src/list.c", "src/match_entry.c", "src/node.c", "src/slug.c", "src/slug.h", "src/str.c", "src/token.c"]
}

View file

@ -1,5 +1,4 @@
add_library(r3 STATIC
${PROJECT_SOURCE_DIR}/3rdparty/zmalloc.c
edge.c
match_entry.c
memory.c

View file

@ -1,4 +1,4 @@
AM_CFLAGS=$(DEPS_CFLAGS) $(GVC_DEPS_CFLAGS) $(JSONC_CFLAGS) -I$(top_builddir) -I$(top_builddir)/include -I$(top_builddir)/3rdparty -Wall -std=c99
AM_CFLAGS=$(DEPS_CFLAGS) $(GVC_DEPS_CFLAGS) $(JSONC_CFLAGS) -I$(top_builddir) -I$(top_builddir)/include -Wall -std=c99
AM_LDFLAGS=$(DEPS_LIBS) $(GVC_DEPS_LIBS) $(JSONC_LIBS)
MAYBE_COVERAGE=--coverage
@ -23,7 +23,7 @@ MOSTLYCLEANFILES = *.gcov *.gcda *.gcno
# libr3_la_LIBADD=$(DEPS_LIBS) $(LIBOBJS) $(ALLOCA)
# libr3core_la_LIBADD=$(DEPS_LIBS)
# libr3core_la_CFLAGS=$(DEPS_CFLAGS) -I$(top_builddir) -I$(top_builddir)/include -I$(top_builddir)/3rdparty -Wall -std=c99
# libr3core_la_CFLAGS=$(DEPS_CFLAGS) -I$(top_builddir) -I$(top_builddir)/include -Wall -std=c99
if ENABLE_GRAPHVIZ
libr3core_la_SOURCES += gvc.c

View file

@ -18,7 +18,6 @@
#include "r3.h"
#include "r3_slug.h"
#include "slug.h"
#include "zmalloc.h"
#define CHECK_PTR(ptr) if (ptr == NULL) return NULL;
@ -36,7 +35,7 @@ void r3_edge_initl(R3Edge *e, const char * pattern, int pattern_len, R3Node * ch
// R3Edge * r3_edge_createl(const char * pattern, int pattern_len, R3Node * child)
// {
// R3Edge * e = (R3Edge*) zmalloc( sizeof(R3Edge) );
// R3Edge * e = (R3Edge*) malloc( sizeof(R3Edge) );
// CHECK_PTR(e);
// e->pattern = (char*) pattern;
// e->pattern_len = pattern_len;
@ -85,7 +84,7 @@ void r3_edge_free(R3Edge * e) {
r3_tree_free(e->child);
}
// free itself
// zfree(e);
// free(e);
}
}

View file

@ -10,7 +10,6 @@
#include <stdlib.h>
#include "r3.h"
#include "r3_gvc.h"
#include "zmalloc.h"
void r3_tree_build_ag_nodes(Agraph_t * g, Agnode_t * ag_parent_node, const node * n, int * node_cnt) {
if (!n)

View file

@ -5,14 +5,13 @@
*/
#include <stdlib.h>
#include "r3_list.h"
#include "zmalloc.h"
/* Naive linked list implementation */
list *
list_create()
{
list *l = (list *) zmalloc(sizeof(list));
list *l = (list *) malloc(sizeof(list));
l->count = 0;
l->head = NULL;
l->tail = NULL;
@ -38,7 +37,7 @@ list_free(l)
}
pthread_mutex_unlock(&(l->mutex));
pthread_mutex_destroy(&(l->mutex));
zfree(l);
free(l);
}
}
@ -48,7 +47,7 @@ list_item * list_add_element(list * l, void * ptr)
pthread_mutex_lock(&(l->mutex));
li = (list_item *) zmalloc(sizeof(list_item));
li = (list_item *) malloc(sizeof(list_item));
li->value = ptr;
li->next = NULL;
li->prev = l->tail;
@ -89,7 +88,7 @@ list_remove_element(l, ptr)
li->next->prev = li->prev;
}
l->count--;
zfree(li);
free(li);
result = 1;
break;
}

View file

@ -13,7 +13,6 @@
#include <stdbool.h>
#include "r3.h"
#include "zmalloc.h"
match_entry * match_entry_createl(const char * path, int path_len) {
match_entry * entry = r3_mem_alloc( sizeof(match_entry) );

View file

@ -13,7 +13,6 @@
#include "slug.h"
#include "str.h"
#include "r3_debug.h"
#include "zmalloc.h"
#ifdef __GNUC__
# define likely(x) __builtin_expect(!!(x), 1)
@ -81,7 +80,7 @@ void r3_tree_free(R3Node * tree) {
pcre_free_study(tree->pcre_extra);
}
#endif
zfree(tree->combined_pattern);
free(tree->combined_pattern);
free(tree);
tree = NULL;
}
@ -99,7 +98,7 @@ R3Edge * r3_node_connectl(R3Node * n, const char * pat, int len, int dupl, R3Nod
return e;
}
if (dupl) {
pat = zstrndup(pat, len);
pat = strndup(pat, len);
}
// e = r3_edge_createl(pat, len, child);
e = r3_node_append_edge(n);
@ -170,7 +169,7 @@ int r3_tree_compile(R3Node *n, char **errstr)
int r3_tree_compile_patterns(R3Node * n, char **errstr) {
R3Edge *e;
char * p;
char * cpat = zcalloc(sizeof(char) * 64 * 3); // XXX
char * cpat = calloc(1, sizeof(char) * 64 * 3); // XXX
if (!cpat) {
asprintf(errstr, "Can not allocate memory");
return -1;
@ -190,7 +189,7 @@ int r3_tree_compile_patterns(R3Node * n, char **errstr) {
char * slug_pat = r3_slug_compile(e->pattern.base, e->pattern.len);
info("slug_pat for pattern: %s\n",slug_pat);
strcat(p, slug_pat);
zfree(slug_pat);
free(slug_pat);
info("temp pattern: %s\n",cpat);
} else {
strncat(p,"^(", 2);
@ -211,14 +210,14 @@ int r3_tree_compile_patterns(R3Node * n, char **errstr) {
// if all edges use opcode, we should skip the combined_pattern.
if ( opcode_cnt == n->edges.size ) {
// zfree(cpat);
// free(cpat);
n->compare_type = NODE_COMPARE_OPCODE;
} else {
n->compare_type = NODE_COMPARE_PCRE;
}
info("COMPARE_TYPE: %d\n",n->compare_type);
zfree(n->combined_pattern);
free(n->combined_pattern);
n->combined_pattern = cpat;
const char *pcre_error;
@ -473,7 +472,7 @@ inline R3Edge * r3_node_find_edge_str(const R3Node * n, const char * str, int st
}
// R3Node * r3_node_create() {
// R3Node * n = (R3Node*) zmalloc( sizeof(R3Node) );
// R3Node * n = (R3Node*) malloc( sizeof(R3Node) );
// CHECK_PTR(n);
// n->edges = NULL;
// n->edge_len = 0;
@ -501,7 +500,7 @@ void r3_route_free(R3Route * route) {
// }
// static bool router_slugs_resize(R3Route * route, int new_cap) {
// route->slugs = zrealloc(route->slugs, sizeof(char**) * new_cap);
// route->slugs = realloc(route->slugs, sizeof(char**) * new_cap);
// route->slugs_cap = new_cap;
// return route->slugs != NULL;
// }
@ -718,7 +717,7 @@ R3Node * r3_tree_insert_pathl_ex(R3Node *tree, const char *path, unsigned int pa
info("cpattern: %s\n", cpattern);
opcode = r3_pattern_to_opcode(cpattern, strlen(cpattern));
info("opcode: %d\n", opcode);
zfree(cpattern);
free(cpattern);
} else {
opcode = OP_EXPECT_NOSLASH;
}
@ -915,11 +914,11 @@ inline int r3_route_cmp(const R3Route *r1, const match_entry *r2) {
// if (n->routes == NULL) {
// n->route_cap = 3;
// n->routes = zmalloc(sizeof(R3Route) * n->route_cap);
// n->routes = malloc(sizeof(R3Route) * n->route_cap);
// }
// if (n->route_len >= n->route_cap) {
// n->route_cap *= 2;
// n->routes = zrealloc(n->routes, sizeof(R3Route) * n->route_cap);
// n->routes = realloc(n->routes, sizeof(R3Route) * n->route_cap);
// }
// n->routes[ n->route_len++ ] = r;
// }

View file

@ -12,12 +12,11 @@
#include "r3_slug.h"
#include "slug.h"
#include "r3_debug.h"
#include "zmalloc.h"
r3_slug_t * r3_slug_new(const char * path, int path_len) {
r3_slug_t * s = zmalloc(sizeof(r3_slug_t));
r3_slug_t * s = malloc(sizeof(r3_slug_t));
if (!s)
return NULL;
s->path = (char*) path;
@ -33,7 +32,7 @@ r3_slug_t * r3_slug_new(const char * path, int path_len) {
}
void r3_slug_free(r3_slug_t * s) {
zfree(s);
free(s);
}

View file

@ -13,7 +13,6 @@
#include "r3_slug.h"
#include "str.h"
#include "slug.h"
#include "zmalloc.h"
static const char * strnchr(const char* str, unsigned int len, int ch) {
for (unsigned int i = 0; i < len; i++) {
@ -187,11 +186,11 @@ char * r3_slug_compile(const char * str, unsigned int len)
s1 = r3_slug_find_placeholder(str, len, &s1_len);
if ( !s1 ) {
return zstrndup(str,len);
return strndup(str,len);
}
char * out = NULL;
if (!(out = zcalloc(sizeof(char) * 200))) {
if (!(out = calloc(1, sizeof(char) * 200))) {
return (NULL);
}
@ -227,7 +226,7 @@ char * ltrim_slash(char* str)
{
char * p = str;
while (*p == '/') p++;
return zstrdup(p);
return strdup(p);
}
void print_indent(int level) {
@ -240,13 +239,13 @@ void print_indent(int level) {
#ifndef HAVE_STRDUP
char *zstrdup(const char *s) {
char *strdup(const char *s) {
char *out;
int count = 0;
while( s[count] )
++count;
++count;
out = zmalloc(sizeof(char) * count);
out = malloc(sizeof(char) * count);
out[--count] = 0;
while( --count >= 0 )
out[count] = s[count];
@ -257,13 +256,13 @@ char *zstrdup(const char *s) {
#ifndef HAVE_STRNDUP
char *zstrndup(const char *s, int n) {
char *strndup(const char *s, int n) {
char *out;
int count = 0;
while( count < n && s[count] )
++count;
++count;
out = zmalloc(sizeof(char) * count);
out = malloc(sizeof(char) * count);
out[--count] = 0;
while( --count >= 0 )
out[count] = s[count];

View file

@ -12,7 +12,6 @@
#include "r3.h"
#include "r3_slug.h"
#include "str_array.h"
#include "zmalloc.h"
#include "memory.h"
void str_array_free(str_array *l) {

View file

@ -1,6 +1,6 @@
TESTS =
AM_CFLAGS=$(DEPS_CFLAGS) $(GVC_DEPS_CFLAGS) $(JSONC_CFLAGS) @CHECK_CFLAGS@ -I$(top_builddir) -I$(top_builddir)/include -I$(top_builddir)/src -I$(top_builddir)/3rdparty -Wall -std=c99 -ggdb `pkg-config --cflags --libs check`
AM_CFLAGS=$(DEPS_CFLAGS) $(GVC_DEPS_CFLAGS) $(JSONC_CFLAGS) @CHECK_CFLAGS@ -I$(top_builddir) -I$(top_builddir)/include -I$(top_builddir)/src -Wall -std=c99 -ggdb `pkg-config --cflags --libs check`
AM_LDFLAGS=$(DEPS_LIBS) $(GVC_DEPS_LIBS) $(JSONC_LIBS) @CHECK_LIBS@ $(top_builddir)/libr3.la
if USE_JEMALLOC

View file

@ -13,7 +13,6 @@
#include "r3.h"
#include "r3_slug.h"
#include "zmalloc.h"
#include "bench.h"

View file

@ -8,7 +8,6 @@
#include <assert.h>
#include "r3.h"
#include "r3_slug.h"
#include "zmalloc.h"
START_TEST (test_routes)
{

View file

@ -10,7 +10,6 @@
#include <stdlib.h>
#include "r3.h"
#include "r3_slug.h"
#include "zmalloc.h"
#include "slug.h"
#include "r3_debug.h"
@ -28,19 +27,19 @@ START_TEST (test_r3_slug_compile)
char * path = "/user/{id}";
char * c = NULL;
ck_assert_str_eq( c = r3_slug_compile(path, strlen(path) ) , "^/user/([^/]+)" );
zfree(c);
free(c);
char * path2 = "/what/{id}-foo";
ck_assert_str_eq( c = r3_slug_compile(path2, strlen(path2) ) , "^/what/([^/]+)-foo" );
zfree(c);
free(c);
char * path3 = "-{id}";
ck_assert_str_eq( c = r3_slug_compile(path3, strlen(path3)), "^-([^/]+)" );
zfree(c);
free(c);
char * path4 = "-{idx:\\d{3}}";
ck_assert_str_eq( c = r3_slug_compile(path4, strlen(path4)), "^-(\\d{3})" );
zfree(c);
free(c);
}
END_TEST

View file

@ -10,7 +10,6 @@
#include <check.h>
#include "r3.h"
#include "r3_slug.h"
#include "zmalloc.h"
START_TEST (test_str_array)
{

View file

@ -5,7 +5,6 @@
#include <assert.h>
#include "r3.h"
#include "r3_slug.h"
#include "zmalloc.h"
#include "bench.h"
#include "../src/r3_debug.h"