Skip to content

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:

build_flags = -DTDSL_DISABLE_DEFAULT_ALLOCATOR
target_compile_definitions(my_app PRIVATE TDSL_DISABLE_DEFAULT_ALLOCATOR)

You must then call tdsl::tdslite_malloc_free(...) before using the driver.