Commit 40379087 authored by Davide Galassi's avatar Davide Galassi

Malloc mock improvement

Keeping track of the state: IDLE, ACTIVE, FAILED
parent 026ba2b8
Pipeline #57787525 failed with stages
in 1 minute and 22 seconds
......@@ -3,7 +3,7 @@
#include <stdio.h>
int g_malloc_mock_count = -1;
int g_malloc_mock_failed = 0;
int g_malloc_mock_state = MALLOC_MOCK_IDLE;
void *__real_malloc(size_t size);
void *__real_realloc(void *ptr, size_t size);
......@@ -12,38 +12,37 @@ void __real_free(void *ptr);
void *__wrap_malloc(size_t size)
{
//printf("Wrap malloc\n");
if (g_malloc_mock_count == 0) {
g_malloc_mock_count = -1;
g_malloc_mock_failed = 1;
return NULL;
}
if (g_malloc_mock_count > 0)
if (g_malloc_mock_state == MALLOC_MOCK_ACTIVE) {
if (g_malloc_mock_count == 0) {
g_malloc_mock_state = MALLOC_MOCK_FAILED;
return NULL;
}
g_malloc_mock_count--;
}
return __real_malloc(size);
}
void *__wrap_realloc(void *ptr, 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)
if (g_malloc_mock_state == MALLOC_MOCK_ACTIVE) {
if (g_malloc_mock_count == 0) {
g_malloc_mock_state = MALLOC_MOCK_FAILED;
return NULL;
}
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)
if (g_malloc_mock_state == MALLOC_MOCK_ACTIVE) {
if (g_malloc_mock_count == 0) {
g_malloc_mock_state = MALLOC_MOCK_FAILED;
return NULL;
}
g_malloc_mock_count--;
}
return __real_calloc(nmemb, size);
}
......
#ifndef MALLOC_MOCK_H_
#define MALLOC_MOCK_H_
#define MALLOC_MOCK_IDLE 0
#define MALLOC_MOCK_ACTIVE 1
#define MALLOC_MOCK_FAILED 2
extern int g_malloc_mock_count;
extern int g_malloc_mock_state;
#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