Skip to content

Commit

Permalink
add debug oriented memory management
Browse files Browse the repository at this point in the history
remove bad assertions
  • Loading branch information
kevyang committed May 13, 2016
1 parent 05c6e1e commit d526f7a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ option(HAVE_ASSERT_LOG "assert_log enabled by default" ON)
option(HAVE_ASSERT_PANIC "assert_panic disabled by default" OFF)
option(HAVE_LOGGING "logging enabled by default" ON)
option(HAVE_STATS "stats enabled by default" ON)
option(HAVE_DEBUG_MM "debugging oriented memory management disabled by default" OFF)
option(COVERAGE "code coverage" OFF)

include(CheckIncludeFiles)
Expand Down
2 changes: 2 additions & 0 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
#cmakedefine HAVE_LOGGING

#cmakedefine HAVE_STATS

#cmakedefine HAVE_DEBUG_MM
7 changes: 3 additions & 4 deletions include/cc_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,9 @@ extern "C" {
# define CC_BACKTRACE 1
#endif

/* TODO: add compile time option to turn chaining on/off */
/*#ifdef HAVE_CHAINED*/
# define CC_HAVE_CHAINED 1
/*#endif*/
#ifdef HAVE_DEBUG_MM
#define CC_DEBUG_MM 1
#endif

#define CC_OK 0
#define CC_ERROR -1
Expand Down
8 changes: 8 additions & 0 deletions include/cc_mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#pragma once

#include <cc_define.h>

#include <stddef.h>

/*
Expand All @@ -41,8 +43,13 @@
#define cc_calloc(_n, _s) \
_cc_calloc((size_t)(_n), (size_t)(_s), __FILE__, __LINE__)

#if defined CC_DEBUG_MM && CC_DEBUG_MM == 1
#define cc_realloc(_p, _s) \
_cc_realloc_move(_p, (size_t)(_s), __FILE__, __LINE__)
#else
#define cc_realloc(_p, _s) \
_cc_realloc(_p, (size_t)(_s), __FILE__, __LINE__)
#endif

#define cc_free(_p) do { \
_cc_free(_p, __FILE__, __LINE__); \
Expand All @@ -59,6 +66,7 @@ void * _cc_alloc(size_t size, const char *name, int line);
void * _cc_zalloc(size_t size, const char *name, int line);
void * _cc_calloc(size_t nmemb, size_t size, const char *name, int line);
void * _cc_realloc(void *ptr, size_t size, const char *name, int line);
void * _cc_realloc_move(void *ptr, size_t size, const char *name, int line);
void _cc_free(void *ptr, const char *name, int line);
void * _cc_mmap(size_t size, const char *name, int line);
int _cc_munmap(void *p, size_t size, const char *name, int line);
38 changes: 28 additions & 10 deletions src/cc_mm.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ _cc_alloc(size_t size, const char *name, int line)
{
void *p;

ASSERT(size != 0);

p = malloc(size);
if (p == NULL) {
if (p == NULL && size != 0) {
log_error("malloc(%zu) failed @ %s:%d", size, name, line);
} else {
log_vverb("malloc(%zu) at %p @ %s:%d", size, p, name, line);
Expand Down Expand Up @@ -70,22 +68,42 @@ _cc_realloc(void *ptr, size_t size, const char *name, int line)
{
void *p;

ASSERT(size != 0);

p = realloc(ptr, size);
if (p == NULL) {
if (p == NULL && size != 0) {
log_error("realloc(%zu) failed @ %s:%d", size, name, line);
} else {
log_vverb("realloc(%zu) at %p @ %s:%d", size, p, name, line);
}

return p;
}

void *
_cc_realloc_move(void *ptr, size_t size, const char *name, int line)
{
void *p = NULL, *pr;

/*
* Calling realloc then malloc allows us to force this function call to
* change the address of the allocated memory block. realloc ensures we can
* copy size bytes, and calling malloc before the realloc'd data is free'd
* gives us a new address for the memory object.
*/
if (((pr = realloc(ptr, size)) == NULL || (p = malloc(size)) == NULL)
&& size != 0) {
log_error("realloc(%zu) failed @ %s:%d", size, name, line);
} else {
log_vverb("realloc(%zu) at %p @ %s:%d", size, p, name, line);
memcpy(p, pr, size);
}

free(pr);
return p;
}

void
_cc_free(void *ptr, const char *name, int line)
{
ASSERT(ptr != NULL);
log_vverb("free(%p) @ %s:%d", ptr, name, line);
free(ptr);
}
Expand All @@ -103,10 +121,10 @@ _cc_mmap(size_t size, const char *name, int line)
* is set appropriately.
*/
p = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS,
-1, 0);
-1, 0);
if (p == ((void *) -1)) {
log_error("mmap %zu bytes @ %s:%d failed: %s", size, name, line,
strerror(errno));
strerror(errno));
return NULL;
}

Expand All @@ -128,7 +146,7 @@ _cc_munmap(void *p, size_t size, const char *name, int line)
status = munmap(p, size);
if (status < 0) {
log_error("munmap %p @ %s:%d failed: %s", p, name, line,
strerror(errno));
strerror(errno));
}

return status;
Expand Down

0 comments on commit d526f7a

Please sign in to comment.