src/core/alloc_func.hpp
author skidd13
Sun, 22 Jun 2008 15:21:51 +0000
changeset 11049 f8bbc9635251
parent 10905 293ba816b703
child 11050 091271fcfbb9
permissions -rw-r--r--
(svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
     1
/* $Id$ */
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
     2
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
     3
/** @file alloc_func.hpp Functions related to the allocation of memory */
6505
abcb0580d976 (svn r8950) -Cleanup: doxygen changes. Mostly @files missing tags and a few comments style.
belugas
parents: 6482
diff changeset
     4
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
     5
#ifndef ALLOC_FUNC_HPP
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
     6
#define ALLOC_FUNC_HPP
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
     7
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
     8
/**
9033
7153b87990f8 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8872
diff changeset
     9
 * Functions to exit badly with an error message.
7153b87990f8 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8872
diff changeset
    10
 * It has to be linked so the error messages are not
7153b87990f8 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8872
diff changeset
    11
 * duplicated in each object file making the final
7153b87990f8 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8872
diff changeset
    12
 * binary needlessly large.
7153b87990f8 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8872
diff changeset
    13
 */
10246
707c09e2e42b (svn r12778) -Codechange: add the NORETURN attribute to *allocError() functions
smatz
parents: 10164
diff changeset
    14
void NORETURN MallocError(size_t size);
707c09e2e42b (svn r12778) -Codechange: add the NORETURN attribute to *allocError() functions
smatz
parents: 10164
diff changeset
    15
void NORETURN ReallocError(size_t size);
9033
7153b87990f8 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8872
diff changeset
    16
7153b87990f8 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8872
diff changeset
    17
/**
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    18
 * Simplified allocation function that allocates the specified number of
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    19
 * elements of the given type. It also explicitly casts it to the requested
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    20
 * type.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    21
 * @note throws an error when there is no memory anymore.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    22
 * @note the memory contains garbage data (i.e. possibly non-zero values).
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    23
 * @param T the type of the variable(s) to allocation.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    24
 * @param num_elements the number of elements to allocate of the given type.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    25
 * @return NULL when num_elements == 0, non-NULL otherwise.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    26
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10905
diff changeset
    27
template <typename T>
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10905
diff changeset
    28
static FORCEINLINE T *MallocT(size_t num_elements)
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    29
{
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    30
	/*
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    31
	 * MorphOS cannot handle 0 elements allocations, or rather that always
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    32
	 * returns NULL. So we do that for *all* allocations, thus causing it
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    33
	 * to behave the same on all OSes.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    34
	 */
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    35
	if (num_elements == 0) return NULL;
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    36
5860
7fdc9b423ba1 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5838
diff changeset
    37
	T *t_ptr = (T*)malloc(num_elements * sizeof(T));
9033
7153b87990f8 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8872
diff changeset
    38
	if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
5860
7fdc9b423ba1 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5838
diff changeset
    39
	return t_ptr;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    40
}
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    41
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    42
/**
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    43
 * Simplified allocation function that allocates the specified number of
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    44
 * elements of the given type. It also explicitly casts it to the requested
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    45
 * type.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    46
 * @note throws an error when there is no memory anymore.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    47
 * @note the memory contains all zero values.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    48
 * @param T the type of the variable(s) to allocation.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    49
 * @param num_elements the number of elements to allocate of the given type.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    50
 * @return NULL when num_elements == 0, non-NULL otherwise.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    51
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10905
diff changeset
    52
template <typename T>
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10905
diff changeset
    53
static FORCEINLINE T *CallocT(size_t num_elements)
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    54
{
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    55
	/*
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    56
	 * MorphOS cannot handle 0 elements allocations, or rather that always
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    57
	 * returns NULL. So we do that for *all* allocations, thus causing it
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    58
	 * to behave the same on all OSes.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    59
	 */
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    60
	if (num_elements == 0) return NULL;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    61
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    62
	T *t_ptr = (T*)calloc(num_elements, sizeof(T));
9033
7153b87990f8 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8872
diff changeset
    63
	if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    64
	return t_ptr;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    65
}
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
diff changeset
    66
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    67
/**
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    68
 * Simplified reallocation function that allocates the specified number of
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    69
 * elements of the given type. It also explicitly casts it to the requested
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    70
 * type. It extends/shrinks the memory allocation given in t_ptr.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    71
 * @note throws an error when there is no memory anymore.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    72
 * @note the memory contains all zero values.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    73
 * @param T the type of the variable(s) to allocation.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    74
 * @param t_ptr the previous allocation to extend/shrink.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    75
 * @param num_elements the number of elements to allocate of the given type.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    76
 * @return NULL when num_elements == 0, non-NULL otherwise.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    77
 */
11049
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10905
diff changeset
    78
template <typename T>
f8bbc9635251 (svn r13606) -Codechange: use "static FORCEINLINE" where possible as default for core functions (big functions use just inline instead)
skidd13
parents: 10905
diff changeset
    79
FORCEINLINE T *ReallocT(T *t_ptr, size_t num_elements)
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    80
{
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    81
	/*
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    82
	 * MorphOS cannot handle 0 elements allocations, or rather that always
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    83
	 * returns NULL. So we do that for *all* allocations, thus causing it
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    84
	 * to behave the same on all OSes.
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    85
	 */
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    86
	if (num_elements == 0) {
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    87
		free(t_ptr);
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    88
		return NULL;
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    89
	}
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    90
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    91
	t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
9033
7153b87990f8 (svn r12115) -Codechange: move malloc/realloc error messages to separate file to spare 4-8kB of binary size
smatz
parents: 8872
diff changeset
    92
	if (t_ptr == NULL) ReallocError(num_elements * sizeof(T));
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    93
	return t_ptr;
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    94
}
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    95
10905
293ba816b703 (svn r13456) -Codechange: use AllocaM() macro instead of alloca() at most places
smatz
parents: 10246
diff changeset
    96
/** alloca() has to be called in the parent function, so define AllocaM() as a macro */
293ba816b703 (svn r13456) -Codechange: use AllocaM() macro instead of alloca() at most places
smatz
parents: 10246
diff changeset
    97
#define AllocaM(T, num_elements) ((T*)alloca((num_elements) * sizeof(T)))
293ba816b703 (svn r13456) -Codechange: use AllocaM() macro instead of alloca() at most places
smatz
parents: 10246
diff changeset
    98
8626
440dfcd14c4a (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8608
diff changeset
    99
#endif /* ALLOC_FUNC_HPP */