Skip to content

Commit ee8f240

Browse files
committed
Hook realloc for test harness
This commit implements test_realloc and makes it possible to adjust the memory size allocated by test_malloc() or test_calloc(). In this commit, there is still room for improvement. When we shrink the allocated memory size, we simply return the original array. This may lead to unused memory. Therefore, we can improve this in the future. Reference: https://danluu.com/malloc-tutorial/ Change-Id: Ic85b35cfff4dc0b6a46a6e81e6c1ce873625f0ac
1 parent 9563d40 commit ee8f240

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

harness.c

+38
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static bool time_limited = false;
6363
typedef enum {
6464
TEST_MALLOC,
6565
TEST_CALLOC,
66+
TEST_REALLOC,
6667
} alloc_t;
6768

6869
/* Internal functions */
@@ -128,6 +129,7 @@ static void *alloc(alloc_t alloc_type, size_t size)
128129
char *msg_alloc_forbidden[] = {
129130
"Calls to malloc are disallowed",
130131
"Calls to calloc are disallowed",
132+
"Calls to realloc are disallowed",
131133
};
132134
report_event(MSG_FATAL, "%s", msg_alloc_forbidden[alloc_type]);
133135
return NULL;
@@ -137,6 +139,7 @@ static void *alloc(alloc_t alloc_type, size_t size)
137139
char *msg_alloc_failure[] = {
138140
"Malloc returning NULL",
139141
"Calloc returning NULL",
142+
"Realloc returning NULL",
140143
};
141144
report_event(MSG_WARN, "%s", msg_alloc_failure[alloc_type]);
142145
return NULL;
@@ -187,6 +190,41 @@ void *test_calloc(size_t nelem, size_t elsize)
187190
return alloc(TEST_CALLOC, nelem * elsize);
188191
}
189192

193+
/*
194+
* This function implements how to adjust the size of memory allocated
195+
* by test_malloc or test_calloc.
196+
*
197+
* First, we check whether the memory is already allocated.
198+
* If it wasn't allocated (p is NULL),
199+
* the fucntion behaves like test_malloc, returning a newly allocated memory.
200+
*
201+
* Otherwise, we check the payload size of the orignal memory.
202+
* - If new_size is less than or equal to it, return the original memory.
203+
* - If new_size is greater than it, we allocate a new memory with new_size.
204+
* Copy the contents from the orginal memory to the newly allocated memory
205+
* ,and then free the original one. Finally, we return the newly one.
206+
*
207+
* Reference: https://danluu.com/malloc-tutorial
208+
*/
209+
void *test_realloc(void *p, size_t new_size)
210+
{
211+
if (!p)
212+
return alloc(TEST_REALLOC, new_size);
213+
214+
const block_element_t *b = find_header(p);
215+
if (b->payload_size >= new_size)
216+
// TODO: Free some once we implement split.
217+
return p;
218+
219+
void *new_ptr = alloc(TEST_REALLOC, new_size);
220+
if (!new_ptr)
221+
return NULL;
222+
memcpy(new_ptr, p, b->payload_size);
223+
test_free(p);
224+
225+
return new_ptr;
226+
}
227+
190228
void test_free(void *p)
191229
{
192230
if (noallocate_mode) {

harness.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
void *test_malloc(size_t size);
1414
void *test_calloc(size_t nmemb, size_t size);
15+
void *test_realloc(void *p, size_t new_size);
1516
void test_free(void *p);
1617
char *test_strdup(const char *s);
17-
/* FIXME: provide test_realloc as well */
1818

1919
#ifdef INTERNAL
2020

@@ -56,6 +56,7 @@ void trigger_exception(char *msg);
5656
/* Tested program use our versions of malloc and free */
5757
#define malloc test_malloc
5858
#define calloc test_calloc
59+
#define realloc test_realloc
5960
#define free test_free
6061

6162
/* Use undef to avoid strdup redefined error */

0 commit comments

Comments
 (0)