qemu/include/qemu/memalign.h
<<
>>
Prefs
   1/*
   2 * Allocation and free functions for aligned memory
   3 *
   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 */
   7
   8#ifndef QEMU_MEMALIGN_H
   9#define QEMU_MEMALIGN_H
  10
  11/**
  12 * qemu_try_memalign: Allocate aligned memory
  13 * @alignment: required alignment, in bytes
  14 * @size: size of allocation, in bytes
  15 *
  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.
  20 *
  21 * The memory allocated through this function must be freed via
  22 * qemu_vfree() (and not via free()).
  23 */
  24void *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
  29 *
  30 * Allocate memory in the same way as qemu_try_memalign(), but
  31 * abort() with an error message if the memory allocation fails.
  32 *
  33 * The memory allocated through this function must be freed via
  34 * qemu_vfree() (and not via free()).
  35 */
  36void *qemu_memalign(size_t alignment, size_t size);
  37/**
  38 * qemu_vfree: Free memory allocated through qemu_memalign
  39 * @ptr: memory to free
  40 *
  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.)
  44 */
  45void qemu_vfree(void *ptr);
  46/*
  47 * It's an analog of GLIB's g_autoptr_cleanup_generic_gfree(), used to define
  48 * g_autofree macro.
  49 */
  50static inline void qemu_cleanup_generic_vfree(void *p)
  51{
  52  void **pp = (void **)p;
  53  qemu_vfree(*pp);
  54}
  55
  56/*
  57 * Analog of g_autofree, but qemu_vfree is called on cleanup instead of g_free.
  58 */
  59#define QEMU_AUTO_VFREE __attribute__((cleanup(qemu_cleanup_generic_vfree)))
  60
  61#endif
  62