Skip to content
Permalink
Browse files

Codechange: No need for AutoFreePtr if there's std::unique_ptr.

  • Loading branch information...
michicc authored and LordAro committed Apr 14, 2019
1 parent 4e85ccf commit 38729297f98f6abb154a6acd37b6c5a1b9dea134
Showing with 7 additions and 41 deletions.
  1. +0 −34 src/core/alloc_type.hpp
  2. +7 −7 src/spriteloader/grf.cpp
@@ -117,38 +117,4 @@ class ZeroedMemoryAllocator
inline void operator delete[](void *ptr) { free(ptr); }
};

/**
* A smart pointer class that free()'s the pointer on destruction.
* @tparam T Storage type.
*/
template <typename T>
class AutoFreePtr
{
T *ptr; ///< Stored pointer.

public:
AutoFreePtr(T *ptr) : ptr(ptr) {}
~AutoFreePtr() { free(this->ptr); }

/**
* Take ownership of a new pointer and free the old one if needed.
* @param ptr NEw pointer.
*/
inline void Assign(T *ptr)
{
free(this->ptr);
this->ptr = ptr;
}

/** Dereference pointer. */
inline T *operator ->() { return this->ptr; }
/** Dereference pointer. */
inline const T *operator ->() const { return this->ptr; }

/** Cast to underlaying regular pointer. */
inline operator T *() { return this->ptr; }
/** Cast to underlaying regular pointer. */
inline operator const T *() const { return this->ptr; }
};

#endif /* ALLOC_TYPE_HPP */
@@ -69,8 +69,8 @@ static bool WarnCorruptSprite(uint8 file_slot, size_t file_pos, int line)
*/
bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t file_pos, SpriteType sprite_type, int64 num, byte type, ZoomLevel zoom_lvl, byte colour_fmt, byte container_format)
{
AutoFreePtr<byte> dest_orig(MallocT<byte>(num));
byte *dest = dest_orig;
std::unique_ptr<byte[]> dest_orig(new byte[num]);
byte *dest = dest_orig.get();
const int64 dest_size = num;

/* Read the file, which has some kind of compression */
@@ -89,7 +89,7 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t fi
} else {
/* Copy bytes from earlier in the sprite */
const uint data_offset = ((code & 7) << 8) | FioReadByte();
if (dest - data_offset < dest_orig) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
if (dest - data_offset < dest_orig.get()) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
int size = -(code >> 3);
num -= size;
if (num < 0) return WarnCorruptSprite(file_slot, file_pos, __LINE__);
@@ -123,10 +123,10 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t fi
}

/* Go to that row */
dest = dest_orig + offset;
dest = dest_orig.get() + offset;

do {
if (dest + (container_format >= 2 && sprite->width > 256 ? 4 : 2) > dest_orig + dest_size) {
if (dest + (container_format >= 2 && sprite->width > 256 ? 4 : 2) > dest_orig.get() + dest_size) {
return WarnCorruptSprite(file_slot, file_pos, __LINE__);
}

@@ -152,7 +152,7 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t fi

data = &sprite->data[y * sprite->width + skip];

if (skip + length > sprite->width || dest + length * bpp > dest_orig + dest_size) {
if (skip + length > sprite->width || dest + length * bpp > dest_orig.get() + dest_size) {
return WarnCorruptSprite(file_slot, file_pos, __LINE__);
}

@@ -188,7 +188,7 @@ bool DecodeSingleSprite(SpriteLoader::Sprite *sprite, uint8 file_slot, size_t fi
warning_level = 6;
}

dest = dest_orig;
dest = dest_orig.get();

for (int i = 0; i < sprite->width * sprite->height; i++) {
byte *pixel = &dest[i * bpp];

0 comments on commit 3872929

Please sign in to comment.
You can’t perform that action at this time.