/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright 1996-1998 John D. Polstra.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR16* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES17* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.18* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,19* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT20* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,21* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY22* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT23* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF24* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.25*/2627#include <sys/param.h>28#include <stddef.h>29#include <stdlib.h>30#include <string.h>31#include <unistd.h>32#include "rtld.h"33#include "rtld_printf.h"34#include "rtld_malloc.h"35#include "rtld_libc.h"3637void *38xcalloc(size_t number, size_t size)39{40void *p;4142p = __crt_calloc(number, size);43if (p == NULL) {44rtld_fdputstr(STDERR_FILENO, "Out of memory\n");45_exit(1);46}47return (p);48}4950void *51xmalloc(size_t size)52{5354void *p;5556p = __crt_malloc(size);57if (p == NULL) {58rtld_fdputstr(STDERR_FILENO, "Out of memory\n");59_exit(1);60}61return (p);62}6364char *65xstrdup(const char *str)66{67char *copy;68size_t len;6970len = strlen(str) + 1;71copy = xmalloc(len);72memcpy(copy, str, len);73return (copy);74}7576void *77xmalloc_aligned(size_t size, size_t align, size_t offset)78{79void *res;8081offset &= align - 1;82if (align < sizeof(void *))83align = sizeof(void *);8485res = __crt_aligned_alloc_offset(align, size, offset);86if (res == NULL) {87rtld_fdputstr(STDERR_FILENO, "Out of memory\n");88_exit(1);89}90return (res);91}929394