Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugging #19

Merged
merged 7 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions MemoryPoolAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class MemoryPoolAllocator {

template<class U>
friend class MemoryPoolAllocator;

template<class A, class B>
friend bool operator==(const MemoryPoolAllocator<A>&, const MemoryPoolAllocator<B>&);

public:
using value_type = T;
Expand Down Expand Up @@ -60,6 +63,11 @@ class MemoryPoolAllocator {
this->block_size = alloc.block_size;
}

MemoryPoolAllocator(const MemoryPoolAllocator& alloc) {
mp = new MemoryPool(alloc.block_size);
this->block_size = alloc.block_size;
}

template<class U>
MemoryPoolAllocator(MemoryPoolAllocator<U>&& alloc) noexcept {
mp = alloc.mp;
Expand All @@ -69,6 +77,14 @@ class MemoryPoolAllocator {
alloc.block_size = 0;
}

MemoryPoolAllocator(MemoryPoolAllocator&& alloc) noexcept {
mp = alloc.mp;
this->block_size = alloc.block_size;

alloc.mp = nullptr;
alloc.block_size = 0;
}

template<class U>
MemoryPoolAllocator& operator=(const MemoryPoolAllocator<U>& alloc) {
if(this->block_size != alloc.block_size) {
Expand All @@ -80,6 +96,16 @@ class MemoryPoolAllocator {
return *this;
}

MemoryPoolAllocator& operator=(const MemoryPoolAllocator& alloc) {
if(this->block_size != alloc.block_size) {
delete mp;

mp = new MemoryPool(alloc.block_size);
this->block_size = alloc.block_size;
}
return *this;
}

template<class U>
MemoryPoolAllocator& operator=(MemoryPoolAllocator<U>&& alloc) noexcept {
if(this->block_size != alloc.block_size) {
Expand All @@ -94,6 +120,19 @@ class MemoryPoolAllocator {
return *this;
}

MemoryPoolAllocator& operator=(MemoryPoolAllocator&& alloc) noexcept {
if(this->block_size != alloc.block_size) {
delete mp;

mp = alloc.mp;
this->block_size = alloc.block_size;

alloc.mp = nullptr;
alloc.block_size = 0;
}
return *this;
}

~MemoryPoolAllocator() {
delete mp;
}
Expand Down Expand Up @@ -129,4 +168,14 @@ class MemoryPoolAllocator {
}
};

template<class A, class B>
inline bool operator==(const MemoryPoolAllocator<A>& a, const MemoryPoolAllocator<B>& b) {
return a.mp == b.mp;
}

template<class A, class B>
inline bool operator!=(const MemoryPoolAllocator<A>& a, const MemoryPoolAllocator<B>& b) {
return !(a == b);
}

}
13 changes: 13 additions & 0 deletions tests/memory_pool_allocator/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.15)
project(MemoryPool)

set(CMAKE_CXX_STANDARD 17)

# Release mode
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")

# Debug mode
# set(CMAKE_BUILD_TYPE Debug)

add_executable(MemoryPool "main.cpp" "../MemoryPool.cpp")
75 changes: 75 additions & 0 deletions tests/memory_pool_allocator/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* AppShift Memory Pool v2.0.0
*
* Copyright 2020-present Sapir Shemer, DevShift (devshift.biz)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Valasiadis Fotios
*/

#include <iostream>
#include "../../MemoryPoolAllocator.h"
#include <vector>
#include <algorithm>

using namespace AppShift::Memory;

template<class T, class AllocA, class AllocB>
inline bool operator!=(std::vector<T, AllocA> a, std::vector<T, AllocB> b) {
return a.size() != b.size() || !std::equal(a.begin(), a.end(), b.begin());
}

template<class A, class B>
inline void assert_equals(const A& a,const B& b, const std::string& message) {
if(a != b) {
std::cerr << message << '\n';
std::exit(0);
}
}

int main() {
MemoryPoolAllocator<int> alloc(1024); // 1kb
std::vector<int, MemoryPoolAllocator<int>> test_vector(alloc); // alloc is copied
std::vector<int> vector;

static_assert(std::is_same<MemoryPoolAllocator<int>::value_type, int>::value);
static_assert(std::is_same<MemoryPoolAllocator<int>::pointer, int*>::value);
static_assert(std::is_same<MemoryPoolAllocator<int>::reference, int&>::value);
static_assert(std::is_same<MemoryPoolAllocator<int>::const_pointer, const int*>::value);
static_assert(std::is_same<MemoryPoolAllocator<int>::value_type, int>::value);
static_assert(!MemoryPoolAllocator<int>::propagate_on_container_copy_assignment::value);
static_assert(MemoryPoolAllocator<int>::propagate_on_container_move_assignment::value);
static_assert(MemoryPoolAllocator<int>::propagate_on_container_swap::value);

int dummy;
assert_equals(alloc.address(dummy), &dummy, "address test");
assert_equals(alloc.max_size(), 256, "max_size test"); // 2^10 / 2^2 = 2^8 = 256

// Triggers allocation && object constructions
test_vector = {4, 6, 43, 92, 543 , 84321, 8543, 134, 95, 0};
vector = {4, 6, 43, 92, 543 , 84321, 8543, 134, 95, 0};
assert_equals(test_vector, vector, "Initializer list test");

// Triggers object destructions
test_vector.resize(5);
vector.resize(5);
assert_equals(test_vector, vector, "resize test");

// Triggers reallocation
test_vector.shrink_to_fit();
vector.shrink_to_fit();
assert_equals(test_vector, vector, "shrink to fit test");

assert_equals(alloc, alloc, "equality test");
}
Binary file added tests/memory_pool_allocator/test
Binary file not shown.