Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / include / qemu / memalign.h
blobfa299f3bf6732cb66ca5f91ab66db66276da3256
1 /*
2 * Allocation and free functions for aligned memory
4 * This work is licensed under the terms of the GNU GPL, version 2 or later.
5 * See the COPYING file in the top-level directory.
6 */
8 #ifndef QEMU_MEMALIGN_H
9 #define QEMU_MEMALIGN_H
11 /**
12 * qemu_try_memalign: Allocate aligned memory
13 * @alignment: required alignment, in bytes
14 * @size: size of allocation, in bytes
16 * Allocate memory on an aligned boundary (i.e. the returned
17 * address will be an exact multiple of @alignment).
18 * @alignment must be a power of 2, or the function will assert().
19 * On success, returns allocated memory; on failure, returns NULL.
21 * The memory allocated through this function must be freed via
22 * qemu_vfree() (and not via free()).
24 void *qemu_try_memalign(size_t alignment, size_t size);
25 /**
26 * qemu_memalign: Allocate aligned memory, without failing
27 * @alignment: required alignment, in bytes
28 * @size: size of allocation, in bytes
30 * Allocate memory in the same way as qemu_try_memalign(), but
31 * abort() with an error message if the memory allocation fails.
33 * The memory allocated through this function must be freed via
34 * qemu_vfree() (and not via free()).
36 void *qemu_memalign(size_t alignment, size_t size);
37 /**
38 * qemu_vfree: Free memory allocated through qemu_memalign
39 * @ptr: memory to free
41 * This function must be used to free memory allocated via qemu_memalign()
42 * or qemu_try_memalign(). (Using the wrong free function will cause
43 * subtle bugs on Windows hosts.)
45 void qemu_vfree(void *ptr);
47 * It's an analog of GLIB's g_autoptr_cleanup_generic_gfree(), used to define
48 * g_autofree macro.
50 static inline void qemu_cleanup_generic_vfree(void *p)
52 void **pp = (void **)p;
53 qemu_vfree(*pp);
57 * Analog of g_autofree, but qemu_vfree is called on cleanup instead of g_free.
59 #define QEMU_AUTO_VFREE __attribute__((cleanup(qemu_cleanup_generic_vfree)))
61 #endif