🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2780

Re: STL allocator spec broken?

#2780
From: Alex Martelli
Date: Thu, 30 Nov 1995 17:52
68 lines
2875 bytes
akv@cacs.usl.edu (Anil Vijendran) writes:
	...
>I have a question in relation to this... I've seen persistence
>systems/libraries where the allocator or the heap itself is passed
>using the placement syntax... for example,

>	ptr = new (pstore) shape;

>I have always been wondering how the constructor of the class could
>allocate its data members on the same heap the top level object was
>placed in.

>I think the placement syntax when extended to persistent stores and
>such, whatever goes as the placement-expression, should be passed to
>the constructor too.. Or can this be done sans such an extension? 

Of course it can!  Just declare your "class shape" constructor to
(perhaps optionally) take a reference-to-allocator, eg:

class allocator {
public: virtual void* givemem(size_t bytes) = 0;
	virtual void freemem(void* ptr) = 0;
// etc
};

class default_allocator: public allocator {
public: virtual void* givemem(size_t bytes) {return ::operator new(bytes); }
  virtual void freemem(void *ptr) {::operator delete(ptr); }
// etc
};

void* operator new(size_t bytes, allocator& al) {
    return al.givemem(bytes);
}

class shape {
    int *amember;
    allocator &_al;
public: shape(allocator &al=default_allocator): _al(al) {
	    amember = new(al) int;
	}
        ~shape() {
	    _al.freemem(amember); // I just can't remember the placement
	                          // delete syntax...:-)
	}
	// etc
};

class pstore: public allocator { // etc
};
// ...
	shape *s = new(pstore) shape(pstore);

Basically, while it _might_ perhaps be nice to have syntax sugar to
avoid having to write pstore twice in this way, I do not think there
is really any burning need for such sugar...


Alex
--
DISCLAIMER: these are TOTALLY personal opinions and viewpoints, NOT connected
in any way with my employer, nor any other organization or individual!
Email: martelli@cadlab.it                            Phone: +39 (51) 597313
CAD.LAB s.p.a., v. Ronzani 7/29, Casalecchio, Italia   Fax: +39 (51) 597120

[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <DIvAAr.IDx@cadlab.it>
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
References: <48leps$pv2@boogie.cs.utexas.edu> <xg3ohtwl6t0.fsf@marsh.cacs.usl.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
From: martelli@cadlab.it (Alex Martelli)
Newsgroups: comp.std.c++
Subject: Re: STL allocator spec broken?
Date: 30 Nov 1995 17:52:37 GMT
Organization: CAD.LAB S.p.A., Bologna, Italia
Sender: news@cadlab.it
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <DIvAAr.IDx@cadlab.it>
References: <48leps$pv2@boogie.cs.utexas.edu> <xg3ohtwl6t0.fsf@marsh.cacs.usl.edu>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
X-Newsreader: NN version 6.5.0 CURRENT #2
Content-Length: 2001
X-Lines: 64
Originator: clamage@taumet