Use brp_mempool for memory management
Try the app note
The app note appnotes\mempool in the SDK
gives you a working example of the implementation (learn more about app notes).
What is brp_mempool?
brp_mempool is a memory pool object provided by the BRP Communication Library "BRP Lib". It provides centralized memory allocation for storing response data for BRP lib functions.
Many wrapper functions in the BALTECH SDK return variable-length data, such as card IDs, feature lists, or firmware strings. Instead of requiring you to pre-allocate buffers of unknown size or manage individual memory allocations, the mempool simplifies memory management:
- Automatic memory management: No manual alloc/free required
- Allocate many, free once: The mempool collects all allocations and frees them together.
- No size guessing: You don't need to know the response size in advance.
Usage patterns
There are two ways to use mempools: simple and advanced. Choose based on how long you need the returned data to remain valid.
Simple usage (internal mempool)
Pass NULL as the mempool parameter to use the protocol's internal mempool:
char *firmware_string;
errcode = brp_Sys_GetInfo(dev, &firmware_string, NULL); // (1)
printf("Firmware: %s\n", firmware_string); // (2)
- Pass
NULLto use the protocol's internal mempool - Use the data immediately
Data is only valid until the next call
When using the internal mempool, the returned data is automatically freed when you call the next wrapper function on the same protocol. Do not store pointers for later use.
// WRONG: features becomes invalid after brp_Sys_GetInfo()
brp_Sys_GetFeatures(dev, &features, &features_len, &max_id, NULL);
brp_Sys_GetInfo(dev, &firmware, NULL); // Overwrites internal mempool!
print_features(features); // UNDEFINED BEHAVIOR - features is invalid
Advanced usage (own mempool)
Create your own mempool when you need data from multiple wrapper function calls simultaneously:
brp_mempool my_mempool = NULL; // (1)
// Both calls allocate into the same pool
brp_Sys_GetFeatures(dev, &features, &features_len, &max_id, &my_mempool);
brp_Sys_GetInfo(dev, &firmware, &my_mempool);
// Both features and firmware are still valid here
printf("Firmware: %s\n", firmware);
for (size_t i = 0; i < features_len; i++) {
printf("Feature: 0x%04X\n", features[i]);
}
brp_mempool_free(&my_mempool); // (2)
- Initialize to
NULL- the API allocates it automatically on first use - Free when done - releases all data allocated from this pool
When to use which pattern
| Scenario | Pattern | Reason |
|---|---|---|
| Single call, use result immediately | Simple (NULL) |
No manual cleanup needed |
| Multiple calls, need all results | Advanced (own mempool) | Data persists across calls |
| Loop calling same wrapper function | Simple (NULL) |
Each iteration overwrites previous |
| Building a data structure from multiple calls | Advanced (own mempool) | Collect all data, then process |
Best practices
Scope and lifetime
Create a mempool at the beginning of a logical operation and free it when the operation is complete:
void read_card_data(brp_protocol dev) {
brp_mempool mempool = NULL;
// ... multiple calls using &mempool ...
brp_mempool_free(&mempool);
}
Copy data if needed beyond the pool's lifetime
If you need data to persist after freeing the mempool, copy it first:
brp_mempool mempool = NULL;
char *firmware;
char saved_firmware[64];
brp_Sys_GetInfo(dev, &firmware, &mempool);
// Copy before freeing
strncpy(saved_firmware, firmware, sizeof(saved_firmware) - 1);
brp_mempool_free(&mempool);
// saved_firmware is still valid, firmware is not