Browse Source

removal of stack allocator

Kajetan Johannes Hammerle 3 years ago
parent
commit
3f19a93439
6 changed files with 14 additions and 177 deletions
  1. 0 53
      memory/StackAllocator.cpp
  2. 0 110
      memory/StackAllocator.h
  3. 3 3
      rendering/FileTexture.cpp
  4. 10 9
      tests/PNGReaderTests.cpp
  5. 1 0
      utils/Buffer.cpp
  6. 0 2
      utils/Buffer.h

+ 0 - 53
memory/StackAllocator.cpp

@@ -1,53 +0,0 @@
-#include <iostream>
-
-#include "memory/StackAllocator.h"
-
-static constexpr int MAX_BYTES = 50 * 1024 * 1024;
-alignas(16) static char data[MAX_BYTES];
-static int index = 0;
-static int lastIndex = 0;
-
-StackAllocator::Pointer incrementIndex(int inc) {
-    StackAllocator::Pointer p = {lastIndex, index};
-    lastIndex = index;
-    if((inc & 0xF) != 0) {
-        inc = (inc & ~0xF) + 16;
-    }
-    index += inc;
-    return p;
-}
-
-StackAllocator::Pointer StackAllocator::allocate(int bytesPerElement, int& elements) {
-    int bytes = bytesPerElement * elements;
-    if(index + bytes <= MAX_BYTES) {
-        return incrementIndex(bytes);
-    }
-    elements = (MAX_BYTES - index) / bytesPerElement;
-    return incrementIndex(bytesPerElement * elements);
-}
-
-void StackAllocator::free(const Pointer& p) {
-    if(p.pointer > index) {
-        return;
-    }
-    index = p.pointer;
-    lastIndex = p.lastPointer;
-}
-
-int StackAllocator::grow(const Pointer& p, int bytesPerElement, int elements) {
-    if(p.pointer != lastIndex) {
-        return 0;
-    }
-    int bytes = bytesPerElement * elements;
-    if(index + bytes <= MAX_BYTES) {
-        index += bytes;
-        return elements;
-    }
-    elements = (MAX_BYTES - index) / bytesPerElement;
-    index += bytesPerElement * elements;
-    return elements;
-}
-
-void* StackAllocator::get(const Pointer& p) {
-    return data + p.pointer;
-}

+ 0 - 110
memory/StackAllocator.h

@@ -1,110 +0,0 @@
-#ifndef STACKALLOCATOR_H
-#define STACKALLOCATOR_H
-
-#include <new>
-#include <type_traits>
-
-namespace StackAllocator {
-    struct Pointer {
-        int lastPointer;
-        int pointer;
-    };
-
-    Pointer allocate(int bytesPerElement, int& elements);
-    void free(const Pointer& p);
-    int grow(const Pointer& p, int bytesPerElement, int elements);
-    void* get(const Pointer& p);
-
-    template<typename T>
-    class Array final {
-        int length;
-        Pointer dataPointer;
-
-    public:
-        Array(int n)
-            : length(n),
-              dataPointer(StackAllocator::allocate(sizeof(T), length)) {
-            for(int i = 0; i < length; i++) {
-                new((*this) + i) T;
-            }
-        }
-
-        ~Array() {
-            for(int i = 0; i < length; i++) {
-                (*this)[i].~T();
-            }
-            StackAllocator::free(dataPointer);
-        }
-
-        Array(const Array&) = delete;
-        Array& operator=(const Array&) = delete;
-        Array(Array&&) = delete;
-        Array& operator=(Array&&) = delete;
-
-        int getLength() const {
-            return length;
-        }
-
-        int grow(int n) {
-            int add = StackAllocator::grow(dataPointer, sizeof(T), n);
-            for(int i = length; i < length + add; i++) {
-                new((*this) + i) T();
-            }
-            length += add;
-            return add;
-        }
-
-        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));
-        }
-    };
-
-    template<typename T>
-    class Object final {
-        int length;
-        Pointer dataPointer;
-
-    public:
-        Object()
-            : length(1),
-              dataPointer(StackAllocator::allocate(sizeof(T), length)) {
-            if(!hasError()) {
-                new(StackAllocator::get(dataPointer)) T;
-            }
-        }
-
-        ~Object() {
-            if(!hasError()) {
-                (*this)->~T();
-            }
-            StackAllocator::free(dataPointer);
-        }
-
-        Object(const Object&) = delete;
-        Object& operator=(const Object&) = delete;
-        Object(Object&&) = delete;
-        Object& operator=(Object&&) = delete;
-
-        bool hasError() {
-            return length != 1;
-        }
-
-        T* operator->() {
-            return static_cast<T*>(StackAllocator::get(dataPointer));
-        }
-
-        operator T&() {
-            return *static_cast<T*>(StackAllocator::get(dataPointer));
-        }
-    };
-}
-
-#endif

+ 3 - 3
rendering/FileTexture.cpp

