]> git.ipfire.org Git - people/ms/u-boot.git/blobdiff - common/dlmalloc.c
Migrate CONFIG_BOOTCOUNT_ALEN to Kconfig
[people/ms/u-boot.git] / common / dlmalloc.c
index b09f5249a902c7991986517dc91f0cfd447789f9..b395eefbf862cdad5808483750f739edcb8213ed 100644 (file)
@@ -1,6 +1,6 @@
 #include <common.h>
 
-#ifdef CONFIG_SANDBOX
+#if defined(CONFIG_UNIT_TEST)
 #define DEBUG
 #endif
 
@@ -149,7 +149,7 @@ gAllocatedSize))
                        {
                                new_address = findRegion (new_address, new_size);
 
-                               if (new_address == 0)
+                               if (!new_address)
                                        return (void*)-1;
 
                                gAddressBase = gNextAddress =
@@ -175,7 +175,7 @@ gAllocatedSize))
                                                                (size + gNextAddress -
                                                                 AlignPage (gNextAddress)),
                                                                MEM_COMMIT, PAGE_READWRITE);
-                       if (res == 0)
+                       if (!res)
                                return (void*)-1;
                }
                tmp = (void*)gNextAddress;
@@ -1254,7 +1254,7 @@ Void_t* mALLOc(bytes) size_t bytes;
 
   INTERNAL_SIZE_T nb;
 
-#ifdef CONFIG_SYS_MALLOC_F_LEN
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
        if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT))
                return malloc_simple(bytes);
 #endif
@@ -1461,7 +1461,7 @@ Void_t* mALLOc(bytes) size_t bytes;
 #if HAVE_MMAP
     /* If big and would otherwise need to extend, try to use mmap instead */
     if ((unsigned long)nb >= (unsigned long)mmap_threshold &&
-       (victim = mmap_chunk(nb)) != 0)
+       (victim = mmap_chunk(nb)))
       return chunk2mem(victim);
 #endif
 
@@ -1522,7 +1522,7 @@ void fREe(mem) Void_t* mem;
   mchunkptr fwd;       /* misc temp for linking */
   int       islr;      /* track whether merging with last_remainder */
 
-#ifdef CONFIG_SYS_MALLOC_F_LEN
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
        /* free() is a no-op - all the memory will be freed on relocation */
        if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT))
                return;
@@ -1671,7 +1671,10 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
   mchunkptr fwd;              /* misc temp for linking */
 
 #ifdef REALLOC_ZERO_BYTES_FREES
-  if (bytes == 0) { fREe(oldmem); return 0; }
+  if (!bytes) {
+       fREe(oldmem);
+       return NULL;
+  }
 #endif
 
   if ((long)bytes < 0) return NULL;
@@ -1679,7 +1682,7 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
   /* realloc of null is supposed to be same as malloc */
   if (oldmem == NULL) return mALLOc(bytes);
 
-#ifdef CONFIG_SYS_MALLOC_F_LEN
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
        if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
                /* This is harder to support and should not be needed */
                panic("pre-reloc realloc() is not supported");
@@ -1703,7 +1706,8 @@ Void_t* rEALLOc(oldmem, bytes) Void_t* oldmem; size_t bytes;
     if(oldsize - SIZE_SZ >= nb) return oldmem; /* do nothing */
     /* Must alloc, copy, free. */
     newmem = mALLOc(bytes);
-    if (newmem == 0) return 0; /* propagate failure */
+    if (!newmem)
+       return NULL; /* propagate failure */
     MALLOC_COPY(newmem, oldmem, oldsize - 2*SIZE_SZ);
     munmap_chunk(oldp);
     return newmem;
@@ -1909,6 +1913,7 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes;
   * fulfill the user's request.
   */
   if (m == NULL) {
+    size_t extra, extra2;
     /*
      * Use bytes not nb, since mALLOc internally calls request2size too, and
      * each call increases the size to allocate, to account for the header.
@@ -1917,9 +1922,27 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes;
     /* Aligned -> return it */
     if ((((unsigned long)(m)) % alignment) == 0)
       return m;
-    /* Otherwise, fail */
+    /*
+     * Otherwise, try again, requesting enough extra space to be able to
+     * acquire alignment.
+     */
     fREe(m);
-    m = NULL;
+    /* Add in extra bytes to match misalignment of unexpanded allocation */
+    extra = alignment - (((unsigned long)(m)) % alignment);
+    m  = (char*)(mALLOc(bytes + extra));
+    /*
+     * m might not be the same as before. Validate that the previous value of
+     * extra still works for the current value of m.
+     * If (!m), extra2=alignment so 
+     */
+    if (m) {
+      extra2 = alignment - (((unsigned long)(m)) % alignment);
+      if (extra2 > extra) {
+        fREe(m);
+        m = NULL;
+      }
+    }
+    /* Fall through to original NULL check and chunk splitting logic */
   }
 
   if (m == NULL) return NULL; /* propagate failure */
@@ -2055,7 +2078,7 @@ Void_t* cALLOc(n, elem_size) size_t n; size_t elem_size;
     return NULL;
   else
   {
-#ifdef CONFIG_SYS_MALLOC_F_LEN
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
        if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
                MALLOC_ZERO(mem, sz);
                return mem;
@@ -2356,9 +2379,9 @@ int mALLOPt(param_number, value) int param_number; int value;
 
 int initf_malloc(void)
 {
-#ifdef CONFIG_SYS_MALLOC_F_LEN
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
        assert(gd->malloc_base);        /* Set up by crt0.S */
-       gd->malloc_limit = CONFIG_SYS_MALLOC_F_LEN;
+       gd->malloc_limit = CONFIG_VAL(SYS_MALLOC_F_LEN);
        gd->malloc_ptr = 0;
 #endif