src/core/alloc_func.hpp
author Tero Marttila <terom@fixme.fi>
Fri, 19 Dec 2008 02:20:26 +0200
changeset 10441 d09735696a9e
parent 9576 55069e97e7d2
permissions -rw-r--r--
don't generate a heightmap for <64 tile maps
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
/**
8537
14be8ee6a268 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8376
diff changeset
     9
 * Functions to exit badly with an error message.
14be8ee6a268 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8376
diff changeset
    10
 * It has to be linked so the error messages are not
14be8ee6a268 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8376
diff changeset
    11
 * duplicated in each object file making the final
14be8ee6a268 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8376
diff changeset
    12
 * binary needlessly large.
14be8ee6a268 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8376
diff changeset
    13
 */
8984
68ef6bf69605 (svn r12778) -Codechange: add the NORETURN attribute to *allocError() functions
smatz
parents: 8925
diff changeset
    14
void NORETURN MallocError(size_t size);
68ef6bf69605 (svn r12778) -Codechange: add the NORETURN attribute to *allocError() functions
smatz
parents: 8925
diff changeset
    15
void NORETURN ReallocError(size_t size);
8537
14be8ee6a268 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8376
diff changeset
    16
14be8ee6a268 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8376
diff changeset
    17
/**
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    18
 * 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
    19
 * 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
    20
 * type.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    21
 * @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
    22
 * @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
    23
 * @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
    24
 * @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
    25
 * @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
    26
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9488
diff changeset
    27
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9488
diff changeset
    28
static FORCEINLINE T *MallocT(size_t num_elements)
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    29
{
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
	 * 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
    32
	 * 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
    33
	 * 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
    34
	 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    35
	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
    36
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    37
	T *t_ptr = (T*)malloc(num_elements * sizeof(T));
8537
14be8ee6a268 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8376
diff changeset
    38
	if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    39
	return t_ptr;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    40
}
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
/**
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    43
 * 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
    44
 * 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
    45
 * type.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    46
 * @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
    47
 * @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
    48
 * @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
    49
 * @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
    50
 * @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
    51
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9488
diff changeset
    52
template <typename T>
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9488
diff changeset
    53
static FORCEINLINE T *CallocT(size_t num_elements)
8130
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
	 * 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
    57
	 * 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
    58
	 * 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
    59
	 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    60
	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
    61
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    62
	T *t_ptr = (T*)calloc(num_elements, sizeof(T));
8537
14be8ee6a268 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8376
diff changeset
    63
	if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    64
	return t_ptr;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    65
}
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
/**
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    68
 * 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
    69
 * 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
    70
 * 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
    71
 * @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
    72
 * @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
    73
 * @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
    74
 * @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
    75
 * @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
    76
 * @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
    77
 */
9575
58d55b1a70c9 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 9488
diff changeset
    78
template <typename T>
9576
55069e97e7d2 (svn r13607) -Fix (r13606): some coding style issues got fixed but some got/stayed broken
skidd13
parents: 9575
diff changeset
    79
static FORCEINLINE T *ReallocT(T *t_ptr, size_t num_elements)
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    80
{
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    81
	/*
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    82
	 * 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
    83
	 * 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
    84
	 * 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
    85
	 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    86
	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
    87
		free(t_ptr);
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    88
		return NULL;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    89
	}
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    90
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    91
	t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
8537
14be8ee6a268 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8376
diff changeset
    92
	if (t_ptr == NULL) ReallocError(num_elements * sizeof(T));
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    93
	return t_ptr;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    94
}
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    95
9488
963e9e278125 (svn r13456) -Codechange: use AllocaM() macro instead of alloca() at most places
smatz
parents: 8984
diff changeset
    96
/** alloca() has to be called in the parent function, so define AllocaM() as a macro */
963e9e278125 (svn r13456) -Codechange: use AllocaM() macro instead of alloca() at most places
smatz
parents: 8984
diff changeset
    97
#define AllocaM(T, num_elements) ((T*)alloca((num_elements) * sizeof(T)))
963e9e278125 (svn r13456) -Codechange: use AllocaM() macro instead of alloca() at most places
smatz
parents: 8984
diff changeset
    98
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    99
#endif /* ALLOC_FUNC_HPP */