Uclinux-2008r1-rc8 (bf561) to VDSP5 transplantation (km): array_size

Source: Internet
Author: User
Tags expression

There is a definition in include/linux/kernel.h:

#define ARRAY_SIZE (arr) (sizeof (ARR)/sizeof ((arr) [0]) + __must_be_array (arr))

This macro definition is used to get the number of elements in an array, and, unlike a general definition, this definition adds the tail of + __must_be_array (arr) and looks at its definition:

/* Force a compilation error if condition is true, but also produce a
result (of value 0 and type size_t), so the expression can be used
e.g. in a structure initializer (or where-ever else comma expressions
aren't permitted). */
#define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
/* &a[0] degrades to a pointer: a different type from an array */
#define __must_be_array(a) /
BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0])))

Here __builtin_types_compatible_p is a built-in definition of GCC, with a description of its role:

-built-in function:int __builtin_types_compatible_p (type1, type2)

You can use the built-in function __builtin_types_compatible_p to determine whether two types the are.

This built-in function returns 1 if the unqualified versions of the types Type1 and type2 (which are types, not expression s) are compatible, 0 otherwise. The result of this built-in function can is used in integer constant expressions.

This built-in function ignores the top level qualifiers (e.g., const, volatile). For example, the int is equivalent to const int.

The type int[] and int[5] are compatible. On the other hand, int and char * are not compatible, even if the size of their types, on the particular architecture The same. Also, the amount of pointer indirection is taken to account when determining similarity. Consequently, short * isn't similar to short * *. Furthermore, two types that are typedefed are considered compatible if their underlying types are compatible.

An enum type was considered to being compatible with another enum type. For example, enum {foo, bar} are similar to enum {hot, dog}.

You are would typically use this function in code whose execution varies depending on the arguments ' types '. For example:

 #define FOO (x)/
({ /
typeof (X) TMP; /
if (__builtin_types_compatible_p (typeof (X), long Double))/
TMP = foo_long_double (TMP); /
Else if (__builtin_types_compatible_p (typeof (X), double))/
TMP = foo_double (TMP) ; /
Else if (__builtin_types_compatible_p (typeof (X), float))/
TMP = foo_float (TMP); /
Else/
Abort (); /
TMP; /
})

Note:this construct is only available for C.

It returns the result of 0 or 1, and substituting it into the Build_bug_on_zero definition can be found that when the return value is 0 o'clock, Build_bug_on_zero becomes a

#define Build_bug_on_zero (e) (sizeof (char[1])-1)

Obviously, Build_bug_on_zero this macro will return 0 at this point. And when __builtin_types_compatible_p is 1 o'clock, Build_bug_on_zero becomes a

#define Build_bug_on_zero (e) (sizeof (Char[-1])-1)

It is obvious that syntax errors are caused, which is why force a compilation error is explained in the comments.

Array_size such a definition cannot be passed in VDSP, but since __must_be_array (arr) is simply to add a compile-time check, we will change it directly to:

#define ARRAY_SIZE (arr) (sizeof (ARR)/sizeof ((arr) [0])/*+ __must_be_array (ARR) * * *)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.