Переглянути джерело

buffer uses stack allocator

Kajetan Johannes Hammerle 3 роки тому
батько
коміт
366e805c17
8 змінених файлів з 47 додано та 28 видалено
  1. 2 0
      Main.cpp
  2. 9 3
      memory/StackAllocator.h
  3. 2 1
      meson.build
  4. 8 15
      tests/BufferTests.cpp
  5. 12 0
      utils/Buffer.cpp
  6. 9 9
      utils/Buffer.h
  7. 1 0
      utils/List.h
  8. 4 0
      utils/StringBuffer.h

+ 2 - 0
Main.cpp

@@ -24,6 +24,8 @@
 #include "wrapper/Framebuffer.h"
 #include "rendering/FileTexture.h"
 
+#include "utils/List.h"
+
 int main(int argAmount, char** args) {
     if(argAmount < 2) {
         std::cout << "missing path to images\n";

+ 9 - 3
memory/StackAllocator.h

@@ -1,6 +1,8 @@
 #ifndef STACKALLOCATOR_H
 #define STACKALLOCATOR_H
 
+#include <new>
+
 namespace StackAllocator {
 
     struct Pointer {
@@ -22,7 +24,7 @@ namespace StackAllocator {
 
         Array(int n) : length(n), dataPointer(StackAllocator::allocate(sizeof (T), length)) {
             for(int i = 0; i < length; i++) {
-                new (&(*this)[i]) T();
+                new ((*this) + i) T;
             }
         }
 
@@ -45,7 +47,7 @@ namespace StackAllocator {
         int grow(int n) {
             int add = StackAllocator::grow(dataPointer, sizeof (T), n);
             for(int i = length; i < length + add; i++) {
-                new (&(*this)[i]) T();
+                new ((*this) + i) T();
             }
             length += add;
             return add;
@@ -54,10 +56,14 @@ namespace StackAllocator {
         T& operator[](int index) {
             return static_cast<T*> (StackAllocator::get(dataPointer))[index];
         }
-        
+
         const T& operator[](int index) const {
             return static_cast<T*> (StackAllocator::get(dataPointer))[index];
         }
+
+        operator T*() {
+            return static_cast<T*> (StackAllocator::get(dataPointer));
+        }
     };
 }
 

+ 2 - 1
meson.build

@@ -44,7 +44,8 @@ sources = ['Main.cpp',
     'input/Buttons.cpp',
     'wrapper/TextureFormat.cpp',
     'memory/StackAllocator.cpp',
-    'tests/StackAllocatorTests.cpp']
+    'tests/StackAllocatorTests.cpp',
+    'utils/Buffer.cpp']
 
 glewDep = dependency('glew')
 glfwDep = dependency('glfw3')

+ 8 - 15
tests/BufferTests.cpp

@@ -3,26 +3,19 @@
 #include "utils/Buffer.h"
 
 static void testAdd(Test& test) {
-    Buffer<10> bits;
-    int a = 5;
-    bits.add(a);
-    test.checkEqual(static_cast<int> (sizeof (a)), bits.getLength(), "add increments length");
-}
-
-static void testOverflow(Test& test) {
-    Buffer<10> bits;
-    for(int i = 0; i < 1000000; i++) {
-        bits.add(5);
-        bits.add(5L);
-        bits.add(5.0f);
-        bits.add(5.0);
+    Buffer buffer(10);
+    for(int i = 0; i < 100000; i++) {
+        buffer.add(5);
+        buffer.add(5L);
+        buffer.add(5.0f);
+        buffer.add(5.0);
     }
-    test.checkEqual(10, bits.getLength(), "add increments length");
+    test.checkEqual(static_cast<int> ((sizeof (int) + sizeof (long) + sizeof (float) + sizeof (double)) * 100000),
+            buffer.getLength(), "add increments length");
 }
 
 void BufferTests::test() {
     Test test("Buffer");
     testAdd(test);
-    testOverflow(test);
     test.finalize();
 }

+ 12 - 0
utils/Buffer.cpp

@@ -0,0 +1,12 @@
+#include "utils/Buffer.h"
+
+Buffer::Buffer(int n) : data(n) {
+}
+
+int Buffer::getLength() const {
+    return length;
+}
+
+Buffer::operator const char*() const {
+    return &(data[0]);
+}

+ 9 - 9
utils/Buffer.h

@@ -3,31 +3,31 @@
 
 #include <cstring>
 
-template<int N>
+#include "memory/StackAllocator.h"
+
 class Buffer {
-    char data[N];
+    StackAllocator::Array<char> data;
     int length = 0;
 
 public:
+    Buffer(int n);
 
     template<typename T>
     Buffer& add(const T& t) {
-        int bytes = N - length;
+        int bytes = data.getLength() - length;
         if(bytes > static_cast<int> (sizeof (T))) {
             bytes = sizeof (T);
+        } else if(data.grow(data.getLength()) > 0) {
+            return add(t);
         }
         memcpy(data + length, &t, bytes);
         length += bytes;
         return *this;
     }
 
-    int getLength() const {
-        return length;
-    }
+    int getLength() const;
 
-    operator const char*() const {
-        return data;
-    }
+    operator const char*() const;
 };
 
 #endif

+ 1 - 0
utils/List.h

@@ -1,6 +1,7 @@
 #ifndef LIST_H
 #define LIST_H
 
+#include <new>
 #include <utility>
 
 #include "utils/StringBuffer.h"

+ 4 - 0
utils/StringBuffer.h

@@ -76,6 +76,10 @@ public:
         data[length] = '\0';
         return *this;
     }
+    
+    StringBuffer& append(char* str) {
+        return append(static_cast<const char*>(str));
+    }
 
     template<typename T>
     StringBuffer& append(const char* format, const T& t) {