mirror of https://github.com/OpenTTD/OpenTTD
(svn r15568) -Cleanup: *allocT/AllocaM doesn't return NULL when allocating fails
parent
8beca127dd
commit
d73c1fa7bf
|
@ -331,7 +331,6 @@ bool BmpReadHeader(BmpBuffer *buffer, BmpInfo *info, BmpData *data)
|
||||||
if (info->palette_size == 0) info->palette_size = 1 << info->bpp;
|
if (info->palette_size == 0) info->palette_size = 1 << info->bpp;
|
||||||
|
|
||||||
data->palette = CallocT<Colour>(info->palette_size);
|
data->palette = CallocT<Colour>(info->palette_size);
|
||||||
if (data->palette == NULL) return false;
|
|
||||||
|
|
||||||
for (i = 0; i < info->palette_size; i++) {
|
for (i = 0; i < info->palette_size; i++) {
|
||||||
data->palette[i].b = ReadByte(buffer);
|
data->palette[i].b = ReadByte(buffer);
|
||||||
|
@ -353,7 +352,6 @@ bool BmpReadBitmap(BmpBuffer *buffer, BmpInfo *info, BmpData *data)
|
||||||
assert(info != NULL && data != NULL);
|
assert(info != NULL && data != NULL);
|
||||||
|
|
||||||
data->bitmap = CallocT<byte>(info->width * info->height * ((info->bpp == 24) ? 3 : 1));
|
data->bitmap = CallocT<byte>(info->width * info->height * ((info->bpp == 24) ? 3 : 1));
|
||||||
if (data->bitmap == NULL) return false;
|
|
||||||
|
|
||||||
/* Load image */
|
/* Load image */
|
||||||
SetStreamOffset(buffer, info->offset);
|
SetStreamOffset(buffer, info->offset);
|
||||||
|
|
|
@ -991,20 +991,17 @@ void SanitizeFilename(char *filename)
|
||||||
|
|
||||||
void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize)
|
void *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize)
|
||||||
{
|
{
|
||||||
FILE *in;
|
FILE *in = fopen(filename, "rb");
|
||||||
byte *mem;
|
|
||||||
size_t len;
|
|
||||||
|
|
||||||
in = fopen(filename, "rb");
|
|
||||||
if (in == NULL) return NULL;
|
if (in == NULL) return NULL;
|
||||||
|
|
||||||
fseek(in, 0, SEEK_END);
|
fseek(in, 0, SEEK_END);
|
||||||
len = ftell(in);
|
size_t len = ftell(in);
|
||||||
fseek(in, 0, SEEK_SET);
|
fseek(in, 0, SEEK_SET);
|
||||||
if (len > maxsize || (mem = MallocT<byte>(len + 1)) == NULL) {
|
if (len > maxsize) {
|
||||||
fclose(in);
|
fclose(in);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
byte *mem = MallocT<byte>(len + 1);
|
||||||
mem[len] = 0;
|
mem[len] = 0;
|
||||||
if (fread(mem, len, 1, in) != 1) {
|
if (fread(mem, len, 1, in) != 1) {
|
||||||
fclose(in);
|
fclose(in);
|
||||||
|
|
|
@ -137,14 +137,6 @@ static bool ReadHeightmapPNG(char *filename, uint *x, uint *y, byte **map)
|
||||||
|
|
||||||
if (map != NULL) {
|
if (map != NULL) {
|
||||||
*map = MallocT<byte>(info_ptr->width * info_ptr->height);
|
*map = MallocT<byte>(info_ptr->width * info_ptr->height);
|
||||||
|
|
||||||
if (*map == NULL) {
|
|
||||||
ShowErrorMessage(STR_PNGMAP_ERR_MISC, STR_PNGMAP_ERROR, 0, 0);
|
|
||||||
fclose(fp);
|
|
||||||
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ReadHeightmapPNGImageData(*map, png_ptr, info_ptr);
|
ReadHeightmapPNGImageData(*map, png_ptr, info_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -253,15 +245,7 @@ static bool ReadHeightmapBMP(char *filename, uint *x, uint *y, byte **map)
|
||||||
}
|
}
|
||||||
|
|
||||||
*map = MallocT<byte>(info.width * info.height);
|
*map = MallocT<byte>(info.width * info.height);
|
||||||
if (*map == NULL) {
|
|
||||||
ShowErrorMessage(STR_PNGMAP_ERR_MISC, STR_BMPMAP_ERROR, 0, 0);
|
|
||||||
fclose(f);
|
|
||||||
BmpDestroyData(&data);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
ReadHeightmapBMPImageData(*map, &info, &data);
|
ReadHeightmapBMPImageData(*map, &info, &data);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BmpDestroyData(&data);
|
BmpDestroyData(&data);
|
||||||
|
|
|
@ -52,11 +52,6 @@ void AllocateMap(uint size_x, uint size_y)
|
||||||
free(_m);
|
free(_m);
|
||||||
free(_me);
|
free(_me);
|
||||||
|
|
||||||
/* XXX @todo handle memory shortage more gracefully
|
|
||||||
* CallocT does the out-of-memory check
|
|
||||||
* Maybe some attemps could be made to try with smaller maps down to 64x64
|
|
||||||
* Maybe check for available memory before doing the calls, after all, we know how big
|
|
||||||
* the map is */
|
|
||||||
_m = CallocT<Tile>(_map_size);
|
_m = CallocT<Tile>(_map_size);
|
||||||
_me = CallocT<TileExtended>(_map_size);
|
_me = CallocT<TileExtended>(_map_size);
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,8 +75,6 @@ struct PersistentStorageArray : BaseStorageArray {
|
||||||
/* We do not have made a backup; lets do so */
|
/* We do not have made a backup; lets do so */
|
||||||
if (this->prev_storage != NULL) {
|
if (this->prev_storage != NULL) {
|
||||||
this->prev_storage = MallocT<TYPE>(SIZE);
|
this->prev_storage = MallocT<TYPE>(SIZE);
|
||||||
if (this->prev_storage == NULL) return;
|
|
||||||
|
|
||||||
memcpy(this->prev_storage, this->storage, sizeof(this->storage));
|
memcpy(this->prev_storage, this->storage, sizeof(this->storage));
|
||||||
|
|
||||||
/* We only need to register ourselves when we made the backup
|
/* We only need to register ourselves when we made the backup
|
||||||
|
|
|
@ -34,7 +34,6 @@ static bool InsSort_Push(Queue *q, void *item, int priority)
|
||||||
{
|
{
|
||||||
InsSortNode *newnode = MallocT<InsSortNode>(1);
|
InsSortNode *newnode = MallocT<InsSortNode>(1);
|
||||||
|
|
||||||
if (newnode == NULL) return false;
|
|
||||||
newnode->item = item;
|
newnode->item = item;
|
||||||
newnode->priority = priority;
|
newnode->priority = priority;
|
||||||
if (q->data.inssort.first == NULL ||
|
if (q->data.inssort.first == NULL ||
|
||||||
|
|
|
@ -127,10 +127,6 @@ static bool MakeBmpImage(const char *name, ScreenshotCallback *callb, void *user
|
||||||
|
|
||||||
/* now generate the bitmap bits */
|
/* now generate the bitmap bits */
|
||||||
void *buff = MallocT<uint8>(padw * maxlines * bpp); // by default generate 128 lines at a time.
|
void *buff = MallocT<uint8>(padw * maxlines * bpp); // by default generate 128 lines at a time.
|
||||||
if (buff == NULL) {
|
|
||||||
fclose(f);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
memset(buff, 0, padw * maxlines); // zero the buffer to have the padding bytes set to 0
|
memset(buff, 0, padw * maxlines); // zero the buffer to have the padding bytes set to 0
|
||||||
|
|
||||||
/* start at the bottom, since bitmaps are stored bottom up. */
|
/* start at the bottom, since bitmaps are stored bottom up. */
|
||||||
|
@ -255,11 +251,6 @@ static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *user
|
||||||
|
|
||||||
/* now generate the bitmap bits */
|
/* now generate the bitmap bits */
|
||||||
void *buff = MallocT<uint8>(w * maxlines * bpp); // by default generate 128 lines at a time.
|
void *buff = MallocT<uint8>(w * maxlines * bpp); // by default generate 128 lines at a time.
|
||||||
if (buff == NULL) {
|
|
||||||
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
||||||
fclose(f);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
memset(buff, 0, w * maxlines * bpp);
|
memset(buff, 0, w * maxlines * bpp);
|
||||||
|
|
||||||
y = 0;
|
y = 0;
|
||||||
|
@ -355,10 +346,6 @@ static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *user
|
||||||
|
|
||||||
/* now generate the bitmap bits */
|
/* now generate the bitmap bits */
|
||||||
uint8 *buff = MallocT<uint8>(w * maxlines); // by default generate 128 lines at a time.
|
uint8 *buff = MallocT<uint8>(w * maxlines); // by default generate 128 lines at a time.
|
||||||
if (buff == NULL) {
|
|
||||||
fclose(f);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
memset(buff, 0, w * maxlines); // zero the buffer to have the padding bytes set to 0
|
memset(buff, 0, w * maxlines); // zero the buffer to have the padding bytes set to 0
|
||||||
|
|
||||||
y = 0;
|
y = 0;
|
||||||
|
|
|
@ -111,7 +111,6 @@ static bool SetBankSource(MixerChannel *mc, const FileEntry *fe)
|
||||||
if (fe->file_size == 0) return false;
|
if (fe->file_size == 0) return false;
|
||||||
|
|
||||||
int8 *mem = MallocT<int8>(fe->file_size);
|
int8 *mem = MallocT<int8>(fe->file_size);
|
||||||
if (mem == NULL) return false;
|
|
||||||
|
|
||||||
FioSeekToFile(fe->file_slot, fe->file_offset);
|
FioSeekToFile(fe->file_slot, fe->file_offset);
|
||||||
FioReadBlock(mem, fe->file_size);
|
FioReadBlock(mem, fe->file_size);
|
||||||
|
|
|
@ -22,8 +22,7 @@ static void PrepareHeader(WAVEHDR *hdr)
|
||||||
hdr->dwBufferLength = _bufsize * 4;
|
hdr->dwBufferLength = _bufsize * 4;
|
||||||
hdr->dwFlags = 0;
|
hdr->dwFlags = 0;
|
||||||
hdr->lpData = MallocT<char>(_bufsize * 4);
|
hdr->lpData = MallocT<char>(_bufsize * 4);
|
||||||
if (hdr->lpData == NULL ||
|
if (waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
|
||||||
waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
|
|
||||||
usererror("waveOutPrepareHeader failed");
|
usererror("waveOutPrepareHeader failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -253,7 +253,6 @@ static inline bool AllocHeightMap()
|
||||||
_height_map.total_size = (_height_map.size_x + 1) * (_height_map.size_y + 1);
|
_height_map.total_size = (_height_map.size_x + 1) * (_height_map.size_y + 1);
|
||||||
_height_map.dim_x = _height_map.size_x + 1;
|
_height_map.dim_x = _height_map.size_x + 1;
|
||||||
_height_map.h = CallocT<height_t>(_height_map.total_size);
|
_height_map.h = CallocT<height_t>(_height_map.total_size);
|
||||||
if (_height_map.h == NULL) return false;
|
|
||||||
|
|
||||||
/* Iterate through height map initialize values */
|
/* Iterate through height map initialize values */
|
||||||
FOR_ALL_TILES_IN_HEIGHT(h) *h = _invalid_height;
|
FOR_ALL_TILES_IN_HEIGHT(h) *h = _invalid_height;
|
||||||
|
|
Loading…
Reference in New Issue