src/core/alloc_type.hpp
author rubidium
Sun, 25 May 2008 19:17:03 +0000
changeset 9354 845e07db4549
parent 9111 48ce04029fe4
child 9575 58d55b1a70c9
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.
8925
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
     1
/* $Id$ */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
     2
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
     3
/** @file alloc_type.hpp Helper types related to the allocation of memory */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
     4
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
     5
#ifndef ALLOC_TYPE_HPP
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
     6
#define ALLOC_TYPE_HPP
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
     7
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
     8
#include "alloc_func.hpp"
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
     9
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    10
/**
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    11
 * A small 'wrapper' for allocations that can be done on most OSes on the
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    12
 * stack, but are just too large to fit in the stack on devices with a small
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    13
 * stack such as the NDS.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    14
 * So when it is possible a stack allocation is made, otherwise a heap
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    15
 * allocation is made and this is freed once the struct goes out of scope.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    16
 * @param T      the type to make the allocation for
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    17
 * @param length the amount of items to allocate
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    18
 */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    19
template <typename T, size_t length>
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    20
struct SmallStackSafeStackAlloc {
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    21
#if !defined(__NDS__)
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    22
	/** Storing the data on the stack */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    23
	T data[length];
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    24
#else
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    25
	/** Storing it on the heap */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    26
	T *data;
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    27
	/** The length (in elements) of data in this allocator. */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    28
	size_t len;
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    29
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    30
	/** Allocating the memory */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    31
	SmallStackSafeStackAlloc() : data(MallocT<T>(length)), len(length) {}
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    32
	/** And freeing when it goes out of scope */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    33
	~SmallStackSafeStackAlloc() { free(data); }
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    34
#endif
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    35
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    36
	/**
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    37
	 * Gets a pointer to the data stored in this wrapper.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    38
	 * @return the pointer.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    39
	 */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    40
	inline operator T* () { return data; }
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    41
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    42
	/**
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    43
	 * Gets a pointer to the data stored in this wrapper.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    44
	 * @return the pointer.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    45
	 */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    46
	inline T* operator -> () { return data; }
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    47
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    48
	/**
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    49
	 * Gets a pointer to the last data element stored in this wrapper.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    50
	 * @note needed because endof does not work properly for pointers.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    51
	 * @return the 'endof' pointer.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    52
	 */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    53
	inline T* EndOf() {
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    54
#if !defined(__NDS__)
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    55
		return endof(data);
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    56
#else
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    57
		return &data[len];
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    58
#endif
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    59
	}
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    60
};
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    61
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    62
/**
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    63
 * Base class that provides memory initialization on dynamically created objects.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    64
 * All allocated memory will be zeroed.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    65
 */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    66
class ZeroedMemoryAllocator
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    67
{
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    68
public:
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    69
	ZeroedMemoryAllocator() {}
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    70
	virtual ~ZeroedMemoryAllocator() {}
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    71
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    72
	/**
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    73
	 * Memory allocator for a single class instance.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    74
	 * @param size the amount of bytes to allocate.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    75
	 * @return the given amounts of bytes zeroed.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    76
	 */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    77
	void *operator new(size_t size) { return CallocT<byte>(size); }
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    78
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    79
	/**
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    80
	 * Memory allocator for an array of class instances.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    81
	 * @param size the amount of bytes to allocate.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    82
	 * @return the given amounts of bytes zeroed.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    83
	 */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    84
	void *operator new[](size_t size) { return CallocT<byte>(size); }
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    85
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    86
	/**
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    87
	 * Memory release for a single class instance.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    88
	 * @param ptr  the memory to free.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    89
	 * @param size the amount of allocated memory (unused).
8957
ef3f499ff423 (svn r12749) -Codechange: store the viewport information in the windows that have a viewport instead of one global array with a viewport for each window, even when they do not use the viewport.
rubidium
parents: 8925
diff changeset
    90
	 *
ef3f499ff423 (svn r12749) -Codechange: store the viewport information in the windows that have a viewport instead of one global array with a viewport for each window, even when they do not use the viewport.
rubidium
parents: 8925
diff changeset
    91
	 * @warning The value of the \a size parameter can only be trusted for
ef3f499ff423 (svn r12749) -Codechange: store the viewport information in the windows that have a viewport instead of one global array with a viewport for each window, even when they do not use the viewport.
rubidium
parents: 8925
diff changeset
    92
	 *          classes that have their own (virtual) destructor method.
8925
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    93
	 */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    94
	void operator delete(void *ptr, size_t size) { free(ptr); }
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    95
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    96
	/**
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    97
	 * Memory release for an array of class instances.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    98
	 * @param ptr  the memory to free.
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
    99
	 * @param size the amount of allocated memory (unused).
8957
ef3f499ff423 (svn r12749) -Codechange: store the viewport information in the windows that have a viewport instead of one global array with a viewport for each window, even when they do not use the viewport.
rubidium
parents: 8925
diff changeset
   100
	 *
ef3f499ff423 (svn r12749) -Codechange: store the viewport information in the windows that have a viewport instead of one global array with a viewport for each window, even when they do not use the viewport.
rubidium
parents: 8925
diff changeset
   101
	 * @warning The value of the \a size parameter can only be trusted for
ef3f499ff423 (svn r12749) -Codechange: store the viewport information in the windows that have a viewport instead of one global array with a viewport for each window, even when they do not use the viewport.
rubidium
parents: 8925
diff changeset
   102
	 *          classes that have their own (virtual) destructor method.
8925
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
   103
	 */
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
   104
	void operator delete[](void *ptr, size_t size) { free(ptr); }
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
   105
};
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
   106
e0d37ce1eba8 (svn r12695) -Codechange: only allocate window structs when needed. Based on a patch by Alberth.
rubidium
parents:
diff changeset
   107
#endif /* ALLOC_TYPE_HPP */