Browse Source

Data::getSize now uses size_t instead of int

Alex Szpakowski 11 years ago
parent
commit
50916825d0

+ 4 - 1
src/common/Data.h

@@ -25,6 +25,9 @@
 #include "config.h"
 #include "config.h"
 #include "Object.h"
 #include "Object.h"
 
 
+// C
+#include <stddef.h>
+
 namespace love
 namespace love
 {
 {
 
 
@@ -49,7 +52,7 @@ public:
 	/**
 	/**
 	 * Gets the size of the Data in bytes.
 	 * Gets the size of the Data in bytes.
 	 **/
 	 **/
-	virtual int getSize() const = 0;
+	virtual size_t getSize() const = 0;
 
 
 }; // Data
 }; // Data
 
 

+ 2 - 2
src/common/wrap_Data.cpp

@@ -31,7 +31,7 @@ Data *luax_checkdata(lua_State *L, int idx)
 int w_Data_getString(lua_State *L)
 int w_Data_getString(lua_State *L)
 {
 {
 	Data *t = luax_checkdata(L, 1);
 	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;
 	return 1;
 }
 }
 
 
@@ -45,7 +45,7 @@ int w_Data_getPointer(lua_State *L)
 int w_Data_getSize(lua_State *L)
 int w_Data_getSize(lua_State *L)
 {
 {
 	Data *t = luax_checkdata(L, 1);
 	Data *t = luax_checkdata(L, 1);
-	lua_pushinteger(L, t->getSize());
+	lua_pushnumber(L, (lua_Number) t->getSize());
 	return 1;
 	return 1;
 }
 }
 
 

+ 6 - 11
src/modules/filesystem/FileData.cpp

@@ -20,9 +20,9 @@
 
 
 #include "FileData.h"
 #include "FileData.h"
 
 
-// STD
+// C++
 #include <iostream>
 #include <iostream>
-#include <climits>
+#include <limits>
 
 
 namespace love
 namespace love
 {
 {
@@ -31,7 +31,7 @@ namespace filesystem
 
 
 FileData::FileData(uint64 size, const std::string &filename)
 FileData::FileData(uint64 size, const std::string &filename)
 	: data(new char[(size_t) size])
 	: data(new char[(size_t) size])
-	, size(size)
+	, size((size_t) size)
 	, filename(filename)
 	, filename(filename)
 {
 {
 	if (filename.rfind('.') != std::string::npos)
 	if (filename.rfind('.') != std::string::npos)
@@ -48,15 +48,10 @@ void *FileData::getData() const
 	return (void *)data;
 	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<size_t>::max();
+	return size > sizemax ? sizemax : (size_t) size;
 }
 }
 
 
 const std::string &FileData::getFilename() const
 const std::string &FileData::getFilename() const

+ 1 - 3
src/modules/filesystem/FileData.h

@@ -49,9 +49,7 @@ public:
 
 
 	// Implements Data.
 	// Implements Data.
 	void *getData() const;
 	void *getData() const;
-	//TODO: Enable this
-	//uint64 getSize() const;
-	int getSize() const;
+	size_t getSize() const;
 
 
 	const std::string &getFilename() const;
 	const std::string &getFilename() const;
 	const std::string &getExtension() const;
 	const std::string &getExtension() const;

+ 3 - 3
src/modules/font/GlyphData.cpp

@@ -64,16 +64,16 @@ void *GlyphData::getData() const
 	return (void *) data;
 	return (void *) data;
 }
 }
 
 
