Jump to content
  • Advertisement
Sign in to follow this  
Misery

Placing a class on memory pool

This topic is 2517 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I would like to ask if I am thinking correctly (or there are some traps I didn't see).
I have a class:

template <class INT_TYPE,class DATA_TYPE>
class SomeClass
{
public:
INT_TYPE NumOfElements;
DATA_TYPE *Data;

// constructor, destructor and so on...
};


Can I allocate the memory from the pool in such a manner (for simplicity I will use PyMem_New() here - python memory pool allocation, returning pointer to void: *void):

int RequestedNumOfElements=SomeValue;
int RequiredSize=sizeof(AnIntType)+RequestedNumOfElements*sizeof(AFloatType);
SomeClass<AnIntType,AFloatType> *pSC=(SomeClass*)PyMem_New(RequiredSize)
*pSC.Data=(DATA_TYPE*)pSC+sizeof(AnIntType);


Am I right to think that I will be able to access my class instance in such a straightforward manner like below?


int a= pSC.NumOfElements;
for (int i=0;i<pSC.NumOfElements;i++) /*do something with */ pSC->Data


Will there be no padding bits, and some other stuff I may not have a clue about? What about compiler optimizations? May they influence the correctness running this code?

Thanks in advance,
and Reagrds,
Misery

Share this post


Link to post
Share on other sites
Advertisement
That code isn't correct - the required size calculation is not always going to give you the right answer. Try it with both template parameters as char for example.

The standard way to put a class into a memory pool is to override operator new and operator delete for the class. See http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.14

Share this post


Link to post
Share on other sites
Hmm... ok... So for my purposes, maybe a better solution would be to allocate separately the class (using new operator - as in the link You gave me), and then the data?

SomeClass *pSC=new(PyMem_New(sizeof(SomeClass() ));
pSC.N=SomeValue;
pSC.Data=new(PyMem_New(pSC.N*sizeof(AFloatType));


Would that work correctly?

Share this post


Link to post
Share on other sites
A common C idiom is the trailing zero length array:

struct Foo
{
int count;
Type data[0];
};

An object of this would be malloced using sizeof(Foo) + n * sizeof(Type);

I believe you can adapt this idiom to C++, but I can't remember if this is actually a standard supported idiom or just a de-facto supported one across most common compilers. Care would obviously have to be taken when "Type" is not POD (i.e. using placement new to call constructors).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!