src/core/alloc_func.hpp
changeset 8537 14be8ee6a268
parent 8376 aab0b14b96e3
child 8798 2c6062ce7dba
equal deleted inserted replaced
8536:33db3feea2af 8537:14be8ee6a268
     2 
     2 
     3 /** @file alloc_func.hpp Functions related to the allocation of memory */
     3 /** @file alloc_func.hpp Functions related to the allocation of memory */
     4 
     4 
     5 #ifndef ALLOC_FUNC_HPP
     5 #ifndef ALLOC_FUNC_HPP
     6 #define ALLOC_FUNC_HPP
     6 #define ALLOC_FUNC_HPP
       
     7 
       
     8 /**
       
     9  * Functions to exit badly with an error message.
       
    10  * It has to be linked so the error messages are not
       
    11  * duplicated in each object file making the final
       
    12  * binary needlessly large.
       
    13  */
       
    14 void MallocError(size_t size);
       
    15 void ReallocError(size_t size);
     7 
    16 
     8 /**
    17 /**
     9  * Simplified allocation function that allocates the specified number of
    18  * Simplified allocation function that allocates the specified number of
    10  * elements of the given type. It also explicitly casts it to the requested
    19  * elements of the given type. It also explicitly casts it to the requested
    11  * type.
    20  * type.
    23 	 * to behave the same on all OSes.
    32 	 * to behave the same on all OSes.
    24 	 */
    33 	 */
    25 	if (num_elements == 0) return NULL;
    34 	if (num_elements == 0) return NULL;
    26 
    35 
    27 	T *t_ptr = (T*)malloc(num_elements * sizeof(T));
    36 	T *t_ptr = (T*)malloc(num_elements * sizeof(T));
    28 	if (t_ptr == NULL) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T));
    37 	if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
    29 	return t_ptr;
    38 	return t_ptr;
    30 }
    39 }
    31 
    40 
    32 /**
    41 /**
    33  * Simplified allocation function that allocates the specified number of
    42  * Simplified allocation function that allocates the specified number of
    47 	 * to behave the same on all OSes.
    56 	 * to behave the same on all OSes.
    48 	 */
    57 	 */
    49 	if (num_elements == 0) return NULL;
    58 	if (num_elements == 0) return NULL;
    50 
    59 
    51 	T *t_ptr = (T*)calloc(num_elements, sizeof(T));
    60 	T *t_ptr = (T*)calloc(num_elements, sizeof(T));
    52 	if (t_ptr == NULL) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T));
    61 	if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
    53 	return t_ptr;
    62 	return t_ptr;
    54 }
    63 }
    55 
    64 
    56 /**
    65 /**
    57  * Simplified reallocation function that allocates the specified number of
    66  * Simplified reallocation function that allocates the specified number of
    75 		free(t_ptr);
    84 		free(t_ptr);
    76 		return NULL;
    85 		return NULL;
    77 	}
    86 	}
    78 
    87 
    79 	t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
    88 	t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
    80 	if (t_ptr == NULL) error("Out of memory. Cannot reallocate %i bytes", num_elements * sizeof(T));
    89 	if (t_ptr == NULL) ReallocError(num_elements * sizeof(T));
    81 	return t_ptr;
    90 	return t_ptr;
    82 }
    91 }
    83 
    92 
    84 /**
    93 /**
    85  * A small 'wrapper' for allocations that can be done on most OSes on the
    94  * A small 'wrapper' for allocations that can be done on most OSes on the