src/core/alloc_func.hpp
author skidd13
Thu, 24 Jan 2008 18:35:35 +0000
changeset 8408 cab5989c2ce2
parent 8376 aab0b14b96e3
child 8537 14be8ee6a268
permissions -rw-r--r--
(svn r11978) -Codechange: Replace some global variables with window owned ones
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
     1
/* $Id$ */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
     2
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
     3
/** @file alloc_func.hpp Functions related to the allocation of memory */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
     4
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
     5
#ifndef ALLOC_FUNC_HPP
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
     6
#define ALLOC_FUNC_HPP
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
     7
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
     8
/**
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
     9
 * Simplified allocation function that allocates the specified number of
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    10
 * elements of the given type. It also explicitly casts it to the requested
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    11
 * type.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    12
 * @note throws an error when there is no memory anymore.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    13
 * @note the memory contains garbage data (i.e. possibly non-zero values).
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    14
 * @param T the type of the variable(s) to allocation.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    15
 * @param num_elements the number of elements to allocate of the given type.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    16
 * @return NULL when num_elements == 0, non-NULL otherwise.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    17
 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    18
template <typename T> FORCEINLINE T* MallocT(size_t num_elements)
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    19
{
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    20
	/*
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    21
	 * MorphOS cannot handle 0 elements allocations, or rather that always
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    22
	 * returns NULL. So we do that for *all* allocations, thus causing it
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    23
	 * to behave the same on all OSes.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    24
	 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    25
	if (num_elements == 0) return NULL;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    26
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    27
	T *t_ptr = (T*)malloc(num_elements * sizeof(T));
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    28
	if (t_ptr == NULL) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T));
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    29
	return t_ptr;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    30
}
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    31
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    32
/**
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    33
 * Simplified allocation function that allocates the specified number of
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    34
 * elements of the given type. It also explicitly casts it to the requested
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    35
 * type.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    36
 * @note throws an error when there is no memory anymore.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    37
 * @note the memory contains all zero values.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    38
 * @param T the type of the variable(s) to allocation.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    39
 * @param num_elements the number of elements to allocate of the given type.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    40
 * @return NULL when num_elements == 0, non-NULL otherwise.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    41
 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    42
template <typename T> FORCEINLINE T* CallocT(size_t num_elements)
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    43
{
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    44
	/*
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    45
	 * MorphOS cannot handle 0 elements allocations, or rather that always
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    46
	 * returns NULL. So we do that for *all* allocations, thus causing it
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    47
	 * to behave the same on all OSes.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    48
	 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    49
	if (num_elements == 0) return NULL;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    50
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    51
	T *t_ptr = (T*)calloc(num_elements, sizeof(T));
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    52
	if (t_ptr == NULL) error("Out of memory. Cannot allocate %i bytes", num_elements * sizeof(T));
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    53
	return t_ptr;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    54
}
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    55
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    56
/**
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    57
 * Simplified reallocation function that allocates the specified number of
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    58
 * elements of the given type. It also explicitly casts it to the requested
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    59
 * type. It extends/shrinks the memory allocation given in t_ptr.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    60
 * @note throws an error when there is no memory anymore.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    61
 * @note the memory contains all zero values.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    62
 * @param T the type of the variable(s) to allocation.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    63
 * @param t_ptr the previous allocation to extend/shrink.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    64
 * @param num_elements the number of elements to allocate of the given type.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    65
 * @return NULL when num_elements == 0, non-NULL otherwise.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    66
 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    67
template <typename T> FORCEINLINE T* ReallocT(T *t_ptr, size_t num_elements)
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    68
{
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    69
	/*
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    70
	 * MorphOS cannot handle 0 elements allocations, or rather that always
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    71
	 * returns NULL. So we do that for *all* allocations, thus causing it
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    72
	 * to behave the same on all OSes.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    73
	 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    74
	if (num_elements == 0) {
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    75
		free(t_ptr);
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    76
		return NULL;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    77
	}
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    78
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    79
	t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    80
	if (t_ptr == NULL) error("Out of memory. Cannot reallocate %i bytes", num_elements * sizeof(T));
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    81
	return t_ptr;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    82
}
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    83
8376
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    84
/**
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    85
 * A small 'wrapper' for allocations that can be done on most OSes on the
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    86
 * stack, but are just too large to fit in the stack on devices with a small
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    87
 * stack such as the NDS.
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    88
 * So when it is possible a stack allocation is made, otherwise a heap
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    89
 * allocation is made and this is freed once the struct goes out of scope.
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    90
 * @param T      the type to make the allocation for
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    91
 * @param length the amount of items to allocate
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    92
 */
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    93
template <typename T, size_t length>
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    94
struct SmallStackSafeStackAlloc {
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    95
#if !defined(__NDS__)
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    96
	/** Storing the data on the stack */
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    97
	T data[length];
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    98
#else
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
    99
	/** Storing it on the heap */
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
   100
	T *data;
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
   101
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
   102
	/** Allocating the memory */
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
   103
	SmallStackSafeStackAlloc() : data(MallocT<T>(length)) {}
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
   104
	/** And freeing when it goes out of scope */
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
   105
	~SmallStackSafeStackAlloc() { free(data); }
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
   106
#endif
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
   107
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
   108
	/**
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
   109
	 * Gets a pointer to the data stored in this wrapper.
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
   110
	 * @return the pointer.
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
   111
	 */
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
   112
	operator T* () { return data; }
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
   113
};
aab0b14b96e3 (svn r11943) -Codechange: add and use a simple structure to support small stacks by allocating it on the heap or pushing a few kB of data onto the stack when there is a large stack.
rubidium
parents: 8130
diff changeset
   114
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
   115
#endif /* ALLOC_FUNC_HPP */