1 /*
2  * Copyright (c) 2009-2021, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Google LLC nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef UPB_MEM_ALLOC_H_
29 #define UPB_MEM_ALLOC_H_
30 
31 // Must be last.
32 #include "upb/port/def.inc"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 typedef struct upb_alloc upb_alloc;
39 
40 /* A combined `malloc()`/`free()` function.
41  * If `size` is 0 then the function acts like `free()`, otherwise it acts like
42  * `realloc()`.  Only `oldsize` bytes from a previous allocation are
43  * preserved. */
44 typedef void* upb_alloc_func(upb_alloc* alloc, void* ptr, size_t oldsize,
45                              size_t size);
46 
47 /* A upb_alloc is a possibly-stateful allocator object.
48  *
49  * It could either be an arena allocator (which doesn't require individual
50  * `free()` calls) or a regular `malloc()` (which does).  The client must
51  * therefore free memory unless it knows that the allocator is an arena
52  * allocator. */
53 struct upb_alloc {
54   upb_alloc_func* func;
55 };
56 
upb_malloc(upb_alloc * alloc,size_t size)57 UPB_INLINE void* upb_malloc(upb_alloc* alloc, size_t size) {
58   UPB_ASSERT(alloc);
59   return alloc->func(alloc, NULL, 0, size);
60 }
61 
upb_realloc(upb_alloc * alloc,void * ptr,size_t oldsize,size_t size)62 UPB_INLINE void* upb_realloc(upb_alloc* alloc, void* ptr, size_t oldsize,
63                              size_t size) {
64   UPB_ASSERT(alloc);
65   return alloc->func(alloc, ptr, oldsize, size);
66 }
67 
upb_free(upb_alloc * alloc,void * ptr)68 UPB_INLINE void upb_free(upb_alloc* alloc, void* ptr) {
69   UPB_ASSERT(alloc);
70   alloc->func(alloc, ptr, 0, 0);
71 }
72 
73 // The global allocator used by upb. Uses the standard malloc()/free().
74 
75 extern upb_alloc upb_alloc_global;
76 
77 /* Functions that hard-code the global malloc.
78  *
79  * We still get benefit because we can put custom logic into our global
80  * allocator, like injecting out-of-memory faults in debug/testing builds. */
81 
upb_gmalloc(size_t size)82 UPB_INLINE void* upb_gmalloc(size_t size) {
83   return upb_malloc(&upb_alloc_global, size);
84 }
85 
upb_grealloc(void * ptr,size_t oldsize,size_t size)86 UPB_INLINE void* upb_grealloc(void* ptr, size_t oldsize, size_t size) {
87   return upb_realloc(&upb_alloc_global, ptr, oldsize, size);
88 }
89 
upb_gfree(void * ptr)90 UPB_INLINE void upb_gfree(void* ptr) { upb_free(&upb_alloc_global, ptr); }
91 
92 #ifdef __cplusplus
93 } /* extern "C" */
94 #endif
95 
96 #include "upb/port/undef.inc"
97 
98 #endif /* UPB_MEM_ALLOC_H_ */
99