]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-boot: Add non-failing allocators
authorJan Janssen <medhefgo@web.de>
Mon, 15 Nov 2021 11:25:04 +0000 (12:25 +0100)
committerJan Janssen <medhefgo@web.de>
Mon, 29 Nov 2021 15:20:32 +0000 (16:20 +0100)
src/boot/efi/util.h

index e925f1728bddb252c941afd4a8754267dd977bec..9505d3bf214c280436a1ff5608f3d5532c98a9d5 100644 (file)
 #define UINT64_MAX ((UINT64) -1)
 #endif
 
+#define assert_alloc_ret(p)     \
+        ({                      \
+                void *_p = (p); \
+                assert(_p);     \
+                _p;             \
+        })
+
+#define xnew_alloc(type, n, alloc)                                           \
+        ({                                                                   \
+                UINTN _alloc_size;                                           \
+                if (__builtin_mul_overflow(sizeof(type), (n), &_alloc_size)) \
+                        assert_not_reached();                                \
+                (type *) alloc(_alloc_size);                                 \
+        })
+
+#define xallocate_pool(size) assert_alloc_ret(AllocatePool(size))
+#define xallocate_zero_pool(size) assert_alloc_ret(AllocateZeroPool(size))
+#define xreallocate_pool(p, old_size, new_size) assert_alloc_ret(ReallocatePool((p), (old_size), (new_size)))
+#define xpool_print(fmt, ...) ((CHAR16 *) assert_alloc_ret(PoolPrint((fmt), ##__VA_ARGS__)))
+#define xstrdup(str) ((CHAR16 *) assert_alloc_ret(StrDuplicate(str)))
+#define xnew(type, n) xnew_alloc(type, (n), xallocate_pool)
+#define xnew0(type, n) xnew_alloc(type, (n), xallocate_zero_pool)
+
 EFI_STATUS parse_boolean(const CHAR8 *v, BOOLEAN *b);
 
 UINT64 ticks_read(void);