(svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
authorsmatz
Mon, 11 Feb 2008 20:23:38 +0000
changeset 9033 7153b87990f8
parent 9032 c6e7eac2c650
child 9034 091b5e829efa
(svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
projects/openttd_vs80.vcproj
projects/openttd_vs90.vcproj
source.list
src/core/alloc_func.cpp
src/core/alloc_func.hpp
--- a/projects/openttd_vs80.vcproj	Mon Feb 11 19:10:33 2008 +0000
+++ b/projects/openttd_vs80.vcproj	Mon Feb 11 20:23:38 2008 +0000
@@ -456,6 +456,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\core\alloc_func.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\articulated_vehicles.cpp"
 				>
 			</File>
@@ -920,7 +924,7 @@
 				>
 			</File>
 			<File
-				RelativePath=".\..\src\core\enum_type.hpp"
+				RelativePath=".\..\src\code\enum_type.hpp"
 				>
 			</File>
 			<File
@@ -1140,7 +1144,7 @@
 				>
 			</File>
 			<File
-				RelativePath=".\..\src\core\overflowsafe_type.hpp"
+				RelativePath=".\..\src\overflowsafe_type.hpp"
 				>
 			</File>
 			<File
--- a/projects/openttd_vs90.vcproj	Mon Feb 11 19:10:33 2008 +0000
+++ b/projects/openttd_vs90.vcproj	Mon Feb 11 20:23:38 2008 +0000
@@ -453,6 +453,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\..\src\core\alloc_func.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\..\src\articulated_vehicles.cpp"
 				>
 			</File>
@@ -917,7 +921,7 @@
 				>
 			</File>
 			<File
-				RelativePath=".\..\src\core\enum_type.hpp"
+				RelativePath=".\..\src\code\enum_type.hpp"
 				>
 			</File>
 			<File
@@ -1137,7 +1141,7 @@
 				>
 			</File>
 			<File
-				RelativePath=".\..\src\core\overflowsafe_type.hpp"
+				RelativePath=".\..\src\overflowsafe_type.hpp"
 				>
 			</File>
 			<File
--- a/source.list	Mon Feb 11 19:10:33 2008 +0000
+++ b/source.list	Mon Feb 11 20:23:38 2008 +0000
@@ -1,5 +1,6 @@
 # Source Files
 airport.cpp
+core/alloc_func.cpp
 articulated_vehicles.cpp
 autoreplace_cmd.cpp
 aystar.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/core/alloc_func.cpp	Mon Feb 11 20:23:38 2008 +0000
@@ -0,0 +1,24 @@
+/* $Id$ */
+
+/** @file alloc_func.cpp functions to 'handle' memory allocation errors */
+
+#include "../stdafx.h"
+#include "alloc_func.hpp"
+
+/**
+ * Function to exit with an error message after malloc() or calloc() have failed
+ * @param size number of bytes we tried to allocate
+ */
+void MallocError(size_t size)
+{
+	error("Out of memory. Cannot allocate %i bytes", size);
+}
+
+/**
+ * Function to exit with an error message after realloc() have failed
+ * @param size number of bytes we tried to allocate
+ */
+void ReallocError(size_t size)
+{
+	error("Out of memory. Cannot reallocate %i bytes", size);
+}
--- a/src/core/alloc_func.hpp	Mon Feb 11 19:10:33 2008 +0000
+++ b/src/core/alloc_func.hpp	Mon Feb 11 20:23:38 2008 +0000
@@ -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 @@
 	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 @@
 	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 @@
 	}
 
 	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;
 }