473,383 Members | 1,742 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,383 software developers and data experts.

Mem pool question

Hi,

I have been playing around with the suggestions and code shown in

http://www.parashift.com/c++-faq-lit...html#faq-11.14

regarding memory allocation/deallocation (mem pools). I think I understand
what the FAQ was getting at (emphasis on "think") so I concluded that
attempting to make a delete statement on an area of memory that was created
in a pool with the placement new operator (overloaded) would not allow me to
call an overloaded delete operator with the signature that included the pool
in the parm list, which was shown in the FAQ example. Am I correct? The
sample code is below. I hard-wired the template selector for this example
after creating a pool_alloc template class in an effort to separate the
different types of memory blocks.

// main.cpp
#include <iostream>

class MemoryPool
{
public:
void *calloc( unsigned size )
{
void *memory = malloc( size );
if( !memory ) { cout << "Out of memory!\n"; return 0; }
memset(memory, 0, size);
return memory;
}
void deallocate( void *block )
{
free( block );
}
};

template <short BLOCK_TYPE = 0>
class pool_alloc
{
public:
inline void * operator new( unsigned s, MemoryPool& p )
{ return p.calloc(s);}

// I want to call this delete
void operator delete( void *mem, MemoryPool& p )
{ if( mem ) p.deallocate( mem );}
};

class Foo : public pool_alloc<1>
{
public:
Foo():fooInt(0){};
Foo( MemoryPool& p, unsigned len ) {};
~Foo() {};

private:
int fooInt;
};

int main( void )
{
Foo *fooP;
MemoryPool p;

fooP = new(p) Foo( p, 3 );

// with this delete statement
//but don't think it's possible or even makes sense
delete fooP;

}

When I compile this code I get the error I thought I'd get:

error: no suitable `operator delete' for `Foo'

And when I add a delete operator without the MemoryPool reference in the
signature, all is well. That too makes sense to me. I don't think there's
a way to call the delete operator and have it "know about" the pool where
the alloc'd memory resides. Again, am I on the right track here?

Thanks in advance,

Jerry
Mar 14 '07 #1
2 3193
And when I add a delete operator without the MemoryPool reference in the
signature, all is well. That too makes sense to me. I don't think
there's a way to call the delete operator and have it "know about" the
pool where the alloc'd memory resides. Again, am I on the right track
here?
I am sorry if this response is basically way OT wrt your Standard C++
question... But for some reason, I felt the need to mention it to you.

:^0

Anyway and FWIW, you can indeed associate a block of allocated memory with
the "slab" it belongs to. This can be as simple as defining a "common value"
for your slabs state to align themselves on. More precisely, the slabs state
will be aligning themselves on boundaries that are compatible with the
"common value". Now, you can define whether the memory blocks will go after
the slab state, or before it... Now, you can grab a pointer to the slab that
a arbitrary memory block belongs to by rounding a pointer to the block up,
or down (e.g., depends on how your define block placement), to a value that
is aligned on a 'common value' boundary. Once your on the boundary you can
cast the aligned pointer to a ptr to a slabs state. Here is brief outline:

http://groups.google.com/group/comp....019dc04362be41

http://groups.google.com/group/comp....3aeaa6ebf5181f
So, technically speaking, if you use something that is analogous to the
above technique, you can use the normal signature of 'operator delete'... No
need to pass a pool pointer when you can simply round a pointer to a block
to the next, or previous "common value" boundary.
----
P.S. and, only if your interested!

I am in the middle of doing 100% complete implementation of a stack-based
allocation scheme. If your even remotely interested, go ahead keep a
periodic watch on the following thread in this group:

http://groups.google.com/group/comp....eee1f61fdbb52c
(everything is fairly low-level at this point, the user-level interface will
be arriving soon...)

