OSDN Git Service

fix static linking of pthread apps
[uclinux-h8/uclibc-ng.git] / libc / stdlib / malloc-simple / alloc.c
index 45faf62..a3c068a 100644 (file)
 #include <sys/mman.h>
 #include <malloc.h>
 
-/* Experimentally off - libc_hidden_proto(memcpy) */
-/*libc_hidden_proto(memset)*/
-libc_hidden_proto(mmap)
-libc_hidden_proto(munmap)
+extern int weak_function __libc_free_aligned(void *ptr) attribute_hidden;
 
 #ifdef L_malloc
 void *malloc(size_t size)
@@ -40,13 +37,15 @@ void *malloc(size_t size)
 #ifdef __ARCH_USE_MMU__
 # define MMAP_FLAGS MAP_PRIVATE | MAP_ANONYMOUS
 #else
-# define MMAP_FLAGS MAP_SHARED | MAP_ANONYMOUS
+# define MMAP_FLAGS MAP_SHARED | MAP_ANONYMOUS | MAP_UNINITIALIZED
 #endif
 
        result = mmap((void *) 0, size + sizeof(size_t), PROT_READ | PROT_WRITE,
                      MMAP_FLAGS, 0, 0);
-       if (result == MAP_FAILED)
+       if (result == MAP_FAILED) {
+               __set_errno(ENOMEM);
                return 0;
+       }
        * (size_t *) result = size;
        return(result + sizeof(size_t));
 }
@@ -64,11 +63,10 @@ void * calloc(size_t nmemb, size_t lsize)
                __set_errno(ENOMEM);
                return NULL;
        }
-       result=malloc(size);
-#if 0
-       /* Standard unix mmap using /dev/zero clears memory so calloc
-        * doesn't need to actually zero anything....
-        */
+       result = malloc(size);
+
+#ifndef __ARCH_USE_MMU__
+       /* mmap'd with MAP_UNINITIALIZED, we have to blank memory ourselves */
        if (result != NULL) {
                memset(result, 0, size);
        }
@@ -100,7 +98,6 @@ void *realloc(void *ptr, size_t size)
 #endif
 
 #ifdef L_free
-extern int weak_function __libc_free_aligned(void *ptr);
 void free(void *ptr)
 {
        if (unlikely(ptr == NULL))
@@ -128,7 +125,7 @@ struct alignlist
        __ptr_t aligned;        /* The address that memaligned returned.  */
        __ptr_t exact;  /* The address that malloc returned.  */
 };
-struct alignlist *_aligned_blocks;
+static struct alignlist *_aligned_blocks;
 
 /* Return memory to the heap. */
 int __libc_free_aligned(void *ptr)
@@ -187,4 +184,5 @@ DONE:
 
        return result;
 }
+libc_hidden_def(memalign)
 #endif