Added:
/trunk/cockpit/src/Allocator.cpp
Modified:
/trunk/cockpit/src/Allocator.h
/trunk/cockpit/src/CMakeLists.txt
=======================================
--- /dev/null
+++ /trunk/cockpit/src/Allocator.cpp Sat Jun 26 20:01:56 2010
@@ -0,0 +1,45 @@
+#include "Allocator.h"
+
+using namespace std;
+using namespace boost;
+
+namespace cockpit {
+
+AllocatorBase::AllocatorBase(boost::uint8_t* _heapBegin, boost::uint8_t*
_heapEnd)
+ : heapBegin(_heapBegin), currentElem(_heapBegin), heapEnd(_heapEnd)
+{
+}
+
+AllocatorBase::~AllocatorBase()
+{
+}
+
+uint8_t* AllocatorBase::allocate(size_t amount)
+{
+ // Our heap is full. Fall back to oldschool new/delete.
+ if(currentElem + amount > heapEnd)
+ return new uint8_t[amount];
+
+ // Heeey, we have room in the heap! Allocate, then return.
+ else
+ return (currentElem += amount) - amount;
+}
+
+// THIS NOT MODIFYING INTERNAL STATE IS VITAL. DO NOT REMOVE THE CONST
+// SPECIFIER. It's necessary to keep this thing mildly thread-safe.
+void AllocatorBase::free(boost::uint8_t*& ptr) const
+{
+ // If the pointer is in the heap, ignore it. Otherwise, call delete[] on
it
+ // since we went oldschool when the heap is full.
+ if((ptr < heapBegin) || (ptr >= heapEnd))
+ delete[] ptr;
+
+ ptr = NULL;
+}
+
+void AllocatorBase::purge()
+{
+ currentElem = heapBegin;
+}
+
+}
=======================================
--- /trunk/cockpit/src/Allocator.h Fri Jun 25 15:12:06 2010
+++ /trunk/cockpit/src/Allocator.h Sat Jun 26 20:01:56 2010
@@ -1,44 +1,29 @@
#pragma once
-#include <cstddef>
#include <boost/cstdint.hpp>
#include <boost/noncopyable.hpp>
-#include <boost/type_traits/is_pod.hpp>
-#include <boost/static_assert.hpp>
+
+#include <cstddef>
#include <cassert>
-#ifdef BOOST_NO_NULLPTR
- #define nullptr NULL
-#endif
-
namespace cockpit {
-// A very basic arena implementation. Useful for the "many small
allocations"
-// that is our current packet construction/deconstruction implementation.
-template <size_t heapSize>
-class Allocator : boost::noncopyable
-{
-private:
- boost::uint8_t heap[heapSize];
+// All allocator actions that don't need to be dependent on the array size
can
+// be done in here. This allows the implementations to be pulled out of the
+// header, reducing compile times significantly.
+class AllocatorBase : boost::noncopyable
+{
+protected:
+ boost::uint8_t* heapBegin;
boost::uint8_t* currentElem;
boost::uint8_t* heapEnd;
+ AllocatorBase(boost::uint8_t* heapBegin, boost::uint8_t* heapEnd);
+ ~AllocatorBase();
+
public:
- Allocator()
- {
- currentElem = heap;
- heapEnd = heap + heapSize;
- }
-
- boost::uint8_t* allocate(size_t amount)
- {
- // Our heap is full. Fall back to oldschool new/delete.
- if(currentElem + amount > heapEnd)
- return new uint8_t[amount];
-
- // Heeey, we have room in the heap! Allocate, then return.
- else
- return (currentElem += amount) - amount;
- }
+ boost::uint8_t* allocate(size_t amount);
+ void free(boost::uint8_t*& ptr) const;
+ void purge();
/**
Allocates a raw type quickly and easily, without ugly casts. Be
@@ -50,25 +35,20 @@
{
return reinterpret_cast<T*>(allocate(sizeof(T)));
}
-
- // THIS NOT MODIFYING INTERNAL STATE IS VITAL. DO NOT REMOVE THE CONST
- // SPECIFIER. It's necessary to keep this thing mildly thread-safe.
- void free(boost::uint8_t*& ptr) const
- {
- // If the pointer is in the heap, ignore it. Otherwise, call delete[] on
it
- // since we went oldschool when the heap is full.
- if((ptr < heap) || (ptr >= heapEnd))
- delete[] ptr;
-
- ptr = nullptr;
- }
-
- // Resets the heap. This is hugely unsafe. Make sure you've called free()
- // on all outstanding pointers BEFORE calling this method. This will not
- // be checked for you.
- void purge()
- {
- currentElem = heap;
+};
+
+// A very basic arena implementation. Useful for the "many small
allocations"
+// that is our current packet construction/deconstruction implementation.
+template <size_t heapSize>
+class Allocator : public AllocatorBase
+{
+private:
+ boost::uint8_t heap[heapSize];
+
+public:
+ Allocator()
+ : AllocatorBase(heap, heap + heapSize)
+ {
}
~Allocator()
@@ -133,7 +113,3 @@
};
}
-
-#ifdef BOOST_NO_NULLPTR
- #undef nullptr
-#endif
=======================================
--- /trunk/cockpit/src/CMakeLists.txt Tue Jun 22 09:55:59 2010
+++ /trunk/cockpit/src/CMakeLists.txt Sat Jun 26 20:01:56 2010
@@ -7,6 +7,7 @@
packet/Registry.cpp
packet/security.cpp
+ Allocator.cpp
Allocator.h
Client.cpp
Client.h