xref: /freebsd/libexec/rtld-elf/xmalloc.c (revision dfe296c43a26175f98ded41168e5c106edd52599)
13124c3e0SJohn Polstra /*-
23124c3e0SJohn Polstra  * Copyright 1996-1998 John D. Polstra.
33124c3e0SJohn Polstra  * All rights reserved.
43124c3e0SJohn Polstra  *
53124c3e0SJohn Polstra  * Redistribution and use in source and binary forms, with or without
63124c3e0SJohn Polstra  * modification, are permitted provided that the following conditions
73124c3e0SJohn Polstra  * are met:
83124c3e0SJohn Polstra  * 1. Redistributions of source code must retain the above copyright
93124c3e0SJohn Polstra  *    notice, this list of conditions and the following disclaimer.
103124c3e0SJohn Polstra  * 2. Redistributions in binary form must reproduce the above copyright
113124c3e0SJohn Polstra  *    notice, this list of conditions and the following disclaimer in the
123124c3e0SJohn Polstra  *    documentation and/or other materials provided with the distribution.
133124c3e0SJohn Polstra  *
143124c3e0SJohn Polstra  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
153124c3e0SJohn Polstra  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
163124c3e0SJohn Polstra  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
173124c3e0SJohn Polstra  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
183124c3e0SJohn Polstra  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
193124c3e0SJohn Polstra  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
203124c3e0SJohn Polstra  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
213124c3e0SJohn Polstra  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
223124c3e0SJohn Polstra  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
233124c3e0SJohn Polstra  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
243124c3e0SJohn Polstra  *
257f3dea24SPeter Wemm  * $FreeBSD$
263124c3e0SJohn Polstra  */
273124c3e0SJohn Polstra 
283124c3e0SJohn Polstra #include <stddef.h>
293124c3e0SJohn Polstra #include <stdlib.h>
303124c3e0SJohn Polstra #include <string.h>
310e9a2605SKonstantin Belousov #include <unistd.h>
320e9a2605SKonstantin Belousov #include "rtld.h"
330e9a2605SKonstantin Belousov #include "rtld_printf.h"
343124c3e0SJohn Polstra 
353124c3e0SJohn Polstra void *
36758ffbfaSKonstantin Belousov xcalloc(size_t number, size_t size)
373124c3e0SJohn Polstra {
38758ffbfaSKonstantin Belousov 	void *p;
39758ffbfaSKonstantin Belousov 
40758ffbfaSKonstantin Belousov 	p = calloc(number, size);
41758ffbfaSKonstantin Belousov 	if (p == NULL) {
42758ffbfaSKonstantin Belousov 		rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
43758ffbfaSKonstantin Belousov 		_exit(1);
44758ffbfaSKonstantin Belousov 	}
45758ffbfaSKonstantin Belousov 	return (p);
463124c3e0SJohn Polstra }
473124c3e0SJohn Polstra 
483124c3e0SJohn Polstra void *
493124c3e0SJohn Polstra xmalloc(size_t size)
503124c3e0SJohn Polstra {
513124c3e0SJohn Polstra     void *p = malloc(size);
520e9a2605SKonstantin Belousov     if (p == NULL) {
530e9a2605SKonstantin Belousov 	rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
540e9a2605SKonstantin Belousov 	_exit(1);
550e9a2605SKonstantin Belousov     }
563124c3e0SJohn Polstra     return p;
573124c3e0SJohn Polstra }
583124c3e0SJohn Polstra 
593124c3e0SJohn Polstra char *
60f7b34303SKonstantin Belousov xstrdup(const char *str)
613124c3e0SJohn Polstra {
62f7b34303SKonstantin Belousov 	char *copy;
63f7b34303SKonstantin Belousov 	size_t len;
64f7b34303SKonstantin Belousov 
65f7b34303SKonstantin Belousov 	len = strlen(str) + 1;
66f7b34303SKonstantin Belousov 	copy = xmalloc(len);
67f7b34303SKonstantin Belousov 	memcpy(copy, str, len);
68f7b34303SKonstantin Belousov 	return (copy);
693124c3e0SJohn Polstra }
70*dfe296c4SKonstantin Belousov 
71*dfe296c4SKonstantin Belousov void *
72*dfe296c4SKonstantin Belousov malloc_aligned(size_t size, size_t align)
73*dfe296c4SKonstantin Belousov {
74*dfe296c4SKonstantin Belousov 	void *mem, *res;
75*dfe296c4SKonstantin Belousov 	uintptr_t x;
76*dfe296c4SKonstantin Belousov 	size_t asize, r;
77*dfe296c4SKonstantin Belousov 
78*dfe296c4SKonstantin Belousov 	r = round(sizeof(void *), align);
79*dfe296c4SKonstantin Belousov 	asize = round(size, align) + r;
80*dfe296c4SKonstantin Belousov 	mem = xmalloc(asize);
81*dfe296c4SKonstantin Belousov 	x = (uintptr_t)mem;
82*dfe296c4SKonstantin Belousov 	res = (void *)round(x, align);
83*dfe296c4SKonstantin Belousov 	*(void **)((uintptr_t)res - sizeof(void *)) = mem;
84*dfe296c4SKonstantin Belousov 	return (res);
85*dfe296c4SKonstantin Belousov }
86*dfe296c4SKonstantin Belousov 
87*dfe296c4SKonstantin Belousov void
88*dfe296c4SKonstantin Belousov free_aligned(void *ptr)
89*dfe296c4SKonstantin Belousov {
90*dfe296c4SKonstantin Belousov 	void *mem;
91*dfe296c4SKonstantin Belousov 	uintptr_t x;
92*dfe296c4SKonstantin Belousov 
93*dfe296c4SKonstantin Belousov 	if (ptr == NULL)
94*dfe296c4SKonstantin Belousov 		return;
95*dfe296c4SKonstantin Belousov 	x = (uintptr_t)ptr;
96*dfe296c4SKonstantin Belousov 	x -= sizeof(void *);
97*dfe296c4SKonstantin Belousov 	mem = *(void **)x;
98*dfe296c4SKonstantin Belousov 	free(mem);
99*dfe296c4SKonstantin Belousov }
100