src/core/alloc_func.hpp
author richk
Tue, 17 Jun 2008 13:22:13 +0000
branchNewGRF_ports
changeset 10994 cd9968b6f96b
parent 10274 b3c58f3df92b
permissions -rw-r--r--
(svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
     1
/* $Id$ */
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
     2
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
     3
/** @file alloc_func.hpp Functions related to the allocation of memory */
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
     4
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
     5
#ifndef ALLOC_FUNC_HPP
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
     6
#define ALLOC_FUNC_HPP
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
     7
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
     8
/**
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6872
diff changeset
     9
 * Functions to exit badly with an error message.
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6872
diff changeset
    10
 * It has to be linked so the error messages are not
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6872
diff changeset
    11
 * duplicated in each object file making the final
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6872
diff changeset
    12
 * binary needlessly large.
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6872
diff changeset
    13
 */
10274
b3c58f3df92b (svn r12806) [NewGRF_ports] -Sync: with trunk r12773:12805.
richk
parents: 10210
diff changeset
    14
void NORETURN MallocError(size_t size);
b3c58f3df92b (svn r12806) [NewGRF_ports] -Sync: with trunk r12773:12805.
richk
parents: 10210
diff changeset
    15
void NORETURN ReallocError(size_t size);
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6872
diff changeset
    16
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6872
diff changeset
    17
/**
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    18
 * Simplified allocation function that allocates the specified number of
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    19
 * elements of the given type. It also explicitly casts it to the requested
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    20
 * type.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    21
 * @note throws an error when there is no memory anymore.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    22
 * @note the memory contains garbage data (i.e. possibly non-zero values).
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    23
 * @param T the type of the variable(s) to allocation.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    24
 * @param num_elements the number of elements to allocate of the given type.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    25
 * @return NULL when num_elements == 0, non-NULL otherwise.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    26
 */
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    27
template <typename T> FORCEINLINE T* MallocT(size_t num_elements)
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    28
{
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    29
	/*
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    30
	 * MorphOS cannot handle 0 elements allocations, or rather that always
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    31
	 * returns NULL. So we do that for *all* allocations, thus causing it
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    32
	 * to behave the same on all OSes.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    33
	 */
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    34
	if (num_elements == 0) return NULL;
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    35
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    36
	T *t_ptr = (T*)malloc(num_elements * sizeof(T));
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6872
diff changeset
    37
	if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    38
	return t_ptr;
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    39
}
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    40
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    41
/**
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    42
 * Simplified allocation function that allocates the specified number of
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    43
 * elements of the given type. It also explicitly casts it to the requested
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    44
 * type.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    45
 * @note throws an error when there is no memory anymore.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    46
 * @note the memory contains all zero values.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    47
 * @param T the type of the variable(s) to allocation.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    48
 * @param num_elements the number of elements to allocate of the given type.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    49
 * @return NULL when num_elements == 0, non-NULL otherwise.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    50
 */
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    51
template <typename T> FORCEINLINE T* CallocT(size_t num_elements)
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    52
{
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    53
	/*
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    54
	 * MorphOS cannot handle 0 elements allocations, or rather that always
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    55
	 * returns NULL. So we do that for *all* allocations, thus causing it
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    56
	 * to behave the same on all OSes.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    57
	 */
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    58
	if (num_elements == 0) return NULL;
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    59
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    60
	T *t_ptr = (T*)calloc(num_elements, sizeof(T));
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6872
diff changeset
    61
	if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    62
	return t_ptr;
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    63
}
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    64
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    65
/**
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    66
 * Simplified reallocation function that allocates the specified number of
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    67
 * elements of the given type. It also explicitly casts it to the requested
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    68
 * type. It extends/shrinks the memory allocation given in t_ptr.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    69
 * @note throws an error when there is no memory anymore.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    70
 * @note the memory contains all zero values.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    71
 * @param T the type of the variable(s) to allocation.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    72
 * @param t_ptr the previous allocation to extend/shrink.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    73
 * @param num_elements the number of elements to allocate of the given type.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    74
 * @return NULL when num_elements == 0, non-NULL otherwise.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    75
 */
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    76
template <typename T> FORCEINLINE T* ReallocT(T *t_ptr, size_t num_elements)
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    77
{
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    78
	/*
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    79
	 * MorphOS cannot handle 0 elements allocations, or rather that always
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    80
	 * returns NULL. So we do that for *all* allocations, thus causing it
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    81
	 * to behave the same on all OSes.
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    82
	 */
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    83
	if (num_elements == 0) {
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    84
		free(t_ptr);
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    85
		return NULL;
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    86
	}
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    87
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    88
	t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6872
diff changeset
    89
	if (t_ptr == NULL) ReallocError(num_elements * sizeof(T));
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    90
	return t_ptr;
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    91
}
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    92
10994
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10274
diff changeset
    93
/** alloca() has to be called in the parent function, so define AllocaM() as a macro */
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10274
diff changeset
    94
#define AllocaM(T, num_elements) ((T*)alloca((num_elements) * sizeof(T)))
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10274
diff changeset
    95
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
diff changeset
    96
#endif /* ALLOC_FUNC_HPP */