@@ -9,10 +9,10 @@ FileTexture::FileTexture(const char* path, int maxMipMaps)
         return;
     }
     List<ColorChannel> buffer;
-    StackAllocator::Array<ColorChannel> buffer(png.getBufferSize());
-    if(buffer.getLength() == png.getBufferSize() && !png.readData(buffer)) {
+    buffer.resize(png.getBufferSize());
+    if(!png.readData(buffer.begin())) {
         texture.setFormat(TextureFormat::color8(png.getChannels()));
-        texture.setData(png.getWidth(), png.getHeight(), buffer);
+        texture.setData(png.getWidth(), png.getHeight(), buffer.begin());
     }
 }
 

+ 10 - 9
tests/PNGReaderTests.cpp

@@ -1,15 +1,16 @@
 #include "tests/PNGReaderTests.h"
-#include "tests/Test.h"
 #include "images/PNGReader.h"
+#include "tests/Test.h"
+#include "utils/List.h"
 #include "utils/StringBuffer.h"
-#include "memory/StackAllocator.h"
 
 static void testRead(Test& test, PNGReader& png, const char* text) {
-    StackAllocator::Array<ColorChannel> buffer(png.getBufferSize());
+    List<ColorChannel> buffer;
+    buffer.resize(png.getBufferSize());
     test.checkEqual(true, png.getBufferSize() > 0, text);
     test.checkEqual(png.getBufferSize(), buffer.getLength(), text);
     if(png.getBufferSize() == buffer.getLength()) {
-        test.checkEqual(false, png.readData(buffer), text);
+        test.checkEqual(false, png.readData(buffer.begin()), text);
     }
 }
 
@@ -34,7 +35,7 @@ static void testReadRGB16(Test& test, const char* path) {
     test.checkEqual(32, png.getWidth(), "rgb16 width");
     test.checkEqual(64, png.getHeight(), "rgb16 height");
     test.checkEqual(3, png.getChannels(), "rgb16 channels");
-    test.checkEqual(32 * 64, png.getBufferSize(), "rgb16 channels");
+    test.checkEqual(32 * 64 * 3, png.getBufferSize(), "rgb16 channels");
     testRead(test, png, "rgb16 read");
 }
 
@@ -47,7 +48,7 @@ static void testReadRGBA8(Test& test, const char* path) {
     test.checkEqual(32, png.getWidth(), "rgba8 width");
     test.checkEqual(64, png.getHeight(), "rgba8 height");
     test.checkEqual(4, png.getChannels(), "rgba8 channels");
-    test.checkEqual(32 * 64, png.getBufferSize(), "rgba8 channels");
+    test.checkEqual(32 * 64 * 4, png.getBufferSize(), "rgba8 channels");
     testRead(test, png, "rgba8 read");
 }
 
@@ -60,7 +61,7 @@ static void testReadRGBA16(Test& test, const char* path) {
     test.checkEqual(32, png.getWidth(), "rgba16 width");
     test.checkEqual(64, png.getHeight(), "rgba16 height");
     test.checkEqual(4, png.getChannels(), "rgba16 channels");
-    test.checkEqual(32 * 64, png.getBufferSize(), "rgba16 channels");
+    test.checkEqual(32 * 64 * 4, png.getBufferSize(), "rgba16 channels");
     testRead(test, png, "rgba16 read");
 }
 
@@ -99,7 +100,7 @@ static void testReadGrayA8(Test& test, const char* path) {
     test.checkEqual(32, png.getWidth(), "graya8 width");
     test.checkEqual(64, png.getHeight(), "graya8 height");
     test.checkEqual(2, png.getChannels(), "graya8 channels");
-    test.checkEqual(32 * 64, png.getBufferSize(), "graya8 channels");
+    test.checkEqual(32 * 64 * 2, png.getBufferSize(), "graya8 channels");
     testRead(test, png, "graya8 read");
 }
 
@@ -112,7 +113,7 @@ static void testReadGrayA16(Test& test, const char* path) {
     test.checkEqual(32, png.getWidth(), "graya16 width");
     test.checkEqual(64, png.getHeight(), "graya16 height");
     test.checkEqual(2, png.getChannels(), "graya16 channels");
-    test.checkEqual(32 * 64, png.getBufferSize(), "graya16 channels");
+    test.checkEqual(32 * 64 * 2, png.getBufferSize(), "graya16 channels");
     testRead(test, png, "graya16 read");
 }
 

+ 1 - 0
utils/Buffer.cpp

@@ -1,5 +1,6 @@
 #include <cstdlib>
 #include <cstring>
+#include <utility>
 
 #include "utils/Buffer.h"
 

+ 0 - 2
utils/Buffer.h

@@ -1,8 +1,6 @@
 #ifndef BUFFER_H
 #define BUFFER_H
 
-#include "memory/StackAllocator.h"
-
 class Buffer {
     int length;
     int capacity;