473,385 Members | 2,028 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,385 software developers and data experts.

Custom Pool Allocator Question

joe
I have created a custom allocator for a multiset, and I am having a
problem it seems because while the allocator has memory available to
be used which is returned, the objects are never constructed on top of
that memory. I expected my "construct" method to be called to handle
this, but no one calls it.

No more than MAX_OBJS object of class Obj will ever be allocated.

#include<iostream>
#include <set>

const int MAX_OBJS = 100;
template<typename T>
class ObjAllocator
{
public:
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

template<typename U>
struct rebind { typedef ObjAllocator<Uother; };

explicit ObjAllocator() : m_available(0) { m_objs = new
char[sizeof(Obj)*MAX_OBJS]; }

~ObjAllocator() {}

explicit ObjAllocator(ObjAllocator const& rhs) {m_available =
rhs.m_available;}

template<typename U>
ObjAllocator(ObjAllocator<Uconst& rhs) { m_available =
rhs.m_available;}

pointer address(reference r) const {return &r;}
const_pointer address(const_reference r) const { return &r;}

pointer allocate(size_type cnt,
typename std::allocator<void>::const_pointer
hint = 0)
{
char *temp = &(m_objs[m_available * sizeof(Obj)]);
m_available = (m_available+1) % MAX_OBJS;
return reinterpret_cast<pointer>(temp);
}

void deallocate(pointer p, size_type) {}

size_type max_size() const { return MAX_OBJS;}

void construct(pointer p, const T& det) {std::cout<<"NEVER EVER
CALLED"<<std::endl;}

void destroy(pointer p){std::cout<<"ALSO NEVER CALLED"<<std::endl;}

int m_available;
char * m_objs;
};

class Thing
{
// Crashes on first "insert"
std::multiset<Obj,ObjComparator,ObjAllocator<Obj m_multiSet;
};

Thanks!

May 8 '07 #1
0 1297

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

Similar topics

0
by: porschberg | last post by:
Hi, I have question regarding the pool library. In my application I read a lot of data records from a database into a small data class and therefor I thought object_pool could help me to reduce...
4
by: Romeo Colacitti | last post by:
I have a need to make a custom quasi-memory allocator, and I remembered a simple ons in K&R2. Looking at the code for it now, I think I notice a "fault" in the design, and I was wondering if...
3
by: Frank Wisniewski | last post by:
I have the following persudo code: //My Form Class class Form1 { //Local Variable for my custom object private MyCustomObject1 //Constructor for Form Class public Constructor{
4
by: fniles | last post by:
When using connection pooling, I understand the default Max Pool Size = 100. What happens when to the 101 or more connection ? I mean, if 101st tries to use the connection pooling, what happen to...
2
by: Josh | last post by:
I have a segmentation fault, and gdb says that the problem is in the line: buffer+=(s+' '); inside the + operator. "buffer" and "s" are both C++ strings. When I print out buffer.c_str()...
3
by: joe | last post by:
I have written a custom std allocator which follows the example in Stroustrup's book. I'm seeing a behavior I don't understand. My allocate method within the allocator looks like the...
3
by: Tom | last post by:
When I set my app pool Identity tab to a particular username and password, I'm prompted for an username and password when I try to access the web site, but when I have Identity set to 'predefined...
6
by: mavrik | last post by:
Hi All, I've created STL allocator which creates memory pool in the beginning, and all maps and vectors will take memory from that memory pool. But...I am observing that application has become...
6
by: Juha Nieminen | last post by:
I tested the speed of a simple program like this: //------------------------------------------------------------ #include <list> #include <boost/pool/pool_alloc.hpp> int main() { typedef...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.