Reputation: 26060
I need a reference counter for an object not allocated on the heap.
I need it to implement a RAII mechanism on objects that cannot be easily copied and destructed:
class File
{
private:
int fd;
public:
File( const std::string &path ) ... // opening file
destroy( ); // actually closing file
File( const File &f ) ... // just copying the fd
~File( ); // doing nothing
}
For a scenario like this a std::shared_ptr
is usually used: the constructor and the destructor of the object whose pointer is shared are called only once.
In my case, however, I'd prefer avoiding allocating the object on the heap. I'd need a shared_object
class that does a job similar to std::shared_ptr
, so that my class' non-copy-constructor and destroy
function (in the example above) are called only once.
Does anything like this exist?
Upvotes: 0
Views: 411
Reputation: 68658
I believe the following architecture meets your requirements:
// pseudo-code
class File
{
private:
int fd;
File* prev;
File* next;
public:
File(const std::string &path) :
fd(open(path)),
prev(0),
next(0)
{}
void destroy()
{
close(fd);
}
File( const File &f )
fd(f.fd),
prev(&f),
next(f.next)
{
if (next)
next->prev = this;
f.next = this;
}
~File()
{
if (prev)
prev->next = next;
if (next)
next->prev = prev;
if ((!prev) && (!next))
destroy();
}
};
A doubly linked-list is maintained between duplicate File instances. The last member of the list, and hence the last duplicate calls destroy. No heap allocation required.
(Obviously this isn't thread safe. You can protect with a mutex, or you can use lock-free methods for maintaining the list.)
Upvotes: 0
Reputation: 29450
You could provide your own deleter to a std::shared_ptr that will call your custom destroy function instead of delete.
class File
{
private:
int fd;
public:
static void destroyThis(File* f){f->destroy();}
File( const std::string &path ) ... // opening file
void destroy( ); // actually closing file
File( const File &f ) ... // You probably don't need this anymore.
~File( ); // doing nothing
};
File fileObj("path");
std::shared_ptr<File> pf(&fileObj,std::bind(&File::destroyThis,std::placeholders::_1));
std::shared_ptr<File> pf2(pf);
Upvotes: 0
Reputation: 45224
If you want to have shared pointer behavior while allocating nothing in dynamic storage ("on the heap") you can look at various smart pointer implementation strategies. In Modern C++ Design, the author discusses many of these strategies in the "Smart Pointers" chapter, which is freely (and legally) available online.
The technique you will be interested in is reference linking. Using this technique, the smart pointer objects are linked together in a bi-directional doubly linked list instead of pointing to a dynamically allocated reference counter.
All that being said, using a std::shared_ptr
, std::unique_ptr
or their Boost variants will probably be much faster to write and easier to maintain. If dynamic allocation and the reference count are ever a bottleneck (I doubt it will be, but then we can't generalize too hastily), you can always take time to use a custom reference linking version instead.
Upvotes: 1