-int GlyphData::getSize() const
+size_t GlyphData::getSize() const
 {
 {
 	switch (format)
 	switch (format)
 	{
 	{
 	case GlyphData::FORMAT_LUMINANCE_ALPHA:
 	case GlyphData::FORMAT_LUMINANCE_ALPHA:
-		return getWidth() * getHeight() * 2;
+		return size_t(getWidth() * getHeight() * 2);
 		break;
 		break;
 	case GlyphData::FORMAT_RGBA:
 	case GlyphData::FORMAT_RGBA:
 	default:
 	default:
-		return getWidth() * getHeight() * 4;
+		return size_t(getWidth() * getHeight() * 4);
 		break;
 		break;
 	}
 	}
 
 

+ 1 - 1
src/modules/font/GlyphData.h

@@ -67,7 +67,7 @@ public:
 
 
 	// Implements Data.
 	// Implements Data.
 	void *getData() const;
 	void *getData() const;
-	int getSize() const;
+	size_t getSize() const;
 
 
 	/**
 	/**
 	 * Gets the height of the glyph.
 	 * Gets the height of the glyph.

+ 4 - 4
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
 void *CompressedData::getData() const
@@ -51,11 +51,11 @@ int CompressedData::getMipmapCount() const
 	return dataImages.size();
 	return dataImages.size();
 }
 }
 
 
-int CompressedData::getSize(int miplevel) const
+size_t CompressedData::getSize(int miplevel) const
 {
 {
 	checkMipmapLevelExists(miplevel);
 	checkMipmapLevelExists(miplevel);
 
 
-	return int(dataImages[miplevel].size);
+	return dataImages[miplevel].size;
 }
 }
 
 
 void *CompressedData::getData(int miplevel) const
 void *CompressedData::getData(int miplevel) const

+ 2 - 2
src/modules/image/CompressedData.h

@@ -71,7 +71,7 @@ public:
 
 
 	// Implements Data.
 	// Implements Data.
 	virtual void *getData() const;
 	virtual void *getData() const;
-	virtual int getSize() const;
+	virtual size_t getSize() const;
 
 
 	/**
 	/**
 	 * Gets the number of mipmaps in this Compressed Image Data.
 	 * 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.
 	 * 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.
 	 * Gets the byte data of a sub-image at the specified mipmap level.

+ 2 - 2
src/modules/image/ImageData.cpp

@@ -38,9 +38,9 @@ ImageData::~ImageData()
 	delete mutex;
 	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
 void *ImageData::getData() const

+ 1 - 1
src/modules/image/ImageData.h

@@ -130,7 +130,7 @@ public:
 
 
 	// Implements Data.
 	// Implements Data.
 	virtual void *getData() const;
 	virtual void *getData() const;
-	virtual int getSize() const;
+	virtual size_t getSize() const;
 
 
 protected:
 protected:
 
 

+ 14 - 14
src/modules/sound/SoundData.cpp

@@ -21,11 +21,11 @@
 #include "SoundData.h"
 #include "SoundData.h"
 
 
 // C
 // C
-#include <climits>
 #include <cstdlib>
 #include <cstdlib>
 #include <cstring>
 #include <cstring>
 
 
-// STL
+// C++
+#include <limits>
 #include <iostream>
 #include <iostream>
 #include <vector>
 #include <vector>
 
 
@@ -48,9 +48,9 @@ SoundData::SoundData(Decoder *decoder)
 	{
 	{
 		// Expand or allocate buffer. Note that realloc may move
 		// Expand or allocate buffer. Note that realloc may move
 		// memory to other locations.
 		// 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;
 				bufferSize <<= 1;
 			data = (uint8 *) realloc(data, bufferSize);
 			data = (uint8 *) realloc(data, bufferSize);
 		}
 		}
@@ -61,21 +61,21 @@ SoundData::SoundData(Decoder *decoder)
 		// Copy memory into new part of memory.
 		// Copy memory into new part of memory.
 		memcpy(data + size, decoder->getBuffer(), decoded);
 		memcpy(data + size, decoder->getBuffer(), decoded);
 
 
-		// Keep this up to date.
-		size += decoded;
-
 		// Overflow check.
 		// Overflow check.
-		if (size < 0)
+		if (size > std::numeric_limits<size_t>::max() - decoded)
 		{
 		{
 			free(data);
 			free(data);
 			throw love::Exception("Not enough memory.");
 			throw love::Exception("Not enough memory.");
 		}
 		}
 
 
+		// Keep this up to date.
+		size += decoded;
+
 		decoded = decoder->decode();
 		decoded = decoder->decode();
 	}
 	}
 
 
 	// Shrink buffer if necessary.
 	// Shrink buffer if necessary.
-	if (data && bufferSize > (size_t) size)
+	if (data && bufferSize > size)
 		data = (uint8 *) realloc(data, size);
 		data = (uint8 *) realloc(data, size);
 
 
 	channels = decoder->getChannels();
 	channels = decoder->getChannels();
@@ -136,7 +136,7 @@ void SoundData::load(int samples, int sampleRate, int bitDepth, int channels, vo
 
 
 	double realsize = samples;
 	double realsize = samples;
 	realsize *= (bitDepth / 8) * channels;
 	realsize *= (bitDepth / 8) * channels;
-	if (realsize > INT_MAX)
+	if (realsize > std::numeric_limits<size_t>::max())
 		throw love::Exception("Data is too big!");
 		throw love::Exception("Data is too big!");
 
 
 	data = (uint8 *) malloc(size);
 	data = (uint8 *) malloc(size);
@@ -154,9 +154,9 @@ void *SoundData::getData() const
 	return (void *)data;
 	return (void *)data;
 }
 }
 
 
-int SoundData::getSize() const
+size_t SoundData::getSize() const
 {
 {
-	return (int)size;
+	return size;
 }
 }
 
 
 int SoundData::getChannels() const
 int SoundData::getChannels() const
@@ -187,7 +187,7 @@ float SoundData::getDuration() const
 void SoundData::setSample(int i, float sample)
 void SoundData::setSample(int i, float sample)
 {
 {
 	// Check range.
 	// 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!");
 		throw love::Exception("Attempt to set out-of-range sample!");
 
 
 	if (bitDepth == 16)
 	if (bitDepth == 16)
@@ -206,7 +206,7 @@ void SoundData::setSample(int i, float sample)
 float SoundData::getSample(int i) const
 float SoundData::getSample(int i) const
 {
 {
 	// Check range.
 	// 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!");
 		throw love::Exception("Attempt to get out-of-range sample!");
 
 
 	if (bitDepth == 16)
 	if (bitDepth == 16)

+ 2 - 2
src/modules/sound/SoundData.h

@@ -43,7 +43,7 @@ public:
 
 
 	// Implements Data.
 	// Implements Data.
 	void *getData() const;
 	void *getData() const;
-	int getSize() const;
+	size_t getSize() const;
 
 
 	virtual int getChannels() const;
 	virtual int getChannels() const;
 	virtual int getBitDepth() const;
 	virtual int getBitDepth() const;
@@ -60,7 +60,7 @@ private:
 	void load(int samples, int sampleRate, int bitDepth, int channels, void *newData = 0);
 	void load(int samples, int sampleRate, int bitDepth, int channels, void *newData = 0);
 
 
 	uint8 *data;
 	uint8 *data;
-	int size;
+	size_t size;
 
 
 	int sampleRate;
 	int sampleRate;
 	int bitDepth;
 	int bitDepth;