1 /* alloc.c
2  *
3  * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
4  *
5  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6  */
7 /*
8  * Parts of the memalign code were stolen from malloc-930716.
9  */
10 
11 #include <features.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdint.h>
17 #include <errno.h>
18 #include <sys/mman.h>
19 #include <malloc.h>
20 
21 extern int weak_function __libc_free_aligned(void *ptr) attribute_hidden;
22 
23 #ifdef L_malloc
malloc(size_t size)24 void *malloc(size_t size)
25 {
26 	void *result;
27 
28 	if (unlikely(size == 0)) {
29 #if defined(__MALLOC_GLIBC_COMPAT__)
30 		size++;
31 #else
32 		/* Some programs will call malloc (0).  Lets be strict and return NULL */
33 		__set_errno(ENOMEM);
34 		return NULL;
35 #endif
36 	}
37 
38 	/* prevent Undefined Behaviour for pointer arithmetic (substract) of too big pointers
39 	 * see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63303
40 	 * No need to check for size + sizeof(size_t) integer overflow since we already check for PTRDIFF_MAX
41 	 */
42 	if (unlikely(size > PTRDIFF_MAX)) {
43 		__set_errno(ENOMEM);
44 		return 0;
45 	}
46 
47 #ifdef __ARCH_USE_MMU__
48 # define MMAP_FLAGS MAP_PRIVATE | MAP_ANONYMOUS
49 #else
50 # define MMAP_FLAGS MAP_SHARED | MAP_ANONYMOUS | MAP_UNINITIALIZED
51 #endif
52 
53 	result = mmap((void *) 0, size + sizeof(size_t), PROT_READ | PROT_WRITE,
54 	              MMAP_FLAGS, 0, 0);
55 	if (result == MAP_FAILED)
56 		return 0;
57 	* (size_t *) result = size;
58 	return(result + sizeof(size_t));
59 }
60 #endif
61 
62 #ifdef L_calloc
calloc(size_t nmemb,size_t lsize)63 void * calloc(size_t nmemb, size_t lsize)
64 {
65 	void *result;
66 	size_t size=lsize * nmemb;
67 
68 	/* guard vs integer overflow, but allow nmemb
69 	 * to fall through and call malloc(0) */
70 	if (nmemb && lsize != (size / nmemb)) {
71 		__set_errno(ENOMEM);
72 		return NULL;
73 	}
74 	result = malloc(size);
75 
76 #ifndef __ARCH_USE_MMU__
77 	/* mmap'd with MAP_UNINITIALIZED, we have to blank memory ourselves */
78 	if (result != NULL) {
79 		memset(result, 0, size);
80 	}
81 #endif
82 	return result;
83 }
84 #endif
85 
86 #ifdef L_realloc
realloc(void * ptr,size_t size)87 void *realloc(void *ptr, size_t size)
88 {
89 	void *newptr = NULL;
90 
91 	if (!ptr)
92 		return malloc(size);
93 	if (!size) {
94 		free(ptr);
95 		return malloc(0);
96 	}
97 
98 	newptr = malloc(size);
99 	if (newptr) {
100 		size_t old_size = *((size_t *) (ptr - sizeof(size_t)));
101 		memcpy(newptr, ptr, (old_size < size ? old_size : size));
102 		free(ptr);
103 	}
104 	return newptr;
105 }
106 #endif
107 
108 #ifdef L_free
free(void * ptr)109 void free(void *ptr)
110 {
111 	if (unlikely(ptr == NULL))
112 		return;
113 	if (unlikely(__libc_free_aligned != NULL)) {
114 		if (__libc_free_aligned(ptr))
115 			return;
116 	}
117 	ptr -= sizeof(size_t);
118 	munmap(ptr, * (size_t *) ptr + sizeof(size_t));
119 }
120 #endif
121 
122 #ifdef L_memalign
123 
124 #include <bits/uClibc_mutex.h>
125 __UCLIBC_MUTEX_INIT(__malloc_lock, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
126 #define __MALLOC_LOCK		__UCLIBC_MUTEX_LOCK(__malloc_lock)
127 #define __MALLOC_UNLOCK		__UCLIBC_MUTEX_UNLOCK(__malloc_lock)
128 
129 /* List of blocks allocated with memalign or valloc */
130 struct alignlist
131 {
132 	struct alignlist *next;
133 	__ptr_t aligned;	/* The address that memaligned returned.  */
134 	__ptr_t exact;	/* The address that malloc returned.  */
135 };
136 static struct alignlist *_aligned_blocks;
137 
138 /* Return memory to the heap. */
__libc_free_aligned(void * ptr)139 int __libc_free_aligned(void *ptr)
140 {
141 	struct alignlist *l;
142 
143 	if (ptr == NULL)
144 		return 0;
145 
146 	__MALLOC_LOCK;
147 	for (l = _aligned_blocks; l != NULL; l = l->next) {
148 		if (l->aligned == ptr) {
149 			/* Mark the block as free */
150 			l->aligned = NULL;
151 			ptr = l->exact;
152 			ptr -= sizeof(size_t);
153 			munmap(ptr, * (size_t *) ptr + sizeof(size_t));
154 			return 1;
155 		}
156 	}
157 	__MALLOC_UNLOCK;
158 	return 0;
159 }
memalign(size_t alignment,size_t size)160 void * memalign (size_t alignment, size_t size)
161 {
162 	void * result;
163 	unsigned long int adj;
164 
165 	if (unlikely(size > PTRDIFF_MAX)) {
166 		__set_errno(ENOMEM);
167 		return NULL;
168 	}
169 
170 	if (unlikely((size + alignment - 1 < size) && (alignment != 0))) {
171 		__set_errno(ENOMEM);
172 		return NULL;
173 	}
174 
175 	result = malloc (size + alignment - 1);
176 	if (result == NULL)
177 		return NULL;
178 
179 	adj = (unsigned long int) ((unsigned long int) ((char *) result - (char *) NULL)) % alignment;
180 	if (adj != 0) {
181 		struct alignlist *l;
182 		__MALLOC_LOCK;
183 		for (l = _aligned_blocks; l != NULL; l = l->next)
184 			if (l->aligned == NULL)
185 				/* This slot is free.  Use it.  */
186 				break;
187 		if (l == NULL) {
188 			l = (struct alignlist *) malloc (sizeof (struct alignlist));
189 			if (l == NULL) {
190 				free(result);
191 				result = NULL;
192 				goto DONE;
193 			}
194 			l->next = _aligned_blocks;
195 			_aligned_blocks = l;
196 		}
197 		l->exact = result;
198 		result = l->aligned = (char *) result + alignment - adj;
199 DONE:
200 		__MALLOC_UNLOCK;
201 	}
202 
203 	return result;
204 }
205 libc_hidden_def(memalign)
206 #endif
207