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