Code: Select all
uint32_t pool_alloc(pool_t *pool) {
inline void full() {
printk("%x\n", pool_query(pool));
panic("pool allocator full");
return 0;
}
uint32_t p, w, b; // pool, word, bit
// Find suitable pool
for (p = 0; pool[p].total == 0; p++)
if (pool[p].setup != 0x4224) full();
if (pool[p].setup != 0x4224) full();
// Find suitable word within pool
for (w = pool[p].first / 32; w < pool[p].upper / 32; w++)
if (pool[p].word[w] != 0xFFFFFFFF) break;
// Find open bit within word
for (b = 0; pool[p].word[w] & (0x1 << b); b++);
if (b == 32) full();
pool[p].word[w] |= (0x1 << b);
pool[p].total --;
if (pool[p].first == ((w << 5) | b)) pool[p].first++;
return ((p << 10) | ((w << 5) | b));
}