The open source OpenXR runtime
at prediction-2 174 lines 4.5 kB view raw
1/* alloc.c -- Memory allocation without mmap. 2 Copyright (C) 2012-2021 Free Software Foundation, Inc. 3 Written by Ian Lance Taylor, Google. 4 5Redistribution and use in source and binary forms, with or without 6modification, are permitted provided that the following conditions are 7met: 8 9 (1) Redistributions of source code must retain the above copyright 10 notice, this list of conditions and the following disclaimer. 11 12 (2) Redistributions in binary form must reproduce the above copyright 13 notice, this list of conditions and the following disclaimer in 14 the documentation and/or other materials provided with the 15 distribution. 16 17 (3) The name of the author may not be used to 18 endorse or promote products derived from this software without 19 specific prior written permission. 20 21THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 25INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 29STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 30IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31POSSIBILITY OF SUCH DAMAGE. */ 32 33#include "config.h" 34 35#include <errno.h> 36#include <stdlib.h> 37#include <sys/types.h> 38 39#include "backtrace.hpp" 40#include "internal.hpp" 41 42#include "../common/TracyAlloc.hpp" 43 44namespace tracy 45{ 46 47/* Allocation routines to use on systems that do not support anonymous 48 mmap. This implementation just uses malloc, which means that the 49 backtrace functions may not be safely invoked from a signal 50 handler. */ 51 52/* Allocate memory like malloc. If ERROR_CALLBACK is NULL, don't 53 report an error. */ 54 55void * 56backtrace_alloc (struct backtrace_state *state ATTRIBUTE_UNUSED, 57 size_t size, backtrace_error_callback error_callback, 58 void *data) 59{ 60 void *ret; 61 62 ret = tracy_malloc (size); 63 if (ret == NULL) 64 { 65 if (error_callback) 66 error_callback (data, "malloc", errno); 67 } 68 return ret; 69} 70 71/* Free memory. */ 72 73void 74backtrace_free (struct backtrace_state *state ATTRIBUTE_UNUSED, 75 void *p, size_t size ATTRIBUTE_UNUSED, 76 backtrace_error_callback error_callback ATTRIBUTE_UNUSED, 77 void *data ATTRIBUTE_UNUSED) 78{ 79 tracy_free (p); 80} 81 82/* Grow VEC by SIZE bytes. */ 83 84void * 85backtrace_vector_grow (struct backtrace_state *state ATTRIBUTE_UNUSED, 86 size_t size, backtrace_error_callback error_callback, 87 void *data, struct backtrace_vector *vec) 88{ 89 void *ret; 90 91 if (size > vec->alc) 92 { 93 size_t alc; 94 void *base; 95 96 if (vec->size == 0) 97 alc = 32 * size; 98 else if (vec->size >= 4096) 99 alc = vec->size + 4096; 100 else 101 alc = 2 * vec->size; 102 103 if (alc < vec->size + size) 104 alc = vec->size + size; 105 106 base = tracy_realloc (vec->base, alc); 107 if (base == NULL) 108 { 109 error_callback (data, "realloc", errno); 110 return NULL; 111 } 112 113 vec->base = base; 114 vec->alc = alc - vec->size; 115 } 116 117 ret = (char *) vec->base + vec->size; 118 vec->size += size; 119 vec->alc -= size; 120 return ret; 121} 122 123/* Finish the current allocation on VEC. */ 124 125void * 126backtrace_vector_finish (struct backtrace_state *state, 127 struct backtrace_vector *vec, 128 backtrace_error_callback error_callback, 129 void *data) 130{ 131 void *ret; 132 133 /* With this allocator we call realloc in backtrace_vector_grow, 134 which means we can't easily reuse the memory here. So just 135 release it. */ 136 if (!backtrace_vector_release (state, vec, error_callback, data)) 137 return NULL; 138 ret = vec->base; 139 vec->base = NULL; 140 vec->size = 0; 141 vec->alc = 0; 142 return ret; 143} 144 145/* Release any extra space allocated for VEC. */ 146 147int 148backtrace_vector_release (struct backtrace_state *state ATTRIBUTE_UNUSED, 149 struct backtrace_vector *vec, 150 backtrace_error_callback error_callback, 151 void *data) 152{ 153 vec->alc = 0; 154 155 if (vec->size == 0) 156 { 157 /* As of C17, realloc with size 0 is marked as an obsolescent feature, use 158 free instead. */ 159 tracy_free (vec->base); 160 vec->base = NULL; 161 return 1; 162 } 163 164 vec->base = tracy_realloc (vec->base, vec->size); 165 if (vec->base == NULL) 166 { 167 error_callback (data, "realloc", errno); 168 return 0; 169 } 170 171 return 1; 172} 173 174}