Changeset - r8037:0f84abbbbbf6
[Not reviewed]
master
0 13 0
rubidium - 17 years ago 2007-12-08 14:50:41
rubidium@openttd.org
(svn r11597) -Change: replace all remaining instances of (re|m|c)alloc with (Re|M|C)allocT and add a check for out-of-memory situations to the *allocT functions.
13 files changed with 19 insertions and 16 deletions:
0 comments (0 inline, 0 general)
src/bmp.cpp
Show inline comments
 
@@ -351,13 +351,13 @@ bool BmpReadHeader(BmpBuffer *buffer, Bm
 
 * 1 bpp and 4 bpp bitmaps are converted to 8 bpp bitmaps
 
 */
 
bool BmpReadBitmap(BmpBuffer *buffer, BmpInfo *info, BmpData *data)
 
{
 
	assert(info != NULL && data != NULL);
 

	
 
	data->bitmap = (byte*)calloc(info->width * info->height, ((info->bpp == 24) ? 3 : 1) * sizeof(byte));
 
	data->bitmap = CallocT<byte>(info->width * info->height * ((info->bpp == 24) ? 3 : 1));
 
	if (data->bitmap == NULL) return false;
 

	
 
	/* Load image */
 
	SetStreamOffset(buffer, info->offset);
 
	switch (info->compression) {
 
	case 0: // no compression
src/fontcache.cpp
Show inline comments
 
@@ -362,13 +362,13 @@ static void SetGlyphPtr(FontSize size, W
 
	_glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].sprite = glyph->sprite;
 
	_glyph_ptr[size][GB(key, 8, 8)][GB(key, 0, 8)].width  = glyph->width;
 
}
 

	
 
void *AllocateFont(size_t size)
 
{
 
	return malloc(size);
 
	return MallocT<byte>(size);
 
}
 

	
 

	
 
/* Check if a glyph should be rendered with antialiasing */
 
static bool GetFontAAState(FontSize size)
 
{
src/helpers.hpp
Show inline comments
 
@@ -9,26 +9,29 @@
 

	
 
/** When allocating using malloc/calloc in C++ it is usually needed to cast the return value
 
*  from void* to the proper pointer type. Another alternative would be MallocT<> as follows */
 
template <typename T> FORCEINLINE T* MallocT(size_t num_elements)
 
{
 
	T *t_ptr = (T*)malloc(num_elements * sizeof(T));
 
	if (t_ptr == NULL && num_elements != 0) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T));
 
	return t_ptr;
 
}
 
/** When allocating using malloc/calloc in C++ it is usually needed to cast the return value
 
*  from void* to the proper pointer type. Another alternative would be MallocT<> as follows */
 
template <typename T> FORCEINLINE T* CallocT(size_t num_elements)
 
{
 
	T *t_ptr = (T*)calloc(num_elements, sizeof(T));
 
	if (t_ptr == NULL && num_elements != 0) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T));
 
	return t_ptr;
 
}
 
/** When allocating using malloc/calloc in C++ it is usually needed to cast the return value
 
*  from void* to the proper pointer type. Another alternative would be MallocT<> as follows */
 
template <typename T> FORCEINLINE T* ReallocT(T* t_ptr, size_t num_elements)
 
{
 
	t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
 
	if (t_ptr == NULL && num_elements != 0) error("Out of memory. Cannot reallocate %i bytes", num_elements * sizeof(T));
 
	return t_ptr;
 
}
 

	
 

	
 
/** type safe swap operation */
 
template<typename T> void Swap(T& a, T& b)
src/misc/blob.hpp
Show inline comments
 
