osdep: Move memalign-related functions to their own header

Move the various memalign-related functions out of osdep.h and into
their own header, which we include only where they are used.
While we're doing this, add some brief documentation comments.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Message-id: 20220226180723.1706285-10-peter.maydell@linaro.org
This commit is contained in:
Peter Maydell 2022-02-26 18:07:23 +00:00
parent 1a11265d7e
commit 5df022cf2e
53 changed files with 112 additions and 20 deletions

61
include/qemu/memalign.h Normal file
View file

@ -0,0 +1,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

View file

@ -379,28 +379,10 @@ extern "C" {
#endif
int qemu_daemon(int nochdir, int noclose);
void *qemu_try_memalign(size_t alignment, size_t size);
void *qemu_memalign(size_t alignment, size_t size);
void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared,
bool noreserve);
void qemu_vfree(void *ptr);
void qemu_anon_ram_free(void *ptr, size_t size);
/*
* 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)))
#ifdef _WIN32
#define HAVE_CHARDEV_SERIAL 1
#elif defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \