Commit 026ba2b8 authored by Davide Galassi's avatar Davide Galassi

Malloc mock fix

Variable shall be extern in header files
parent e30ff7fa
Pipeline #57714866 passed with stages
in 1 minute and 17 seconds
......@@ -2,7 +2,8 @@
#include <stdlib.h>
#include <stdio.h>
int g_malloc_fail_count = -1;
int g_malloc_mock_count = -1;
int g_malloc_mock_failed = 0;
void *__real_malloc(size_t size);
void *__real_realloc(void *ptr, size_t size);
......@@ -12,28 +13,37 @@ 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;
if (g_malloc_mock_count == 0) {
g_malloc_mock_count = -1;
g_malloc_mock_failed = 1;
return NULL;
}
if (g_malloc_fail_count > 0)
g_malloc_fail_count--;
if (g_malloc_mock_count > 0)
g_malloc_mock_count--;
return __real_malloc(size);
}
void *__wrap_realloc(void *ptr, size_t size)
{
if (g_malloc_fail_count == 0) {
g_malloc_fail_count = -1;
if (g_malloc_mock_count == 0) {
g_malloc_mock_count = -1;
g_malloc_mock_failed = 1;
return NULL;
}
if (g_malloc_fail_count > 0)
g_malloc_fail_count--;
if (g_malloc_mock_count > 0)
g_malloc_mock_count--;
return __real_realloc(ptr, size);
}
void *__wrap_calloc(size_t nmemb, size_t size)
{
if (g_malloc_mock_count == 0) {
g_malloc_mock_count = -1;
g_malloc_mock_failed = 1;
return NULL;
}
if (g_malloc_mock_count > 0)
g_malloc_mock_count--;
return __real_calloc(nmemb, size);
}
......
#ifndef MALLOC_MOCK_H_
#define MALLOC_MOCK_H_
int g_malloc_fail_count;
extern int g_malloc_mock_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