@@ -97,17 +97,14 @@ void free(void* ptr) {
97
97
98
98
void * realloc (void * ptr , size_t size ) {
99
99
if (ptr == NULL ) {
100
- return malloc (size );
100
+ return malloc (size ); // Use your malloc
101
101
}
102
102
103
103
if (size == 0 ) {
104
- free (ptr );
104
+ free (ptr ); // Use your free
105
105
return NULL ;
106
106
}
107
107
108
- // Align allocation to page size
109
- size_t aligned_size = (size + PAGE_SIZE - 1 ) & ~(PAGE_SIZE - 1 );
110
-
111
108
Node * current = head ;
112
109
while (current != NULL && current -> data != ptr ) {
113
110
current = current -> next ;
@@ -117,19 +114,24 @@ void* realloc(void* ptr, size_t size) {
117
114
return NULL ; // Invalid pointer
118
115
}
119
116
120
- if (current -> size >= aligned_size ) {
121
- // Existing block is large enough
122
- return ptr ;
117
+ if (current -> size == size ) {
118
+ return ptr ; // No change needed
123
119
}
124
120
125
- // Allocate a new block and copy data
126
- void * new_ptr = malloc (size );
121
+ // Try to expand in place (more efficient)
122
+ // ... (Implementation for expanding in place, if possible) ...
123
+
124
+ // If expanding in place isn't possible, allocate a new block
125
+ void * new_ptr = malloc (size ); // Use your malloc
127
126
if (new_ptr == NULL ) {
128
127
return NULL ;
129
128
}
130
129
131
- memcpy (new_ptr , ptr , current -> size );
132
- free (ptr );
130
+ size_t copy_size = (size < current -> size ) ? size : current -> size ; // Don't copy more than the smaller size
131
+ memcpy (new_ptr , ptr , copy_size );
132
+ free (ptr ); // Use your free
133
133
134
+ current -> data = new_ptr ;
135
+ current -> size = size ; // Update the size!
134
136
return new_ptr ;
135
137
}
0 commit comments