Skip to content

Commit 7c6f197

Browse files
authored
Fixing realloc's behavior
Sometimes, realloc would allocate slightly too big memory blocks.
1 parent 69e0687 commit 7c6f197

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

libc/src/malloc.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,33 +116,33 @@ void * base_realloc(void * ptr, size_t size) {
116116
if (!ptr)
117117
return malloc(size);
118118

119-
size = (size + sizeof(heap_t) + 7) & ~7;
119+
size_t block_size = (size + sizeof(heap_t) + 7) & ~7;
120120

121121
prev = (heap_t *) ((uintptr_t) ptr - sizeof(heap_t));
122122

123123
// New memory block shorter ?
124-
if (prev->size >= size) {
125-
prev->size = size;
124+
if (prev->size >= block_size) {
125+
prev->size = block_size;
126126
if (!prev->next)
127-
sbrk(ptr + size - sbrk(0));
127+
sbrk(ptr + block_size - sbrk(0));
128128

129129
return ptr;
130130
}
131131

132132
// New memory block larger
133133
// Is it the last one ?
134134
if (!prev->next) {
135-
new = sbrk(size - prev->size);
135+
new = sbrk(block_size - prev->size);
136136
if (!new)
137137
return NULL;
138138

139-
prev->size = size;
139+
prev->size = block_size;
140140
return ptr;
141141
}
142142

143143
// Do we have free memory after it ?
144-
if ((prev->next->ptr - ptr) > size) {
145-
prev->size = size;
144+
if ((prev->next->ptr - ptr) > block_size) {
145+
prev->size = block_size;
146146
return ptr;
147147
}
148148

0 commit comments

Comments
 (0)