diff options
author | Tim King <taking@google.com> | 2016-11-06 22:27:00 -0800 |
---|---|---|
committer | Tim King <taking@google.com> | 2016-11-06 22:27:00 -0800 |
commit | b92880a5cfb374f4650052ba93919f473075926a (patch) | |
tree | bc3ea5bceac2be29de3c3155c00bce8968fd7c91 /src/base | |
parent | 7fa16f98bbc1cdfa450c55086dc093a9963b63d5 (diff) |
Adds a C++05 version of unique_ptr. Used this to solve a garbage collection problem caused by memory leaks of heap allocated Parsers.
Diffstat (limited to 'src/base')
-rw-r--r-- | src/base/Makefile.am | 1 | ||||
-rw-r--r-- | src/base/cvc4_unique_ptr.h | 70 |
2 files changed, 71 insertions, 0 deletions
diff --git a/src/base/Makefile.am b/src/base/Makefile.am index bda063176..94e45c542 100644 --- a/src/base/Makefile.am +++ b/src/base/Makefile.am @@ -20,6 +20,7 @@ libbase_la_SOURCES = \ configuration_private.h \ cvc4_assert.cpp \ cvc4_assert.h \ + cvc4_unique_ptr.h \ exception.cpp \ exception.h \ listener.cpp \ diff --git a/src/base/cvc4_unique_ptr.h b/src/base/cvc4_unique_ptr.h new file mode 100644 index 000000000..9642d0185 --- /dev/null +++ b/src/base/cvc4_unique_ptr.h @@ -0,0 +1,70 @@ +/********************* */ +/*! \file cvc4_unique_ptr.h + ** \verbatim + ** Top contributors (to current version): + ** Tim King + ** This file is part of the CVC4 project. + ** Copyright (c) 2009-2016 by the authors listed in the file AUTHORS + ** in the top-level source directory) and their institutional affiliations. + ** All rights reserved. See the file COPYING in the top-level source + ** directory for licensing information.\endverbatim + ** + ** \brief A CVC4 variant of unique_ptr for C++05. + ** + ** A CVC4 variant of unique_ptr for C++05. + **/ + +#include "cvc4_public.h" + +#ifndef __CVC4__UNIQUE_PTR_H +#define __CVC4__UNIQUE_PTR_H + +namespace CVC4 { + +/** + * A CVC4 variant of unique_ptr for C++05. + * + * This is a variant of unique_ptr that is not designed for move semantics. + * These are appropriate to own pointer allocations on the stack that should be + * deleted when an exception is thrown. These should be used with care within + * heap based data structures, and never as the return value of a function. + */ +template <class T> +class UniquePtr { + public: + UniquePtr() : d_pointer(NULL) {} + UniquePtr(T* pointer) : d_pointer(pointer) {} + ~UniquePtr() { delete d_pointer; } + + void reset(T* pointer) { + delete d_pointer; + d_pointer = pointer; + } + + /** Gives up ownership of the pointer to the caller. */ + T* release() { + T* copy = d_pointer; + d_pointer = NULL; + return copy; + } + + /** Returns the pointer. */ + T* get() const { return d_pointer; } + + /** Returns the pointer. Undefined if the pointer is null. */ + T* operator->() const { return d_pointer; } + + /** Returns true if the pointer is not-null. */ + operator bool() const { return d_pointer != NULL; } + + private: + UniquePtr(const UniquePtr*) CVC4_UNDEFINED; + UniquePtr& operator=(const UniquePtr&) CVC4_UNDEFINED; + + /** An owned pointer object allocated by `new` or NULL. */ + T* d_pointer; +}; /* class UniquePtr */ + +} /* CVC4 namespace */ + +#endif /* __CVC4__UNIQUE_PTR_H */ |