-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refactor/arena' into feature/default-parameters
- Loading branch information
Showing
14 changed files
with
258 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#ifndef ARTIC_ARENA_H | ||
#define ARTIC_ARENA_H | ||
|
||
#include <type_traits> | ||
#include <memory> | ||
#include <vector> | ||
|
||
template<typename T> | ||
/** works like unique_ptr but doesn't actually own anything */ | ||
struct arena_ptr { | ||
T* _ptr; | ||
|
||
arena_ptr() : _ptr(nullptr) {} | ||
arena_ptr(T* ptr) : _ptr(ptr) {} | ||
|
||
// template<typename S, std::enable_if_t<std::is_convertible_v<S*, T*>, bool> = true> | ||
// arena_ptr(arena_ptr<S>& other) : _ptr(other._ptr) {} | ||
|
||
template<typename S, std::enable_if_t<std::is_convertible_v<S*, T*>, bool> = true> | ||
arena_ptr(arena_ptr<S>&& other) : _ptr(other._ptr) { | ||
other._ptr = nullptr; | ||
} | ||
~arena_ptr() { | ||
_ptr = nullptr; | ||
} | ||
|
||
// arena_ptr<T>& operator=(const arena_ptr<T>& other) { _ptr = other._ptr; return *this; } | ||
arena_ptr<T>& operator=(arena_ptr<T>&& other) { _ptr = other._ptr; other._ptr = nullptr; return *this; } | ||
|
||
T* operator->() const { return _ptr; } | ||
T& operator*() const { return *_ptr; } | ||
operator bool() const { return _ptr; } | ||
|
||
T* get() const { return _ptr; } | ||
|
||
void swap(arena_ptr<T>& other) { | ||
T* tmp = other._ptr; | ||
other._ptr = _ptr; | ||
_ptr = tmp; | ||
} | ||
}; | ||
|
||
struct Arena { | ||
Arena(); | ||
~Arena(); | ||
|
||
template<typename T, typename ...Args> | ||
arena_ptr<T> make_ptr(Args&& ...args) { | ||
void* ptr = alloc(sizeof(T)); | ||
new (ptr) T (std::forward<Args>(args)...); | ||
return arena_ptr<T>(static_cast<T*>(ptr)); | ||
} | ||
private: | ||
void* alloc(size_t); | ||
void grow(); | ||
|
||
size_t _block_size; | ||
size_t _available; | ||
std::vector<void*> _data; | ||
}; | ||
|
||
#endif // ARTIC_ARENA_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "artic/arena.h" | ||
|
||
#include <cstdlib> | ||
|
||
Arena::Arena() : _block_size(4096) { | ||
_data = { malloc(_block_size) }; | ||
_available = _block_size; | ||
} | ||
|
||
Arena::~Arena() { | ||
for (auto& ptr : _data) | ||
free(ptr); | ||
} | ||
|
||
void Arena::grow() { | ||
_block_size *= 2; | ||
_data.push_back( malloc(_block_size) ); | ||
_available = _block_size; | ||
} | ||
|
||
void* Arena::alloc(size_t size) { | ||
while (size > _available) | ||
grow(); | ||
size_t ptr = reinterpret_cast<size_t>(_data.back()) + _block_size - _available; | ||
_available -= size; | ||
return reinterpret_cast<void*>(ptr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.