@@ -295,13 +295,13 @@ public:
 
		return min_alloc;
 
	}
 

	
 
	/** all allocation should happen here */
 
	static FORCEINLINE CHdr* RawAlloc(bsize_t num_bytes)
 
	{
 
		return (CHdr*)malloc(num_bytes);
 
		return (CHdr*)MallocT<byte>(num_bytes);
 
	}
 

	
 
	/** all deallocations should happen here */
 
	static FORCEINLINE void RawFree(CHdr* p)
 
	{
 
		free(p);
src/misc/fixedsizearray.hpp
Show inline comments
 
@@ -31,13 +31,13 @@ struct CFixedSizeArrayT {
 
	static const int ThdrSize  = sizeof(CHdr);   // size of header
 

	
 
	/** Default constructor. Preallocate space for items and header, then initialize header. */
 
	CFixedSizeArrayT()
 
	{
 
		// allocate block for header + items (don't construct items)
 
		m_items = (Titem*)(((int8*)malloc(ThdrSize + Tcapacity * sizeof(Titem))) + ThdrSize);
 
		m_items = (Titem*)((MallocT<int8>(ThdrSize + Tcapacity * sizeof(Titem))) + ThdrSize);
 
		SizeRef() = 0; // initial number of items
 
		RefCnt() = 1; // initial reference counter
 
	}
 

	
 
	/** Copy constructor. Preallocate space for items and header, then initialize header. */
 
	CFixedSizeArrayT(const CFixedSizeArrayT<Titem_, Tcapacity_>& src)
src/queue.cpp
Show inline comments
 
@@ -307,13 +307,13 @@ void init_Hash(Hash* h, Hash_HashProc* h
 
#ifdef HASH_DEBUG
 
	debug("Allocated hash: %p", h);
 
#endif
 
	h->hash = hash;
 
	h->size = 0;
 
	h->num_buckets = num_buckets;
 
	h->buckets = (HashNode*)malloc(num_buckets * (sizeof(*h->buckets) + sizeof(*h->buckets_in_use)));
 
	h->buckets = (HashNode*)MallocT<byte>(num_buckets * (sizeof(*h->buckets) + sizeof(*h->buckets_in_use)));
 
#ifdef HASH_DEBUG
 
	debug("Buckets = %p", h->buckets);
 
#endif
 
	h->buckets_in_use = (bool*)(h->buckets + num_buckets);
 
	for (i = 0; i < num_buckets; i++) h->buckets_in_use[i] = false;
 
}
src/saveload.cpp
Show inline comments
 
@@ -572,13 +572,13 @@ static void SlString(void *ptr, size_t l
 
			case SLE_VAR_STR:
 
			case SLE_VAR_STRQ: // Malloc'd string, free previous incarnation, and allocate
 
				free(*(char**)ptr);
 
				if (len == 0) {
 
					*(char**)ptr = NULL;
 
				} else {
 
					*(char**)ptr = (char*)malloc(len + 1); // terminating '\0'
 
					*(char**)ptr = MallocT<char>(len + 1); // terminating '\0'
 
					ptr = *(char**)ptr;
 
					SlCopyBytes(ptr, len);
 
				}
 
				break;
 
		}
 

	
 
@@ -1057,13 +1057,13 @@ static void WriteLZO(uint size)
 
	if (fwrite(out, outlen + sizeof(uint32)*2, 1, _sl.fh) != 1) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE);
 
}
 

	
 
static bool InitLZO()
 
{
 
	_sl.bufsize = LZO_SIZE;
 
	_sl.buf = _sl.buf_ori = (byte*)malloc(LZO_SIZE);
 
	_sl.buf = _sl.buf_ori = MallocT<byte>(LZO_SIZE);
 
	return true;
 
}
 

	
 
static void UninitLZO()
 
{
 
	free(_sl.buf_ori);
 
@@ -1082,13 +1082,13 @@ static void WriteNoComp(uint size)
 
	fwrite(_sl.buf, 1, size, _sl.fh);
 
}
 

	
 
static bool InitNoComp()
 
{
 
	_sl.bufsize = LZO_SIZE;
 
	_sl.buf = _sl.buf_ori = (byte*)malloc(LZO_SIZE);
 
	_sl.buf = _sl.buf_ori = MallocT<byte>(LZO_SIZE);
 
	return true;
 
}
 

	
 
static void UninitNoComp()
 
