Skip to content

Commit

Permalink
util/oslib: Assert qemu_try_memalign() alignment is a power of 2
Browse files Browse the repository at this point in the history
qemu_try_memalign() expects a power of 2 alignment:

- posix_memalign(3):

  The address of the allocated memory will be a multiple of alignment,
  which must be a power of two and a multiple of sizeof(void *).

- _aligned_malloc()

  The alignment value, which must be an integer power of 2.

Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Message-Id: <20201021173803.2619054-3-philmd@redhat.com>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
  • Loading branch information
philmd authored and rth7680 committed Jan 7, 2021
1 parent dfbd0b8 commit ed6f53f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
2 changes: 2 additions & 0 deletions util/oslib-posix.c
Expand Up @@ -201,6 +201,8 @@ void *qemu_try_memalign(size_t alignment, size_t size)

if (alignment < sizeof(void*)) {
alignment = sizeof(void*);
} else {
g_assert(is_power_of_2(alignment));
}

#if defined(CONFIG_POSIX_MEMALIGN)
Expand Down
1 change: 1 addition & 0 deletions util/oslib-win32.c
Expand Up @@ -58,6 +58,7 @@ void *qemu_try_memalign(size_t alignment, size_t size)
void *ptr;

g_assert(size != 0);
g_assert(is_power_of_2(alignment));
ptr = _aligned_malloc(alignment, size);
trace_qemu_memalign(alignment, size, ptr);
return ptr;
Expand Down

0 comments on commit ed6f53f

Please sign in to comment.