colibri_imx6: fix video stdout in default environment
[oweals/u-boot.git] / common / dlmalloc.c
index adc680e95938b7bb6b213f7c691e7de3cad69d1e..b29a7cfd93d3e0324ad8b4c82be2d35314debb74 100644 (file)
@@ -1,6 +1,7 @@
 #include <common.h>
+#include <log.h>
 
-#ifdef CONFIG_SANDBOX
+#if CONFIG_IS_ENABLED(UNIT_TEST)
 #define DEBUG
 #endif
 
@@ -149,7 +150,7 @@ gAllocatedSize))
                        {
                                new_address = findRegion (new_address, new_size);
 
-                               if (new_address == 0)
+                               if (!new_address)
                                        return (void*)-1;
 
                                gAddressBase = gNextAddress =
@@ -175,7 +176,7 @@ gAllocatedSize))
                                                                (size + gNextAddress -
                                                                 AlignPage (gNextAddress)),
                                                                MEM_COMMIT, PAGE_READWRITE);
-                       if (res == 0)
+                       if (!res)
                                return (void*)-1;
                }
                tmp = (void*)gNextAddress;
@@ -280,6 +281,7 @@ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
            |             Unused space (may be 0 bytes long)                .
            .                                                               .
            .                                                               |
+
 nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     `foot:' |             Size of chunk, in bytes                           |
            +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
@@ -574,6 +576,10 @@ static void malloc_bin_reloc(void)
 static inline void malloc_bin_reloc(void) {}
 #endif
 
+#ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT
+static void malloc_init(void);
+#endif
+
 ulong mem_malloc_start = 0;
 ulong mem_malloc_end = 0;
 ulong mem_malloc_brk = 0;
@@ -604,6 +610,10 @@ void mem_malloc_init(ulong start, ulong size)
        mem_malloc_end = start + size;
        mem_malloc_brk = start;
 
+#ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT
+       malloc_init();
+#endif
+
        debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start,
              mem_malloc_end);
 #ifdef CONFIG_SYS_MALLOC_CLEAR_ON_INIT
@@ -708,7 +718,36 @@ static unsigned int max_n_mmaps = 0;
 static unsigned long max_mmapped_mem = 0;
 #endif
 
+#ifdef CONFIG_SYS_MALLOC_DEFAULT_TO_INIT
+static void malloc_init(void)
+{
+       int i, j;
+
+       debug("bins (av_ array) are at %p\n", (void *)av_);
+
+       av_[0] = NULL; av_[1] = NULL;
+       for (i = 2, j = 2; i < NAV * 2 + 2; i += 2, j++) {
+               av_[i] = bin_at(j - 2);
+               av_[i + 1] = bin_at(j - 2);
+
+               /* Just print the first few bins so that
+                * we can see there are alright.
+                */
+               if (i < 10)
+                       debug("av_[%d]=%lx av_[%d]=%lx\n",
+                             i, (ulong)av_[i],
+                             i + 1, (ulong)av_[i + 1]);
+       }
 
+       /* Init the static bookkeeping as well */
+       sbrk_base = (char *)(-1);
+       max_sbrked_mem = 0;
+       max_total_mem = 0;
+#ifdef DEBUG
+       memset((void *)&current_mallinfo, 0, sizeof(struct mallinfo));
+#endif
+}
+#endif
 
 /*
   Debugging support
@@ -1051,9 +1090,6 @@ static mchunkptr mremap_chunk(p, new_size) mchunkptr p; size_t new_size;
 
 #endif /* HAVE_MMAP */
 
-
-
-
 /*
   Extend the top-most chunk by obtaining memory from system.
   Main interface to sbrk (but see also malloc_trim).
@@ -1254,7 +1290,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 +1497,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 +1558,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 +1707,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 +1718,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 +1742,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;
@@ -1887,6 +1927,12 @@ Void_t* mEMALIGn(alignment, bytes) size_t alignment; size_t bytes;
 
   if ((long)bytes < 0) return NULL;
 
+#if CONFIG_VAL(SYS_MALLOC_F_LEN)
+       if (!(gd->flags & GD_FLG_FULL_MALLOC_INIT)) {
+               return memalign_simple(alignment, bytes);
+       }
+#endif
+
   /* If need less alignment than we give anyway, just relay to malloc */
 
   if (alignment <= MALLOC_ALIGNMENT) return mALLOc(bytes);
@@ -2074,9 +2120,9 @@ 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);
+               memset(mem, 0, sz);
                return mem;
        }
 #endif
@@ -2375,9 +2421,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