OSDN Git Service

fix static linking of pthread apps
[uclinux-h8/uclibc-ng.git] / libc / stdlib / malloc-simple / alloc.c
index 0b84207..a3c068a 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
 #include <errno.h>
 #include <sys/mman.h>
+#include <malloc.h>
 
-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)
@@ -32,20 +29,23 @@ void *malloc(size_t size)
                size++;
 #else
                /* Some programs will call malloc (0).  Lets be strict and return NULL */
-               return 0;
+               __set_errno(ENOMEM);
+               return NULL;
 #endif
        }
 
 #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));
 }
@@ -63,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);
        }
@@ -90,7 +89,8 @@ void *realloc(void *ptr, size_t size)
 
        newptr = malloc(size);
        if (newptr) {
-               memcpy(newptr, ptr, *((size_t *) (ptr - sizeof(size_t))));
+               size_t old_size = *((size_t *) (ptr - sizeof(size_t)));
+               memcpy(newptr, ptr, (old_size < size ? old_size : size));
                free(ptr);
        }
        return newptr;
@@ -98,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))
@@ -115,7 +114,7 @@ void free(void *ptr)
 #ifdef L_memalign
 
 #include <bits/uClibc_mutex.h>
-__UCLIBC_MUTEX_STATIC(__malloc_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
+__UCLIBC_MUTEX_INIT(__malloc_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
 #define __MALLOC_LOCK          __UCLIBC_MUTEX_LOCK(__malloc_lock)
 #define __MALLOC_UNLOCK                __UCLIBC_MUTEX_UNLOCK(__malloc_lock)
 
@@ -126,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)
@@ -185,4 +184,5 @@ DONE:
 
        return result;
 }
+libc_hidden_def(memalign)
 #endif