summaryrefslogtreecommitdiff
path: root/src/core/alloc_func.hpp
diff options
context:
space:
mode:
authorsmatz <smatz@openttd.org>2008-02-11 20:23:38 +0000
committersmatz <smatz@openttd.org>2008-02-11 20:23:38 +0000
commitf19eca0905389586d4a171bba3e044cf3357eef6 (patch)
tree385716e6b5f4563484639b6dd1694fd8fed1a1e3 /src/core/alloc_func.hpp
parentf175e462081e9b0c6e75fccdbe9cb8469bacd55e (diff)
downloadopenttd-f19eca0905389586d4a171bba3e044cf3357eef6.tar.xz
(svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
Diffstat (limited to 'src/core/alloc_func.hpp')
-rw-r--r--src/core/alloc_func.hpp15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/core/alloc_func.hpp b/src/core/alloc_func.hpp
index 97e598598..a2cae3e3d 100644
--- a/src/core/alloc_func.hpp
+++ b/src/core/alloc_func.hpp
@@ -6,6 +6,15 @@
#define ALLOC_FUNC_HPP
/**
+ * Functions to exit badly with an error message.
+ * It has to be linked so the error messages are not
+ * duplicated in each object file making the final
+ * binary needlessly large.
+ */
+void MallocError(size_t size);
+void ReallocError(size_t size);
+
+/**
* Simplified allocation function that allocates the specified number of
* elements of the given type. It also explicitly casts it to the requested
* type.
@@ -25,7 +34,7 @@ template <typename T> FORCEINLINE T* MallocT(size_t num_elements)
if (num_elements == 0) return NULL;
T *t_ptr = (T*)malloc(num_elements * sizeof(T));
- if (t_ptr == NULL) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T));
+ if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
return t_ptr;
}
@@ -49,7 +58,7 @@ template <typename T> FORCEINLINE T* CallocT(size_t num_elements)
if (num_elements == 0) return NULL;
T *t_ptr = (T*)calloc(num_elements, sizeof(T));
- if (t_ptr == NULL) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T));
+ if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
return t_ptr;
}
@@ -77,7 +86,7 @@ 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) error("Out of memory. Cannot reallocate %i bytes", num_elements * sizeof(T));
+ if (t_ptr == NULL) ReallocError(num_elements * sizeof(T));
return t_ptr;
}