Let HandleWindowDragging return a boolean status
[openttd/fttd.git] / src / core / alloc_func.cpp
blob01fa06105144c434c04ce6c757c673378cbcf9aa
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file alloc_func.cpp Functions to 'handle' memory allocation errors */
12 #include "../stdafx.h"
14 #include <stdlib.h>
16 #include "alloc_func.hpp"
18 /** Trigger an abort on out of memory. */
19 void NORETURN out_of_memory (void)
21 error ("Out of memory.");
24 /** Allocate uninitialised dynamic memory, and error out on failure. */
25 char *xmalloc (size_t size)
27 if (size == 0) return NULL;
28 void *p = malloc (size);
29 if (p == NULL) out_of_memory();
30 return (char*) p;
33 /** Allocate uninitialised dynamic memory, and error out on failure. */
34 void *xmalloc (size_t n, size_t size)
36 if (n == 0 || size == 0) return NULL;
37 size_t total = n * size;
38 if (total / size != n) out_of_memory();
39 return xmalloc (total);
42 /** Allocate zeroed dynamic memory, and error out on failure. */
43 void *xcalloc (size_t n, size_t size)
45 if (n == 0 || size == 0) return NULL;
46 void *p = calloc (n, size);
47 if (p == NULL) out_of_memory();
48 return p;
51 /** Reallocate dynamic memory, and error out on failure. */
52 char *xrealloc (void *p, size_t size)
54 if (size == 0) {
55 free (p);
56 return NULL;
59 void *q = realloc (p, size);
60 if (q == NULL) out_of_memory();
61 return (char*) q;
64 /** Reallocate dynamic memory, and error out on failure. */
65 void *xrealloc (void *p, size_t n, size_t size)
67 if (n == 0 || size == 0) {
68 free (p);
69 return NULL;
72 size_t total = n * size;
73 if (total / size != n) out_of_memory();
74 return xrealloc (p, total);