2016-06-19 15:25:58 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include "tests.h"
|
|
|
|
|
|
|
|
void *__real_malloc(size_t size);
|
|
|
|
void __real_free(void *ptr);
|
|
|
|
void *__real_calloc(size_t nmemb, size_t size);
|
|
|
|
void *__real_realloc(void *ptr, size_t size);
|
|
|
|
|
2016-06-19 16:06:16 +00:00
|
|
|
enum wrapper_behavior _memory_behavior = WRAPPER_INVOKE_CMOCKA;
|
|
|
|
int malloc_callcount = 0,
|
|
|
|
free_callcount = 0,
|
|
|
|
calloc_callcount = 0,
|
|
|
|
realloc_callcount = 0;
|
2016-06-19 15:25:58 +00:00
|
|
|
|
|
|
|
int reset_mem_wrappers(void **state) {
|
2016-06-19 16:06:16 +00:00
|
|
|
_memory_behavior = WRAPPER_INVOKE_CMOCKA;
|
|
|
|
malloc_callcount =
|
|
|
|
free_callcount =
|
|
|
|
calloc_callcount =
|
|
|
|
realloc_callcount = 0;
|
2016-06-19 15:25:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void memory_behavior(enum wrapper_behavior behavior) {
|
|
|
|
_memory_behavior = behavior;
|
|
|
|
}
|
|
|
|
|
2016-06-19 16:06:16 +00:00
|
|
|
int malloc_calls() {
|
|
|
|
return malloc_callcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
int free_calls() {
|
|
|
|
return free_callcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
int calloc_calls() {
|
|
|
|
return calloc_callcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
int realloc_calls() {
|
|
|
|
return realloc_callcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
int alloc_calls() {
|
|
|
|
return malloc_callcount + calloc_callcount;
|
|
|
|
}
|
|
|
|
|
2016-06-19 15:25:58 +00:00
|
|
|
void *__wrap_malloc(size_t size) {
|
2016-06-19 16:06:16 +00:00
|
|
|
++malloc_callcount;
|
2016-06-19 15:25:58 +00:00
|
|
|
switch (_memory_behavior) {
|
|
|
|
case WRAPPER_INVOKE_CMOCKA:
|
|
|
|
return test_malloc(size);
|
|
|
|
case WRAPPER_DO_ASSERTIONS:
|
|
|
|
check_expected(size);
|
|
|
|
return mock_type(void *);
|
|
|
|
case WRAPPER_INVOKE_REAL:
|
|
|
|
default:
|
|
|
|
return __real_malloc(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void __wrap_free(void *ptr) {
|
2016-06-19 16:06:16 +00:00
|
|
|
++free_callcount;
|
2016-06-19 15:25:58 +00:00
|
|
|
switch (_memory_behavior) {
|
|
|
|
case WRAPPER_INVOKE_CMOCKA:
|
|
|
|
test_free(ptr);
|
|
|
|
break;
|
|
|
|
case WRAPPER_DO_ASSERTIONS:
|
|
|
|
check_expected_ptr(ptr);
|
|
|
|
break;
|
|
|
|
case WRAPPER_INVOKE_REAL:
|
|
|
|
default:
|
|
|
|
__real_free(ptr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *__wrap_calloc(size_t nmemb, size_t size) {
|
2016-06-19 16:06:16 +00:00
|
|
|
++calloc_callcount;
|
2016-06-19 15:25:58 +00:00
|
|
|
switch (_memory_behavior) {
|
|
|
|
case WRAPPER_INVOKE_CMOCKA:
|
|
|
|
return test_calloc(nmemb, size);
|
|
|
|
case WRAPPER_DO_ASSERTIONS:
|
|
|
|
check_expected(nmemb);
|
|
|
|
check_expected(size);
|
|
|
|
return mock_type(void *);
|
|
|
|
case WRAPPER_INVOKE_REAL:
|
|
|
|
default:
|
|
|
|
return __real_calloc(nmemb, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *__wrap_realloc(void *ptr, size_t size) {
|
2016-06-19 16:06:16 +00:00
|
|
|
++realloc_callcount;
|
2016-06-19 15:25:58 +00:00
|
|
|
switch (_memory_behavior) {
|
|
|
|
case WRAPPER_INVOKE_CMOCKA:
|
|
|
|
return test_realloc(ptr, size);
|
|
|
|
case WRAPPER_DO_ASSERTIONS:
|
|
|
|
check_expected_ptr(ptr);
|
|
|
|
check_expected(size);
|
|
|
|
return mock_type(void *);
|
|
|
|
case WRAPPER_INVOKE_REAL:
|
|
|
|
default:
|
|
|
|
return __real_realloc(ptr, size);
|
|
|
|
}
|
|
|
|
}
|