1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright 2023 Red Hat
6 #ifndef VDO_MEMORY_ALLOC_H
7 #define VDO_MEMORY_ALLOC_H
9 #include <linux/cache.h>
10 #include <linux/io.h> /* for PAGE_SIZE */
12 #include "permassert.h"
13 #include "thread-registry.h"
15 /* Custom memory allocation function that tracks memory usage */
16 int __must_check
vdo_allocate_memory(size_t size
, size_t align
, const char *what
, void *ptr
);
19 * Allocate storage based on element counts, sizes, and alignment.
21 * This is a generalized form of our allocation use case: It allocates an array of objects,
22 * optionally preceded by one object of another type (i.e., a struct with trailing variable-length
23 * array), with the alignment indicated.
25 * Why is this inline? The sizes and alignment will always be constant, when invoked through the
26 * macros below, and often the count will be a compile-time constant 1 or the number of extra bytes
27 * will be a compile-time constant 0. So at least some of the arithmetic can usually be optimized
28 * away, and the run-time selection between allocation functions always can. In many cases, it'll
29 * boil down to just a function call with a constant size.
31 * @count: The number of objects to allocate
32 * @size: The size of an object
33 * @extra: The number of additional bytes to allocate
34 * @align: The required alignment
35 * @what: What is being allocated (for error logging)
36 * @ptr: A pointer to hold the allocated memory
38 * Return: VDO_SUCCESS or an error code
40 static inline int __vdo_do_allocation(size_t count
, size_t size
, size_t extra
,
41 size_t align
, const char *what
, void *ptr
)
43 size_t total_size
= count
* size
+ extra
;
46 if ((size
> 0) && (count
> ((SIZE_MAX
- extra
) / size
))) {
48 * This is kind of a hack: We rely on the fact that SIZE_MAX would cover the entire
49 * address space (minus one byte) and thus the system can never allocate that much
50 * and the call will always fail. So we can report an overflow as "out of memory"
51 * by asking for "merely" SIZE_MAX bytes.
53 total_size
= SIZE_MAX
;
56 return vdo_allocate_memory(total_size
, align
, what
, ptr
);
60 * Allocate one or more elements of the indicated type, logging an error if the allocation fails.
61 * The memory will be zeroed.
63 * @COUNT: The number of objects to allocate
64 * @TYPE: The type of objects to allocate. This type determines the alignment of the allocation.
65 * @WHAT: What is being allocated (for error logging)
66 * @PTR: A pointer to hold the allocated memory
68 * Return: VDO_SUCCESS or an error code
70 #define vdo_allocate(COUNT, TYPE, WHAT, PTR) \
71 __vdo_do_allocation(COUNT, sizeof(TYPE), 0, __alignof__(TYPE), WHAT, PTR)
74 * Allocate one object of an indicated type, followed by one or more elements of a second type,
75 * logging an error if the allocation fails. The memory will be zeroed.
77 * @TYPE1: The type of the primary object to allocate. This type determines the alignment of the
79 * @COUNT: The number of objects to allocate
80 * @TYPE2: The type of array objects to allocate
81 * @WHAT: What is being allocated (for error logging)
82 * @PTR: A pointer to hold the allocated memory
84 * Return: VDO_SUCCESS or an error code
86 #define vdo_allocate_extended(TYPE1, COUNT, TYPE2, WHAT, PTR) \
89 TYPE1 **_ptr = (PTR); \
90 BUILD_BUG_ON(__alignof__(TYPE1) < __alignof__(TYPE2)); \
91 _result = __vdo_do_allocation(COUNT, \
101 * Allocate memory starting on a cache line boundary, logging an error if the allocation fails. The
102 * memory will be zeroed.
104 * @size: The number of bytes to allocate
105 * @what: What is being allocated (for error logging)
106 * @ptr: A pointer to hold the allocated memory
108 * Return: VDO_SUCCESS or an error code
110 static inline int __must_check
vdo_allocate_cache_aligned(size_t size
, const char *what
, void *ptr
)
112 return vdo_allocate_memory(size
, L1_CACHE_BYTES
, what
, ptr
);
116 * Allocate one element of the indicated type immediately, failing if the required memory is not
117 * immediately available.
119 * @size: The number of bytes to allocate
120 * @what: What is being allocated (for error logging)
122 * Return: pointer to the memory, or NULL if the memory is not available.
124 void *__must_check
vdo_allocate_memory_nowait(size_t size
, const char *what
);
126 int __must_check
vdo_reallocate_memory(void *ptr
, size_t old_size
, size_t size
,
127 const char *what
, void *new_ptr
);
129 int __must_check
vdo_duplicate_string(const char *string
, const char *what
,
132 /* Free memory allocated with vdo_allocate(). */
133 void vdo_free(void *ptr
);
135 static inline void *__vdo_forget(void **ptr_ptr
)
137 void *ptr
= *ptr_ptr
;
144 * Null out a pointer and return a copy to it. This macro should be used when passing a pointer to
145 * a function for which it is not safe to access the pointer once the function returns.
147 #define vdo_forget(ptr) __vdo_forget((void **) &(ptr))
149 void vdo_memory_init(void);
151 void vdo_memory_exit(void);
153 void vdo_register_allocating_thread(struct registered_thread
*new_thread
,
154 const bool *flag_ptr
);
156 void vdo_unregister_allocating_thread(void);
158 void vdo_get_memory_stats(u64
*bytes_used
, u64
*peak_bytes_used
);
160 void vdo_report_memory_usage(void);
162 #endif /* VDO_MEMORY_ALLOC_H */