Skip to content

Commit 9283e3c

Browse files
Support customize FL_INDEX_MAX to reduce the memory overhead
user can define the max pool size through TLSF_MAX_POOL_SIZE Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com> Change-Id: I021b816f65c1bc5c1025969bc6cc458029f3bc88
1 parent deff9ab commit 9283e3c

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

tlsf.c

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@
4040
#define TLSF_64BIT
4141
#endif
4242

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+
4366
/*
4467
** gcc 3.4 and above have builtin support, specialized for architecture.
4568
** Some compilers masquerade as gcc; patchlevel test filters them out.
@@ -147,29 +170,16 @@ tlsf_decl int tlsf_fls(unsigned int word)
147170
#else
148171
/* Fall back to generic implementation. */
149172

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-
164173
/* Implement ffs in terms of fls. */
165174
tlsf_decl int tlsf_ffs(unsigned int word)
166175
{
167-
return tlsf_fls_generic(word & (~word + 1)) - 1;
176+
const unsigned int reverse = word & (~word + 1);
177+
return TLSF_FLS32(reverse) - 1;
168178
}
169179

170180
tlsf_decl int tlsf_fls(unsigned int word)
171181
{
172-
return tlsf_fls_generic(word) - 1;
182+
return TLSF_FLS32(word) - 1;
173183
}
174184

175185
#endif
@@ -234,7 +244,9 @@ enum tlsf_private
234244
** blocks below that size into the 0th first-level list.
235245
*/
236246

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)
238250
/*
239251
** TODO: We can increase this to support larger sizes, at the expense
240252
** of more overhead in the TLSF structure.

0 commit comments

Comments
 (0)