14a49301eSmrg/**************************************************************************
24a49301eSmrg *
3af69d88dSmrg * Copyright 2007-2008 VMware, Inc.
44a49301eSmrg * All Rights Reserved.
54a49301eSmrg *
64a49301eSmrg * Permission is hereby granted, free of charge, to any person obtaining a
74a49301eSmrg * copy of this software and associated documentation files (the
84a49301eSmrg * "Software"), to deal in the Software without restriction, including
94a49301eSmrg * without limitation the rights to use, copy, modify, merge, publish,
104a49301eSmrg * distribute, sub license, and/or sell copies of the Software, and to
114a49301eSmrg * permit persons to whom the Software is furnished to do so, subject to
124a49301eSmrg * the following conditions:
134a49301eSmrg *
144a49301eSmrg * The above copyright notice and this permission notice (including the
154a49301eSmrg * next paragraph) shall be included in all copies or substantial portions
164a49301eSmrg * of the Software.
174a49301eSmrg *
184a49301eSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
194a49301eSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
204a49301eSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21af69d88dSmrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
224a49301eSmrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
234a49301eSmrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
244a49301eSmrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
254a49301eSmrg *
264a49301eSmrg **************************************************************************/
274a49301eSmrg
284a49301eSmrg#ifndef U_POINTER_H
294a49301eSmrg#define U_POINTER_H
304a49301eSmrg
314a49301eSmrg#include "pipe/p_compiler.h"
324a49301eSmrg
334a49301eSmrg#ifdef __cplusplus
344a49301eSmrgextern "C" {
354a49301eSmrg#endif
364a49301eSmrg
3701e04c3fSmrgstatic inline intptr_t
384a49301eSmrgpointer_to_intptr( const void *p )
394a49301eSmrg{
404a49301eSmrg   union {
414a49301eSmrg      const void *p;
424a49301eSmrg      intptr_t i;
434a49301eSmrg   } pi;
444a49301eSmrg   pi.p = p;
454a49301eSmrg   return pi.i;
464a49301eSmrg}
474a49301eSmrg
4801e04c3fSmrgstatic inline void *
494a49301eSmrgintptr_to_pointer( intptr_t i )
504a49301eSmrg{
514a49301eSmrg   union {
524a49301eSmrg      void *p;
534a49301eSmrg      intptr_t i;
544a49301eSmrg   } pi;
554a49301eSmrg   pi.i = i;
564a49301eSmrg   return pi.p;
574a49301eSmrg}
584a49301eSmrg
5901e04c3fSmrgstatic inline uintptr_t
604a49301eSmrgpointer_to_uintptr( const void *ptr )
614a49301eSmrg{
624a49301eSmrg   union {
634a49301eSmrg      const void *p;
644a49301eSmrg      uintptr_t u;
654a49301eSmrg   } pu;
664a49301eSmrg   pu.p = ptr;
674a49301eSmrg   return pu.u;
684a49301eSmrg}
694a49301eSmrg
7001e04c3fSmrgstatic inline void *
714a49301eSmrguintptr_to_pointer( uintptr_t u )
724a49301eSmrg{
734a49301eSmrg   union {
744a49301eSmrg      void *p;
754a49301eSmrg      uintptr_t u;
764a49301eSmrg   } pu;
774a49301eSmrg   pu.u = u;
784a49301eSmrg   return pu.p;
794a49301eSmrg}
804a49301eSmrg
814a49301eSmrg/**
824a49301eSmrg * Return a pointer aligned to next multiple of N bytes.
834a49301eSmrg */
8401e04c3fSmrgstatic inline void *
854a49301eSmrgalign_pointer( const void *unaligned, uintptr_t alignment )
864a49301eSmrg{
874a49301eSmrg   uintptr_t aligned = (pointer_to_uintptr( unaligned ) + alignment - 1) & ~(alignment - 1);
884a49301eSmrg   return uintptr_to_pointer( aligned );
894a49301eSmrg}
904a49301eSmrg
914a49301eSmrg
924a49301eSmrg/**
934a49301eSmrg * Return a pointer aligned to next multiple of 16 bytes.
944a49301eSmrg */
9501e04c3fSmrgstatic inline void *
964a49301eSmrgalign16( void *unaligned )
974a49301eSmrg{
984a49301eSmrg   return align_pointer( unaligned, 16 );
994a49301eSmrg}
1004a49301eSmrg
1013464ebd5Sriastradhtypedef void (*func_pointer)(void);
1023464ebd5Sriastradh
10301e04c3fSmrgstatic inline func_pointer
1043464ebd5Sriastradhpointer_to_func( void *p )
1053464ebd5Sriastradh{
1063464ebd5Sriastradh   union {
1073464ebd5Sriastradh      void *p;
1083464ebd5Sriastradh      func_pointer f;
1093464ebd5Sriastradh   } pf;
1103464ebd5Sriastradh   pf.p = p;
1113464ebd5Sriastradh   return pf.f;
1123464ebd5Sriastradh}
1133464ebd5Sriastradh
11401e04c3fSmrgstatic inline void *
1153464ebd5Sriastradhfunc_to_pointer( func_pointer f )
1163464ebd5Sriastradh{
1173464ebd5Sriastradh   union {
1183464ebd5Sriastradh      void *p;
1193464ebd5Sriastradh      func_pointer f;
1203464ebd5Sriastradh   } pf;
1213464ebd5Sriastradh   pf.f = f;
1223464ebd5Sriastradh   return pf.p;
1233464ebd5Sriastradh}
1244a49301eSmrg
1254a49301eSmrg
1264a49301eSmrg#ifdef __cplusplus
1274a49301eSmrg}
1284a49301eSmrg#endif
1294a49301eSmrg
1304a49301eSmrg#endif /* U_POINTER_H */
131