11cb0ef41Sopenharmony_ci/*
21cb0ef41Sopenharmony_ci * nghttp3
31cb0ef41Sopenharmony_ci *
41cb0ef41Sopenharmony_ci * Copyright (c) 2019 nghttp3 contributors
51cb0ef41Sopenharmony_ci * Copyright (c) 2017 ngtcp2 contributors
61cb0ef41Sopenharmony_ci * Copyright (c) 2014 nghttp2 contributors
71cb0ef41Sopenharmony_ci *
81cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining
91cb0ef41Sopenharmony_ci * a copy of this software and associated documentation files (the
101cb0ef41Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
111cb0ef41Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
121cb0ef41Sopenharmony_ci * distribute, sublicense, and/or sell copies of the Software, and to
131cb0ef41Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
141cb0ef41Sopenharmony_ci * the following conditions:
151cb0ef41Sopenharmony_ci *
161cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice shall be
171cb0ef41Sopenharmony_ci * included in all copies or substantial portions of the Software.
181cb0ef41Sopenharmony_ci *
191cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
201cb0ef41Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
211cb0ef41Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
221cb0ef41Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
231cb0ef41Sopenharmony_ci * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
241cb0ef41Sopenharmony_ci * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
251cb0ef41Sopenharmony_ci * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
261cb0ef41Sopenharmony_ci */
271cb0ef41Sopenharmony_ci#include "nghttp3_mem.h"
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci#include <stdio.h>
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cistatic void *default_malloc(size_t size, void *user_data) {
321cb0ef41Sopenharmony_ci  (void)user_data;
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  return malloc(size);
351cb0ef41Sopenharmony_ci}
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_cistatic void default_free(void *ptr, void *user_data) {
381cb0ef41Sopenharmony_ci  (void)user_data;
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci  free(ptr);
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_cistatic void *default_calloc(size_t nmemb, size_t size, void *user_data) {
441cb0ef41Sopenharmony_ci  (void)user_data;
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  return calloc(nmemb, size);
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_cistatic void *default_realloc(void *ptr, size_t size, void *user_data) {
501cb0ef41Sopenharmony_ci  (void)user_data;
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  return realloc(ptr, size);
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_cistatic nghttp3_mem mem_default = {NULL, default_malloc, default_free,
561cb0ef41Sopenharmony_ci                                  default_calloc, default_realloc};
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ciconst nghttp3_mem *nghttp3_mem_default(void) { return &mem_default; }
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci#ifndef MEMDEBUG
611cb0ef41Sopenharmony_civoid *nghttp3_mem_malloc(const nghttp3_mem *mem, size_t size) {
621cb0ef41Sopenharmony_ci  return mem->malloc(size, mem->user_data);
631cb0ef41Sopenharmony_ci}
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_civoid nghttp3_mem_free(const nghttp3_mem *mem, void *ptr) {
661cb0ef41Sopenharmony_ci  mem->free(ptr, mem->user_data);
671cb0ef41Sopenharmony_ci}
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_civoid *nghttp3_mem_calloc(const nghttp3_mem *mem, size_t nmemb, size_t size) {
701cb0ef41Sopenharmony_ci  return mem->calloc(nmemb, size, mem->user_data);
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_civoid *nghttp3_mem_realloc(const nghttp3_mem *mem, void *ptr, size_t size) {
741cb0ef41Sopenharmony_ci  return mem->realloc(ptr, size, mem->user_data);
751cb0ef41Sopenharmony_ci}
761cb0ef41Sopenharmony_ci#else  /* MEMDEBUG */
771cb0ef41Sopenharmony_civoid *nghttp3_mem_malloc_debug(const nghttp3_mem *mem, size_t size,
781cb0ef41Sopenharmony_ci                               const char *func, const char *file,
791cb0ef41Sopenharmony_ci                               size_t line) {
801cb0ef41Sopenharmony_ci  void *nptr = mem->malloc(size, mem->user_data);
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci  fprintf(stderr, "malloc %p size=%zu in %s at %s:%zu\n", nptr, size, func,
831cb0ef41Sopenharmony_ci          file, line);
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  return nptr;
861cb0ef41Sopenharmony_ci}
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_civoid nghttp3_mem_free_debug(const nghttp3_mem *mem, void *ptr, const char *func,
891cb0ef41Sopenharmony_ci                            const char *file, size_t line) {
901cb0ef41Sopenharmony_ci  fprintf(stderr, "free ptr=%p in %s at %s:%zu\n", ptr, func, file, line);
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  mem->free(ptr, mem->user_data);
931cb0ef41Sopenharmony_ci}
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_civoid nghttp3_mem_free2_debug(const nghttp3_free free_func, void *ptr,
961cb0ef41Sopenharmony_ci                             void *user_data, const char *func,
971cb0ef41Sopenharmony_ci                             const char *file, size_t line) {
981cb0ef41Sopenharmony_ci  fprintf(stderr, "free ptr=%p in %s at %s:%zu\n", ptr, func, file, line);
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci  free_func(ptr, user_data);
1011cb0ef41Sopenharmony_ci}
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_civoid *nghttp3_mem_calloc_debug(const nghttp3_mem *mem, size_t nmemb,
1041cb0ef41Sopenharmony_ci                               size_t size, const char *func, const char *file,
1051cb0ef41Sopenharmony_ci                               size_t line) {
1061cb0ef41Sopenharmony_ci  void *nptr = mem->calloc(nmemb, size, mem->user_data);
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  fprintf(stderr, "calloc %p nmemb=%zu size=%zu in %s at %s:%zu\n", nptr, nmemb,
1091cb0ef41Sopenharmony_ci          size, func, file, line);
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci  return nptr;
1121cb0ef41Sopenharmony_ci}
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_civoid *nghttp3_mem_realloc_debug(const nghttp3_mem *mem, void *ptr, size_t size,
1151cb0ef41Sopenharmony_ci                                const char *func, const char *file,
1161cb0ef41Sopenharmony_ci                                size_t line) {
1171cb0ef41Sopenharmony_ci  void *nptr = mem->realloc(ptr, size, mem->user_data);
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci  fprintf(stderr, "realloc %p ptr=%p size=%zu in %s at %s:%zu\n", nptr, ptr,
1201cb0ef41Sopenharmony_ci          size, func, file, line);
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci  return nptr;
1231cb0ef41Sopenharmony_ci}
1241cb0ef41Sopenharmony_ci#endif /* MEMDEBUG */
125