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