colibri_imx6: fix video stdout in default environment
[oweals/u-boot.git] / include / malloc.h
index 08469bc1bc427ee39d053cea031dbe58c03a2172..f66c2e86176c3bda6eba8359d2c5d4ac15baf115 100644 (file)
@@ -6,8 +6,8 @@
 * VERSION 2.6.6  Sun Mar  5 19:10:03 2000  Doug Lea  (dl at gee)
 
    Note: There may be an updated version of this malloc obtainable at
-           ftp://g.oswego.edu/pub/misc/malloc.c
-         Check before installing!
+          ftp://g.oswego.edu/pub/misc/malloc.c
+        Check before installing!
 
 * Why use this malloc?
 
@@ -84,7 +84,7 @@
        and status information.
 
   Minimum allocated size: 4-byte ptrs:  16 bytes    (including 4 overhead)
-                          8-byte ptrs:  24/32 bytes (including, 4/8 overhead)
+                         8-byte ptrs:  24/32 bytes (including, 4/8 overhead)
 
        When a chunk is freed, 12 (for 4byte ptrs) or 20 (for 8 byte
        ptrs but 4 byte size) or 24 (for 8/8) additional bytes are
@@ -96,7 +96,7 @@
        pointer to something of the minimum allocatable size.
 
   Maximum allocated size: 4-byte size_t: 2^31 -  8 bytes
