Use your own memory allocator¶
tdslite takes a little heap memory while a query runs, for the descriptors of
the result set's columns and fields. By default it calls malloc and free.
Replace them when you want to trace allocations, serve them from a static pool,
or run on a platform without a heap.
Set the functions¶
Write a pair of functions with these signatures, and hand them to tdslite once,
before the first connect:
void * my_malloc(unsigned long amount) noexcept {
return ::malloc(amount);
}
void my_free(void * ptr) noexcept {
::free(ptr);
}
void setup() {
tdsl::tdslite_malloc_free(my_malloc, my_free);
// ... then connect as usual
}
Set both or neither: passing only one of them is an error.
If my_malloc returns nullptr, the query that needed the memory fails with
response_error, and the connection stays usable.
Remove the default allocator¶
To make sure nothing in your program pulls in malloc through tdslite, define
TDSL_DISABLE_DEFAULT_ALLOCATOR before including tdslite, e.g. in your build
flags:
You must then call tdsl::tdslite_malloc_free(...) before using the driver.
Related¶
- Memory model: what tdslite allocates and when.
- Example sketch: 06 - Custom memory allocator functions