/src/memory/tlsf/tlsfbits.h

https://bitbucket.org/vivkin/gam3b00bs/ · C Header · 116 lines · 68 code · 23 blank · 25 comment · 14 complexity · ed12583b70a728808486349aa54b3789 MD5 · raw file

  1. #ifndef INCLUDED_tlsfbits
  2. #define INCLUDED_tlsfbits
  3. #if defined(__cplusplus)
  4. #define tlsf_decl inline
  5. #else
  6. #define tlsf_decl static
  7. #endif
  8. /*
  9. ** Architecture-specific bit manipulation routines.
  10. **
  11. ** TLSF achieves O(1) cost for malloc and free operations by limiting
  12. ** the search for a free block to a free list of guaranteed size
  13. ** adequate to fulfill the request, combined with efficient free list
  14. ** queries using bitmasks and architecture-specific bit-manipulation
  15. ** routines.
  16. **
  17. ** Most modern processors provide instructions to count leading zeroes
  18. ** in a word, find the lowest and highest set bit, etc. These
  19. ** specific implementations will be used when available, falling back
  20. ** to a reasonably efficient generic implementation.
  21. **
  22. ** NOTE: TLSF spec relies on ffs/fls returning value 0..31.
  23. ** ffs/fls return 1-32 by default, returning 0 for error.
  24. */
  25. /*
  26. ** gcc 3.4 and above have builtin support, specialized for architecture.
  27. ** Some compilers masquerade as gcc; patchlevel test filters them out.
  28. */
  29. #if defined (__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) \
  30. && defined (__GNUC_PATCHLEVEL__)
  31. tlsf_decl int tlsf_ffs(unsigned int word)
  32. {
  33. return __builtin_ffs(word) - 1;
  34. }
  35. tlsf_decl int tlsf_fls(unsigned int word)
  36. {
  37. const int bit = word ? 32 - __builtin_clz(word) : 0;
  38. return bit - 1;
  39. }
  40. #elif defined (_MSC_VER) && defined (_M_IX86) && (_MSC_VER >= 1400)
  41. /* Microsoft Visual C++ 2005 support on x86 architectures. */
  42. #include <intrin.h>
  43. #pragma intrinsic(_BitScanReverse)
  44. #pragma intrinsic(_BitScanForward)
  45. tlsf_decl int tlsf_fls(unsigned int word)
  46. {
  47. unsigned long index;
  48. return _BitScanReverse(&index, word) ? index : -1;
  49. }
  50. tlsf_decl int tlsf_ffs(unsigned int word)
  51. {
  52. unsigned long index;
  53. return _BitScanForward(&index, word) ? index : -1;
  54. }
  55. #elif defined (_MSC_VER) && defined (_M_PPC)
  56. /* Microsoft Visual C++ support on PowerPC architectures. */
  57. #include <ppcintrinsics.h>
  58. tlsf_decl int tlsf_fls(unsigned int word)
  59. {
  60. const int bit = 32 - _CountLeadingZeros(word);
  61. return bit - 1;
  62. }
  63. tlsf_decl int tlsf_ffs(unsigned int word)
  64. {
  65. const unsigned int reverse = word & (~word + 1);
  66. const int bit = 32 - _CountLeadingZeros(reverse);
  67. return bit - 1;
  68. }
  69. #else
  70. /* Fall back to generic implementation. */
  71. tlsf_decl int tlsf_fls_generic(unsigned int word)
  72. {
  73. int bit = 32;
  74. if (!word) bit -= 1;
  75. if (!(word & 0xffff0000)) { word <<= 16; bit -= 16; }
  76. if (!(word & 0xff000000)) { word <<= 8; bit -= 8; }
  77. if (!(word & 0xf0000000)) { word <<= 4; bit -= 4; }
  78. if (!(word & 0xc0000000)) { word <<= 2; bit -= 2; }
  79. if (!(word & 0x80000000)) { word <<= 1; bit -= 1; }
  80. return bit;
  81. }
  82. /* Implement ffs in terms of fls. */
  83. tlsf_decl int tlsf_ffs(unsigned int word)
  84. {
  85. return tlsf_fls_generic(word & (~word + 1)) - 1;
  86. }
  87. tlsf_decl int tlsf_fls(unsigned int word)
  88. {
  89. return tlsf_fls_generic(word) - 1;
  90. }
  91. #endif
  92. #undef tlsf_decl
  93. #endif