diff options
author | rubidium <rubidium@openttd.org> | 2007-12-08 14:50:41 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2007-12-08 14:50:41 +0000 |
commit | f1e4914b5f379f9821274e8ddc4196b152fcc9b0 (patch) | |
tree | b8ebe2d1af4279edf6f4a53f991c963c77f5bba0 | |
parent | 73c58d8a409d7e5f3dbb864be893a1c0b34defd6 (diff) | |
download | openttd-f1e4914b5f379f9821274e8ddc4196b152fcc9b0.tar.xz |
(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.
-rw-r--r-- | src/bmp.cpp | 2 | ||||
-rw-r--r-- | src/fontcache.cpp | 2 | ||||
-rw-r--r-- | src/helpers.hpp | 3 | ||||
-rw-r--r-- | src/misc/blob.hpp | 2 | ||||
-rw-r--r-- | src/misc/fixedsizearray.hpp | 2 | ||||
-rw-r--r-- | src/queue.cpp | 2 | ||||
-rw-r--r-- | src/saveload.cpp | 10 | ||||
-rw-r--r-- | src/settings.cpp | 2 | ||||
-rw-r--r-- | src/spritecache.cpp | 2 | ||||
-rw-r--r-- | src/spriteloader/png.cpp | 2 | ||||
-rw-r--r-- | src/texteff.cpp | 2 | ||||
-rw-r--r-- | src/video/dedicated_v.cpp | 2 | ||||
-rw-r--r-- | src/win32.cpp | 2 |
13 files changed, 19 insertions, 16 deletions
diff --git a/src/bmp.cpp b/src/bmp.cpp index a96c3680c..fa78b55a8 100644 --- a/src/bmp.cpp +++ b/src/bmp.cpp @@ -354,7 +354,7 @@ 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 */ diff --git a/src/fontcache.cpp b/src/fontcache.cpp index 607265d19..03977c402 100644 --- a/src/fontcache.cpp +++ b/src/fontcache.cpp @@ -365,7 +365,7 @@ static void SetGlyphPtr(FontSize size, WChar key, const GlyphEntry *glyph) void *AllocateFont(size_t size) { - return malloc(size); + return MallocT<byte>(size); } diff --git a/src/helpers.hpp b/src/helpers.hpp index 3edbcf3b8..651cb0f3c 100644 --- a/src/helpers.hpp +++ b/src/helpers.hpp @@ -12,6 +12,7 @@ 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 @@ -19,6 +20,7 @@ template <typename T> FORCEINLINE T* MallocT(size_t num_elements) 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 @@ -26,6 +28,7 @@ template <typename T> FORCEINLINE T* CallocT(size_t num_elements) 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; } diff --git a/src/misc/blob.hpp b/src/misc/blob.hpp index 78cac0f72..fd08385f8 100644 --- a/src/misc/blob.hpp +++ b/src/misc/blob.hpp @@ -298,7 +298,7 @@ public: /** 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 */ diff --git a/src/misc/fixedsizearray.hpp b/src/misc/fixedsizearray.hpp index ee4ea14ca..d0a987e1a 100644 --- a/src/misc/fixedsizearray.hpp +++ b/src/misc/fixedsizearray.hpp @@ -34,7 +34,7 @@ struct CFixedSizeArrayT { 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 } diff --git a/src/queue.cpp b/src/queue.cpp index 53a63bc30..177845865 100644 --- a/src/queue.cpp +++ b/src/queue.cpp @@ -310,7 +310,7 @@ void init_Hash(Hash* h, Hash_HashProc* hash, uint num_buckets) 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 diff --git a/src/saveload.cpp b/src/saveload.cpp index 7358f6a32..43ce745db 100644 --- a/src/saveload.cpp +++ b/src/saveload.cpp @@ -575,7 +575,7 @@ static void SlString(void *ptr, size_t length, VarType conv) 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); } @@ -1060,7 +1060,7 @@ static void WriteLZO(uint size) 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; } @@ -1085,7 +1085,7 @@ static void WriteNoComp(uint size) 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; } @@ -1154,7 +1154,7 @@ static bool InitReadZlib() 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; } @@ -1194,7 +1194,7 @@ static bool InitWriteZlib() 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; } diff --git a/src/settings.cpp b/src/settings.cpp index 68b187707..6e94cebea 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -80,7 +80,7 @@ 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; diff --git a/src/spritecache.cpp b/src/spritecache.cpp index c303629d0..90abb0526 100644 --- a/src/spritecache.cpp +++ b/src/spritecache.cpp @@ -491,7 +491,7 @@ const void *GetRawSprite(SpriteID sprite, bool real_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; diff --git a/src/spriteloader/png.cpp b/src/spriteloader/png.cpp index 2bb7227d5..7429dda68 100644 --- a/src/spriteloader/png.cpp +++ b/src/spriteloader/png.cpp @@ -144,7 +144,7 @@ static bool LoadPNG(SpriteLoader::Sprite *sprite, const char *filename, uint32 i 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; diff --git a/src/texteff.cpp b/src/texteff.cpp index eae4e0aed..c0a136c26 100644 --- a/src/texteff.cpp +++ b/src/texteff.cpp @@ -292,7 +292,7 @@ TextEffectID AddTextEffect(StringID msg, int x, int y, uint16 duration, TextEffe /* 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; } diff --git a/src/video/dedicated_v.cpp b/src/video/dedicated_v.cpp index e7b9302fe..0d4afae5f 100644 --- a/src/video/dedicated_v.cpp +++ b/src/video/dedicated_v.cpp @@ -133,7 +133,7 @@ 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]; diff --git a/src/win32.cpp b/src/win32.cpp index e0298a0f8..0e3d4f4ef 100644 --- a/src/win32.cpp +++ b/src/win32.cpp @@ -322,7 +322,7 @@ static void SubmitFile(HWND wnd, const TCHAR *file) 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; |