Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a209e4f

Browse files
authored
Merge pull request #284 from chensheep/dev_test_realloc
Implement test_realloc
2 parents 6a93f2f + a6678e3 commit a209e4f

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

‎harness.c

Lines changed: 25 additions & 0 deletions
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,28 @@ void *test_calloc(size_t nelem, size_t elsize)
187190
return alloc(TEST_CALLOC, nelem * elsize);
188191
}
189192

193+
/*
194+
* Implementation of adjusting the size of the memory allocated
195+
* by test_malloc or test_calloc.
196+
*/
197+
void *test_realloc(void *p, size_t new_size)
198+
{
199+
if (!p)
200+
return alloc(TEST_REALLOC, new_size);
201+
202+
const block_element_t *b = find_header(p);
203+
if (b->payload_size >= new_size)
204+
return p;
205+
206+
void *new_ptr = alloc(TEST_REALLOC, new_size);
207+
if (!new_ptr)
208+
return NULL;
209+
memcpy(new_ptr, p, b->payload_size);
210+
test_free(p);
211+
212+
return new_ptr;
213+
}
214+
190215
void test_free(void *p)
191216
{
192217
if (noallocate_mode) {

‎harness.h

Lines changed: 2 additions & 1 deletion
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)

AltStyle によって変換されたページ (->オリジナル) /