diff options
author | Daniel Friesel <derf@finalrewind.org> | 2018-12-11 10:35:51 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2018-12-11 10:35:51 +0100 |
commit | 437646b8bf5eed344b4132b96b459a1572f19ce9 (patch) | |
tree | 7981d7b0357ac262a8bc60bba14661098710fbed | |
parent | c1027b36455cf47d336d42e2e42a63f096f7e772 (diff) |
add optional malloc tracing
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | src/lib/capnp-c/capn-malloc.cc | 19 | ||||
-rw-r--r-- | src/lib/ubjson/ubjr.c | 45 | ||||
-rw-r--r-- | src/lib/ubjson/ubjw.c | 10 |
4 files changed, 52 insertions, 27 deletions
@@ -125,6 +125,11 @@ ifeq (${ostream}, 1) COMMON_FLAGS += -DWITH_OSTREAM endif +ifeq (${trace_malloc}, 1) + CXX_TARGETS += src/lib/mpmalloc.cc + COMMON_FLAGS += -DMULTIPASS_TRACE_MALLOC +endif + include src/arch/${arch}/Makefile.inc stack: default diff --git a/src/lib/capnp-c/capn-malloc.cc b/src/lib/capnp-c/capn-malloc.cc index 7308061..8a6cd1c 100644 --- a/src/lib/capnp-c/capn-malloc.cc +++ b/src/lib/capnp-c/capn-malloc.cc @@ -19,6 +19,13 @@ #include <limits.h> #include <errno.h> +#ifdef MULTIPASS_TRACE_MALLOC +#include "lib/mpmalloc.h" +#else +#define mpcalloc calloc +#define mpfree free +#endif + /* * 8 byte alignment is required for struct capn_segment. * This struct check_segment_alignment verifies this at compile time. @@ -36,12 +43,12 @@ struct check_segment_alignment { static struct capn_segment *create(void *u, uint32_t id, int sz) { struct capn_segment *s; sz += sizeof(*s); - if (sz < 4096) { + if (sz < 4096) { // TODO auskommentieren? sz = 4096; } else { sz = (sz + 4095) & ~4095; } - s = (struct capn_segment*) calloc(1, sz); + s = (struct capn_segment*) mpcalloc(1, sz); s->data = (char*) (s+1); s->cap = sz - sizeof(*s); s->user = s; @@ -62,7 +69,7 @@ void capn_free(struct capn *c) { struct capn_segment *s = c->seglist; while (s != NULL) { struct capn_segment *n = s->next; - free(s->user); + mpfree(s->user); s = n; } capn_reset_copy(c); @@ -72,7 +79,7 @@ void capn_reset_copy(struct capn *c) { struct capn_segment *s = c->copylist; while (s != NULL) { struct capn_segment *n = s->next; - free(s->user); + mpfree(s->user); s = n; } c->copy = NULL; @@ -156,7 +163,7 @@ static int init_fp(struct capn *c, FILE *f, struct capn_stream *z, int packed) { } /* Allocate space for the data and the capn_segment structs */ - s = (struct capn_segment*) calloc(1, total + (sizeof(*s) * segnum)); + s = (struct capn_segment*) mpcalloc(1, total + (sizeof(*s) * segnum)); if (!s) goto err; @@ -179,7 +186,7 @@ static int init_fp(struct capn *c, FILE *f, struct capn_stream *z, int packed) { err: memset(c, 0, sizeof(*c)); - free(s); + mpfree(s); return -1; } diff --git a/src/lib/ubjson/ubjr.c b/src/lib/ubjson/ubjr.c index afa357b..e45bd95 100644 --- a/src/lib/ubjson/ubjr.c +++ b/src/lib/ubjson/ubjr.c @@ -3,6 +3,13 @@ #include <stdlib.h>
#include <string.h>
+#ifdef MULTIPASS_TRACE_MALLOC
+#include "lib/mpmalloc.h"
+#else
+#define mpmalloc malloc
+#define mpfree free
+#endif
+
#if _MSC_VER
#define inline __inline
#endif
@@ -33,7 +40,7 @@ ubjr_context_t* ubjr_open_callback(void* userdata, void(*error_cb)(const char* error_msg)
)
{
- ubjr_context_t* ctx = (ubjr_context_t*)malloc(sizeof(ubjr_context_t));
+ ubjr_context_t* ctx = (ubjr_context_t*)mpmalloc(sizeof(ubjr_context_t));
ctx->userdata = userdata;
ctx->read_cb = read_cb;
ctx->peek_cb = peek_cb;
@@ -57,8 +64,8 @@ ubjr_context_t* ubjr_open_callback(void* userdata, size_t ubjr_close_context(ubjr_context_t* ctx)
{
size_t n = ctx->total_read;
- free(ctx->userdata);
- free(ctx);
+ mpfree(ctx->userdata);
+ mpfree(ctx);
return n;
}
@@ -90,7 +97,7 @@ struct mem_r_fd };
static int memclose(void* mfd)
{
- //free(mfd);
+ //mpfree(mfd);
return 0;
}
static size_t memread(void* data, size_t size, size_t count, struct mem_r_fd* fp)
@@ -112,7 +119,7 @@ static int mempeek(struct mem_r_fd* mfd) ubjr_context_t* ubjr_open_memory(const uint8_t* be, const uint8_t* en)
{
- struct mem_r_fd* mfd = (struct mem_r_fd*)malloc(sizeof(struct mem_r_fd));
+ struct mem_r_fd* mfd = (struct mem_r_fd*)mpmalloc(sizeof(struct mem_r_fd));
mfd->current = be;
mfd->begin = be;
mfd->end = en;
@@ -160,7 +167,7 @@ size_t ubjr_local_type_size(UBJ_TYPE typ) static inline priv_ubjr_sorted_key_t* priv_ubjr_object_build_sorted_keys(ubjr_object_t* obj)
{
- priv_ubjr_sorted_key_t* sorted_keysmem = malloc(obj->size*sizeof(priv_ubjr_sorted_key_t));
+ priv_ubjr_sorted_key_t* sorted_keysmem = mpmalloc(obj->size*sizeof(priv_ubjr_sorted_key_t));
size_t i;
for (i = 0; i < obj->size; i++)
{
@@ -216,7 +223,7 @@ static inline void priv_ubjr_read_to_ptr(ubjr_context_t* ctx, uint8_t* dst, UBJ_ }
case UBJ_CHAR:
{
- tstr = malloc(n + 1);
+ tstr = mpmalloc(n + 1);
priv_ubjr_context_read(ctx, tstr, n);
tstr[n] = 0;
*(ubjr_string_t*)dst = tstr;
@@ -341,7 +348,7 @@ static inline ubjr_array_t priv_ubjr_read_raw_array(ubjr_context_t* ctx) uint8_t dselect;
priv_ubjr_context_getc(ctx);//skip over the '@' marker
myarray.num_dims = priv_ubjr_context_getc(ctx);//since max is 8, no type indicator needed...always a int7 type
- myarray.dims = malloc(sizeof(size_t)*myarray.num_dims);
+ myarray.dims = mpmalloc(sizeof(size_t)*myarray.num_dims);
myarray.size = 1;
for (dselect = 0; dselect < myarray.num_dims; dselect++)
{
@@ -357,7 +364,7 @@ static inline ubjr_array_t priv_ubjr_read_raw_array(ubjr_context_t* ctx) {
myarray.originally_sized = 0;
size_t arrpot = 0;
- myarray.values=malloc(1*ls+1); //the +1 is for memory for the 0-size elements
+ myarray.values=mpmalloc(1*ls+1); //the +1 is for memory for the 0-size elements
for (myarray.size = 0; priv_ubjr_context_peek(ctx) != ']'; myarray.size++)
{
if (myarray.size >= (1ULL << arrpot))
@@ -373,7 +380,7 @@ static inline ubjr_array_t priv_ubjr_read_raw_array(ubjr_context_t* ctx) {
myarray.originally_sized = 1;
size_t i;
- myarray.values = malloc(ls*myarray.size+1);
+ myarray.values = mpmalloc(ls*myarray.size+1);
size_t sz = UBJI_TYPE_size[myarray.type];
if (sz >= 0 && myarray.type != UBJ_STRING && myarray.type != UBJ_HIGH_PRECISION && myarray.type != UBJ_CHAR && myarray.type != UBJ_MIXED) //constant size,fastread
@@ -391,7 +398,7 @@ static inline ubjr_array_t priv_ubjr_read_raw_array(ubjr_context_t* ctx) }
if (myarray.dims == NULL)
{
- myarray.dims = malloc(sizeof(size_t));
+ myarray.dims = mpmalloc(sizeof(size_t));
myarray.dims[0] = myarray.size;
}
return myarray;
@@ -408,8 +415,8 @@ static inline ubjr_object_t priv_ubjr_read_raw_object(ubjr_context_t* ctx) {
myobject.originally_sized = 0;
size_t arrpot = 0;
- myobject.values = malloc(1 * ls + 1); //the +1 is for memory for the 0-size elements
- myobject.keys = malloc(1 * sizeof(ubjr_string_t));
+ myobject.values = mpmalloc(1 * ls + 1); //the +1 is for memory for the 0-size elements
+ myobject.keys = mpmalloc(1 * sizeof(ubjr_string_t));
for (myobject.size = 0; priv_ubjr_context_peek(ctx) != '}'; myobject.size++)
{
if (myobject.size >= (1ULL << arrpot))
@@ -427,8 +434,8 @@ static inline ubjr_object_t priv_ubjr_read_raw_object(ubjr_context_t* ctx) {
size_t i;
myobject.originally_sized = 1;
- myobject.values = malloc(ls*myobject.size + 1);
- myobject.keys = malloc(myobject.size * sizeof(ubjr_string_t));
+ myobject.values = mpmalloc(ls*myobject.size + 1);
+ myobject.keys = mpmalloc(myobject.size * sizeof(ubjr_string_t));
for (i = 0; i < myobject.size; i++)
{
@@ -452,7 +459,7 @@ static inline priv_ubjr_cleanup_container(UBJ_TYPE type,size_t size,void* values priv_ubjr_cleanup_pointer(type,(void*)viter);
}
}
- free(values);
+ mpfree(values);
}
static inline void priv_ubjr_cleanup_pointer(UBJ_TYPE typ,void* value)
{
@@ -478,14 +485,14 @@ static inline void priv_ubjr_cleanup_pointer(UBJ_TYPE typ,void* value) case UBJ_STRING:
{
ubjr_string_t* st=(ubjr_string_t*)value;
- free((void*)*st);
+ mpfree((void*)*st);
break;
}
case UBJ_ARRAY:
{
ubjr_array_t* arr=(ubjr_array_t*)value;
priv_ubjr_cleanup_container(arr->type,arr->size,arr->values);
- free(arr->dims);
+ mpfree(arr->dims);
break;
}
case UBJ_OBJECT:
@@ -495,7 +502,7 @@ static inline void priv_ubjr_cleanup_pointer(UBJ_TYPE typ,void* value) priv_ubjr_cleanup_container(UBJ_STRING,obj->size,obj->keys);
if(obj->metatable)
{
- free(obj->metatable);
+ mpfree(obj->metatable);
}
break;
}
diff --git a/src/lib/ubjson/ubjw.c b/src/lib/ubjson/ubjw.c index 5cd060a..c69e630 100644 --- a/src/lib/ubjson/ubjw.c +++ b/src/lib/ubjson/ubjw.c @@ -1,6 +1,12 @@ #include "ubj.h"
#include "ubj_internal.h"
+#ifdef MULTIPASS_TRACE_MALLOC
+#include "lib/mpmalloc.h"
+#else
+#define mpmalloc malloc
+#endif
+
#define CONTAINER_IS_SIZED 0x1
#define CONTAINER_IS_TYPED 0x2
#define CONTAINER_IS_UBJ_ARRAY 0x4
@@ -47,7 +53,7 @@ ubjw_context_t* ubjw_open_callback(void* userdata, void (*error_cb)(const char* error_msg)
)
{
- ubjw_context_t* ctx = (ubjw_context_t*)malloc(sizeof(ubjw_context_t));
+ ubjw_context_t* ctx = (ubjw_context_t*)mpmalloc(sizeof(ubjw_context_t));
ctx->userdata = userdata;
ctx->write_cb = write_cb;
ctx->close_cb = close_cb;
@@ -96,7 +102,7 @@ static size_t memwrite(const void* data, size_t size, size_t count, struct mem_w ubjw_context_t* ubjw_open_memory(uint8_t* be, uint8_t* en)
{
- struct mem_w_fd* mfd = (struct mem_w_fd*)malloc(sizeof(struct mem_w_fd));
+ struct mem_w_fd* mfd = (struct mem_w_fd*)mpmalloc(sizeof(struct mem_w_fd));
mfd->current = be;
mfd->begin = be;
mfd->end = en;
|