forked from jan-van-bergen/GPU-Raytracer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d846ab6
commit 255f0d5
Showing
17 changed files
with
316 additions
and
101 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
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,57 @@ | ||
#pragma once | ||
#include <new> | ||
|
||
#include "Util/Util.h" | ||
|
||
#define KILOBYTE(n) (n * 1024) | ||
#define MEGABYTE(n) (n * 1024 * 1024) | ||
#define GIGABYTE(n) (n * 1024 * 1024 * 1024) | ||
|
||
struct Allocator { | ||
Allocator() = default; | ||
|
||
NON_COPYABLE(Allocator); | ||
NON_MOVEABLE(Allocator); | ||
|
||
virtual ~Allocator() { } | ||
|
||
template<typename T, typename ... Args> | ||
static T * alloc(Allocator * allocator, Args && ... args) { | ||
if (allocator) { | ||
return new (allocator->alloc(sizeof(T))) T { std::forward<Args>(args) ... }; | ||
} else { | ||
return new T { std::forward<Args>(args) ... }; | ||
} | ||
} | ||
|
||
template<typename T, typename ... Args> | ||
static T * alloc_array(Allocator * allocator, size_t count) { | ||
if (allocator) { | ||
return new (allocator->alloc(count * sizeof(T))) T[count]; | ||
} else { | ||
return new T[count]; | ||
} | ||
} | ||
|
||
template<typename T> | ||
static void free(Allocator * allocator, T * ptr) { | ||
if (allocator) { | ||
allocator->free(ptr); | ||
} else { | ||
delete ptr; | ||
} | ||
} | ||
|
||
template<typename T> | ||
static void free_array(Allocator * allocator, T * ptr) { | ||
if (allocator) { | ||
allocator->free(ptr); | ||
} else { | ||
delete [] ptr; | ||
} | ||
} | ||
|
||
protected: | ||
virtual char * alloc(size_t num_bytes) = 0; | ||
virtual void free (void * ptr) = 0; | ||
}; |
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,45 @@ | ||
#pragma once | ||
#include "Allocator.h" | ||
|
||
template<size_t Size = MEGABYTE(4)> | ||
struct LinearAllocator final : Allocator { | ||
char * data = nullptr; | ||
size_t offset = 0; | ||
LinearAllocator<Size> * next = nullptr; | ||
|
||
LinearAllocator() { | ||
data = new char[Size]; | ||
} | ||
|
||
~LinearAllocator() { | ||
ASSERT(data); | ||
delete [] data; | ||
if (next) { | ||
next->~LinearAllocator(); | ||
} | ||
} | ||
|
||
char * alloc(size_t num_bytes) override { | ||
if (num_bytes >= Size) { | ||
return new char[num_bytes]; // Fall back to heap allocation | ||
} | ||
if (offset + num_bytes <= Size) { | ||
char * result = data + offset; | ||
offset += num_bytes; | ||
return result; | ||
} else if (!next) { | ||
next = new LinearAllocator(); | ||
} | ||
return next->alloc(num_bytes); | ||
} | ||
|
||
void free(void * ptr) override { | ||
if (ptr >= data && ptr < data + Size) { | ||
// Do nothing, memory will be freed in bulk inside the destructor | ||
} else if (next) { | ||
next->free(ptr); | ||
} else { | ||
delete [] ptr; // ptr was not in any valid range, must have been a heap allocated pointer | ||
} | ||
} | ||
}; |
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,19 @@ | ||
#pragma once | ||
#include "Allocator.h" | ||
|
||
#include "Device/CUDAMemory.h" | ||
|
||
struct PinnedAllocator final : Allocator { | ||
char * alloc(size_t num_bytes) override { | ||
return CUDAMemory::malloc_pinned<char>(num_bytes); | ||
} | ||
|
||
void free(void * ptr) override { | ||
CUDAMemory::free_pinned(ptr); | ||
} | ||
|
||
static PinnedAllocator * instance() { | ||
static PinnedAllocator allocator = { }; | ||
return &allocator; | ||
} | ||
}; |
Oops, something went wrong.