/* alloc.c -- Memory allocation without mmap.1Copyright (C) 2012-2021 Free Software Foundation, Inc.2Written by Ian Lance Taylor, Google.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are6met:78(1) Redistributions of source code must retain the above copyright9notice, this list of conditions and the following disclaimer.1011(2) Redistributions in binary form must reproduce the above copyright12notice, this list of conditions and the following disclaimer in13the documentation and/or other materials provided with the14distribution.1516(3) The name of the author may not be used to17endorse or promote products derived from this software without18specific prior written permission.1920THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR21IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,24INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES25(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR26SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,28STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING29IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE30POSSIBILITY OF SUCH DAMAGE. */3132#include "config.h"3334#include <errno.h>35#include <stdlib.h>36#include <sys/types.h>3738#include "backtrace.h"39#include "internal.h"4041/* Allocation routines to use on systems that do not support anonymous42mmap. This implementation just uses malloc, which means that the43backtrace functions may not be safely invoked from a signal44handler. */4546/* Allocate memory like malloc. If ERROR_CALLBACK is NULL, don't47report an error. */4849void *50backtrace_alloc (struct backtrace_state *state ATTRIBUTE_UNUSED,51size_t size, backtrace_error_callback error_callback,52void *data)53{54void *ret;5556ret = malloc (size);57if (ret == NULL)58{59if (error_callback)60error_callback (data, "malloc", errno);61}62return ret;63}6465/* Free memory. */6667void68backtrace_free (struct backtrace_state *state ATTRIBUTE_UNUSED,69void *p, size_t size ATTRIBUTE_UNUSED,70backtrace_error_callback error_callback ATTRIBUTE_UNUSED,71void *data ATTRIBUTE_UNUSED)72{73free (p);74}7576/* Grow VEC by SIZE bytes. */7778void *79backtrace_vector_grow (struct backtrace_state *state ATTRIBUTE_UNUSED,80size_t size, backtrace_error_callback error_callback,81void *data, struct backtrace_vector *vec)82{83void *ret;8485if (size > vec->alc)86{87size_t alc;88void *base;8990if (vec->size == 0)91alc = 32 * size;92else if (vec->size >= 4096)93alc = vec->size + 4096;94else95alc = 2 * vec->size;9697if (alc < vec->size + size)98alc = vec->size + size;99100base = realloc (vec->base, alc);101if (base == NULL)102{103error_callback (data, "realloc", errno);104return NULL;105}106107vec->base = base;108vec->alc = alc - vec->size;109}110111ret = (char *) vec->base + vec->size;112vec->size += size;113vec->alc -= size;114return ret;115}116117/* Finish the current allocation on VEC. */118119void *120backtrace_vector_finish (struct backtrace_state *state,121struct backtrace_vector *vec,122backtrace_error_callback error_callback,123void *data)124{125void *ret;126127/* With this allocator we call realloc in backtrace_vector_grow,128which means we can't easily reuse the memory here. So just129release it. */130if (!backtrace_vector_release (state, vec, error_callback, data))131return NULL;132ret = vec->base;133vec->base = NULL;134vec->size = 0;135vec->alc = 0;136return ret;137}138139/* Release any extra space allocated for VEC. */140141int142backtrace_vector_release (struct backtrace_state *state ATTRIBUTE_UNUSED,143struct backtrace_vector *vec,144backtrace_error_callback error_callback,145void *data)146{147vec->alc = 0;148149if (vec->size == 0)150{151/* As of C17, realloc with size 0 is marked as an obsolescent feature, use152free instead. */153free (vec->base);154vec->base = NULL;155return 1;156}157158vec->base = realloc (vec->base, vec->size);159if (vec->base == NULL)160{161error_callback (data, "realloc", errno);162return 0;163}164165return 1;166}167168169