I am using a dual-core device and have a requirement for core A to create a structure of data that contains arguments for a list of functions running on core B, periodically updating it and informing core B. The number of arguments and types can change during run time.
My plan was as follows..
Create a ParamsInterface base class and each function would have it's own parameter class that derives from the base class. Using polymorphism, this would allow me to create a vector of ParamsInterface* in shared memory, and populate it from core A.
Core A populates the vector by newing whichever child class it needs, and pushing the returned pointer to the vector.
Example core A:
volatile std::vector<ParamsInterface*> sharedParams __attribute__((section(".shared_memory")))
class ParamsInterface
{
public:
virtual ~ParamsInterface(){};
};
class Function1Params: public ParamsInterface
{
public:
float mParam1;
bool mParam2;
};
void populate()
{
Function1Params *params = new Function1Params ;
params->mParam1= 1.1;
params->mParam2 = true;
sharedParams.push_back(params);
}
Core B should then be able to statically cast the pointer to the child type, since it knows which type each member of the vector will be. Of course access is controlled by hardware semaphore, and all pointers are deleted before refreshing the list.
Example core B:
void process(ParamsInterface* params)
{
Function1Params paramsCasted = static_cast<Function1Params *>(params);
processFunc1(paramsCasted->mParam1, paramsCasted->mParam2);
}
The problem is that when core B reads this shared, uncached data, the address pointed to by ParamsInterface* is not correct, and I get a hard fault when trying to cast. I have checked the pointer created by Core A, and it points to a memory region accessible to both cores.
My question is two fold, firstly, am I doing something incorrect here - perhaps expecting a method to work that can never work? And secondly, am I missing a much better way to do this? I've left out the information about how the list of functions work to try and keep the question concise.
Many thanks for any help.
EDIT:
So it seems that the issue is to do with where the ParamsInterface objects are being created. Core A creates them and they are being placed in heap, which unfortunately happens to be in an area that isn't in a managed, and core B apparently cannot access.
If I use placement new to place them in a managed section of memory, I have to specify a precise address, but this would get very complicated since all of the ParamsInterface objects could be a different size.
So is there a way to new() 'polymorphic' objects into a container in a specific memory section? Or do I need to write some sort of custom memory pool manager?
Thanks