From eb891da6489cf0585964363ed229fab6dc7176f1 Mon Sep 17 00:00:00 2001
From: j2-tulloch <james2.tulloch@live.uwe.ac.uk>
Date: Wed, 20 Dec 2023 20:59:57 +0000
Subject: [PATCH] Upload bumpall.hpp

---
 Worksheet2/bumpall.hpp | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 Worksheet2/bumpall.hpp

diff --git a/Worksheet2/bumpall.hpp b/Worksheet2/bumpall.hpp
new file mode 100644
index 0000000..ce88b2a
--- /dev/null
+++ b/Worksheet2/bumpall.hpp
@@ -0,0 +1,40 @@
+#include <cstddef> 
+#include <iostream> 
+
+
+class BumpAllocator {
+    char* start;   // Pointer to the start of the allocated memory block
+    char* current; // Pointer to the current position in the memory block
+    size_t size;   // Total size of the memory block
+
+public:
+    // Constructor for the BumpAllocator
+    BumpAllocator(size_t size) : size(size) {
+        start = new char[size]; // Allocate a memory block of the given size
+        current = start;        // Initialize the current pointer to the start of the block
+    }
+
+    void* allocate(size_t allocSize) {
+        // Check if there is enough space left in the block for the requested size
+        if (current + allocSize - start > size) {
+            //std::cout << "Not enough space in the block for allocation!" << std::endl;
+            return nullptr; // Return nullptr if there is not enough space
+        }
+        void* alloc = current; // Store the current position to return
+        current += allocSize;  // "Bump" the current pointer forward by the requested size
+        //std::cout << "Memory allocated successfully!" << std::endl;
+        return alloc;          // Return the original current position
+    }
+
+    // Function to reset the allocator
+    void reset() {
+        current = start; // Reset the current pointer back to the start of the block
+        //std::cout << "Allocator reset!" << std::endl;
+    }
+
+    // Destructor for the BumpAllocator
+    ~BumpAllocator() {
+        delete[] start; // Deallocate the memory block when the allocator is destroyed
+        //std::cout << "Memory block deallocated!" << std::endl;
+    }
+};
-- 
GitLab