OSDN Git Service

fix static linking of pthread apps
[uclinux-h8/uclibc-ng.git] / libc / stdlib / malloc-standard / realloc.c
index 36bfe4d..e49d111 100644 (file)
   Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
 */
 
-#define mremap __mremap
-
 #include "malloc.h"
 
 
-
 /* ------------------------------ realloc ------------------------------ */
 void* realloc(void* oldmem, size_t bytes)
 {
@@ -47,18 +44,19 @@ void* realloc(void* oldmem, size_t bytes)
     size_t* s;               /* copy source */
     size_t* d;               /* copy destination */
 
+    void *retval;
 
     /* Check for special cases.  */
     if (! oldmem)
        return malloc(bytes);
     if (! bytes) {
        free (oldmem);
-       return malloc(bytes);
+       return NULL;
     }
 
-    LOCK;
-    av = get_malloc_state();
     checked_request2size(bytes, nb);
+    __MALLOC_LOCK;
+    av = get_malloc_state();
 
     oldp    = mem2chunk(oldmem);
     oldsize = chunksize(oldp);
@@ -83,8 +81,8 @@ void* realloc(void* oldmem, size_t bytes)
                set_head_size(oldp, nb);
                av->top = chunk_at_offset(oldp, nb);
                set_head(av->top, (newsize - nb) | PREV_INUSE);
-               UNLOCK;
-               return chunk2mem(oldp);
+               retval = chunk2mem(oldp);
+               goto DONE;
            }
 
            /* Try to expand forward into next chunk;  split off remainder below */
@@ -100,8 +98,8 @@ void* realloc(void* oldmem, size_t bytes)
            else {
                newmem = malloc(nb - MALLOC_ALIGN_MASK);
                if (newmem == 0) {
-                   UNLOCK;
-                   return 0; /* propagate failure */
+                   retval = 0; /* propagate failure */
+                   goto DONE;
                }
 
                newp = mem2chunk(newmem);
@@ -128,7 +126,7 @@ void* realloc(void* oldmem, size_t bytes)
                    assert(ncopies >= 3);
 
                    if (ncopies > 9)
-                       __memcpy(d, s, copysize);
+                       memcpy(d, s, copysize);
 
                    else {
                        *(d+0) = *(s+0);
@@ -150,8 +148,8 @@ void* realloc(void* oldmem, size_t bytes)
 
                    free(oldmem);
                    check_inuse_chunk(newp);
-                   UNLOCK;
-                   return chunk2mem(newp);
+                   retval = chunk2mem(newp);
+                   goto DONE;
                }
            }
        }
@@ -176,8 +174,8 @@ void* realloc(void* oldmem, size_t bytes)
        }
 
        check_inuse_chunk(newp);
-       UNLOCK;
-       return chunk2mem(newp);
+       retval = chunk2mem(newp);
+       goto DONE;
     }
 
     /*
@@ -195,8 +193,8 @@ void* realloc(void* oldmem, size_t bytes)
 
        /* don't need to remap if still within same page */
        if (oldsize == newsize - offset) {
-           UNLOCK;
-           return oldmem;
+           retval = oldmem;
+           goto DONE;
        }
 
        cp = (char*)mremap((char*)oldp - offset, oldsize + offset, newsize, 1);
@@ -217,8 +215,8 @@ void* realloc(void* oldmem, size_t bytes)
            if (sum > (unsigned long)(av->max_total_mem))
                av->max_total_mem = sum;
 
-           UNLOCK;
-           return chunk2mem(newp);
+           retval = chunk2mem(newp);
+           goto DONE;
        }
 
        /* Note the extra (sizeof(size_t)) overhead. */
@@ -228,12 +226,15 @@ void* realloc(void* oldmem, size_t bytes)
            /* Must alloc, copy, free. */
            newmem = malloc(nb - MALLOC_ALIGN_MASK);
            if (newmem != 0) {
-               __memcpy(newmem, oldmem, oldsize - 2*(sizeof(size_t)));
+               memcpy(newmem, oldmem, oldsize - 2*(sizeof(size_t)));
                free(oldmem);
            }
        }
-       UNLOCK;
-       return newmem;
+       retval = newmem;
     }
+
+ DONE:
+    __MALLOC_UNLOCK;
+    return retval;
 }