diff --git a/src/common/Data.h b/src/common/Data.h index ffea71cf9..1ba34c49e 100644 --- a/src/common/Data.h +++ b/src/common/Data.h @@ -25,6 +25,9 @@ #include "config.h" #include "Object.h" +// C +#include + namespace love { @@ -49,7 +52,7 @@ public: /** * Gets the size of the Data in bytes. **/ - virtual int getSize() const = 0; + virtual size_t getSize() const = 0; }; // Data diff --git a/src/common/wrap_Data.cpp b/src/common/wrap_Data.cpp index c093a78ff..76a8b6871 100644 --- a/src/common/wrap_Data.cpp +++ b/src/common/wrap_Data.cpp @@ -31,7 +31,7 @@ Data *luax_checkdata(lua_State *L, int idx) int w_Data_getString(lua_State *L) { Data *t = luax_checkdata(L, 1); - lua_pushlstring(L, (const char *) t->getData(), (size_t) t->getSize()); + lua_pushlstring(L, (const char *) t->getData(), t->getSize()); return 1; } @@ -45,7 +45,7 @@ int w_Data_getPointer(lua_State *L) int w_Data_getSize(lua_State *L) { Data *t = luax_checkdata(L, 1); - lua_pushinteger(L, t->getSize()); + lua_pushnumber(L, (lua_Number) t->getSize()); return 1; } diff --git a/src/modules/filesystem/FileData.cpp b/src/modules/filesystem/FileData.cpp index dd59c73e9..4da8ea431 100644 --- a/src/modules/filesystem/FileData.cpp +++ b/src/modules/filesystem/FileData.cpp @@ -20,9 +20,9 @@ #include "FileData.h" -// STD +// C++ #include -#include +#include namespace love { @@ -31,7 +31,7 @@ namespace filesystem FileData::FileData(uint64 size, const std::string &filename) : data(new char[(size_t) size]) - , size(size) + , size((size_t) size) , filename(filename) { if (filename.rfind('.') != std::string::npos) @@ -48,15 +48,10 @@ void *FileData::getData() const return (void *)data; } -// TODO: Enable this -/*uint64 FileData::getSize() const +size_t FileData::getSize() const { - return size; -}*/ - -int FileData::getSize() const -{ - return size > INT_MAX ? INT_MAX : (int) size; + size_t sizemax = std::numeric_limits::max(); + return size > sizemax ? sizemax : (size_t) size; } const std::string &FileData::getFilename() const diff --git a/src/modules/filesystem/FileData.h b/src/modules/filesystem/FileData.h index e74866be5..55a0eee06 100644 --- a/src/modules/filesystem/FileData.h +++ b/src/modules/filesystem/FileData.h @@ -49,9 +49,7 @@ public: // Implements Data. void *getData() const; - //TODO: Enable this - //uint64 getSize() const; - int getSize() const; + size_t getSize() const; const std::string &getFilename() const; const std::string &getExtension() const; diff --git a/src/modules/font/GlyphData.cpp b/src/modules/font/GlyphData.cpp index 27cf32af8..7ed2b3db2 100644 --- a/src/modules/font/GlyphData.cpp +++ b/src/modules/font/GlyphData.cpp @@ -64,16 +64,16 @@ void *GlyphData::getData() const return (void *) data; } -int GlyphData::getSize() const +size_t GlyphData::getSize() const { switch (format) { case GlyphData::FORMAT_LUMINANCE_ALPHA: - return getWidth() * getHeight() * 2; + return size_t(getWidth() * getHeight() * 2); break; case GlyphData::FORMAT_RGBA: default: - return getWidth() * getHeight() * 4; + return size_t(getWidth() * getHeight() * 4); break; } diff --git a/src/modules/font/GlyphData.h b/src/modules/font/GlyphData.h index 6144dcb9b..a77755944 100644 --- a/src/modules/font/GlyphData.h +++ b/src/modules/font/GlyphData.h @@ -67,7 +67,7 @@ public: // Implements Data. void *getData() const; - int getSize() const; + size_t getSize() const; /** * Gets the height of the glyph. diff --git a/src/modules/image/CompressedData.cpp b/src/modules/image/CompressedData.cpp index fc85e51f0..e4d47331c 100644 --- a/src/modules/image/CompressedData.cpp +++ b/src/modules/image/CompressedData.cpp @@ -36,9 +36,9 @@ CompressedData::~CompressedData() { } -int CompressedData::getSize() const +size_t CompressedData::getSize() const { - return int(dataSize); + return dataSize; } void *CompressedData::getData() const @@ -51,11 +51,11 @@ int CompressedData::getMipmapCount() const return dataImages.size(); } -int CompressedData::getSize(int miplevel) const +size_t CompressedData::getSize(int miplevel) const { checkMipmapLevelExists(miplevel); - return int(dataImages[miplevel].size); + return dataImages[miplevel].size; } void *CompressedData::getData(int miplevel) const diff --git a/src/modules/image/CompressedData.h b/src/modules/image/CompressedData.h index 9df985b4f..42ee5e58d 100644 --- a/src/modules/image/CompressedData.h +++ b/src/modules/image/CompressedData.h @@ -71,7 +71,7 @@ public: // Implements Data. virtual void *getData() const; - virtual int getSize() const; + virtual size_t getSize() const; /** * Gets the number of mipmaps in this Compressed Image Data. @@ -82,7 +82,7 @@ public: /** * Gets the size in bytes of a sub-image at the specified mipmap level. **/ - int getSize(int miplevel) const; + size_t getSize(int miplevel) const; /** * Gets the byte data of a sub-image at the specified mipmap level. diff --git a/src/modules/image/ImageData.cpp b/src/modules/image/ImageData.cpp index e71f7feb3..4264555da 100644 --- a/src/modules/image/ImageData.cpp +++ b/src/modules/image/ImageData.cpp @@ -38,9 +38,9 @@ ImageData::~ImageData() delete mutex; } -int ImageData::getSize() const +size_t ImageData::getSize() const { - return getWidth()*getHeight()*sizeof(pixel); + return size_t(getWidth()*getHeight())*sizeof(pixel); } void *ImageData::getData() const diff --git a/src/modules/image/ImageData.h b/src/modules/image/ImageData.h index 3349722fd..f85494d8b 100644 --- a/src/modules/image/ImageData.h +++ b/src/modules/image/ImageData.h @@ -130,7 +130,7 @@ public: // Implements Data. virtual void *getData() const; - virtual int getSize() const; + virtual size_t getSize() const; protected: diff --git a/src/modules/sound/SoundData.cpp b/src/modules/sound/SoundData.cpp index 6b7dbc538..81b5f5f5c 100644 --- a/src/modules/sound/SoundData.cpp +++ b/src/modules/sound/SoundData.cpp @@ -21,11 +21,11 @@ #include "SoundData.h" // C -#include #include #include -// STL +// C++ +#include #include #include @@ -48,9 +48,9 @@ SoundData::SoundData(Decoder *decoder) { // Expand or allocate buffer. Note that realloc may move // memory to other locations. - if (!data || bufferSize < (size_t) size + decoded) + if (!data || bufferSize < size + decoded) { - while (bufferSize < (size_t) size + decoded) + while (bufferSize < size + decoded) bufferSize <<= 1; data = (uint8 *) realloc(data, bufferSize); } @@ -61,21 +61,21 @@ SoundData::SoundData(Decoder *decoder) // Copy memory into new part of memory. memcpy(data + size, decoder->getBuffer(), decoded); - // Keep this up to date. - size += decoded; - // Overflow check. - if (size < 0) + if (size > std::numeric_limits::max() - decoded) { free(data); throw love::Exception("Not enough memory."); } + // Keep this up to date. + size += decoded; + decoded = decoder->decode(); } // Shrink buffer if necessary. - if (data && bufferSize > (size_t) size) + if (data && bufferSize > size) data = (uint8 *) realloc(data, size); channels = decoder->getChannels(); @@ -136,7 +136,7 @@ void SoundData::load(int samples, int sampleRate, int bitDepth, int channels, vo double realsize = samples; realsize *= (bitDepth / 8) * channels; - if (realsize > INT_MAX) + if (realsize > std::numeric_limits::max()) throw love::Exception("Data is too big!"); data = (uint8 *) malloc(size); @@ -154,9 +154,9 @@ void *SoundData::getData() const return (void *)data; } -int SoundData::getSize() const +size_t SoundData::getSize() const { - return (int)size; + return size; } int SoundData::getChannels() const @@ -187,7 +187,7 @@ float SoundData::getDuration() const void SoundData::setSample(int i, float sample) { // Check range. - if (i < 0 || i >= size/(bitDepth/8)) + if (i < 0 || (size_t) i >= size/(bitDepth/8)) throw love::Exception("Attempt to set out-of-range sample!"); if (bitDepth == 16) @@ -206,7 +206,7 @@ void SoundData::setSample(int i, float sample) float SoundData::getSample(int i) const { // Check range. - if (i < 0 || i >= size/(bitDepth/8)) + if (i < 0 || (size_t) i >= size/(bitDepth/8)) throw love::Exception("Attempt to get out-of-range sample!"); if (bitDepth == 16) diff --git a/src/modules/sound/SoundData.h b/src/modules/sound/SoundData.h index cdc8734a4..1a06e6964 100644 --- a/src/modules/sound/SoundData.h +++ b/src/modules/sound/SoundData.h @@ -43,7 +43,7 @@ public: // Implements Data. void *getData() const; - int getSize() const; + size_t getSize() const; virtual int getChannels() const; virtual int getBitDepth() const; @@ -60,7 +60,7 @@ private: void load(int samples, int sampleRate, int bitDepth, int channels, void *newData = 0); uint8 *data; - int size; + size_t size; int sampleRate; int bitDepth;