{
 
	free(_sl.buf_ori);
 
@@ -1151,13 +1151,13 @@ static z_stream _z;
 
static bool InitReadZlib()
 
{
 
	memset(&_z, 0, sizeof(_z));
 
	if (inflateInit(&_z) != Z_OK) return false;
 

	
 
	_sl.bufsize = 4096;
 
	_sl.buf = _sl.buf_ori = (byte*)malloc(4096 + 4096); // also contains fread buffer
 
	_sl.buf = _sl.buf_ori = MallocT<byte>(4096 + 4096); // also contains fread buffer
 
	return true;
 
}
 

	
 
static uint ReadZlib()
 
{
 
	int r;
 
@@ -1191,13 +1191,13 @@ static void UninitReadZlib()
 
static bool InitWriteZlib()
 
{
 
	memset(&_z, 0, sizeof(_z));
 
	if (deflateInit(&_z, 6) != Z_OK) return false;
 

	
 
	_sl.bufsize = 4096;
 
	_sl.buf = _sl.buf_ori = (byte*)malloc(4096); // also contains fread buffer
 
	_sl.buf = _sl.buf_ori = MallocT<byte>(4096); // also contains fread buffer
 
	return true;
 
}
 

	
 
static void WriteZlibLoop(z_streamp z, byte *p, uint len, int mode)
 
{
 
	byte buf[1024]; // output buffer
src/settings.cpp
Show inline comments
 
@@ -77,13 +77,13 @@ struct SettingsMemoryPool {
 

	
 
static SettingsMemoryPool *pool_new(uint minsize)
 
{
 
	SettingsMemoryPool *p;
 
	if (minsize < 4096 - 12) minsize = 4096 - 12;
 

	
 
	p = (SettingsMemoryPool*)malloc(sizeof(SettingsMemoryPool) - 1 + minsize);
 
	p = (SettingsMemoryPool*)MallocT<byte>(sizeof(SettingsMemoryPool) - 1 + minsize);
 
	p->pos = 0;
 
	p->size = minsize;
 
	p->next = NULL;
 
	return p;
 
}
 

	
src/spritecache.cpp
Show inline comments
 
@@ -488,13 +488,13 @@ const void *GetRawSprite(SpriteID sprite
 
}
 

	
 

	
 
void GfxInitSpriteMem()
 
{
 
	/* initialize sprite cache heap */
 
	if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)malloc(_sprite_cache_size * 1024 * 1024);
 
	if (_spritecache_ptr == NULL) _spritecache_ptr = (MemBlock*)MallocT<byte>(_sprite_cache_size * 1024 * 1024);
 

	
 
	/* A big free block */
 
	_spritecache_ptr->size = ((_sprite_cache_size * 1024 * 1024) - sizeof(MemBlock)) | S_FREE_MASK;
 
	/* Sentinel block (identified by size == 0) */
 
	NextBlock(_spritecache_ptr)->size = 0;
 

	
src/spriteloader/png.cpp
Show inline comments
 
@@ -141,13 +141,13 @@ static bool LoadPNG(SpriteLoader::Sprite
 

	
 
		pixelsize = sizeof(uint32);
 
	} else {
 
		pixelsize = sizeof(uint8);
 
	}
 

	
 
	row_pointer = (png_byte *)malloc(info_ptr->width * pixelsize);
 
	row_pointer = (png_byte *)MallocT<byte>(info_ptr->width * pixelsize);
 
	if (row_pointer == NULL) {
 
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
 
		return false;
 
	}
 

	
 
	for (i = 0; i < info_ptr->height; i++) {
src/texteff.cpp
Show inline comments
 
@@ -289,13 +289,13 @@ TextEffectID AddTextEffect(StringID msg,
 
		if (_text_effect_list[i].string_id == INVALID_STRING_ID) break;
 
	}
 

	
 
	/* If there is none found, we grow the array */
 
	if (i == _num_text_effects) {
 
		_num_text_effects += 25;
 
		_text_effect_list = (TextEffect*) realloc(_text_effect_list, _num_text_effects * sizeof(TextEffect));
 
		_text_effect_list = ReallocT<TextEffect>(_text_effect_list, _num_text_effects);
 
		for (; i < _num_text_effects; i++) _text_effect_list[i].string_id = INVALID_STRING_ID;
 
		i = _num_text_effects - 1;
 
	}
 

	
 
	te = &_text_effect_list[i];
 

	
src/video/dedicated_v.cpp
Show inline comments
 
@@ -130,13 +130,13 @@ static FVideoDriver_Dedicated iFVideoDri
 

	
 

	
 
const char *VideoDriver_Dedicated::Start(const char * const *parm)
 
{
 
	int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
 
	if (bpp == 0) _dedicated_video_mem = NULL;
 
	else          _dedicated_video_mem = malloc(_cur_resolution[0] * _cur_resolution[1] * (bpp / 8));
 
	else          _dedicated_video_mem = MallocT<byte>(_cur_resolution[0] * _cur_resolution[1] * (bpp / 8));
 

	
 
	_screen.width = _screen.pitch = _cur_resolution[0];
 
	_screen.height = _cur_resolution[1];
 

	
 
	SetDebugString("net=6");
 

	
src/win32.cpp
Show inline comments
 
@@ -319,13 +319,13 @@ static void SubmitFile(HWND wnd, const T
 
	h = CreateFile(file, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
 
	if (h == NULL) return;
 

	
 
	size = GetFileSize(h, NULL);
 
	if (size > 500000) goto error1;
 

	
 
	mem = malloc(size);
 
	mem = MallocT<byte>(size);
 
	if (mem == NULL) goto error1;
 

	
 
	if (!ReadFile(h, mem, size, &read, NULL) || read != size) goto error2;
 

	
 
	SubmitCrashReport(wnd, mem, size, file);
 

	
0 comments (0 inline, 0 general)