src/core/alloc_func.hpp
author rubidium
Wed, 09 Jan 2008 18:11:12 +0000
branchnoai
changeset 9723 eee46cb39750
child 9724 b39bc69bb2f2
permissions -rw-r--r--
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
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
/**
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
     9
 * Simplified allocation function that allocates the specified number of
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    10
 * 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
    11
 * type.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    12
 * @note throws an error when there is no memory anymore.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    13
 * @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
    14
 * @param T the type of the variable(s) to allocation.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    15
 * @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
    16
 * @return NULL when num_elements == 0, non-NULL otherwise.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    17
 */
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    18
template <typename T> FORCEINLINE T* MallocT(size_t num_elements)
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    19
{
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    20
	/*
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    21
	 * MorphOS cannot handle 0 elements allocations, or rather that always
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    22
	 * 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
    23
	 * to behave the same on all OSes.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    24
	 */
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    25
	if (num_elements == 0) return NULL;
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
	T *t_ptr = (T*)malloc(num_elements * sizeof(T));
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    28
	if (t_ptr == NULL) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T));
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    29
	return t_ptr;
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    30
}
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    31
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    32
/**
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    33
 * Simplified allocation function that allocates the specified number of
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    34
 * 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
    35
 * type.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    36
 * @note throws an error when there is no memory anymore.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    37
 * @note the memory contains all zero values.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    38
 * @param T the type of the variable(s) to allocation.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    39
 * @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
    40
 * @return NULL when num_elements == 0, non-NULL otherwise.
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
template <typename T> FORCEINLINE T* CallocT(size_t num_elements)
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    43
{
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    44
	/*
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    45
	 * MorphOS cannot handle 0 elements allocations, or rather that always
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    46
	 * 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
    47
	 * to behave the same on all OSes.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    48
	 */
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    49
	if (num_elements == 0) return NULL;
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
	T *t_ptr = (T*)calloc(num_elements, sizeof(T));
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    52
	if (t_ptr == NULL) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T));
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    53
	return t_ptr;
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    54
}
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    55
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    56
/**
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    57
 * Simplified reallocation function that allocates the specified number of
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    58
 * 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
    59
 * type. It extends/shrinks the memory allocation given in t_ptr.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    60
 * @note throws an error when there is no memory anymore.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    61
 * @note the memory contains all zero values.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    62
 * @param T the type of the variable(s) to allocation.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    63
 * @param t_ptr the previous allocation to extend/shrink.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    64
 * @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
    65
 * @return NULL when num_elements == 0, non-NULL otherwise.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    66
 */
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    67
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
    68
{
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    69
	/*
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    70
	 * MorphOS cannot handle 0 elements allocations, or rather that always
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    71
	 * 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
    72
	 * to behave the same on all OSes.
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    73
	 */
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    74
	if (num_elements == 0) {
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    75
		free(t_ptr);
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    76
		return NULL;
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
	t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    80
	if (t_ptr == NULL) error("Out of memory. Cannot reallocate %i bytes", num_elements * sizeof(T));
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    81
	return t_ptr;
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
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
diff changeset
    84
#endif /* ALLOC_FUNC_HPP */