Explorar o código

Add LinearAllocator

Daniele Bartolini %!s(int64=12) %!d(string=hai) anos
pai
achega
721495a4da
Modificáronse 4 ficheiros con 151 adicións e 0 borrados
  1. 2 0
      src/CMakeLists.txt
  2. 1 0
      src/Crown.h
  3. 82 0
      src/core/mem/LinearAllocator.cpp
  4. 66 0
      src/core/mem/LinearAllocator.h

+ 2 - 0
src/CMakeLists.txt

@@ -152,6 +152,7 @@ set (MEM_SRC
 	core/mem/Memory.cpp
 	core/mem/Allocator.cpp
 	core/mem/MallocAllocator.cpp
+	core/mem/LinearAllocator.cpp
 	core/mem/ProxyAllocator.cpp
 )
 
@@ -160,6 +161,7 @@ set (MEM_HEADERS
 	core/mem/Memory.h
 	core/mem/Allocator.h
 	core/mem/MallocAllocator.h
+	core/mem/LinearAllocator.h
 	core/mem/ProxyAllocator.h
 )
 

+ 1 - 0
src/Crown.h

@@ -74,6 +74,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Memory.h"
 #include "Allocator.h"
 #include "MallocAllocator.h"
+#include "LinearAllocator.h"
 #include "ProxyAllocator.h"
 
 // Core/Filesystem

+ 82 - 0
src/core/mem/LinearAllocator.cpp

@@ -0,0 +1,82 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "LinearAllocator.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+LinearAllocator::LinearAllocator(void* start, size_t size) :
+	m_physical_start(start),
+	m_total_size(size),
+	m_offset(0)
+{
+}
+
+//-----------------------------------------------------------------------------
+LinearAllocator::~LinearAllocator()
+{
+	CE_ASSERT(m_offset == 0, "Memory leak of %d bytes", m_offset);
+}
+
+//-----------------------------------------------------------------------------
+void* LinearAllocator::allocate(size_t size, size_t align)
+{
+	const size_t actual_size = size + align;
+
+	// Memory exhausted
+	if (m_offset + actual_size > m_total_size)
+	{
+		return NULL;
+	}
+
+	void* user_ptr = memory::align((char*) m_physical_start + m_offset, align);
+
+	m_offset += actual_size;
+
+	return user_ptr;
+}
+
+//-----------------------------------------------------------------------------
+void LinearAllocator::deallocate(void* /*data*/)
+{
+	CE_ASSERT(false, "Cannot deallocate from linear allocator");
+}
+
+//-----------------------------------------------------------------------------
+void LinearAllocator::clear()
+{
+	m_offset = 0;
+}
+
+//-----------------------------------------------------------------------------
+size_t LinearAllocator::allocated_size()
+{
+	return m_offset;
+}
+
+} // namespace crown

+ 66 - 0
src/core/mem/LinearAllocator.h

@@ -0,0 +1,66 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "Allocator.h"
+
+namespace crown
+{
+
+/// Allocates memory linearly from a predefined chunk
+/// and frees all the allocations with a single call to clear()
+class LinearAllocator : public Allocator
+{
+public:
+
+				LinearAllocator(void* start, size_t size);
+				~LinearAllocator();
+
+	/// @a copydoc Allocator::allocate()
+	void*		allocate(size_t size, size_t align = memory::DEFAULT_ALIGN);
+
+	/// @a copydoc Allocator::deallocate()
+	/// @note
+	/// The linear allocator does not support deallocating
+	/// individual allocations, rather you have to call
+	/// clear() to free all memory at once.
+	void		deallocate(void* data);
+
+	/// Frees all the allocations made by allocate()
+	void		clear();
+
+	/// @a copydoc Allocator::allocated_size()
+	size_t		allocated_size();
+
+private:
+
+	void*		m_physical_start;
+	size_t		m_total_size;
+	size_t		m_offset;
+};
+
+} // namespace crown