summaryrefslogtreecommitdiff
path: root/com32/lib/realloc.c
blob: 577c2001a5527bc936672f9b98deac69eca8dd4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * realloc.c
 */

#include <stdlib.h>
#include <string.h>

#include "malloc.h"

/* FIXME: This is cheesy, it should be fixed later */

void *realloc(void *ptr, size_t size)
{
  struct free_arena_header *ah;
  void *newptr;
  size_t oldsize;

  if ( !ptr )
    return malloc(size);

  if ( size == 0 ) {
    free(ptr);
    return NULL;
  }

  /* Add the obligatory arena header, and round up */
  size = (size+2*sizeof(struct arena_header)-1) & ARENA_SIZE_MASK;

  ah = (struct free_arena_header *)
    ((struct arena_header *)ptr - 1);

  if ( ah->a.size >= size && size >= (ah->a.size >> 2) ) {
    /* This field is a good size already. */
    return ptr;
  } else {
    /* Make me a new block.  This is kind of bogus; we should
       be checking the adjacent blocks to see if we can do an
       in-place adjustment... fix that later. */
    
    oldsize = ah->a.size - sizeof(struct arena_header);

    newptr = malloc(size);
    memcpy(newptr, ptr, (size < oldsize) ? size : oldsize);
    free(ptr);

    return newptr;
  }
}