From 6a17e17bafabb4050327d13b34be62392b5ca569 Mon Sep 17 00:00:00 2001 From: Alexandre Bique Date: Fri, 15 May 2009 13:58:28 +0100 Subject: [PATCH] [ozulis] trying to debug current memory management --- README | 1 + src/ozulis/compiler.cc | 2 +- src/ozulis/core/CMakeLists.txt | 2 + src/ozulis/core/ref-counted-object.cc | 5 +- src/ozulis/core/ref-counted-object.hh | 18 ++-- src/ozulis/core/ref-counted-object.hxx | 158 ++++++++++++++++++---------- src/ozulis/core/tests/CMakeLists.txt | 11 ++ src/ozulis/core/tests/ref-counted-object.cc | 25 +++++ 8 files changed, 158 insertions(+), 64 deletions(-) create mode 100644 src/ozulis/core/tests/CMakeLists.txt create mode 100644 src/ozulis/core/tests/ref-counted-object.cc diff --git a/README b/README index 8744794..c4d1a0b 100644 --- a/README +++ b/README @@ -27,6 +27,7 @@ Dependancies: - http://rapidxml.sourceforge.net/ - jaula - http://morongo.homelinux.net/jaula/ + - duma You also need to apply a patch on /usr/share/bison/glr.c, the patch can be found in patches/ diff --git a/src/ozulis/compiler.cc b/src/ozulis/compiler.cc index 55e2e1a..87e1920 100644 --- a/src/ozulis/compiler.cc +++ b/src/ozulis/compiler.cc @@ -95,8 +95,8 @@ namespace ozulis Compiler::work(ParseTask & task) { ast::File * file = lang::ParserFactory::parseFile(task.filename); - files_[file->path]; file->path = task.filename; + files_[file->path] = file; if (astDumpParser_) { std::cout << "Parsed AST" << std::endl; visitors::AsciiPrinter v; diff --git a/src/ozulis/core/CMakeLists.txt b/src/ozulis/core/CMakeLists.txt index 9f89f55..0424159 100644 --- a/src/ozulis/core/CMakeLists.txt +++ b/src/ozulis/core/CMakeLists.txt @@ -18,3 +18,5 @@ ENDIF(UNIX) ADD_LIBRARY(ozulis-core SHARED ${CORE_SRCS}) SET_TARGET_PROPERTIES(ozulis-core PROPERTIES VERSION 1.0.0 SOVERSION 1) INSTALL(TARGETS ozulis-core DESTINATION lib) + +ADD_SUBDIRECTORY(tests) \ No newline at end of file diff --git a/src/ozulis/core/ref-counted-object.cc b/src/ozulis/core/ref-counted-object.cc index 27947b0..26d0ab5 100644 --- a/src/ozulis/core/ref-counted-object.cc +++ b/src/ozulis/core/ref-counted-object.cc @@ -8,13 +8,14 @@ namespace ozulis { RefCountedObject::~RefCountedObject() { + assert(refCount_ == 0); } void RefCountedObject::use() { refCount_++; - printf("\e[0;32m%s:%p %d\e[m\n", __PRETTY_FUNCTION__, this, refCount_); + printf("\e[0;32m[%p] %s count=%d\e[m\n", this, __PRETTY_FUNCTION__, refCount_); } void @@ -22,7 +23,7 @@ namespace ozulis { assert(refCount_ > 0); --refCount_; - printf("\e[0;31m%s:%p %d\e[m\n", __PRETTY_FUNCTION__, this, refCount_); + printf("\e[0;31m[%p] %s count=%d\e[m\n", this, __PRETTY_FUNCTION__, refCount_); if (refCount_ == 0) delete this; } diff --git a/src/ozulis/core/ref-counted-object.hh b/src/ozulis/core/ref-counted-object.hh index 0ac41b3..c5ba331 100644 --- a/src/ozulis/core/ref-counted-object.hh +++ b/src/ozulis/core/ref-counted-object.hh @@ -17,15 +17,16 @@ namespace ozulis void use(); void release(); - template + template class Ptr { public: typedef T * ptr_t; - template - inline Ptr(const Ptr &); inline Ptr(); + template + inline Ptr(const Ptr &); + inline Ptr(const Ptr &); inline Ptr(T * ptr); inline ~Ptr(); @@ -35,14 +36,17 @@ namespace ozulis inline T * operator->(); inline const T * operator->() const; - inline void operator=(T * ptr); - template - inline void operator=(const Ptr & ptr); + inline void operator=(T const * ptr); + template + inline void operator=(V * ptr); + template + inline void operator=(Ptr const & ptr); + inline void operator=(Ptr const & ptr); inline operator T *(); inline operator const T *() const; inline operator bool() const; - template + template inline operator Ptr (); inline T * ptr(); diff --git a/src/ozulis/core/ref-counted-object.hxx b/src/ozulis/core/ref-counted-object.hxx index 5f923e6..9ac30ac 100644 --- a/src/ozulis/core/ref-counted-object.hxx +++ b/src/ozulis/core/ref-counted-object.hxx @@ -1,3 +1,5 @@ +#include + namespace ozulis { namespace core @@ -5,6 +7,7 @@ namespace ozulis RefCountedObject::RefCountedObject() : refCount_(0) { + printf("[%p] %s\n", this, __PRETTY_FUNCTION__); } uint32_t @@ -13,134 +16,181 @@ namespace ozulis return refCount_; } - template - template + template + template RefCountedObject::Ptr::Ptr(const Ptr & other) - : ptr_(0) + : ptr_(const_cast (other.ptr())) { - puts(__PRETTY_FUNCTION__); - *this = other; + ptr_->use(); assert(ptr_->refCount() >= 2); + + printf("[%p -> %p] %s\n", this, ptr_, __PRETTY_FUNCTION__); } - template + template + RefCountedObject::Ptr::Ptr(const Ptr & other) + : ptr_(const_cast (other.ptr())) + { + ptr_->use(); + assert(ptr_->refCount() >= 2); + + printf("[%p -> %p] %s\n", this, ptr_, __PRETTY_FUNCTION__); + } + + template RefCountedObject::Ptr::Ptr() : ptr_(0) { + printf("[%p -> %p] %s\n", this, ptr_, __PRETTY_FUNCTION__); } - template + template RefCountedObject::Ptr::Ptr(T * _ptr) : ptr_(_ptr) { - puts(__PRETTY_FUNCTION__); + printf("[%p -> %p] %s\n", this, ptr_, __PRETTY_FUNCTION__); + if (_ptr) _ptr->use(); } - template + template + void + RefCountedObject::Ptr::operator=(T const * _ptr) + { + if (_ptr == ptr_) + return; + + if (ptr_) + ptr_->release(); + ptr_ = const_cast (_ptr); + if (ptr_) + ptr_->use(); + + printf("[%p -> %p] %s\n", this, ptr_, __PRETTY_FUNCTION__); + } + + template + template + void + RefCountedObject::Ptr::operator=(V * _ptr) + { + if (_ptr == ptr_) + return; + + if (ptr_) + ptr_->release(); + ptr_ = _ptr; + if (_ptr) + _ptr->use(); + + printf("[%p -> %p] %s\n", this, ptr_, __PRETTY_FUNCTION__); + } + + template + template + void + RefCountedObject::Ptr::operator=(Ptr const & _ptr) + { + if (_ptr.ptr() == ptr_) + return; + + if (ptr_) + ptr_->release(); + ptr_ = _ptr.ptr(); + if (ptr_) + ptr_->use(); + + printf("[%p -> %p] %s\n", this, ptr_, __PRETTY_FUNCTION__); + } + + template + void + RefCountedObject::Ptr::operator=(Ptr const & _ptr) + { + if (_ptr.ptr() == ptr_) + return; + + if (ptr_) + ptr_->release(); + ptr_ = const_cast(_ptr.ptr()); + if (ptr_) + ptr_->use(); + + printf("[%p -> %p] %s\n", this, ptr_, __PRETTY_FUNCTION__); + } + + template RefCountedObject::Ptr::~Ptr() { - puts(__PRETTY_FUNCTION__); + printf("[%p -> %p] %s\n", this, ptr_, __PRETTY_FUNCTION__); + if (ptr_) ptr_->release(); ptr_ = 0; } - template + template T * RefCountedObject::Ptr::operator->() { return ptr_; } - template + template const T * RefCountedObject::Ptr::operator->() const { return ptr_; } - template + template T & RefCountedObject::Ptr::operator*() { return *ptr_; } - template + template const T & RefCountedObject::Ptr::operator*() const { return *ptr_; } - template + template RefCountedObject::Ptr::operator T *() { return ptr_; } - template + template RefCountedObject::Ptr::operator const T *() const { return ptr_; } - template + template RefCountedObject::Ptr::operator bool() const { return ptr_ != 0; } - template - template + template + template RefCountedObject::Ptr::operator RefCountedObject::Ptr() { return Ptr(reinterpret_cast (ptr_)); } - template - void - RefCountedObject::Ptr::operator=(T * _ptr) - { - puts(__PRETTY_FUNCTION__); - - if (_ptr == ptr_) - return; - - if (ptr_) - ptr_->release(); - ptr_ = _ptr; - if (ptr_) - ptr_->use(); - } - - template - template - void - RefCountedObject::Ptr::operator=(const Ptr & _ptr) - { - puts(__PRETTY_FUNCTION__); - - if (_ptr.ptr() == ptr_) - return; - - if (ptr_) - ptr_->release(); - ptr_ = _ptr.ptr(); - if (ptr_) - ptr_->use(); - } - - template + template T * RefCountedObject::Ptr::ptr() { return ptr_; } - template + template const T * RefCountedObject::Ptr::ptr() const { diff --git a/src/ozulis/core/tests/CMakeLists.txt b/src/ozulis/core/tests/CMakeLists.txt new file mode 100644 index 0000000..aec2465 --- /dev/null +++ b/src/ozulis/core/tests/CMakeLists.txt @@ -0,0 +1,11 @@ +ADD_EXECUTABLE(ref-counted-object ref-counted-object.cc) +TARGET_LINK_LIBRARIES(ref-counted-object + ozulis-core + + boost_program_options${BOOST_LIB_SUFFIX} + boost_iostreams${BOOST_LIB_SUFFIX} + boost_system${BOOST_LIB_SUFFIX} + boost_filesystem${BOOST_LIB_SUFFIX} + + duma +) \ No newline at end of file diff --git a/src/ozulis/core/tests/ref-counted-object.cc b/src/ozulis/core/tests/ref-counted-object.cc new file mode 100644 index 0000000..ef64271 --- /dev/null +++ b/src/ozulis/core/tests/ref-counted-object.cc @@ -0,0 +1,25 @@ +#include + +#include +#include + +class Obj : public ozulis::core::RefCountedObject +{ +}; + +typedef ozulis::core::RefCountedObject::Ptr Ptr; + +void test1() +{ + Ptr p = new Obj; + Ptr p0 = p.ptr(); + Ptr p2(p); + Ptr p1 = p; +} + +int main() +{ + test1(); + + return 0; +} -- 2.11.4.GIT