-                          8-byte size_t: 2^63 - 16 bytes
+                         8-byte size_t: 2^63 - 16 bytes
 
        It is assumed that (possibly signed) size_t bit values suffice to
        represent chunk sizes. `Possibly signed' is due to the fact
        make the normal worst-case wastage 15 bytes (i.e., up to 15
        more bytes will be allocated than were requested in malloc), with
        two exceptions:
-         1. Because requests for zero bytes allocate non-zero space,
-            the worst case wastage for a request of zero bytes is 24 bytes.
-         2. For requests >= mmap_threshold that are serviced via
-            mmap(), the worst case wastage is 8 bytes plus the remainder
-            from a system page (the minimal mmap unit); typically 4096 bytes.
+        1. Because requests for zero bytes allocate non-zero space,
+           the worst case wastage for a request of zero bytes is 24 bytes.
+        2. For requests >= mmap_threshold that are serviced via
+           mmap(), the worst case wastage is 8 bytes plus the remainder
+           from a system page (the minimal mmap unit); typically 4096 bytes.
 
 * Limitations
 
   MORECORE_FAILURE          (default: -1)
      The value returned upon failure of MORECORE.
   MORECORE_CLEARS           (default 1)
-     True (1) if the routine mapped to MORECORE zeroes out memory (which
+     true (1) if the routine mapped to MORECORE zeroes out memory (which
      holds for sbrk).
   DEFAULT_TRIM_THRESHOLD
   DEFAULT_TOP_PAD
 */
 
 \f
-
+#ifndef __MALLOC_H__
+#define __MALLOC_H__
 
 /* Preliminaries */
 
@@ -284,14 +285,6 @@ extern "C" {
 
 */
 
-#ifdef DEBUG
-/* #include <assert.h> */
-#define assert(x) ((void)0)
-#else
-#define assert(x) ((void)0)
-#endif
-
-
 /*
   INTERNAL_SIZE_T is the word-size used for internal bookkeeping
   of chunk sizes. On a 64-bit machine, you can reduce malloc
@@ -372,8 +365,8 @@ void* memset(void*, int, size_t);
 void* memcpy(void*, const void*, size_t);
 #else
 #ifdef WIN32
-// On Win32 platforms, 'memset()' and 'memcpy()' are already declared in
-// 'windows.h'
+/* On Win32 platforms, 'memset()' and 'memcpy()' are already declared in */
+/* 'windows.h' */
 #else
 Void_t* memset();
 Void_t* memcpy();
@@ -393,14 +386,14 @@ do {                                                                          \
   if(mzsz <= 9*sizeof(mzsz)) {                                                \
     INTERNAL_SIZE_T* mz = (INTERNAL_SIZE_T*) (charp);                         \
     if(mzsz >= 5*sizeof(mzsz)) {     *mz++ = 0;                               \
-                                     *mz++ = 0;                               \
+                                    *mz++ = 0;                               \
       if(mzsz >= 7*sizeof(mzsz)) {   *mz++ = 0;                               \
-                                     *mz++ = 0;                               \
-        if(mzsz >= 9*sizeof(mzsz)) { *mz++ = 0;                               \
-                                     *mz++ = 0; }}}                           \
-                                     *mz++ = 0;                               \
-                                     *mz++ = 0;                               \
-                                     *mz   = 0;                               \
+                                    *mz++ = 0;                               \
+       if(mzsz >= 9*sizeof(mzsz)) { *mz++ = 0;                               \
+                                    *mz++ = 0; }}}                           \
+                                    *mz++ = 0;                               \
+                                    *mz++ = 0;                               \
+                                    *mz   = 0;                               \
   } else memset((charp), 0, mzsz);                                            \
 } while(0)
 
@@ -411,14 +404,14 @@ do {                                                                          \
     INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) (src);                        \
     INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) (dest);                       \
     if(mcsz >= 5*sizeof(mcsz)) {     *mcdst++ = *mcsrc++;                     \
-                                     *mcdst++ = *mcsrc++;                     \
+                                    *mcdst++ = *mcsrc++;                     \
       if(mcsz >= 7*sizeof(mcsz)) {   *mcdst++ = *mcsrc++;                     \
-                                     *mcdst++ = *mcsrc++;                     \
-        if(mcsz >= 9*sizeof(mcsz)) { *mcdst++ = *mcsrc++;                     \
-                                     *mcdst++ = *mcsrc++; }}}                 \
-                                     *mcdst++ = *mcsrc++;                     \
-                                     *mcdst++ = *mcsrc++;                     \
-                                     *mcdst   = *mcsrc  ;                     \
+                                    *mcdst++ = *mcsrc++;                     \
+       if(mcsz >= 9*sizeof(mcsz)) { *mcdst++ = *mcsrc++;                     \
+                                    *mcdst++ = *mcsrc++; }}}                 \
+                                    *mcdst++ = *mcsrc++;                     \
+                                    *mcdst++ = *mcsrc++;                     \
+                                    *mcdst   = *mcsrc  ;                     \
   } else memcpy(dest, src, mcsz);                                             \
 } while(0)
 
@@ -494,7 +487,7 @@ do {                                                                          \
 ***/
 #undef HAVE_MREMAP     /* Not available for U-Boot */
 
-#if HAVE_MMAP
+#ifdef HAVE_MMAP
 
 #include <unistd.h>
 #include <fcntl.h>
@@ -567,7 +560,6 @@ do {                                                                          \
 #endif
 
 
-
 /*
 
   This version of malloc supports the standard SVID/XPG mallinfo
@@ -595,7 +587,7 @@ do {                                                                          \
 
 /* #define HAVE_USR_INCLUDE_MALLOC_H */
 
-#if HAVE_USR_INCLUDE_MALLOC_H
+#ifdef HAVE_USR_INCLUDE_MALLOC_H
 #include "/usr/include/malloc.h"
 #else
 
@@ -631,7 +623,6 @@ struct mallinfo {
 #define M_MMAP_MAX          -4
 
 
-
 #ifndef DEFAULT_TRIM_THRESHOLD
 #define DEFAULT_TRIM_THRESHOLD (128 * 1024)
 #endif
@@ -695,11 +686,11 @@ struct mallinfo {
       retain whenever sbrk is called. It is used in two ways internally:
 
       * When sbrk is called to extend the top of the arena to satisfy
-        a new malloc request, this much padding is added to the sbrk
-        request.
+       a new malloc request, this much padding is added to the sbrk
+       request.
 
       * When malloc_trim is called automatically from free(),
-        it is used as the `pad' argument.
+       it is used as the `pad' argument.
 
       In both cases, the actual amount of padding is rounded
       so that the end of the arena is always a system page boundary.
@@ -745,15 +736,15 @@ struct mallinfo {
 
       However, it has the disadvantages that:
 
-         1. The space cannot be reclaimed, consolidated, and then
-            used to service later requests, as happens with normal chunks.
-         2. It can lead to more wastage because of mmap page alignment
-            requirements
-         3. It causes malloc performance to be more dependent on host
-            system memory management support routines which may vary in
-            implementation quality and may impose arbitrary
-            limitations. Generally, servicing a request via normal
-            malloc steps is faster than going through a system's mmap.
+        1. The space cannot be reclaimed, consolidated, and then
+           used to service later requests, as happens with normal chunks.
+        2. It can lead to more wastage because of mmap page alignment
+           requirements
+        3. It causes malloc performance to be more dependent on host
+           system memory management support routines which may vary in
+           implementation quality and may impose arbitrary
+           limitations. Generally, servicing a request via normal
+           malloc steps is faster than going through a system's mmap.
 
       All together, these considerations should lead you to use mmap
       only for relatively large requests.
@@ -762,9 +753,8 @@ struct mallinfo {
 */
 
 
-
 #ifndef DEFAULT_MMAP_MAX
-#if HAVE_MMAP
+#ifdef HAVE_MMAP
 #define DEFAULT_MMAP_MAX       (64)
 #else
 #define DEFAULT_MMAP_MAX       (0)
@@ -775,15 +765,15 @@ struct mallinfo {
     M_MMAP_MAX is the maximum number of requests to simultaneously
       service using mmap. This parameter exists because:
 
-         1. Some systems have a limited number of internal tables for
-            use by mmap.
-         2. In most systems, overreliance on mmap can degrade overall
-            performance.
-         3. If a program allocates many large regions, it is probably
-            better off using normal sbrk-based allocation routines that
-            can reclaim and reallocate normal heap memory. Using a
-            small value allows transition into this mode after the
-            first few allocations.
+        1. Some systems have a limited number of internal tables for
+           use by mmap.
+        2. In most systems, overreliance on mmap can degrade overall
+           performance.
+        3. If a program allocates many large regions, it is probably
+           better off using normal sbrk-based allocation routines that
+           can reclaim and reallocate normal heap memory. Using a
+           small value allows transition into this mode after the
+           first few allocations.
 
       Setting to 0 disables all use of mmap.  If HAVE_MMAP is not set,
       the default value is 0, and attempts to set it to non-zero values
@@ -791,8 +781,6 @@ struct mallinfo {
 */
 
 
-
-
 /*
     USE_DL_PREFIX will prefix all public routines with the string 'dl'.
       Useful to quickly avoid procedure declaration conflicts and linker
@@ -800,10 +788,13 @@ struct mallinfo {
 
 */
 
-/* #define USE_DL_PREFIX */
-
-
-
+/*
+ * Rename the U-Boot alloc functions so that sandbox can still use the system
+ * ones
+ */
+#ifdef CONFIG_SANDBOX
+#define USE_DL_PREFIX
+#endif
 
 /*
 
@@ -886,33 +877,66 @@ extern Void_t*     sbrk();
 
 #else
 
-#ifdef USE_DL_PREFIX
-#define cALLOc         dlcalloc
-#define fREe           dlfree
-#define mALLOc         dlmalloc
-#define mEMALIGn       dlmemalign
-#define rEALLOc                dlrealloc
-#define vALLOc         dlvalloc
-#define pvALLOc                dlpvalloc
-#define mALLINFo       dlmallinfo
-#define mALLOPt                dlmallopt
-#else /* USE_DL_PREFIX */
-#define cALLOc         calloc
-#define fREe           free
-#define mALLOc         malloc
-#define mEMALIGn       memalign
-#define rEALLOc                realloc
-#define vALLOc         valloc
-#define pvALLOc                pvalloc
-#define mALLINFo       mallinfo
-#define mALLOPt                mallopt
-#endif /* USE_DL_PREFIX */
+#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
+#define malloc malloc_simple
+#define realloc realloc_simple
+#define memalign memalign_simple
+static inline void free(void *ptr) {}
+void *calloc(size_t nmemb, size_t size);
+void *realloc_simple(void *ptr, size_t size);
+void malloc_simple_info(void);
+#else
 
-#endif
+# ifdef USE_DL_PREFIX
+# define cALLOc                dlcalloc
+# define fREe          dlfree
+# define mALLOc                dlmalloc
+# define mEMALIGn      dlmemalign
+# define rEALLOc               dlrealloc
+# define vALLOc                dlvalloc
+# define pvALLOc               dlpvalloc
+# define mALLINFo      dlmallinfo
+# define mALLOPt               dlmallopt
+
+/* Ensure that U-Boot actually uses these too */
+#define calloc dlcalloc
+#define free(ptr) dlfree(ptr)
+#define malloc(x) dlmalloc(x)
+#define memalign dlmemalign
+#define realloc dlrealloc
+#define valloc dlvalloc
+#define pvalloc dlpvalloc
+#define mallinfo() dlmallinfo()
+#define mallopt dlmallopt
+#define malloc_trim dlmalloc_trim
+#define malloc_usable_size dlmalloc_usable_size
+#define malloc_stats dlmalloc_stats
+
+# else /* USE_DL_PREFIX */
+# define cALLOc                calloc
+# define fREe          free
+# define mALLOc                malloc
+# define mEMALIGn      memalign
+# define rEALLOc               realloc
+# define vALLOc                valloc
+# define pvALLOc               pvalloc
+# define mALLINFo      mallinfo
+# define mALLOPt               mallopt
+# endif /* USE_DL_PREFIX */
+
+#endif
+
+/* Set up pre-relocation malloc() ready for use */
+int initf_malloc(void);
 
 /* Public routines */
 
-#if __STD_C
+/* Simple versions which can be used when space is tight */
+void *malloc_simple(size_t size);
+void *memalign_simple(size_t alignment, size_t bytes);
+
+#pragma GCC visibility push(hidden)
+# if __STD_C
 
 Void_t* mALLOc(size_t);
 void    fREe(Void_t*);
@@ -927,7 +951,7 @@ size_t  malloc_usable_size(Void_t*);
 void    malloc_stats(void);
 int     mALLOPt(int, int);
 struct mallinfo mALLINFo(void);
-#else
+# else
 Void_t* mALLOc();
 void    fREe();
 Void_t* rEALLOc();
@@ -941,9 +965,21 @@ size_t  malloc_usable_size();
 void    malloc_stats();
 int     mALLOPt();
 struct mallinfo mALLINFo();
+# endif
 #endif
+#pragma GCC visibility pop
 
+/*
+ * Begin and End of memory area for malloc(), and current "brk"
+ */
+extern ulong mem_malloc_start;
+extern ulong mem_malloc_end;
+extern ulong mem_malloc_brk;
+
+void mem_malloc_init(ulong start, ulong size);
 
 #ifdef __cplusplus
 };  /* end of extern "C" */
 #endif
+
+#endif /* __MALLOC_H__ */