WPILibC++ 2027.0.0-alpha-3
Loading...
Searching...
No Matches
alloc.h
Go to the documentation of this file.
1// Protocol Buffers - Google's data interchange format
2// Copyright 2023 Google LLC. All rights reserved.
3//
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file or at
6// https://developers.google.com/open-source/licenses/bsd
7
8#ifndef UPB_MEM_ALLOC_H_
9#define UPB_MEM_ALLOC_H_
10
11#include <stddef.h>
12
13// Must be last.
14#include "upb/port/def.inc"
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20typedef struct upb_alloc upb_alloc;
21
22/* A combined `malloc()`/`free()` function.
23 * If `size` is 0 then the function acts like `free()`, otherwise it acts like
24 * `realloc()`. Only `oldsize` bytes from a previous allocation are
25 * preserved. If `actual_size` is not null and the allocator supports it, the
26 * actual size of the resulting allocation is stored in `actual_size`. If
27 * `actual_size` is not null, you must zero out the memory pointed to by
28 * `actual_size` before calling. */
29typedef void* upb_alloc_func(upb_alloc* alloc, void* ptr, size_t oldsize,
30 size_t size, size_t* actual_size);
31
32/* A upb_alloc is a possibly-stateful allocator object.
33 *
34 * It could either be an arena allocator (which doesn't require individual
35 * `free()` calls) or a regular `malloc()` (which does). The client must
36 * therefore free memory unless it knows that the allocator is an arena
37 * allocator. */
41
42UPB_INLINE void* upb_malloc(upb_alloc* alloc, size_t size) {
43 UPB_ASSERT(alloc);
44 return alloc->func(alloc, NULL, 0, size, NULL);
45}
46
47typedef struct {
48 void* p;
49 size_t n;
51
53 UPB_ASSERT(alloc);
54 upb_SizedPtr result;
55 result.n = 0;
56 result.p = alloc->func(alloc, NULL, 0, size, &result.n);
57 result.n = result.p != NULL ? UPB_MAX(result.n, size) : 0;
58 return result;
59}
60
61UPB_INLINE void* upb_realloc(upb_alloc* alloc, void* ptr, size_t oldsize,
62 size_t size) {
63 UPB_ASSERT(alloc);
64 return alloc->func(alloc, ptr, oldsize, size, NULL);
65}
66
67UPB_INLINE void upb_free(upb_alloc* alloc, void* ptr) {
68 UPB_ASSERT(alloc);
69 alloc->func(alloc, ptr, 0, 0, NULL);
70}
71
72UPB_INLINE void upb_free_sized(upb_alloc* alloc, void* ptr, size_t size) {
73 UPB_ASSERT(alloc);
74 alloc->func(alloc, ptr, size, 0, NULL);
75}
76
77// The global allocator used by upb. Uses the standard malloc()/free().
78
80
81/* Functions that hard-code the global malloc.
82 *
83 * We still get benefit because we can put custom logic into our global
84 * allocator, like injecting out-of-memory faults in debug/testing builds. */
85
86UPB_INLINE void* upb_gmalloc(size_t size) {
87 return upb_malloc(upb_alloc_global(), size);
88}
89
90UPB_INLINE void* upb_grealloc(void* ptr, size_t oldsize, size_t size) {
91 return upb_realloc(upb_alloc_global(), ptr, oldsize, size);
92}
93
95
96#ifdef __cplusplus
97} /* extern "C" */
98#endif
99
100#include "upb/port/undef.inc"
101
102#endif /* UPB_MEM_ALLOC_H_ */
UPB_INLINE void * upb_realloc(upb_alloc *alloc, void *ptr, size_t oldsize, size_t size)
Definition alloc.h:61
UPB_INLINE void * upb_malloc(upb_alloc *alloc, size_t size)
Definition alloc.h:42
UPB_INLINE void * upb_gmalloc(size_t size)
Definition alloc.h:86
void * upb_alloc_func(upb_alloc *alloc, void *ptr, size_t oldsize, size_t size, size_t *actual_size)
Definition alloc.h:29
UPB_INLINE void upb_free_sized(upb_alloc *alloc, void *ptr, size_t size)
Definition alloc.h:72
UPB_INLINE void upb_free(upb_alloc *alloc, void *ptr)
Definition alloc.h:67
upb_alloc * upb_alloc_global(void)
UPB_INLINE void * upb_grealloc(void *ptr, size_t oldsize, size_t size)
Definition alloc.h:90
UPB_INLINE upb_SizedPtr upb_SizeReturningMalloc(upb_alloc *alloc, size_t size)
Definition alloc.h:52
UPB_INLINE void upb_gfree(void *ptr)
Definition alloc.h:94
#define UPB_ASSERT(expr)
Definition def.inc:329
#define UPB_MAX(x, y)
Definition def.inc:300
#define UPB_INLINE
Definition def.inc:144
auto ptr(T p) -> const void *
Converts p to const void* for pointer formatting.
Definition format.h:3963
Definition alloc.h:47
void * p
Definition alloc.h:48
size_t n
Definition alloc.h:49
Definition alloc.h:38
upb_alloc_func * func
Definition alloc.h:39