Once I get to the threading aspects, I will include comp.programming.threads
in the conversation. Luckily for me this allocator is very thread-local, so
I can Standard C++ to implement most of its logic.
Mar 14 '07 #2
On Wed, 14 Mar 2007 15:23:16 -0400, "Jerry Adair" wrote:
>I have been playing around with the suggestions and code shown in
http://www.parashift.com/c++-faq-lit...html#faq-11.14

regarding memory allocation/deallocation (mem pools). I think I understand
what the FAQ was getting at (emphasis on "think") so I concluded that
attempting to make a delete statement on an area of memory that was created
in a pool with the placement new operator (overloaded) would not allow me to
call an overloaded delete operator with the signature that included the pool
in the parm list, which was shown in the FAQ example. Am I correct? The
sample code is below. I hard-wired the template selector for this example
after creating a pool_alloc template class in an effort to separate the
different types of memory blocks.

// main.cpp
#include <iostream>

class MemoryPool
{
public:
void *calloc( unsigned size )
{
void *memory = malloc( size );
if( !memory ) { cout << "Out of memory!\n"; return 0; }
memset(memory, 0, size);
return memory;
}
void deallocate( void *block )
{
free( block );
}
};

template <short BLOCK_TYPE = 0>
class pool_alloc
{
public:
inline void * operator new( unsigned s, MemoryPool& p )
{ return p.calloc(s);}

// I want to call this delete
void operator delete( void *mem, MemoryPool& p )
{ if( mem ) p.deallocate( mem );}
};

class Foo : public pool_alloc<1>
{
public:
Foo():fooInt(0){};
Foo( MemoryPool& p, unsigned len ) {};
~Foo() {};

private:
int fooInt;
};

int main( void )
{
Foo *fooP;
MemoryPool p;

fooP = new(p) Foo( p, 3 );

// with this delete statement
//but don't think it's possible or even makes sense
delete fooP;
// it's possible but probably doesn't make sense
fooP->~Foo();
pool_alloc<1::operator delete (fooP, p);

Best wishes,
Roland Pibinger
Mar 14 '07 #3

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

Similar topics

9
by: Philip Lawatsch | last post by:
Hi, I have some questions about whats written in http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.14 (Describing some memory pool) #1 From what i understand this will also work for new...
2
by: VinDotNet | last post by:
Here's the Scenario I am facing: I've got a winform managed c++ client which connects to my managed COM+ server application (by making createinstance, then typecasting the object to an interface,...
8
by: ra294 | last post by:
I have an ASP.net application using SQL Server 2000 that every once in a while I am getting this error: "System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to...
9
by: Abhishek Srivastava | last post by:
Hello All, In IIS 6.0 We have a concept of worker processes and application pools. As I understand it, we can have multiple worker process per appliction pool. Each worker process is dedicated...
5
by: J-T | last post by:
I guess I'm a litte bit confused about app pool and worker process. In IIS 6.0 We have a concept of worker processes and application pools. As I understand it, we can have multiple worker process...
1
by: henley.steve | last post by:
Hello, I have a problem with an application wherein the code as designed originally was not ensuring closing of connections when the application encountered an exception. I have fixed this...
3
by: Vlad Hrybok | last post by:
Hi all, I recently installed 64 bit version of Vista hoping to make it my primary development setup. I was able to do work with IIS7 and ASP.NET 2.0/Visua Studio 2005 combo, but hit a roadblock...
3
by: Venkat | last post by:
Hi, I am working on an application (developed using c#2.0) which needs to do a big job and when you start the job in a single thread it takes long time to complete. So, we want to break the job...
5
markrawlingson
by: markrawlingson | last post by:
Hey guys, Having a bit of a complicated issue here so please bare with me while I explain. I'm also not a system admin and don't know a whole lot about IIS, so i apologize in advance. I...
4
by: Oriane | last post by:
Hello, I will soon installed a small Ajax Asp.Net "single page" site on a client site. This page is simply polling a Asp.Net Web service, which fetchs and returns parameters (temperature, air...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.