SUMMARY
I've been trying to use the fixed_pool
feature in my project, but the malloc() function keeps failing, returning nullptr
.
I also tried to run the sample code in the docs : https://www.intel.com/content/www/us/en/docs/onetbb/developer-guide-api-reference/2022-1/fixed-pool.html but it keeps failing.
Note that I've integrated onetbb libs and includes into my project directly (external/includes - external/lib), but this shouldn't make any difference right ?
TESTED VERSIONS
I tested this on several versions :
ENVIRONMENT
OBSERVED BEHAVIOR
malloc() on tbb::fixed_pool keeps returning nullptr, while the pool is correctly allocated.
REPRODUCE
Test program (test.cpp)
#define TBB_PREVIEW_MEMORY_POOL 1
#include <iostream>
#include "oneapi/tbb/memory_pool.h"
int main() {
char buf[1024];
oneapi::tbb::fixed_pool my_pool(buf, 1024);
std::cout << "Pool address: " << &my_pool << "\n";
void* my_ptr = my_pool.malloc(10);
if (!my_ptr) {
std::cout << "FAILED TO ALLOCATE\n";
} else {
std::cout << my_ptr << "\n";
}
my_pool.free(my_ptr);
}
Compiled with :
g++ -Iexternal/include test.cpp -Lexternal/lib -ltbb -ltbbmalloc
LD_LIBRARY_PATH=./external/lib ./a.out
My stdout :
Pool address: 0x7ffdbce9dbc0
FAILED TO ALLOCATE
there's a minimum size for the buffer of 64 KB.
char buf[1024 * 64];
there's around 32 KB of bookkeeping, so this 64 KB buffer only gives you 32 KB for your data, and you cannot pass less than 64 KB. if you use 128 KB you get to use 96 KB of them, etc..
also note that chunks are 16 KB for each size, so with 64 bytes you only get to allocate 2 chunks for two different sizes, you cannot allocate 3 different sizes ... and keep the buffer divisible by 16 KB
on another note, most implementations of pmr::synchronized_pool_resource
have an initial bookkeeping cost of less than 2 KB .... which is a lot less than 32 KB, but they usually have less performance.