Skip to content

Commit

Permalink
std::vector types now allocate from ImageArena
Browse files Browse the repository at this point in the history
  • Loading branch information
Cacodemon345 committed Sep 10, 2023
1 parent c4753f8 commit 2f93cad
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/common/textures/formats/stbtexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
class FStbTexture : public FImageSource
{
private:
std::vector<int> durations;
std::vector<int, ImageArenaAllocator<int>> durations;

public:
FStbTexture (int lumpnum, int w, int h, int frames, int* delays);
Expand Down
8 changes: 5 additions & 3 deletions src/common/textures/formats/webptexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class FWebPTexture : public FImageSource
int GetDurationOfFrame(int frame) override;

private:
std::vector<int> durations;
std::vector<int, ImageArenaAllocator<int>> durations;
};


Expand Down Expand Up @@ -122,8 +122,10 @@ FWebPTexture::FWebPTexture(int lumpnum, int w, int h, int xoff, int yoff, bool t
Height = h;
LeftOffset = xoff;
TopOffset = yoff;
durations = FrameDurations;
FrameOffsets = offsets;
durations.resize(FrameDurations.size());
std::copy(FrameDurations.begin(), FrameDurations.end(), durations.begin());
FrameOffsets.resize(offsets.size());
std::copy(offsets.begin(), offsets.end(), FrameOffsets.begin());
NumOfFrames = durations.size();
if (!NumOfFrames)
NumOfFrames = 1;
Expand Down
34 changes: 33 additions & 1 deletion src/common/textures/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,37 @@ struct PalettedPixels

};

// Allocator for std::vector that allocates from ImageArena.
template <class T>
class ImageArenaAllocator
{
public:
typedef T value_type;

ImageArenaAllocator() = default;

template <class U>
constexpr ImageArenaAllocator(const ImageArenaAllocator<U>&) noexcept {}

[[nodiscard]] T* allocate(std::size_t n) {
if (n > std::numeric_limits<std::size_t>::max() / sizeof(T))
throw std::bad_array_new_length();

if (auto p = static_cast<T*>(ImageArena.Alloc(n * sizeof(T))))
{
return p;
}

throw std::bad_alloc();
}

void deallocate(T* p, std::size_t n) noexcept
{
// FMemArena can't deallocate, only allocate.
memset(p, 0, sizeof(T) * n);
}
};

// This represents a naked image. It has no high level logic attached to it.
// All it can do is provide raw image data to its users.
class FImageSource
Expand All @@ -69,7 +100,8 @@ class FImageSource
int SourceLump;
int Width = 0, Height = 0;
int LeftOffset = 0, TopOffset = 0; // Global offsets stored in the image.
std::vector<std::pair<int, int>> FrameOffsets; // Per-frame, non-global offsets stored in animated images.
// Per-frame, non-global offsets stored in animated images.
std::vector<std::pair<int, int>, ImageArenaAllocator<std::pair<int, int>>> FrameOffsets;
bool bUseGamePalette = false; // true if this is an image without its own color set.
int ImageID = -1;
int NumOfFrames = 1;
Expand Down

0 comments on commit 2f93cad

Please sign in to comment.