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 
default_malloc(size_t size,void * mem_user_data)29 static void *default_malloc(size_t size, void *mem_user_data) {
30   (void)mem_user_data;
31 
32   return malloc(size);
33 }
34 
default_free(void * ptr,void * mem_user_data)35 static void default_free(void *ptr, void *mem_user_data) {
36   (void)mem_user_data;
37 
38   free(ptr);
39 }
40 
default_calloc(size_t nmemb,size_t size,void * mem_user_data)41 static void *default_calloc(size_t nmemb, size_t size, void *mem_user_data) {
42   (void)mem_user_data;
43 
44   return calloc(nmemb, size);
45 }
46 
default_realloc(void * ptr,size_t size,void * mem_user_data)47 static void *default_realloc(void *ptr, size_t size, void *mem_user_data) {
48   (void)mem_user_data;
49 
50   return realloc(ptr, size);
51 }
52 
53 static nghttp3_mem mem_default = {NULL, default_malloc, default_free,
54                                   default_calloc, default_realloc};
55 
nghttp3_mem_default(void)56 const nghttp3_mem *nghttp3_mem_default(void) { return &mem_default; }
57 
nghttp3_mem_malloc(const nghttp3_mem * mem,size_t size)58 void *nghttp3_mem_malloc(const nghttp3_mem *mem, size_t size) {
59   return mem->malloc(size, mem->mem_user_data);
60 }
61 
nghttp3_mem_free(const nghttp3_mem * mem,void * ptr)62 void nghttp3_mem_free(const nghttp3_mem *mem, void *ptr) {
63   mem->free(ptr, mem->mem_user_data);
64 }
65 
nghttp3_mem_free2(const nghttp3_free free_func,void * ptr,void * mem_user_data)66 void nghttp3_mem_free2(const nghttp3_free free_func, void *ptr,
67                        void *mem_user_data) {
68   free_func(ptr, mem_user_data);
69 }
70 
nghttp3_mem_calloc(const nghttp3_mem * mem,size_t nmemb,size_t size)71 void *nghttp3_mem_calloc(const nghttp3_mem *mem, size_t nmemb, size_t size) {
72   return mem->calloc(nmemb, size, mem->mem_user_data);
73 }
74 
nghttp3_mem_realloc(const nghttp3_mem * mem,void * ptr,size_t size)75 void *nghttp3_mem_realloc(const nghttp3_mem *mem, void *ptr, size_t size) {
76   return mem->realloc(ptr, size, mem->mem_user_data);
77 }
78