PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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