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