Jelajahi Sumber

Rename MallocAllocator to HeapAllocator

Daniele Bartolini 12 tahun lalu
induk
melakukan
0d24721723

+ 2 - 2
samples/terrain/Terrain.h

@@ -31,7 +31,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Intersection.h"
 #include "Ray.h"
 #include "Renderer.h"
-#include "MallocAllocator.h"
+#include "HeapAllocator.h"
 
 #define MAX_BRUSH_SIZE 256
 
@@ -74,7 +74,7 @@ public:
 
 private:
 
-	MallocAllocator	m_allocator;
+	HeapAllocator	m_allocator;
 
 	uint32_t		mSizeX;				// X in meters
 	uint32_t		mSizeZ;				// Z in meters

+ 1 - 1
samples/terrain/terrain_main.cpp

@@ -211,7 +211,7 @@ public:
 
 private:
 
-	MallocAllocator m_allocator;
+	HeapAllocator m_allocator;
 	FPSSystem* system;
 	Camera* cam;
 	Mat4 ortho;

+ 2 - 2
src/ArchiveBundle.h

@@ -28,7 +28,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Types.h"
 #include "Bundle.h"
-#include "MallocAllocator.h"
+#include "HeapAllocator.h"
 
 namespace crown
 {
@@ -71,7 +71,7 @@ public:
 
 private:
 
-	MallocAllocator	m_allocator;
+	HeapAllocator	m_allocator;
 
 	Filesystem&		m_filesystem;
 

+ 2 - 2
src/CMakeLists.txt

@@ -149,7 +149,7 @@ set (MEM_SRC
 	core/mem/malloc.c
 	core/mem/Memory.cpp
 	core/mem/Allocator.cpp
-	core/mem/MallocAllocator.cpp
+	core/mem/HeapAllocator.cpp
 	core/mem/LinearAllocator.cpp
 	core/mem/StackAllocator.cpp
 	core/mem/ProxyAllocator.cpp
@@ -159,7 +159,7 @@ set (MEM_HEADERS
 	core/mem/malloc.h
 	core/mem/Memory.h
 	core/mem/Allocator.h
-	core/mem/MallocAllocator.h
+	core/mem/HeapAllocator.h
 	core/mem/LinearAllocator.h
 	core/mem/StackAllocator.h
 	core/mem/ProxyAllocator.h

+ 1 - 1
src/Crown.h

@@ -73,7 +73,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 // Core/Mem
 #include "Memory.h"
 #include "Allocator.h"
-#include "MallocAllocator.h"
+#include "HeapAllocator.h"
 #include "LinearAllocator.h"
 #include "StackAllocator.h"
 #include "ProxyAllocator.h"

+ 3 - 3
src/ResourceManager.h

@@ -30,7 +30,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "List.h"
 #include "Queue.h"
 #include "Resource.h"
-#include "MallocAllocator.h"
+#include "HeapAllocator.h"
 #include "Thread.h"
 #include "Mutex.h"
 #include "Cond.h"
@@ -150,9 +150,9 @@ private:
 	// Archive whether to look for resources
 	Bundle&					m_resource_bundle;
 	// Used to strore resource memory
-	MallocAllocator			m_resource_allocator;
+	HeapAllocator			m_resource_allocator;
 
-	MallocAllocator			m_allocator;
+	HeapAllocator			m_allocator;
 	// The master lookup table
 	List<ResourceEntry>		m_resources;
 

+ 3 - 3
src/core/filesystem/File.cpp

@@ -27,7 +27,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "File.h"
 #include "Types.h"
 #include "Compressor.h"
-#include "MallocAllocator.h"
+#include "HeapAllocator.h"
 
 namespace crown
 {
@@ -35,7 +35,7 @@ namespace crown
 //-----------------------------------------------------------------------------
 bool File::compress_to(File& file, size_t size, size_t& zipped_size, Compressor& compressor)
 {
-	MallocAllocator allocator;
+	HeapAllocator allocator;
 	void* in_buffer = (void*)allocator.allocate(size);
 
 	read(in_buffer, size);
@@ -50,7 +50,7 @@ bool File::compress_to(File& file, size_t size, size_t& zipped_size, Compressor&
 //-----------------------------------------------------------------------------
 bool File::uncompress_to(File& file, size_t& unzipped_size, Compressor& compressor)
 {
-	MallocAllocator allocator;
+	HeapAllocator allocator;
 
 	size_t file_size = size();
 	void* in_buffer = (void*)allocator.allocate(file_size);

+ 2 - 2
src/core/filesystem/Filesystem.h

@@ -29,7 +29,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "String.h"
 #include "OS.h"
 #include "File.h"
-#include "MallocAllocator.h"
+#include "HeapAllocator.h"
 
 namespace crown
 {
@@ -153,7 +153,7 @@ private:
 	
 private:
 
-	MallocAllocator		m_allocator;
+	HeapAllocator		m_allocator;
 
 	char				m_root_path[os::MAX_PATH_LENGTH];
 

+ 2 - 2
src/core/mem/Allocator.cpp

@@ -24,12 +24,12 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#include "MallocAllocator.h"
+#include "HeapAllocator.h"
 
 namespace crown
 {
 
-MallocAllocator g_default_allocator;
+HeapAllocator g_default_allocator;
 Allocator& default_allocator()
 {
 	return g_default_allocator;

+ 11 - 11
src/core/mem/MallocAllocator.cpp → src/core/mem/HeapAllocator.cpp

@@ -24,7 +24,7 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#include "MallocAllocator.h"
+#include "HeapAllocator.h"
 #include "Assert.h"
 #include "malloc.h"
 
@@ -32,21 +32,21 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-MallocAllocator::MallocAllocator() :
+HeapAllocator::HeapAllocator() :
 	m_allocated_size(0),
 	m_allocation_count(0)
 {
 }
 
 //-----------------------------------------------------------------------------
-MallocAllocator::~MallocAllocator()
+HeapAllocator::~HeapAllocator()
 {
 	CE_ASSERT(m_allocation_count == 0 && allocated_size() == 0,
 		"Missing %d deallocations causing a leak of %d bytes", m_allocation_count, allocated_size());
 }
 
 //-----------------------------------------------------------------------------
-void* MallocAllocator::allocate(size_t size, size_t align)
+void* HeapAllocator::allocate(size_t size, size_t align)
 {
 	size_t actual_size = actual_allocation_size(size, align);
 
@@ -64,7 +64,7 @@ void* MallocAllocator::allocate(size_t size, size_t align)
 }
 
 //-----------------------------------------------------------------------------
-void MallocAllocator::deallocate(void* data)
+void HeapAllocator::deallocate(void* data)
 {
 	Header* h = header(data);
 
@@ -75,13 +75,13 @@ void MallocAllocator::deallocate(void* data)
 }
 
 //-----------------------------------------------------------------------------
-size_t MallocAllocator::allocated_size()
+size_t HeapAllocator::allocated_size()
 {
 	return m_allocated_size;
 }
 
 //-----------------------------------------------------------------------------
-size_t MallocAllocator::get_size(void* data)
+size_t HeapAllocator::get_size(void* data)
 {
 	Header* h = header(data);
 
@@ -89,13 +89,13 @@ size_t MallocAllocator::get_size(void* data)
 }
 
 //-----------------------------------------------------------------------------
-size_t MallocAllocator::actual_allocation_size(size_t size, size_t align)
+size_t HeapAllocator::actual_allocation_size(size_t size, size_t align)
 {
 	return size + align + sizeof(Header);
 }
 
 //-----------------------------------------------------------------------------
-MallocAllocator::Header* MallocAllocator::header(void* data)
+HeapAllocator::Header* HeapAllocator::header(void* data)
 {
 	uint32_t* ptr = (uint32_t*)data;
 	ptr--;
@@ -109,13 +109,13 @@ MallocAllocator::Header* MallocAllocator::header(void* data)
 }
 
 //-----------------------------------------------------------------------------
-void* MallocAllocator::data(Header* header, size_t align)
+void* HeapAllocator::data(Header* header, size_t align)
 {
 	return memory::align_top(header + 1, align);
 }
 
 //-----------------------------------------------------------------------------
-void MallocAllocator::pad(Header* header, void* data)
+void HeapAllocator::pad(Header* header, void* data)
 {
 	uint32_t* p = (uint32_t*)(header + 1);
 

+ 3 - 3
src/core/mem/MallocAllocator.h → src/core/mem/HeapAllocator.h

@@ -32,12 +32,12 @@ namespace crown
 {
 
 /// Allocator based on C malloc().
-class MallocAllocator : public Allocator
+class HeapAllocator : public Allocator
 {
 public:
 
-				MallocAllocator();
-				~MallocAllocator();
+				HeapAllocator();
+				~HeapAllocator();
 
 	/// @copydoc Allocator::allocate()
 	void*		allocate(size_t size, size_t align = memory::DEFAULT_ALIGN);

+ 1 - 1
src/core/mem/Memory.cpp

@@ -25,7 +25,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "Memory.h"
-#include "MallocAllocator.h"
+#include "HeapAllocator.h"
 
 //-----------------------------------------------------------------------------
 void* operator new(size_t)

+ 20 - 0
src/core/mem/Memory.h

@@ -28,6 +28,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Types.h"
 #include "Assert.h"
+#include "OS.h"
 
 namespace crown
 {
@@ -55,5 +56,24 @@ inline void* align_top(void* p, size_t align)
 	return (void*) ptr;
 }
 
+/// Dumps the memory content pointed by @a p
+inline void dump(void* p, size_t size, size_t word_size)
+{
+	uint8_t* mem = (uint8_t*) p;
+
+	for (size_t i = 0; i < size; i++, mem++)
+	{
+		if (i % word_size == 0)
+		{
+			os::printf("\n");
+			os::printf("[.. %.4X] ", (size_t)mem);
+		}
+
+		os::printf("%.2X ", *mem);
+	}
+
+	os::printf("\n");
+}
+
 } // namespace memory
 } // namespace crown

+ 2 - 2
src/renderers/gl/GLRenderer.h

@@ -34,7 +34,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "IndexBuffer.h"
 #include "RenderBuffer.h"
 #include "IdTable.h"
-#include "MallocAllocator.h"
+#include "HeapAllocator.h"
 
 namespace crown
 {
@@ -170,7 +170,7 @@ private:
 
 private:
 
-	MallocAllocator		m_allocator;
+	HeapAllocator		m_allocator;
 
 	// Matrices
 	Mat4				m_matrix[MT_COUNT];

+ 14 - 4
tests/allocators.cpp

@@ -1,5 +1,6 @@
 #include "Allocator.h"
-#include "MallocAllocator.h"
+#include "HeapAllocator.h"
+#include "StackAllocator.h"
 #include <cstdio>
 #include "Assert.h"
 
@@ -7,16 +8,25 @@ using namespace crown;
 
 int main()
 {
-	MallocAllocator malloc_allocator;
+	HeapAllocator malloc_allocator;
 
 	char* char_buffer = (char*)malloc_allocator.allocate(128);
 	CE_ASSERT(malloc_allocator.allocated_size() >= 128, "Allocated size differs from requested size");
 
-	printf("MallocAllocator::get_allocated_size(): %d\n", malloc_allocator.allocated_size());
+	printf("HeapAllocator::get_allocated_size(): %d\n", malloc_allocator.allocated_size());
 
 	malloc_allocator.deallocate(char_buffer);
 
-	printf("MallocAllocator::get_allocated_size(): %d\n", malloc_allocator.allocated_size());
+	printf("HeapAllocator::get_allocated_size(): %d\n", malloc_allocator.allocated_size());
 	//CE_ASSERT(malloc_allocator.get_allocated_size() == 0);
+
+	uint8_t buffer[1024 * 1024];
+
+	StackAllocator stack(buffer, 1024 * 1024);
+
+	stack.allocate(12);
+	stack.allocate(5);
+
+	memory::dump(buffer, stack.allocated_size(), 4);
 }
 

+ 2 - 2
tests/compressors.cpp

@@ -1,6 +1,6 @@
 #include <cstdio>
 #include <cstring>
-#include "MallocAllocator.h"
+#include "HeapAllocator.h"
 #include "ZipCompressor.h"
 
 using namespace crown;
@@ -8,7 +8,7 @@ using namespace crown;
 int main(int argc, char** argv)
 {
 	
-	MallocAllocator allocator;
+	HeapAllocator allocator;
 	ZipCompressor compressor(allocator);
 
 	const char* uncompressed_string = "letstry";

+ 1 - 1
tests/containers.cpp

@@ -6,7 +6,7 @@ using namespace crown;
 
 int main()
 {
-	MallocAllocator allocator;
+	HeapAllocator allocator;
 
 	List<int> int_list(allocator);
 

+ 12 - 12
tests/messages.cpp

@@ -4,7 +4,7 @@
 #include "OS.h"
 #include "NetAddress.h"
 #include "BitMessage.h"
-#include "MallocAllocator.h"
+#include "HeapAllocator.h"
 
 using namespace crown;
 
@@ -15,7 +15,7 @@ void test_int8()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 	
-	MallocAllocator allocator;
+	HeapAllocator allocator;
   	network::BitMessage m = network::BitMessage(allocator);
 	
 	int8_t res;
@@ -50,7 +50,7 @@ void test_uint8()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 	
-	MallocAllocator allocator;
+	HeapAllocator allocator;
   	network::BitMessage m = network::BitMessage(allocator);
 
 	uint8_t res;
@@ -85,7 +85,7 @@ void test_int16()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 	
-	MallocAllocator allocator;
+	HeapAllocator allocator;
   	network::BitMessage m = network::BitMessage(allocator);  
 	
 	int16_t res;
@@ -117,7 +117,7 @@ void test_uint16()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 
-	MallocAllocator allocator;
+	HeapAllocator allocator;
   	network::BitMessage m = network::BitMessage(allocator);
 
 	uint16_t res;
@@ -149,7 +149,7 @@ void test_int32()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 
-	MallocAllocator allocator;
+	HeapAllocator allocator;
   	network::BitMessage m = network::BitMessage(allocator);
 	
 	int32_t res;
@@ -182,7 +182,7 @@ void test_float()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 
-	MallocAllocator allocator;
+	HeapAllocator allocator;
   	network::BitMessage m = network::BitMessage(allocator);
 
 	float res;
@@ -216,7 +216,7 @@ void test_vec3()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 
-	MallocAllocator allocator;
+	HeapAllocator allocator;
   	network::BitMessage m = network::BitMessage(allocator);
 	
 	
@@ -250,7 +250,7 @@ void test_string()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 
-	MallocAllocator allocator;
+	HeapAllocator allocator;
   	network::BitMessage m = network::BitMessage(allocator);
 	
 	uint8_t tmp[16];
@@ -288,7 +288,7 @@ void test_data()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 
-	MallocAllocator allocator;
+	HeapAllocator allocator;
   	network::BitMessage m = network::BitMessage(allocator);
 	
 	uint8_t tmp[] = "test generic";
@@ -323,7 +323,7 @@ void test_net_address()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 	
-	MallocAllocator allocator;
+	HeapAllocator allocator;
 	network::BitMessage m = network::BitMessage(allocator);
 
 	uint8_t tmp[16];
@@ -368,7 +368,7 @@ int main()
 	test_data();
 	test_net_address();
 */
-	MallocAllocator allocator;
+	HeapAllocator allocator;
 	network::BitMessage msg = network::BitMessage(allocator);
 	
 	uint32_t protocol_id = 0xFFFFFFFF;

+ 2 - 2
tools/editors/world-editor/terrain/Heightfield.h

@@ -27,7 +27,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 
 #include "Types.h"
-#include "MallocAllocator.h"
+#include "HeapAllocator.h"
 
 namespace crown
 {
@@ -72,7 +72,7 @@ private:
 
 private:
 
-	MallocAllocator	m_allocator;
+	HeapAllocator	m_allocator;
 
 	uint32_t		m_initial_width;
 	uint32_t		m_initial_height;