src/core/alloc_func.hpp
author rubidium
Sun, 25 May 2008 19:17:03 +0000
changeset 9354 845e07db4549
parent 8984 68ef6bf69605
child 9488 963e9e278125
permissions -rw-r--r--
(svn r13251) -Codechange: rename _patches to _settings as that is more logic.
-Codechange: move all Settings into substructs of _settings in a way that they are logically grouped.
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
 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    27
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
    28
{
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
	 * 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
    31
	 * 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
    32
	 * 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
    33
	 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    34
	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
    35
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    36
	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
    37
	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
    38
	return t_ptr;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    39
}
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
 * 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
    43
 * 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
    44
 * type.
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    45
 * @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
    46
 * @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
    47
 * @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
    48
 * @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
    49
 * @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
    50
 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    51
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
    52
{
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    53
	/*
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    54
	 * 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
    55
	 * 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
    56
	 * 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
    57
	 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    58
	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
    59
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    60
	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
    61
	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
    62
	return t_ptr;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    63
}
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    64
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
 * 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
    67
 * 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
    68
 * 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
    69
 * @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
    70
 * @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
    71
 * @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
    72
 * @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
    73
 * @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
    74
 * @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
    75
 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    76
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
    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
	 * 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
    80
	 * 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
    81
	 * 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
    82
	 */
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    83
	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
    84
		free(t_ptr);
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    85
		return NULL;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    86
	}
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    87
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    88
	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
    89
	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
    90
	return t_ptr;
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    91
}
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    92
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents:
diff changeset
    93
#endif /* ALLOC_FUNC_HPP */