File tree Expand file tree Collapse file tree 2 files changed +29
-1
lines changed
Expand file tree Collapse file tree 2 files changed +29
-1
lines changed Original file line number Diff line number Diff 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+
190217void test_free (void * p )
191218{
192219 if (noallocate_mode ) {
Original file line number Diff line number Diff line change 1212
1313void * test_malloc (size_t size );
1414void * test_calloc (size_t nmemb , size_t size );
15+ void * test_realloc (void * p , size_t new_size );
1516void test_free (void * p );
1617char * 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 */
You can’t perform that action at this time.
0 commit comments