memory pool

Aug 18, 2011 at 5:47pm
I haven't had much experience with templates, but I wanted to make a memory pool class which can allocate any kind of object which is specified when the constructor is called, and allow them to be returned sequentially one at a time by a function call without again passing a template parameter. This was my first attempt but I realised it's impossible to do it this way as we don't know the type in getNext()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class MemoryPool{
	template<typename T>
	MemoryPool(int numObjects)
	{
		position = 0;
		size = sizeof(T);
		memory = new T[numObjects];
	}

	T* getNext()
	{
		position++;
		return static_cast<T*>(memory[position*size-size]);
	}

private:

	void* memory;
	int size;
	int position;
};


Is there any way of storing a type? I can't help feeling that my approach is all wrong either, I don't know how I got this far into C++ without ever really using templates...so if anyone has made a memory pool before and can suggest a better way of doing it that'd be great.

Thanks.
Aug 18, 2011 at 7:12pm
You have some pretty good timing. This sounds like what 'auto' was repurposed to do in the recently finalized C++11 standard.
Aug 18, 2011 at 7:34pm
I have no idea what you are talking about. A constructor is only called once, so you have one template ergument per one instance of the class. That's just a regular template. Swap lines 1 and 2 and it should (almost) work.

Where you thinking about some sort of stack that could hold any kind of elements?
Aug 18, 2011 at 7:47pm
@ hamsterman: It sounds like he just wants it to hold one datatype. But "T" is created when the object is initiated so this should work right? I thought the OP wanted to use the new 'auto' so that they don't have to type as much?
Aug 18, 2011 at 7:55pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template<typename T>
class MemoryPool{
    MemoryPool(int numObjects)
    {
        position = 0;
        memory = new T[numObjects];
    }

    T* getNext()
    {
        position++;
        return memory[position-1];
    }

private:

    T* memory;
    int position;
};
Is the way to do it then. "T" is "created" at compile time.
Aug 18, 2011 at 8:15pm
Oh...well that was embarassingly easy. Thanks guys.
Aug 18, 2011 at 8:26pm
Glad I could help. Now, here's some template-foo, in case you do want a type preserving stack.
http://pastebin.com/kzD0TsD8
I guess that Nil is a bit unnecessary..
Aug 18, 2011 at 8:57pm
I just wanted  to  add that if you want to delay the  T  constructor calls,
you could reserve memory using malloc in your MemoryPool constructor
and then  use  placement new inside your  getNext  function (you could
provide several  getNext  overloads with varying number of arguments).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cstdlib>

using namespace std;

struct MyStruct
{
    int    a;
    double b;

    MyStruct(int a_, double b_):
        a(a_), b(b_) {}
};

int main()
{
    void * pv = malloc(sizeof(MyStruct));

    MyStruct * pms = new (pv) MyStruct(1, 2.3);

    cout << "pv  = " << pv  << endl;
    cout << "pms = " << pms << endl;

    cout << "\na = " << pms->a << endl;
    cout <<   "b = " << pms->b << endl;

    pms->~MyStruct();

    free(pv);
}

Food for thought -> http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.14
Last edited on Aug 18, 2011 at 8:59pm
Topic archived. No new replies allowed.