1
0
Fork 0

Codechange: use std::unique_ptr over manual memory management

pull/13328/head
Rubidium 2025-01-15 21:01:21 +01:00 committed by rubidium42
parent 8942ac1e3e
commit 8f7468483a
3 changed files with 13 additions and 25 deletions

View File

@ -20,11 +20,6 @@
/** Instantiation of the 32bpp with animation blitter factory. */
static FBlitter_32bppAnim iFBlitter_32bppAnim;
Blitter_32bppAnim::~Blitter_32bppAnim()
{
free(this->anim_alloc);
}
template <BlitterMode mode>
inline void Blitter_32bppAnim::Draw(const Blitter::BlitterParams *bp, ZoomLevel zoom)
{
@ -545,13 +540,12 @@ void Blitter_32bppAnim::PostResize()
if (_screen.width != this->anim_buf_width || _screen.height != this->anim_buf_height ||
_screen.pitch != this->anim_buf_pitch) {
/* The size of the screen changed; we can assume we can wipe all data from our buffer */
free(this->anim_alloc);
this->anim_buf_width = _screen.width;
this->anim_buf_height = _screen.height;
this->anim_buf_pitch = (_screen.width + 7) & ~7;
this->anim_alloc = CallocT<uint16_t>(this->anim_buf_pitch * this->anim_buf_height + 8);
this->anim_alloc = std::make_unique<uint16_t[]>(this->anim_buf_pitch * this->anim_buf_height + 8);
/* align buffer to next 16 byte boundary */
this->anim_buf = reinterpret_cast<uint16_t *>((reinterpret_cast<uintptr_t>(this->anim_alloc) + 0xF) & (~0xF));
this->anim_buf = reinterpret_cast<uint16_t *>((reinterpret_cast<uintptr_t>(this->anim_alloc.get()) + 0xF) & (~0xF));
}
}

View File

@ -16,7 +16,7 @@
class Blitter_32bppAnim : public Blitter_32bppOptimized {
protected:
uint16_t *anim_buf; ///< In this buffer we keep track of the 8bpp indexes so we can do palette animation
void *anim_alloc; ///< The raw allocated buffer, not necessarily aligned correctly
std::unique_ptr<uint16_t[]> anim_alloc; ///< The raw allocated buffer, not necessarily aligned correctly
int anim_buf_width; ///< The width of the animation buffer.
int anim_buf_height; ///< The height of the animation buffer.
int anim_buf_pitch; ///< The pitch of the animation buffer (width rounded up to 16 byte boundary).
@ -25,7 +25,6 @@ protected:
public:
Blitter_32bppAnim() :
anim_buf(nullptr),
anim_alloc(nullptr),
anim_buf_width(0),
anim_buf_height(0),
anim_buf_pitch(0)
@ -33,8 +32,6 @@ public:
this->palette = _cur_palette;
}
~Blitter_32bppAnim();
void Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom) override;
void DrawColourMappingRect(void *dst, int width, int height, PaletteID pal) override;
void SetPixel(void *video, int x, int y, uint8_t colour) override;

View File

@ -290,7 +290,7 @@ template <bool Tpal_to_rgb> Sprite *Blitter_32bppOptimized::EncodeInternal(const
/* streams of pixels (a, r, g, b channels)
*
* stored in separated stream so data are always aligned on 4B boundary */
Colour *dst_px_orig[ZOOM_LVL_END];
std::array<std::unique_ptr<Colour[]>, ZOOM_LVL_END> dst_px_orig;
/* interleaved stream of 'm' channel and 'n' channel
* 'n' is number of following pixels with the same alpha channel class
@ -298,7 +298,7 @@ template <bool Tpal_to_rgb> Sprite *Blitter_32bppOptimized::EncodeInternal(const
*
* it has to be stored in one stream so fewer registers are used -
* x86 has problems with register allocation even with this solution */
uint16_t *dst_n_orig[ZOOM_LVL_END];
std::array<std::unique_ptr<uint16_t[]>, ZOOM_LVL_END> dst_n_orig;
/* lengths of streams */
uint32_t lengths[ZOOM_LVL_END][2];
@ -320,11 +320,11 @@ template <bool Tpal_to_rgb> Sprite *Blitter_32bppOptimized::EncodeInternal(const
uint size = src_orig->height * src_orig->width;
dst_px_orig[z] = CallocT<Colour>(size + src_orig->height * 2);
dst_n_orig[z] = CallocT<uint16_t>(size * 2 + src_orig->height * 4 * 2);
dst_px_orig[z] = std::make_unique<Colour[]>(size + src_orig->height * 2);
dst_n_orig[z] = std::make_unique<uint16_t[]>(size * 2 + src_orig->height * 4 * 2);
uint32_t *dst_px_ln = (uint32_t *)dst_px_orig[z];
uint32_t *dst_n_ln = (uint32_t *)dst_n_orig[z];
uint32_t *dst_px_ln = reinterpret_cast<uint32_t *>(dst_px_orig[z].get());
uint32_t *dst_n_ln = reinterpret_cast<uint32_t *>(dst_n_orig[z].get());
const SpriteLoader::CommonPixel *src = (const SpriteLoader::CommonPixel *)src_orig->data;
@ -405,8 +405,8 @@ template <bool Tpal_to_rgb> Sprite *Blitter_32bppOptimized::EncodeInternal(const
dst_n_ln = (uint32_t *)dst_n;
}
lengths[z][0] = (uint8_t *)dst_px_ln - (uint8_t *)dst_px_orig[z]; // all are aligned to 4B boundary
lengths[z][1] = (uint8_t *)dst_n_ln - (uint8_t *)dst_n_orig[z];
lengths[z][0] = reinterpret_cast<uint8_t *>(dst_px_ln) - reinterpret_cast<uint8_t *>(dst_px_orig[z].get()); // all are aligned to 4B boundary
lengths[z][1] = reinterpret_cast<uint8_t *>(dst_n_ln) - reinterpret_cast<uint8_t *>(dst_n_orig[z].get());
}
uint len = 0; // total length of data
@ -428,11 +428,8 @@ template <bool Tpal_to_rgb> Sprite *Blitter_32bppOptimized::EncodeInternal(const
dst->offset[z][0] = z == zoom_min ? 0 : lengths[z - 1][1] + dst->offset[z - 1][1];
dst->offset[z][1] = lengths[z][0] + dst->offset[z][0];
memcpy(dst->data + dst->offset[z][0], dst_px_orig[z], lengths[z][0]);
memcpy(dst->data + dst->offset[z][1], dst_n_orig[z], lengths[z][1]);
free(dst_px_orig[z]);
free(dst_n_orig[z]);
memcpy(dst->data + dst->offset[z][0], dst_px_orig[z].get(), lengths[z][0]);
memcpy(dst->data + dst->offset[z][1], dst_n_orig[z].get(), lengths[z][1]);
}
return dest_sprite;