diff --git a/Makefile.am b/Makefile.am index 0efeaba2..825465f8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -58,6 +58,8 @@ UTIL_FILES = \ maths/vector.hpp \ matrix.cpp \ matrix.hpp \ + memory.cpp \ + memory.hpp \ nocopy.hpp \ perlin.cpp \ perlin.hpp \ diff --git a/memory.cpp b/memory.cpp new file mode 100644 index 00000000..ca568025 --- /dev/null +++ b/memory.cpp @@ -0,0 +1,20 @@ +/* + * This file is part of libgim. + * + * libgim is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * libgim is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * Copyright 2011 Danny Robson + */ + +#include "memory.hpp" diff --git a/memory.hpp b/memory.hpp new file mode 100644 index 00000000..4ea18f5c --- /dev/null +++ b/memory.hpp @@ -0,0 +1,49 @@ +/* + * This file is part of libgim. + * + * libgim is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * libgim is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * Copyright 2011 Danny Robson + */ + +#ifndef __MEMORY_HPP +#define __MEMORY_HPP + +template +class scoped_malloc { +public: + scoped_malloc (T *_data): + m_data (_data) + { ; } + + scoped_malloc (scoped_malloc &&rhs): + m_data (rhs.m_data) + { + rhs.m_data = nullptr; + } + + ~scoped_malloc () { + free (m_data); + } + + T* data (void) { return m_data; } + const T* data (void) const { return m_data; } + + bool operator! (void) { return !m_data; } + +protected: + T* m_data; +}; + +#endif