native heap services and overloading new
I have some code where I'd like to be able to dynamically allocate memory in realtime. Since the code is written in C++, I was thinking of just making a huge global RT_HEAP and overloading the new and delete operators to use the rt_heap_alloc and rt_heap_free functions. First off, I'm wondering if there are any caveats to this sort of thing that wouldn't be obvious to someone new to xenomai like myself. Second, if I fail to call rt_heap_delete before the program terminates, does the heap remain registered hang potentially causing problems when I try to re-allocate the heap when the program is run again? Thanks.
-Brandt
Quoted 9 lines · Brandt Erickson
I have some code where I'd like to be able to dynamically allocate memory in realtime. Since the code is written in C++, I was thinking of just making a huge global
RT_HEAPand overloading the new and delete operators to use thert_heap_allocandrt_heap_freefunctions. First off, I'm wondering if there are any caveats to this sort of thing that wouldn't be obvious to someone new to xenomai like myself. Second, if I fail to callrt_heap_deletebefore the program terminates, does the heap remain registered hang potentially causing problems when I try to re-allocate the heap when the program is run again? Thanks.
For now: yes, the heap will stay, no automatic cleanup (the posix skin already have this). You could then run into naming conflicts and you lose memory, of course.
But you could install some signal handler for cleaning up. We do so in our native-skin-based framework (also C++, but no "evil" ;) features in use). We even catch SIGSEGV this way.
Jan
Quoted 11 lines · Jan Kiszka
Quoted 9 lines · Brandt Erickson
I have some code where I'd like to be able to dynamically allocate memory in realtime. Since the code is written in C++, I was thinking of just making a huge global
RT_HEAPand overloading the new and delete operators to use thert_heap_allocandrt_heap_freefunctions. First off, I'm wondering if there are any caveats to this sort of thing that wouldn't be obvious to someone new to xenomai like myself. Second, if I fail to callrt_heap_deletebefore the program terminates, does the heap remain registered hang potentially causing problems when I try to re-allocate the heap when the program is run again? Thanks.
Quoted 8 lines
For now: yes, the heap will stay, no automatic cleanup (the posix skin already have this). You could then run into naming conflicts and you lose memory, of course.
But you could install some signal handler for cleaning up. We do so in our native-skin-based framework (also C++, but no "evil" ;) features in use). We even catch SIGSEGV this way.
Another simple option: the binding operation is particularly useful for recycling/discarding lingering objects.
RT_HEAP app_heap;
void init_heap (void)
{
RT_HEAP old_heap;
if (rt_heap_bind(&old_heap, "foo", TM_NONBLOCK) == 0)
rt_heap_delete(&old_heap);
rt_heap_create(&app_heap, "foo", ...);
}Signature · 1 line
Philippe.