summaryrefslogtreecommitdiffstats
path: root/include/qemu/memalign.h
blob: fa299f3bf6732cb66ca5f91ab66db66276da3256 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * Allocation and free functions for aligned memory
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

#ifndef QEMU_MEMALIGN_H
#define QEMU_MEMALIGN_H

/**
 * qemu_try_memalign: Allocate aligned memory
 * @alignment: required alignment, in bytes
 * @size: size of allocation, in bytes
 *
 * Allocate memory on an aligned boundary (i.e. the returned
 * address will be an exact multiple of @alignment).
 * @alignment must be a power of 2, or the function will assert().
 * On success, returns allocated memory; on failure, returns NULL.
 *
 * The memory allocated through this function must be freed via
 * qemu_vfree() (and not via free()).
 */
void *qemu_try_memalign(size_t alignment, size_t size);
/**
 * qemu_memalign: Allocate aligned memory, without failing
 * @alignment: required alignment, in bytes
 * @size: size of allocation, in bytes
 *
 * Allocate memory in the same way as qemu_try_memalign(), but
 * abort() with an error message if the memory allocation fails.
 *
 * The memory allocated through this function must be freed via
 * qemu_vfree() (and not via free()).
 */
void *qemu_memalign(size_t alignment, size_t size);
/**
 * qemu_vfree: Free memory allocated through qemu_memalign
 * @ptr: memory to free
 *
 * This function must be used to free memory allocated via qemu_memalign()
 * or qemu_try_memalign(). (Using the wrong free function will cause
 * subtle bugs on Windows hosts.)
 */
void qemu_vfree(void *ptr);
/*
 * It's an analog of GLIB's g_autoptr_cleanup_generic_gfree(), used to define
 * g_autofree macro.
 */
static inline void qemu_cleanup_generic_vfree(void *p)
{
  void **pp = (void **)p;
  qemu_vfree(*pp);
}

/*
 * Analog of g_autofree, but qemu_vfree is called on cleanup instead of g_free.
 */
#define QEMU_AUTO_VFREE __attribute__((cleanup(qemu_cleanup_generic_vfree)))

#endif