Skip to content

Commit

Permalink
Add mhook module, apply malloc/free checks to list
Browse files Browse the repository at this point in the history
  • Loading branch information
w32zhong committed Aug 24, 2016
1 parent a55954a commit dbc57a1
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions list/dep/dep-mhook.mk
@@ -0,0 +1,2 @@
CFLAGS +=
LDFLAGS += -L "../mhook/$(BUILD_DIR)"
4 changes: 4 additions & 0 deletions list/run/test-insert.c
@@ -1,5 +1,7 @@
#include <stdio.h>
#include <stdlib.h>

#include "mhook/mhook.h"
#include "list.h"

struct T
Expand Down Expand Up @@ -35,5 +37,7 @@ int main()
printf("NULL \n");

list_release(&li);

mhook_print_unfree();
return 0;
}
3 changes: 3 additions & 0 deletions list/run/test-sort.c
Expand Up @@ -3,6 +3,7 @@
#include <time.h>
#include <assert.h>

#include "mhook/mhook.h"
#include "list.h"

struct T
Expand Down Expand Up @@ -78,5 +79,7 @@ main()

/* free list */
list_release(&list);

mhook_print_unfree();
return 0;
}
5 changes: 5 additions & 0 deletions mhook/Makefile
@@ -0,0 +1,5 @@
include ../rules.mk
include ../module.mk

clean:
@ echo 'clean'
30 changes: 30 additions & 0 deletions mhook/mhook.c
@@ -0,0 +1,30 @@
#include <stdio.h>
#include "mhook.h"

static uint64_t unfree = 0;

uint64_t mhook_unfree()
{
return unfree;
}

void mhook_print_unfree()
{
printf("Unfree mallocs: %lu.\n", unfree);
}

void *__real_malloc(size_t);

void *__wrap_malloc(size_t c)
{
unfree++;
return __real_malloc(c);
}

void *__real_free(size_t);

void *__wrap_free(size_t c)
{
unfree--;
return __real_free(c);
}
9 changes: 9 additions & 0 deletions mhook/mhook.h
@@ -0,0 +1,9 @@
#pragma once
#include <stdint.h>
#include <malloc.h>

uint64_t mhook_unfree();
void mhook_print_unfree();

void *__wrap_malloc(size_t);
void *__wrap_free(size_t);
3 changes: 2 additions & 1 deletion rules.mk
Expand Up @@ -33,7 +33,8 @@ COMPILE_CXX = $(CXX) -c $(CFLAGS) $(filter %.cpp, $^) -o $@
CCDH = gcc -dH

# linker
LD := gcc
MHOOK_FLAGS := -Wl,--wrap,malloc -Wl,--wrap,free
LD := gcc $(MHOOK_FLAGS)
COLOR_LINK = @ tput setaf 5 && echo '[link] $@' && tput sgr0

# cycling libraries even if we use "-( .. -)", this is necessary
Expand Down

0 comments on commit dbc57a1

Please sign in to comment.