mirror of
https://github.com/yuzu-emu/unicorn.git
synced 2024-12-23 19:35:29 +00:00
ARRAY_SIZE: check that argument is an array
It's a familiar pattern: some code uses ARRAY_SIZE, then refactoring changes the argument from an array to a pointer to a dynamically allocated buffer. Code keeps compiling but any ARRAY_SIZE calls now return the size of the pointer divided by element size. Let's add build time checks to ARRAY_SIZE before we allow more of these in the code-base. Backports commit ed63ec0d22ccdce3b2222d9a514423b7fbba3a0d from qemu
This commit is contained in:
parent
ac013df0a2
commit
0455644974
|
@ -184,8 +184,20 @@
|
||||||
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
|
#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#define QEMU_IS_ARRAY(x) (x)
|
||||||
|
#else
|
||||||
|
/*
|
||||||
|
* &(x)[0] is always a pointer - if it's same type as x then the argument is a
|
||||||
|
* pointer, not an array.
|
||||||
|
*/
|
||||||
|
#define QEMU_IS_ARRAY(x) (!__builtin_types_compatible_p(typeof(x), \
|
||||||
|
typeof(&(x)[0])))
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef ARRAY_SIZE
|
#ifndef ARRAY_SIZE
|
||||||
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
#define ARRAY_SIZE(x) ((sizeof(x) / sizeof((x)[0])) + \
|
||||||
|
QEMU_BUILD_BUG_ON_ZERO(!QEMU_IS_ARRAY(x)))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void *qemu_try_memalign(size_t alignment, size_t size);
|
void *qemu_try_memalign(size_t alignment, size_t size);
|
||||||
|
|
Loading…
Reference in a new issue