From 05169f53be6afe74387a36af1b5c8a6cc1c4d480 Mon Sep 17 00:00:00 2001 From: smatz Date: Mon, 11 Feb 2008 20:23:38 +0000 Subject: (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size --- src/core/alloc_func.cpp | 24 ++++++++++++++++++++++++ src/core/alloc_func.hpp | 15 ++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/core/alloc_func.cpp (limited to 'src') diff --git a/src/core/alloc_func.cpp b/src/core/alloc_func.cpp new file mode 100644 index 000000000..930482aa7 --- /dev/null +++ b/src/core/alloc_func.cpp @@ -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); +} 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 @@ -5,6 +5,15 @@ #ifndef ALLOC_FUNC_HPP #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 @@ -25,7 +34,7 @@ template 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 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 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; } -- cgit v1.2.3-70-g09d2