Browse Source

* Handle malloc(0), realloc(NULL,...) and realloc(...,0) in kernel mode.

tags/v0.99.beta14
Sam Hocevar sam 19 years ago
parent
commit
9b548949f6
1 changed files with 15 additions and 3 deletions
  1. +15
    -3
      kernel/kernel.c

+ 15
- 3
kernel/kernel.c View File

@@ -55,6 +55,8 @@ void cmain(unsigned long int magic, unsigned long int addr)
void *malloc(size_t size) void *malloc(size_t size)
{ {
uint32_t *p = freemem; uint32_t *p = freemem;
if(!size)
return NULL;
size = (size + 0x7) / 4; size = (size + 0x7) / 4;
*p = size; *p = size;
freemem += size + 1; freemem += size + 1;
@@ -68,10 +70,20 @@ void free(void *ptr)


void *realloc(void *ptr, size_t size) void *realloc(void *ptr, size_t size)
{ {
uint32_t oldsize = ((uint32_t *)ptr)[-1];
uint32_t oldsize;
void *p; void *p;
if(oldsize >= size)
return ptr;

if(!size)
return NULL;

if(!ptr)
oldsize = 0;
else
{
oldsize = ((uint32_t *)ptr)[-1];
if(oldsize >= size)
return ptr;
}


p = malloc(size); p = malloc(size);
memcpy(p, ptr, oldsize); memcpy(p, ptr, oldsize);


Loading…
Cancel
Save