/src/samba/third_party/ngtcp2/lib/ngtcp2_mem.c
Line | Count | Source |
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 | | |
30 | 0 | static void *default_malloc(size_t size, void *user_data) { |
31 | 0 | (void)user_data; |
32 | |
|
33 | 0 | return malloc(size); |
34 | 0 | } |
35 | | |
36 | 0 | static void default_free(void *ptr, void *user_data) { |
37 | 0 | (void)user_data; |
38 | |
|
39 | 0 | free(ptr); |
40 | 0 | } |
41 | | |
42 | 0 | static void *default_calloc(size_t nmemb, size_t size, void *user_data) { |
43 | 0 | (void)user_data; |
44 | |
|
45 | 0 | return calloc(nmemb, size); |
46 | 0 | } |
47 | | |
48 | 0 | static void *default_realloc(void *ptr, size_t size, void *user_data) { |
49 | 0 | (void)user_data; |
50 | |
|
51 | 0 | return realloc(ptr, size); |
52 | 0 | } |
53 | | |
54 | | static const ngtcp2_mem mem_default = { |
55 | | .malloc = default_malloc, |
56 | | .free = default_free, |
57 | | .calloc = default_calloc, |
58 | | .realloc = default_realloc, |
59 | | }; |
60 | | |
61 | 0 | const ngtcp2_mem *ngtcp2_mem_default(void) { return &mem_default; } |
62 | | |
63 | | #ifndef MEMDEBUG |
64 | 0 | void *ngtcp2_mem_malloc(const ngtcp2_mem *mem, size_t size) { |
65 | 0 | return mem->malloc(size, mem->user_data); |
66 | 0 | } |
67 | | |
68 | 0 | void ngtcp2_mem_free(const ngtcp2_mem *mem, void *ptr) { |
69 | 0 | mem->free(ptr, mem->user_data); |
70 | 0 | } |
71 | | |
72 | 0 | void *ngtcp2_mem_calloc(const ngtcp2_mem *mem, size_t nmemb, size_t size) { |
73 | 0 | return mem->calloc(nmemb, size, mem->user_data); |
74 | 0 | } |
75 | | |
76 | 0 | void *ngtcp2_mem_realloc(const ngtcp2_mem *mem, void *ptr, size_t size) { |
77 | 0 | return mem->realloc(ptr, size, mem->user_data); |
78 | 0 | } |
79 | | #else /* defined(MEMDEBUG) */ |
80 | | void *ngtcp2_mem_malloc_debug(const ngtcp2_mem *mem, size_t size, |
81 | | const char *func, const char *file, size_t line) { |
82 | | void *nptr = mem->malloc(size, mem->user_data); |
83 | | |
84 | | fprintf(stderr, "malloc %p size=%zu in %s at %s:%zu\n", nptr, size, func, |
85 | | file, line); |
86 | | |
87 | | return nptr; |
88 | | } |
89 | | |
90 | | void ngtcp2_mem_free_debug(const ngtcp2_mem *mem, void *ptr, const char *func, |
91 | | const char *file, size_t line) { |
92 | | fprintf(stderr, "free ptr=%p in %s at %s:%zu\n", ptr, func, file, line); |
93 | | |
94 | | mem->free(ptr, mem->user_data); |
95 | | } |
96 | | |
97 | | void *ngtcp2_mem_calloc_debug(const ngtcp2_mem *mem, size_t nmemb, size_t size, |
98 | | const char *func, const char *file, size_t line) { |
99 | | void *nptr = mem->calloc(nmemb, size, mem->user_data); |
100 | | |
101 | | fprintf(stderr, "calloc %p nmemb=%zu size=%zu in %s at %s:%zu\n", nptr, nmemb, |
102 | | size, func, file, line); |
103 | | |
104 | | return nptr; |
105 | | } |
106 | | |
107 | | void *ngtcp2_mem_realloc_debug(const ngtcp2_mem *mem, void *ptr, size_t size, |
108 | | const char *func, const char *file, |
109 | | size_t line) { |
110 | | void *nptr = mem->realloc(ptr, size, mem->user_data); |
111 | | |
112 | | fprintf(stderr, "realloc %p ptr=%p size=%zu in %s at %s:%zu\n", nptr, ptr, |
113 | | size, func, file, line); |
114 | | |
115 | | return nptr; |
116 | | } |
117 | | #endif /* defined(MEMDEBUG) */ |