Click here to Skip to main content
15,892,805 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
GeneralRe: converting BSTR to char* Pin
peterchen23-Nov-04 22:37
peterchen23-Nov-04 22:37 
GeneralRe: custom allocator for one container instance Pin
Joaquín M López Muñoz22-Nov-04 9:42
Joaquín M López Muñoz22-Nov-04 9:42 
GeneralRe: custom allocator for one container instance Pin
peterchen22-Nov-04 13:04
peterchen22-Nov-04 13:04 
GeneralRe: custom allocator for one container instance Pin
Joaquín M López Muñoz22-Nov-04 21:50
Joaquín M López Muñoz22-Nov-04 21:50 
GeneralRe: custom allocator for one container instance Pin
Andrew Walker22-Nov-04 16:02
Andrew Walker22-Nov-04 16:02 
GeneralRe: custom allocator for one container instance Pin
Joaquín M López Muñoz22-Nov-04 21:57
Joaquín M López Muñoz22-Nov-04 21:57 
GeneralRe: custom allocator for one container instance Pin
peterchen22-Nov-04 23:21
peterchen22-Nov-04 23:21 
GeneralRe: custom allocator for one container instance Pin
Joaquín M López Muñoz22-Nov-04 23:03
Joaquín M López Muñoz22-Nov-04 23:03 
Please find below a (working) prototype for the kind of stuff you're looking for. Things to keep in mind:
  • The allocator does not check whether it's running out of space. It is up to you to dimension the pool appropriately
  • No alignment issues are consided, though this is not a problem on Intel architectures.
Please consider this as a very rough prototype, but it can get you started.
#include <stddef.h>
 
template<typename T,size_t pool_size>
class brute_allocator
{
public:
  brute_allocator():
    pool(new char[pool_size]),next(pool)
  {}
 
  brute_allocator(const brute_allocator&):
    pool(new char[pool_size]),next(pool)
  {}
 
  brute_allocator& operator=(const brute_allocator&)
  {
    return *this;
  }
 
  ~brute_allocator()
  {
    delete[]pool;
  }
 
  typedef size_t     size_type;
  typedef ptrdiff_t  difference_type;
  typedef T         *pointer;
  typedef const T   *const_pointer;
  typedef T&         reference;
  typedef const T&   const_reference;
  typedef T          value_type;
 
  template<typename Other> struct rebind
  {
    typedef brute_allocator<Other,pool_size> other;
  };
 
  pointer address(reference x)const{return &x;}
  const_pointer address(const_reference x)const{return &x;}
 
  pointer allocate(size_type n,const void *hint=0)
  {
    return reinterpret_cast<pointer>(_Charalloc(sizeof(T)));
  }
 
  char* _Charalloc(size_type n)
  {
    char* res=next;
    next+=n;
    return res;
  }
 
  void deallocate(void *p,size_type n)
  {
  }
 
  void construct(pointer p,const T& val)
  {
    new ((void *)p) T(val);
  }
 
  void destroy(pointer p)
  {
    p->T::~T();
  }
 
  size_type max_size()const
  {
    size_type n=(size_type)(-1)/sizeof(T);
    return (0<n?n:1);
  }
 
  /* Every two instances are treated as different */
 
  bool operator ==(const brute_allocator& x) const
  {
    return this==&x;
  }
 
  bool operator !=(const brute_allocator& x) const
  {
    return this!=&x;
  }
 
  /* This two member functions never get called, but VC complains if no
   * declaration for them is provided. See documentation for this
   * VC++ confirmed bug in the Microsoft Knowledge Base, article Q166721.
   * I think this has been fixed in VC++ 6.0.
   */
 
  bool operator <(const brute_allocator&) const;
  bool operator >(const brute_allocator&) const;
 
private:
  char* pool;
  char* next;
};
 
#include <map>
#include <iostream>
 
typedef brute_allocator<int,20000> b_allocator;
typedef std::map<int,int,std::less<int>,b_allocator> fast_map;
 
int main()
{
  fast_map fm;
 
  for(int i=0;i<500;++i)fm[i]=2*i;
 
  for(fast_map::iterator it=fm.begin(),it_end=fm.end();it!=it_end;++it){
    std::cout<<it->first<<" "<<it->second<<std::endl;
  }
 
  return 0;
}


Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
Want a Boost forum in Code Project? Vote here[^]!
Generalcustom allocator for one container instance Pin
peterchen22-Nov-04 8:19
peterchen22-Nov-04 8:19 
Generallist::sort() question Pin
nm_11421-Nov-04 16:51
nm_11421-Nov-04 16:51 
GeneralRe: list::sort() question Pin
Ryan Binns21-Nov-04 18:06
Ryan Binns21-Nov-04 18:06 
GeneralRe: list::sort() question Pin
nm_11421-Nov-04 18:58
nm_11421-Nov-04 18:58 
GeneralRe: list::sort() question Pin
peterchen22-Nov-04 8:23
peterchen22-Nov-04 8:23 
GeneralRe: list::sort() question Pin
nm_11422-Nov-04 19:29
nm_11422-Nov-04 19:29 
GeneralRe: list::sort() question Pin
peterchen22-Nov-04 23:15
peterchen22-Nov-04 23:15 
GeneralRe: list::sort() question Pin
nm_11423-Nov-04 13:38
nm_11423-Nov-04 13:38 
GeneralRe: list::sort() question Pin
peterchen23-Nov-04 13:49
peterchen23-Nov-04 13:49 
GeneralRe: list::sort() question Pin
nm_11423-Nov-04 14:08
nm_11423-Nov-04 14:08 
GeneralRe: list::sort() question Pin
toxcct23-Nov-04 8:12
toxcct23-Nov-04 8:12 
GeneralRe: list::sort() question Pin
peterchen23-Nov-04 13:58
peterchen23-Nov-04 13:58 
GeneralRe: list::sort() question Pin
toxcct23-Nov-04 21:37
toxcct23-Nov-04 21:37 
GeneralRe: list::sort() question Pin
peterchen23-Nov-04 22:28
peterchen23-Nov-04 22:28 
GeneralRe: list::sort() question Pin
Nemanja Trifunovic24-Nov-04 2:21
Nemanja Trifunovic24-Nov-04 2:21 
GeneralATL7 and VS.NET2k3 wizard, completely stuck Pin
seriema19-Nov-04 10:59
seriema19-Nov-04 10:59 
GeneralRe: ATL7 and VS.NET2k3 wizard, completely stuck Pin
Michael Dunn19-Nov-04 13:25
sitebuilderMichael Dunn19-Nov-04 13:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.