Other Parts Discussed in Thread: SYSBIOS
Hello,
I just wanted to share a tidbit that took me sometime to figure out and to which I did not find any suggestions for on the forum. My task was to "make" a C++ application run on on the DSPs. In theory, it is possible, surely, but as an embedded engineer - C++ is not my strong suit or favorite choice. My first challenge was the standard library, <vector>. This library was using the C++ new() function. This new() function was defaulting to memory allocation from L2 cache. Maybe this is okay for some, but these vectors get quite large and very quickly don't fit in L2. I looked for a few days on how to solve this. I would like to save others some time.
You have to use a custom allocator. I found one here that started me on the right foot:
http://www.josuttis.com/libbook/memory/myalloc1.cpp.html
Here I replaced the new() and delete() calls with Memory_alloc and Memory_free DSP calls and I told it what heap to use. I had created the heap statically in my .cfg file.
Then you add the custom allocator to your vector variable declaration.
Problem #2 was multicore. I now need to change this custom allocator to accept a variable in the constructor which will tell it which heap to use so that I don't have to have an allocator for each core. It seems the behavoir of the vector class is such that it uses the copy constructors as defined in the above example. So I created a member variable, mHeap, and populate it - first through a constructor with a specific allocator instance and then it must be populated again in the copy constructor.
Now my variable declaration looks like this:
const myLib::myAlloc<int> allocSrc1(myHeap);
vector<ObservationDataStruct, myLib::myAlloc<int> > myVect(allocSrc1);
The first line creates an allocator instance that uses a specific heap. The second line creates a vector using the "custom allocator template" and then gets passed an instance of this allocator. When the vector is created, it will call the copy constructor in the example file. In this copy constructor, you will need to add a line to copy the heap variable from the original allocator instance.
//Create an allocator instance and give it the heap you want to use.
myAlloc( const ti_sysbios_heaps_HeapMem_Handle heap) throw()
{ mHeap = heap;}
//container classes will call this copy constructor first.
//give it the proper heap number.
template <class U>
myAlloc(const myAlloc<U>& ma) throw()
{ mHeap = ma.mHeap;}
//Container classes will call this copy constructor second.
//again, give it the proper heap number
//no one knows why it does so many copies...
myAlloc(const myAlloc& ma) throw()
{ mHeap = ma.mHeap;}
Well, so anyhow, hopefully this will help someone else. Works great for me and allows me to control where these vectors are getting created. I think I will have to use it for maps too.
Brandy
