Commit af0471c0 authored by Davide Galassi's avatar Davide Galassi

First rudimental malloc mock implementation

parent 9348c86f
......@@ -4,11 +4,12 @@ CC := gcc
CFLAGS := -O0 -g3 -Wall
CPPFLAGS = -I. -I../include -I../src
LDFLAGS := --coverage
LDFLAGS := --coverage -Wl,--wrap=malloc,--wrap=realloc,--wrap=calloc,--wrap=free
objs := \
test.o \
malloc_mock.o \
version_test.o \
memxor_test.o \
base64_test.o \
......
#include "malloc_mock.h"
#include <stdlib.h>
#include <stdio.h>
int g_malloc_fail_count = -1;
void *__real_malloc(size_t size);
void *__real_realloc(void *ptr, size_t size);
void *__real_calloc(size_t nmemb, size_t size);
void __real_free(void *ptr);
void *__wrap_malloc(size_t size)
{
//printf("Wrap malloc\n");
if (g_malloc_fail_count == 0) {
g_malloc_fail_count = -1;
return NULL;
}
if (g_malloc_fail_count > 0)
g_malloc_fail_count--;
return __real_malloc(size);
}
void *__wrap_realloc(void *ptr, size_t size)
{
if (g_malloc_fail_count == 0) {
g_malloc_fail_count = -1;
return NULL;
}
if (g_malloc_fail_count > 0)
g_malloc_fail_count--;
return __real_realloc(ptr, size);
}
void *__wrap_calloc(size_t nmemb, size_t size)
{
return __real_calloc(nmemb, size);
}
void __wrap_free(void *ptr)
{
__real_free(ptr);
}
#ifndef MALLOC_MOCK_H_
#define MALLOC_MOCK_H_
int g_malloc_fail_count;
#endif /* MALLOC_MOCK_H_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment