|
40 | 40 | #define TLSF_64BIT |
41 | 41 | #endif |
42 | 42 |
|
| 43 | +/* |
| 44 | +** Returns one plus the index of the most significant 1-bit of n, |
| 45 | +** or if n is zero, returns zero. |
| 46 | +*/ |
| 47 | +#ifdef TLSF_64BIT |
| 48 | +#define TLSF_FLS(n) ((n) & 0xffffffff00000000ull ? 32 + TLSF_FLS32((size_t)(n) >> 32) : TLSF_FLS32(n)) |
| 49 | +#else |
| 50 | +#define TLSF_FLS(n) TLSF_FLS32(n) |
| 51 | +#endif |
| 52 | + |
| 53 | +#define TLSF_FLS32(n) ((n) & 0xffff0000 ? 16 + TLSF_FLS16((n) >> 16) : TLSF_FLS16(n)) |
| 54 | +#define TLSF_FLS16(n) ((n) & 0xff00 ? 8 + TLSF_FLS8 ((n) >> 8) : TLSF_FLS8 (n)) |
| 55 | +#define TLSF_FLS8(n) ((n) & 0xf0 ? 4 + TLSF_FLS4 ((n) >> 4) : TLSF_FLS4 (n)) |
| 56 | +#define TLSF_FLS4(n) ((n) & 0xc ? 2 + TLSF_FLS2 ((n) >> 2) : TLSF_FLS2 (n)) |
| 57 | +#define TLSF_FLS2(n) ((n) & 0x2 ? 1 + TLSF_FLS1 ((n) >> 1) : TLSF_FLS1 (n)) |
| 58 | +#define TLSF_FLS1(n) ((n) & 0x1 ? 1 : 0) |
| 59 | + |
| 60 | +/* |
| 61 | +** Returns round up value of log2(n). |
| 62 | +** Note: it is used at compile time. |
| 63 | +*/ |
| 64 | +#define TLSF_LOG2_CEIL(n) ((n) & (n - 1) ? TLSF_FLS(n) : TLSF_FLS(n) - 1) |
| 65 | + |
43 | 66 | /* |
44 | 67 | ** gcc 3.4 and above have builtin support, specialized for architecture. |
45 | 68 | ** Some compilers masquerade as gcc; patchlevel test filters them out. |
@@ -147,29 +170,16 @@ tlsf_decl int tlsf_fls(unsigned int word) |
147 | 170 | #else |
148 | 171 | /* Fall back to generic implementation. */ |
149 | 172 |
|
150 | | -tlsf_decl int tlsf_fls_generic(unsigned int word) |
151 | | -{ |
152 | | - int bit = 32; |
153 | | - |
154 | | - if (!word) bit -= 1; |
155 | | - if (!(word & 0xffff0000)) { word <<= 16; bit -= 16; } |
156 | | - if (!(word & 0xff000000)) { word <<= 8; bit -= 8; } |
157 | | - if (!(word & 0xf0000000)) { word <<= 4; bit -= 4; } |
158 | | - if (!(word & 0xc0000000)) { word <<= 2; bit -= 2; } |
159 | | - if (!(word & 0x80000000)) { word <<= 1; bit -= 1; } |
160 | | - |
161 | | - return bit; |
162 | | -} |
163 | | - |
164 | 173 | /* Implement ffs in terms of fls. */ |
165 | 174 | tlsf_decl int tlsf_ffs(unsigned int word) |
166 | 175 | { |
167 | | - return tlsf_fls_generic(word & (~word + 1)) - 1; |
| 176 | + const unsigned int reverse = word & (~word + 1); |
| 177 | + return TLSF_FLS32(reverse) - 1; |
168 | 178 | } |
169 | 179 |
|
170 | 180 | tlsf_decl int tlsf_fls(unsigned int word) |
171 | 181 | { |
172 | | - return tlsf_fls_generic(word) - 1; |
| 182 | + return TLSF_FLS32(word) - 1; |
173 | 183 | } |
174 | 184 |
|
175 | 185 | #endif |
@@ -234,7 +244,9 @@ enum tlsf_private |
234 | 244 | ** blocks below that size into the 0th first-level list. |
235 | 245 | */ |
236 | 246 |
|
237 | | -#if defined (TLSF_64BIT) |
| 247 | +#if defined (TLSF_MAX_POOL_SIZE) |
| 248 | + FL_INDEX_MAX = TLSF_LOG2_CEIL(TLSF_MAX_POOL_SIZE), |
| 249 | +#elif defined (TLSF_64BIT) |
238 | 250 | /* |
239 | 251 | ** TODO: We can increase this to support larger sizes, at the expense |
240 | 252 | ** of more overhead in the TLSF structure. |
|
0 commit comments