VirtualBox

Changeset 73097 in vbox for trunk/include/iprt


Ignore:
Timestamp:
Jul 12, 2018 9:06:33 PM (7 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
123672
Message:

*: Made RT_UOFFSETOF, RT_OFFSETOF, RT_UOFFSETOF_ADD and RT_OFFSETOF_ADD work like builtin_offsetof() and require compile time resolvable requests, adding RT_UOFFSETOF_DYN for the dynamic questions that can only be answered at runtime.

Location:
trunk/include/iprt
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/cdefs.h

    r72774 r73097  
    22652265 * Our own special offsetof() variant, returns a signed result.
    22662266 *
    2267  * This differs from the usual offsetof() in that it's not relying on builtin
    2268  * compiler stuff and thus can use variables in arrays the structure may
    2269  * contain. This is useful to determine the sizes of structures ending
    2270  * with a variable length field. For gcc >= 4.4 see @bugref{7775}.
    2271  *
    22722267 * @returns offset into the structure of the specified member. signed.
    22732268 * @param   type    Structure type.
    22742269 * @param   member  Member.
    2275  */
    2276 #if defined(__cplusplus) && RT_GNUC_PREREQ(4, 4)
    2277 # define RT_OFFSETOF(type, member)              ( (int)(uintptr_t)&( ((type *)(void *)0x1000)->member) - 0x1000 )
    2278 #else
    2279 # define RT_OFFSETOF(type, member)              ( (int)(uintptr_t)&( ((type *)(void *)0)->member) )
     2270 *
     2271 * @remarks Only use this for static offset calculations. Please
     2272 *          use RT_UOFFSETOF_DYN for dynamic ones (i.e. involves
     2273 *          non-constant array indexing).
     2274 *
     2275 */
     2276#if RT_GNUC_PREREQ(4, 0)
     2277# define RT_OFFSETOF(type, member)              ( (int)__builtin_offsetof(type, member) )
     2278#else
     2279# define RT_OFFSETOF(type, member)              ( (int)(intptr_t)&( ((type *)(void *)0)->member) )
    22802280#endif
    22812281
    22822282/** @def RT_UOFFSETOF
    2283  * Our own special offsetof() variant, returns an unsigned result.
    2284  *
    2285  * This differs from the usual offsetof() in that it's not relying on builtin
    2286  * compiler stuff and thus can use variables in arrays the structure may
    2287  * contain. This is useful to determine the sizes of structures ending
    2288  * with a variable length field. For gcc >= 4.4 see @bugref{7775}.
     2283 * Our own offsetof() variant, returns an unsigned result.
    22892284 *
    22902285 * @returns offset into the structure of the specified member. unsigned.
    22912286 * @param   type    Structure type.
    22922287 * @param   member  Member.
    2293  */
    2294 #if defined(__cplusplus) && RT_GNUC_PREREQ(4, 4)
    2295 # define RT_UOFFSETOF(type, member)             ( (uintptr_t)&( ((type *)(void *)0x1000)->member) - 0x1000 )
     2288 *
     2289 * @remarks Only use this for static offset calculations. Please
     2290 *          use RT_UOFFSETOF_DYN for dynamic ones (i.e. involves
     2291 *          non-constant array indexing).
     2292 */
     2293#if RT_GNUC_PREREQ(4, 0)
     2294# define RT_UOFFSETOF(type, member)             ( (uintptr_t)__builtin_offsetof(type, member) )
    22962295#else
    22972296# define RT_UOFFSETOF(type, member)             ( (uintptr_t)&( ((type *)(void *)0)->member) )
     
    23052304 * @param   member  Member.
    23062305 * @param   addend  The addend to add to the offset.
     2306 *
     2307 * @remarks Only use this for static offset calculations.
    23072308 */
    23082309#define RT_OFFSETOF_ADD(type, member, addend)   ( (int)RT_UOFFSETOF_ADD(type, member, addend) )
     
    23152316 * @param   member  Member.
    23162317 * @param   addend  The addend to add to the offset.
    2317  */
    2318 #define RT_UOFFSETOF_ADD(type, member, addend)  ( (uintptr_t)&( ((type *)(void *)(uintptr_t)(addend))->member) )
     2318 *
     2319 * @remarks Only use this for static offset calculations.
     2320 */
     2321#if RT_GNUC_PREREQ(4, 0)
     2322# define RT_UOFFSETOF_ADD(type, member, addend)  ( (uintptr_t)(__builtin_offsetof(type, member) + (addend)))
     2323#else
     2324# define RT_UOFFSETOF_ADD(type, member, addend)  ( (uintptr_t)&( ((type *)(void *)(uintptr_t)(addend))->member) )
     2325#endif
     2326
     2327/** @def RT_UOFFSETOF_DYN
     2328 * Dynamic (runtime) structure offset calculations, involving
     2329 * indexing of array members via variable.
     2330 *
     2331 * @returns offset into the structure of the specified member. signed.
     2332 * @param   type        Structure type.
     2333 * @param   memberarray Member.
     2334 */
     2335#if defined(__cplusplus) && RT_GNUC_PREREQ(4, 4)
     2336# define RT_UOFFSETOF_DYN(type, memberarray)    ( (uintptr_t)&( ((type *)(void *)0x1000)->memberarray) - 0x1000 )
     2337#else
     2338# define RT_UOFFSETOF_DYN(type, memberarray)    ( (uintptr_t)&( ((type *)(void *)0)->memberarray) )
     2339#endif
     2340
    23192341
    23202342/** @def RT_SIZEOFMEMB
  • trunk/include/iprt/nt/nt.h

    r72639 r73097  
    14941494#if !defined(NtCurrentTeb) && !defined(IPRT_NT_HAVE_CURRENT_TEB_MACRO)
    14951495# ifdef RT_ARCH_X86
    1496 DECL_FORCE_INLINE(PTEB)     RTNtCurrentTeb(void) { return (PTEB)__readfsdword(RT_OFFSETOF(TEB_COMMON, NtTib.Self)); }
    1497 DECL_FORCE_INLINE(PPEB)     RTNtCurrentPeb(void) { return (PPEB)__readfsdword(RT_OFFSETOF(TEB_COMMON, ProcessEnvironmentBlock)); }
    1498 DECL_FORCE_INLINE(uint32_t) RTNtCurrentThreadId(void) { return __readfsdword(RT_OFFSETOF(TEB_COMMON, ClientId.UniqueThread)); }
    1499 DECL_FORCE_INLINE(NTSTATUS) RTNtLastStatusValue(void) { return (NTSTATUS)__readfsdword(RT_OFFSETOF(TEB_COMMON, LastStatusValue)); }
    1500 DECL_FORCE_INLINE(uint32_t) RTNtLastErrorValue(void)  { return __readfsdword(RT_OFFSETOF(TEB_COMMON, LastErrorValue)); }
     1496DECL_FORCE_INLINE(PTEB)     RTNtCurrentTeb(void) { return (PTEB)__readfsdword(RT_UOFFSETOF(TEB_COMMON, NtTib.Self)); }
     1497DECL_FORCE_INLINE(PPEB)     RTNtCurrentPeb(void) { return (PPEB)__readfsdword(RT_UOFFSETOF(TEB_COMMON, ProcessEnvironmentBlock)); }
     1498DECL_FORCE_INLINE(uint32_t) RTNtCurrentThreadId(void) { return __readfsdword(RT_UOFFSETOF(TEB_COMMON, ClientId.UniqueThread)); }
     1499DECL_FORCE_INLINE(NTSTATUS) RTNtLastStatusValue(void) { return (NTSTATUS)__readfsdword(RT_UOFFSETOF(TEB_COMMON, LastStatusValue)); }
     1500DECL_FORCE_INLINE(uint32_t) RTNtLastErrorValue(void)  { return __readfsdword(RT_UOFFSETOF(TEB_COMMON, LastErrorValue)); }
    15011501# elif defined(RT_ARCH_AMD64)
    1502 DECL_FORCE_INLINE(PTEB)     RTNtCurrentTeb(void) { return (PTEB)__readgsqword(RT_OFFSETOF(TEB_COMMON, NtTib.Self)); }
    1503 DECL_FORCE_INLINE(PPEB)     RTNtCurrentPeb(void) { return (PPEB)__readgsqword(RT_OFFSETOF(TEB_COMMON, ProcessEnvironmentBlock)); }
    1504 DECL_FORCE_INLINE(uint32_t) RTNtCurrentThreadId(void) { return __readgsdword(RT_OFFSETOF(TEB_COMMON, ClientId.UniqueThread)); }
    1505 DECL_FORCE_INLINE(NTSTATUS) RTNtLastStatusValue(void) { return (NTSTATUS)__readgsdword(RT_OFFSETOF(TEB_COMMON, LastStatusValue)); }
    1506 DECL_FORCE_INLINE(uint32_t) RTNtLastErrorValue(void)  { return __readgsdword(RT_OFFSETOF(TEB_COMMON, LastErrorValue)); }
     1502DECL_FORCE_INLINE(PTEB)     RTNtCurrentTeb(void) { return (PTEB)__readgsqword(RT_UOFFSETOF(TEB_COMMON, NtTib.Self)); }
     1503DECL_FORCE_INLINE(PPEB)     RTNtCurrentPeb(void) { return (PPEB)__readgsqword(RT_UOFFSETOF(TEB_COMMON, ProcessEnvironmentBlock)); }
     1504DECL_FORCE_INLINE(uint32_t) RTNtCurrentThreadId(void) { return __readgsdword(RT_UOFFSETOF(TEB_COMMON, ClientId.UniqueThread)); }
     1505DECL_FORCE_INLINE(NTSTATUS) RTNtLastStatusValue(void) { return (NTSTATUS)__readgsdword(RT_UOFFSETOF(TEB_COMMON, LastStatusValue)); }
     1506DECL_FORCE_INLINE(uint32_t) RTNtLastErrorValue(void)  { return __readgsdword(RT_UOFFSETOF(TEB_COMMON, LastErrorValue)); }
    15071507# else
    15081508#  error "Port me"
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette