473,406 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Question about Memory Pool Example in C++ Faq

Hi,

I have some questions about whats written in
http://www.parashift.com/c++-faq-lit...html#faq-11.14
(Describing some memory pool)

#1 From what i understand this will also work for new x[] and delete
[]x, or did i misunderstand something ?

and,

#2 At the time of destruction i do know from which pool my object came,
and thus i'd like to avoid using a global delete operator.

The example says that I could simply call the destructor and then my
pool's dealloc method.
But, what do I have to do if I do not have one single object, but an
array of objects ?
Call each objects destructor and then a dealloc for the whole array ?
Isn't there a way to simply call some delete[] (mypool) my_array ?

with kind regards philip

Jul 19 '05 #1
9 7205


John Harrison wrote:

#2 At the time of destruction i do know from which pool my object came,
and thus i'd like to avoid using a global delete operator.

The example says that I could simply call the destructor and then my
pool's dealloc method.
But, what do I have to do if I do not have one single object, but an
array of objects ?
Call each objects destructor and then a dealloc for the whole array ?

That's right.


But this is a hassle for me, since I then have to rember the size of
each array (the number of elements in there) or otherwise i would not be
able to delete it correctly.

Looks like this will just increase my overhead and not give me any
significant speedups.

My problem is, i'm doing billions (!) of new and delete (and copy ctors)
on small int arrays, which results in quie slow code.
Isn't there a way to simply call some delete[] (mypool) my_array ?

It might work, but rules of C++ say that if you use placement new you must
use explicit delete. So, best stick to the rules.


Well, not sure if you understood what i meant.
I'm looking for something like

int * foo = new (mypool) int[x];
delete[] (mypool) foo;

Something that will call the ctors and then call my pools delete, but
for arrays, and for my specific pool, just like the placement new does.

with kind regards philip

Jul 19 '05 #2

"Philip Lawatsch" <ph****@waug.at> wrote in message
news:3f********@e-post.inode.at...


John Harrison wrote:

#2 At the time of destruction i do know from which pool my object came,
and thus i'd like to avoid using a global delete operator.

The example says that I could simply call the destructor and then my
pool's dealloc method.
But, what do I have to do if I do not have one single object, but an
array of objects ?
Call each objects destructor and then a dealloc for the whole array ?

That's right.


But this is a hassle for me, since I then have to rember the size of
each array (the number of elements in there) or otherwise i would not be
able to delete it correctly.

Looks like this will just increase my overhead and not give me any
significant speedups.

My problem is, i'm doing billions (!) of new and delete (and copy ctors)
on small int arrays, which results in quie slow code.


Perhaps you should have a look at the small object allocator that is part of
the Loki library. I did some tests on it myself and found a significant
speed up compared to the allocator that comes with my compiler.

http://sourceforge.net/projects/loki-lib/

Also described in the book 'Modern C++ Design' by Andrei Alexandrescu. Well
worth having.
Isn't there a way to simply call some delete[] (mypool) my_array ?

It might work, but rules of C++ say that if you use placement new you must use explicit delete. So, best stick to the rules.


Well, not sure if you understood what i meant.
I'm looking for something like

int * foo = new (mypool) int[x];
delete[] (mypool) foo;


Well that's fine.

Something that will call the ctors and then call my pools delete, but
for arrays, and for my specific pool, just like the placement new does.

with kind regards philip


john
Jul 19 '05 #3


John Harrison wrote:
Well, not sure if you understood what i meant.
I'm looking for something like

int * foo = new (mypool) int[x];
delete[] (mypool) foo;

Well that's fine.


Really ?
Neat, from what I understood this is not valid, I really gotta write
some small example and give it a try.
Perhaps the FAQ should be updated slightly to make that more clear

with kind regards Philip

Jul 19 '05 #4

"Philip Lawatsch" <ph****@waug.at> wrote in message
news:3f********@e-post.inode.at...


John Harrison wrote:
Well, not sure if you understood what i meant.
I'm looking for something like

int * foo = new (mypool) int[x];
delete[] (mypool) foo;

Well that's fine.


Really ?
Neat, from what I understood this is not valid, I really gotta write
some small example and give it a try.
Perhaps the FAQ should be updated slightly to make that more clear

with kind regards Philip


Actually its not, I don't know what I was thinking of. Sorry about that.

Placement delete does exist but it is only called during exception handling,
it can't be called explicitly. E.g.

#include <iostream>
using namespace std;

class Pool
{
};

class X
{
public:
X() { /*throw "error";*/ }
void* operator new[](size_t bytes, Pool& p)
{
cout << "placement new\n";
return malloc(bytes);
}
void operator delete[](void* ptr, Pool& p)
{
cout << "placement delete\n";
free(ptr);
}
void operator delete[](void* ptr)
{
cout << "ordinary delete\n";
free(ptr);
}
};

int main()
{
Pool pool;
try
{
X* x = new (pool) X[10];
delete[] x;
}
catch (...)
{
}
}

This program prints

placement new
ordinary delete

but uncommect the throw statement from the X constructor and it prints

placement new
placement delete

See http://std.dkuug.dk/jtc1/sc22/wg21/d...1995/N0642.pdf for
instance.

john
Jul 19 '05 #5
>
Placement delete does exist but it is only called during exception handling, it can't be called explicitly. E.g.

#include <iostream>
using namespace std;

class Pool
{
};

class X
{
public:
X() { /*throw "error";*/ }
void* operator new[](size_t bytes, Pool& p)
{
cout << "placement new\n";
return malloc(bytes);
}
void operator delete[](void* ptr, Pool& p)
{
cout << "placement delete\n";
free(ptr);
}
void operator delete[](void* ptr)
{
cout << "ordinary delete\n";
free(ptr);
}
};

int main()
{
Pool pool;
try
{
X* x = new (pool) X[10];
delete[] x;
}
catch (...)
{
}
}

This program prints

placement new
ordinary delete

but uncommect the throw statement from the X constructor and it prints

placement new
placement delete

See http://std.dkuug.dk/jtc1/sc22/wg21/d...1995/N0642.pdf for
instance.

john


However AFAIK you can call placement delete explicitly, e.g. in terms of the
code above

X::operator delete[](x, pool);

of course you have to call destructors explicitly as well.

I think you know as much about this topic as I do (if not more).

john
Jul 19 '05 #6
Philip Lawatsch <ph****@waug.at> wrote in message news:<3f********@e-post.inode.at>...
John Harrison wrote:
#2 At the time of destruction i do know from which pool my object came,
and thus i'd like to avoid using a global delete operator.

The example says that I could simply call the destructor and then my
pool's dealloc method. But, what do I have to do if I do not have
one single object, but an array of objects ?
Call each objects destructor and then a dealloc for the whole array ?


That's right.


But this is a hassle for me, since I then have to rember the size of
each array (the number of elements in there) or otherwise i would not be
able to delete it correctly.

My problem is, i'm doing billions (!) of new and delete (and copy ctors)
on small int arrays, which results in quie slow code.


No, it won't be slow - at least not for the reasons mentioned -
because deleting an int is a no-op.

You don't even have to write the explicit dtor call, though.
int, like all C types is called a 'POD' in C++. That means Plain Old
Data. For C compatibility, you can still treat those types like in C.

Regards
--
Michiel Salters
Jul 19 '05 #7


Michiel Salters wrote:

No, it won't be slow - at least not for the reasons mentioned -
because deleting an int is a no-op.

You don't even have to write the explicit dtor call, though.
int, like all C types is called a 'POD' in C++. That means Plain Old
Data. For C compatibility, you can still treat those types like in C.


Well, for the sake of code beautifullness, i'd like to let the compiler
handle all of these.

This stuff would go right into an array template class, and thus i'd
have to distinguish between atomic types and objects, which is something
i'd rather avoid. (I do not have rtti support in my programme for
instance, and have no intentions to add it)

But, from all the answers i've got it seems that everything boils down
to exaclty this.

Sad though, since i thought that there was a better solution to this
Btw, is there a nice way to differ between atomic (PODs) and other types
at compile time ? (for templates)

Like

template <class A>
class foo
{
void something()
{
if (A kindof POD)
somecode
else
othercode
}
};
But at compile time, NOT runtime ?!
with kind regarsd philip

Jul 19 '05 #8


John Harrison wrote:
However AFAIK you can call placement delete explicitly, e.g. in terms of the
code above

X::operator delete[](x, pool);

of course you have to call destructors explicitly as well.


Hmmmm ...

Just a thought, my pools delete ops will only get called in case of an
exception by new, isn't it ?

Wouldnt it be possible for me to write a delete operator that takes a
ptr to my Base class and call the destructors in there ?

pseudo code:

class memory_pool
{

void operator delete (void * ptr, Pool & pool) // used in case of
exception at new

void operator delete (void * ptr) // used for atomic types
void operator delete (Base * ptr) // used for everything derived from Base
}

and have a template class

template <class A>
{
void free()
{
some_pool::operator delete (data);
}

A * data

}

In this case the free method of the template would always choose the
correct delete itself, isn't it ?
Its just really bad if you try to delete something that needs a dtor but
is NOT derived from Base ...

with kind regards philip

Jul 19 '05 #9
On Wed, 09 Jul 2003 17:10:28 +0200, Philip Lawatsch <ph****@waug.at>
wrote:
Btw, is there a nice way to differ between atomic (PODs) and other types
at compile time ? (for templates)
Yes, type traits, although compiler support is required to determine
that some types are PODS. A type traits implementation without
compiler support can manage to tell that these are PODs:

int
double
etc.
int*
T*
POD[]
etc.

but can't for UDT's like this:

struct POD
{
int x;
int y;
};

Some compilers do provide support with intrinsic type traits (SGI's
and perhaps Metroworks?)

Like

template <class A>
class foo
{
void something()
{
if (A kindof POD)
somecode
else
othercode
}
};
But at compile time, NOT runtime ?!


Here's one way:

template<class A, bool impl = is_pod<A>::value>
class foo
{
//pod implementation
};

template <class A>
class foo<A, false>
{
//non pod implenentation
};

Obviously you can select at a finer grain if you prefer, using
forwarding to overloaded "something_impl" functions taking a true_type
and false_type.

You can get type_traits from www.boost.org, or you could implement
them yourself if you really wanted.

Tom
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Tron Thomas | last post by:
As part of applying for a programming position at a company, I recently I had submitted some code samples to one of the developers for review. This is the feedback I received: One of his...
18
by: Tron Thomas | last post by:
Given the following information about memory management in C++: ----- The c-runtime dynamic memory manager (and most other commercial memory managers) has issues with fragmentation similar to a...
22
by: xixi | last post by:
hi, we are using db2 udb v8.1 for windows, i have changed the buffer pool size to accommadate better performance, say size 200000, if i have multiple connection to the same database from...
5
by: bull | last post by:
hi could any one explain with example the following in a better way to understand 1. what is stack in memory, how the programs are stored in stack , what is the use of stack 2. What is heap in...
24
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as...
0
by: Raj | last post by:
We are on a db2 v8.2 (fix 8) with DPF & intra parllelism. Below are sort related configuration settings ...
2
by: Jerry Adair | last post by:
Hi, I have been playing around with the suggestions and code shown in http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.14 regarding memory allocation/deallocation (mem pools). I...
17
by: frederic.pica | last post by:
Greets, I've some troubles getting my memory freed by python, how can I force it to release the memory ? I've tried del and gc.collect() with no success. Here is a code sample, parsing an XML...
11
by: Grey Alien | last post by:
I am looking to write a very simple memory pool library to store only one data type at a time - i.e. to provide a contiguous block of memory to be alloc'd free'd by the calling program. I am I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.