13124c3e0SJohn Polstra /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3e6209940SPedro F. Giffuni *
43124c3e0SJohn Polstra * Copyright 1996-1998 John D. Polstra.
53124c3e0SJohn Polstra * All rights reserved.
63124c3e0SJohn Polstra *
73124c3e0SJohn Polstra * Redistribution and use in source and binary forms, with or without
83124c3e0SJohn Polstra * modification, are permitted provided that the following conditions
93124c3e0SJohn Polstra * are met:
103124c3e0SJohn Polstra * 1. Redistributions of source code must retain the above copyright
113124c3e0SJohn Polstra * notice, this list of conditions and the following disclaimer.
123124c3e0SJohn Polstra * 2. Redistributions in binary form must reproduce the above copyright
133124c3e0SJohn Polstra * notice, this list of conditions and the following disclaimer in the
143124c3e0SJohn Polstra * documentation and/or other materials provided with the distribution.
153124c3e0SJohn Polstra *
163124c3e0SJohn Polstra * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
173124c3e0SJohn Polstra * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
183124c3e0SJohn Polstra * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
193124c3e0SJohn Polstra * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
203124c3e0SJohn Polstra * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
213124c3e0SJohn Polstra * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223124c3e0SJohn Polstra * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
233124c3e0SJohn Polstra * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
243124c3e0SJohn Polstra * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
253124c3e0SJohn Polstra * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
263124c3e0SJohn Polstra */
273124c3e0SJohn Polstra
282f06c66aSKonstantin Belousov #include <sys/param.h>
293124c3e0SJohn Polstra #include <stddef.h>
303124c3e0SJohn Polstra #include <stdlib.h>
313124c3e0SJohn Polstra #include <string.h>
320e9a2605SKonstantin Belousov #include <unistd.h>
330e9a2605SKonstantin Belousov #include "rtld.h"
340e9a2605SKonstantin Belousov #include "rtld_printf.h"
351a3b2ebfSKonstantin Belousov #include "rtld_malloc.h"
36b54a59f3SAlex Richardson #include "rtld_libc.h"
373124c3e0SJohn Polstra
383124c3e0SJohn Polstra void *
xcalloc(size_t number,size_t size)39758ffbfaSKonstantin Belousov xcalloc(size_t number, size_t size)
403124c3e0SJohn Polstra {
41758ffbfaSKonstantin Belousov void *p;
42758ffbfaSKonstantin Belousov
431a3b2ebfSKonstantin Belousov p = __crt_calloc(number, size);
44758ffbfaSKonstantin Belousov if (p == NULL) {
45758ffbfaSKonstantin Belousov rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
46758ffbfaSKonstantin Belousov _exit(1);
47758ffbfaSKonstantin Belousov }
48758ffbfaSKonstantin Belousov return (p);
493124c3e0SJohn Polstra }
503124c3e0SJohn Polstra
513124c3e0SJohn Polstra void *
xmalloc(size_t size)523124c3e0SJohn Polstra xmalloc(size_t size)
533124c3e0SJohn Polstra {
541a3b2ebfSKonstantin Belousov
551a3b2ebfSKonstantin Belousov void *p;
561a3b2ebfSKonstantin Belousov
571a3b2ebfSKonstantin Belousov p = __crt_malloc(size);
580e9a2605SKonstantin Belousov if (p == NULL) {
590e9a2605SKonstantin Belousov rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
600e9a2605SKonstantin Belousov _exit(1);
610e9a2605SKonstantin Belousov }
621a3b2ebfSKonstantin Belousov return (p);
633124c3e0SJohn Polstra }
643124c3e0SJohn Polstra
653124c3e0SJohn Polstra char *
xstrdup(const char * str)66f7b34303SKonstantin Belousov xstrdup(const char *str)
673124c3e0SJohn Polstra {
68f7b34303SKonstantin Belousov char *copy;
69f7b34303SKonstantin Belousov size_t len;
70f7b34303SKonstantin Belousov
71f7b34303SKonstantin Belousov len = strlen(str) + 1;
72f7b34303SKonstantin Belousov copy = xmalloc(len);
73f7b34303SKonstantin Belousov memcpy(copy, str, len);
74f7b34303SKonstantin Belousov return (copy);
753124c3e0SJohn Polstra }
76dfe296c4SKonstantin Belousov
77dfe296c4SKonstantin Belousov void *
xmalloc_aligned(size_t size,size_t align,size_t offset)78*feaae6baSKonstantin Belousov xmalloc_aligned(size_t size, size_t align, size_t offset)
79dfe296c4SKonstantin Belousov {
80*feaae6baSKonstantin Belousov void *res;
81dfe296c4SKonstantin Belousov
822f06c66aSKonstantin Belousov offset &= align - 1;
83eab68f79SDavid Xu if (align < sizeof(void *))
84eab68f79SDavid Xu align = sizeof(void *);
85209782e0SDavid Xu
86*feaae6baSKonstantin Belousov res = __crt_aligned_alloc_offset(align, size, offset);
87*feaae6baSKonstantin Belousov if (res == NULL) {
88*feaae6baSKonstantin Belousov rtld_fdputstr(STDERR_FILENO, "Out of memory\n");
89*feaae6baSKonstantin Belousov _exit(1);
90dfe296c4SKonstantin Belousov }
91*feaae6baSKonstantin Belousov return (res);
92dfe296c4SKonstantin Belousov }
93