1/* 2 * nghttp3 3 * 4 * Copyright (c) 2019 nghttp3 contributors 5 * Copyright (c) 2017 ngtcp2 contributors 6 * Copyright (c) 2014 nghttp2 contributors 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining 9 * a copy of this software and associated documentation files (the 10 * "Software"), to deal in the Software without restriction, including 11 * without limitation the rights to use, copy, modify, merge, publish, 12 * distribute, sublicense, and/or sell copies of the Software, and to 13 * permit persons to whom the Software is furnished to do so, subject to 14 * the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be 17 * included in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 */ 27#include "nghttp3_mem.h" 28 29#include <stdio.h> 30 31static void *default_malloc(size_t size, void *user_data) { 32 (void)user_data; 33 34 return malloc(size); 35} 36 37static void default_free(void *ptr, void *user_data) { 38 (void)user_data; 39 40 free(ptr); 41} 42 43static void *default_calloc(size_t nmemb, size_t size, void *user_data) { 44 (void)user_data; 45 46 return calloc(nmemb, size); 47} 48 49static void *default_realloc(void *ptr, size_t size, void *user_data) { 50 (void)user_data; 51 52 return realloc(ptr, size); 53} 54 55static nghttp3_mem mem_default = {NULL, default_malloc, default_free, 56 default_calloc, default_realloc}; 57 58const nghttp3_mem *nghttp3_mem_default(void) { return &mem_default; } 59 60#ifndef MEMDEBUG 61void *nghttp3_mem_malloc(const nghttp3_mem *mem, size_t size) { 62 return mem->malloc(size, mem->user_data); 63} 64 65void nghttp3_mem_free(const nghttp3_mem *mem, void *ptr) { 66 mem->free(ptr, mem->user_data); 67} 68 69void *nghttp3_mem_calloc(const nghttp3_mem *mem, size_t nmemb, size_t size) { 70 return mem->calloc(nmemb, size, mem->user_data); 71} 72 73void *nghttp3_mem_realloc(const nghttp3_mem *mem, void *ptr, size_t size) { 74 return mem->realloc(ptr, size, mem->user_data); 75} 76#else /* MEMDEBUG */ 77void *nghttp3_mem_malloc_debug(const nghttp3_mem *mem, size_t size, 78 const char *func, const char *file, 79 size_t line) { 80 void *nptr = mem->malloc(size, mem->user_data); 81 82 fprintf(stderr, "malloc %p size=%zu in %s at %s:%zu\n", nptr, size, func, 83 file, line); 84 85 return nptr; 86} 87 88void nghttp3_mem_free_debug(const nghttp3_mem *mem, void *ptr, const char *func, 89 const char *file, size_t line) { 90 fprintf(stderr, "free ptr=%p in %s at %s:%zu\n", ptr, func, file, line); 91 92 mem->free(ptr, mem->user_data); 93} 94 95void nghttp3_mem_free2_debug(const nghttp3_free free_func, void *ptr, 96 void *user_data, const char *func, 97 const char *file, size_t line) { 98 fprintf(stderr, "free ptr=%p in %s at %s:%zu\n", ptr, func, file, line); 99 100 free_func(ptr, user_data); 101} 102 103void *nghttp3_mem_calloc_debug(const nghttp3_mem *mem, size_t nmemb, 104 size_t size, const char *func, const char *file, 105 size_t line) { 106 void *nptr = mem->calloc(nmemb, size, mem->user_data); 107 108 fprintf(stderr, "calloc %p nmemb=%zu size=%zu in %s at %s:%zu\n", nptr, nmemb, 109 size, func, file, line); 110 111 return nptr; 112} 113 114void *nghttp3_mem_realloc_debug(const nghttp3_mem *mem, void *ptr, size_t size, 115 const char *func, const char *file, 116 size_t line) { 117 void *nptr = mem->realloc(ptr, size, mem->user_data); 118 119 fprintf(stderr, "realloc %p ptr=%p size=%zu in %s at %s:%zu\n", nptr, ptr, 120 size, func, file, line); 121 122 return nptr; 123} 124#endif /* MEMDEBUG */ 125