util.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (C) 2011-2017 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #ifndef _UTIL_H_
  32. #define _UTIL_H_
  33. #include "arch.h"
  34. #include <assert.h>
  35. #ifdef __cplusplus
  36. #define GET_PTR(t, p, offset) reinterpret_cast<t*>( reinterpret_cast<size_t>(p) + static_cast<size_t>(offset) )
  37. #define PTR_DIFF(p1, p2) ((reinterpret_cast<size_t>(p1) - reinterpret_cast<size_t>(p2)))
  38. #else
  39. #define GET_PTR(t, p, offset) (t*)( (size_t)(p) + (size_t)(offset) )
  40. #define PTR_DIFF(p1, p2) ((size_t)(p1) - (size_t)(p2))
  41. #endif
  42. #define DIFF(p1, p2) (assert((size_t)(p1) >= (size_t)(p2)), ((size_t)(p1) - (size_t)(p2)))
  43. #define DIFF64(p1, p2) (assert((uint64_t)(p1) >= (uint64_t)(p2)), ((uint64_t)(p1) - (uint64_t)(p2)))
  44. #define SE_PAGE_SHIFT 12
  45. #define SE_BULK_PAGE_FRAME_SHIFT 4
  46. #define SE_BULK_PAGE_FRAME_SIZE (1 << SE_BULK_PAGE_FRAME_SHIFT)
  47. #define SE_BULK_PAGE_FRAME_MASK (SE_BULK_PAGE_FRAME_SIZE-1)
  48. #define SE_BULK_PAGE_SHIFT (SE_PAGE_SHIFT + SE_BULK_PAGE_FRAME_SHIFT)
  49. #define SE_BULK_PAGE_SIZE (1 << SE_BULK_PAGE_SHIFT)
  50. #define SE_GUARD_PAGE_SHIFT SE_PAGE_SHIFT
  51. #define SE_GUARD_PAGE_SIZE SE_PAGE_SIZE
  52. #define ROUND_TO(x, align) (((x) + ((align)-1)) & ~((align)-1))
  53. #define ROUND_TO_PAGE(x) ROUND_TO(x, SE_PAGE_SIZE)
  54. #define TRIM_TO_PAGE(x) ((x) & ~(SE_PAGE_SIZE-1))
  55. #define PAGE_OFFSET(x) ((x) & (SE_PAGE_SIZE -1))
  56. #ifdef __cplusplus
  57. #define PAGE_ALIGN(t, x) reinterpret_cast<t*>((reinterpret_cast<size_t>(x)+(SE_PAGE_SIZE-1)) & (~(SE_PAGE_SIZE-1)))
  58. #else
  59. #define PAGE_ALIGN(t, x) (t*)( ((size_t)(x)+(SE_PAGE_SIZE-1)) & (~(SE_PAGE_SIZE-1)) )
  60. #endif
  61. #define IS_PAGE_ALIGNED(x) (!((size_t)(x)&(SE_PAGE_SIZE-1)))
  62. #define MIN(x, y) (((x)>(y))?(y):(x))
  63. #define MAX(x, y) (((x)>(y))?(x):(y))
  64. #define ARRAY_LENGTH(x) (sizeof(x)/sizeof(x[0]))
  65. /* used to eliminate `unused variable' warning */
  66. #define UNUSED(val) (void)(val)
  67. #include <stddef.h>
  68. #define container_of(ptr, type, member) (type *)( (char *)(ptr) - offsetof(type,member) )
  69. #endif