Skip to content

Use small intrusive pointer in irep #786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/util/cow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#pragma once

template <typename T> class cow final {
public:
cow() = default;

template <typename... Ts>
explicit cow(Ts &&... ts) : t_(new T(std::forward<Ts>(ts)...)) {
t_->increment_use_count();
}

cow(const cow &rhs) {
if (!rhs.t_->is_unshareable()) {
t_ = rhs.t_;
t_->increment_use_count();
} else {
t_ = new T(*t_);
}
}

cow &operator=(const cow &rhs) {
auto copy(rhs);
swap(copy);
return *this;
}

cow(cow &&rhs) { swap(rhs); }

cow &operator=(cow &&rhs) {
swap(rhs);
return *this;
}

~cow() {
if (t_->is_unshareable()) {
delete t_;
} else {
t_->decrement_use_count();
if (t_->get_use_count() == 0) {
delete t_;
}
}
}

void swap(cow &rhs) {
using std::swap;
swap(t_, rhs.t_);
}

const T &operator*() const { return *t_; }

T &write(bool mark_unshareable) {
if (!t_->is_unshareable() && t_->get_use_count() != 1) {

} else {

}
}

private:
T *t_ = nullptr;
};

class cow_base {
public:
cow_base() = default;
cow_base(const cow_base &) {}
cow_base &operator=(const cow_base &) {}
cow_base(cow_base &&) {}
cow_base &operator=(cow_base &&) {}

void increment_use_count() { use_count_ += 1; }
void decrement_use_count() { use_count_ -= 1; }
std::size_t get_use_count() const { return use_count_; }

void set_unshareable(bool u) { use_count_ = u ? unshareable : 1; }
bool is_unshareable() { return use_count_ == unshareable; }

protected:
~cow_base() = default;

private:
static const std::size_t unshareable =
std::numeric_limits<std::size_t>::max();
std::size_t use_count_ = 0;
};
182 changes: 3 additions & 179 deletions src/util/irep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ Author: Daniel Kroening, kroening@kroening.com
#include <iostream>
#endif

irept nil_rep_storage;

#ifdef SHARING
irept::dt irept::empty_d;
#endif

/*******************************************************************\

Function: named_subt_lower_bound
Expand Down Expand Up @@ -75,177 +69,13 @@ Function: get_nil_irep

const irept &get_nil_irep()
{
static irept nil_rep_storage;
if(nil_rep_storage.id().empty()) // initialized?
nil_rep_storage.id(ID_nil);
return nil_rep_storage;
}

/*******************************************************************\

Function: irept::detach

Inputs:

Outputs:

Purpose:

\*******************************************************************/

#ifdef SHARING
void irept::detach()
{
#ifdef IREP_DEBUG
std::cout << "DETACH1: " << data << std::endl;
#endif

if(data==&empty_d)
{
data=new dt;

#ifdef IREP_DEBUG
std::cout << "ALLOCATED " << data << std::endl;
#endif
}
else if(data->ref_count>1)
{
dt *old_data(data);
data=new dt(*old_data);

#ifdef IREP_DEBUG
std::cout << "ALLOCATED " << data << std::endl;
#endif

data->ref_count=1;
remove_ref(old_data);
}

assert(data->ref_count==1);

#ifdef IREP_DEBUG
std::cout << "DETACH2: " << data << std::endl;
#endif
}
#endif

/*******************************************************************\

Function: irept::remove_ref

Inputs:

Outputs:

Purpose:

\*******************************************************************/

#ifdef SHARING
void irept::remove_ref(dt *old_data)
{
if(old_data==&empty_d)
return;

#if 0
nonrecursive_destructor(old_data);
#else

assert(old_data->ref_count!=0);

#ifdef IREP_DEBUG
std::cout << "R: " << old_data << " " << old_data->ref_count << std::endl;
#endif

old_data->ref_count--;
if(old_data->ref_count==0)
{
#ifdef IREP_DEBUG
std::cout << "D: " << pretty() << std::endl;
std::cout << "DELETING " << old_data->data
<< " " << old_data << std::endl;
old_data->clear();
std::cout << "DEALLOCATING " << old_data << "\n";
#endif

// may cause recursive call
delete old_data;

#ifdef IREP_DEBUG
std::cout << "DONE\n";
#endif
}
#endif
}
#endif

/*******************************************************************\

Function: irept::nonrecursive_destructor

Inputs:

Outputs:

Purpose: Does the same as remove_ref, but
using an explicit stack instead of recursion.

\*******************************************************************/

#ifdef SHARING
void irept::nonrecursive_destructor(dt *old_data)
{
std::vector<dt *> stack(1, old_data);

while(!stack.empty())
{
dt *d=stack.back();
stack.erase(--stack.end());
if(d==&empty_d)
continue;

assert(d->ref_count!=0);
d->ref_count--;

if(d->ref_count==0)
{
stack.reserve(stack.size()+
d->named_sub.size()+
d->comments.size()+
d->sub.size());

for(named_subt::iterator
it=d->named_sub.begin();
it!=d->named_sub.end();
it++)
{
stack.push_back(it->second.data);
it->second.data=&empty_d;
}

for(named_subt::iterator
it=d->comments.begin();
it!=d->comments.end();
it++)
{
stack.push_back(it->second.data);
it->second.data=&empty_d;
}

for(subt::iterator
it=d->sub.begin();
it!=d->sub.end();
it++)
{
stack.push_back(it->data);
it->data=&empty_d;
}

// now delete, won't do recursion
delete d;
}
nil_rep_storage.id(ID_nil);
}
return nil_rep_storage;
}
#endif

/*******************************************************************\

Expand All @@ -261,9 +91,6 @@ Function: irept::move_to_named_sub

void irept::move_to_named_sub(const irep_namet &name, irept &irep)
{
#ifdef SHARING
detach();
#endif
add(name).swap(irep);
irep.clear();
}
Expand All @@ -282,9 +109,6 @@ Function: irept::move_to_sub

void irept::move_to_sub(irept &irep)
{
#ifdef SHARING
detach();
#endif
get_sub().push_back(get_nil_irep());
get_sub().back().swap(irep);
}
Expand Down
Loading