Skip to content

Commit a867c69

Browse files
committed
Implement test_realloc
Change-Id: I87149ac233572dece7f3ec8b7d3320d07544f855
1 parent 9563d40 commit a867c69

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

harness.c

+27
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,33 @@ void *test_calloc(size_t nelem, size_t elsize)
187187
return alloc(TEST_CALLOC, nelem * elsize);
188188
}
189189

190+
void *test_realloc(void *p, size_t size)
191+
{
192+
/* Reference: Malloc tutorial
193+
* https://danluu.com/malloc-tutorial/
194+
*/
195+
196+
if (!p) {
197+
return alloc(TEST_MALLOC, size);
198+
}
199+
200+
const block_element_t *b = find_header(p);
201+
if (b->payload_size >= size) {
202+
return p;
203+
}
204+
205+
void *new_ptr;
206+
new_ptr = alloc(TEST_MALLOC, size);
207+
if (!new_ptr) {
208+
return NULL;
209+
}
210+
memcpy(new_ptr, p, b->payload_size);
211+
test_free(p);
212+
213+
return new_ptr;
214+
}
215+
216+
190217
void test_free(void *p)
191218
{
192219
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)