Files @ r10737:2346af7e04e8
Branch filter:

Location: cpp/openttd-patchpack/source/src/core/alloc_type.hpp

translators
(svn r15070) -Update: WebTranslator2 update to 2009-01-13 18:42:22
brazilian_portuguese - 16 fixed by tucalipe (16)
catalan - 8 fixed by arnaullv (8)
croatian - 24 fixed by tifached (24)
czech - 8 fixed by Hadez (8)
dutch - 2 fixed by Excel20 (2)
finnish - 7 fixed, 1 changed by UltimateSephiroth (8)
hungarian - 7 fixed, 2 changed by IPG (2), alyr (7)
indonesian - 23 fixed, 2 changed by fanioz (25)
italian - 7 fixed, 1 changed by lorenzodv (8)
japanese - 59 fixed by ickoonite (59)
polish - 3 fixed by xaxa (3)
romanian - 23 fixed, 1 changed by kkmic (24)
slovak - 59 fixed by James (59)
spanish - 58 fixed by Dominus (30), eusebio (28)
turkish - 7 fixed, 1 changed by Emin (8)
/* $Id$ */

/** @file alloc_type.hpp Helper types related to the allocation of memory */

#ifndef ALLOC_TYPE_HPP
#define ALLOC_TYPE_HPP

#include "alloc_func.hpp"

/**
 * A small 'wrapper' for allocations that can be done on most OSes on the
 * stack, but are just too large to fit in the stack on devices with a small
 * stack such as the NDS.
 * So when it is possible a stack allocation is made, otherwise a heap
 * allocation is made and this is freed once the struct goes out of scope.
 * @param T      the type to make the allocation for
 * @param length the amount of items to allocate
 */
template <typename T, size_t length>
struct SmallStackSafeStackAlloc {
#if !defined(__NDS__)
	/** Storing the data on the stack */
	T data[length];
#else
	/** Storing it on the heap */
	T *data;
	/** The length (in elements) of data in this allocator. */
	size_t len;

	/** Allocating the memory */
	SmallStackSafeStackAlloc() : data(MallocT<T>(length)), len(length) {}

	/** And freeing when it goes out of scope */
	~SmallStackSafeStackAlloc()
	{
		free(data);
	}
#endif

	/**
	 * Gets a pointer to the data stored in this wrapper.
	 * @return the pointer.
	 */
	FORCEINLINE operator T *()
	{
		return data;
	}

	/**
	 * Gets a pointer to the data stored in this wrapper.
	 * @return the pointer.
	 */
	FORCEINLINE T *operator -> ()
	{
		return data;
	}

	/**
	 * Gets a pointer to the last data element stored in this wrapper.
	 * @note needed because endof does not work properly for pointers.
	 * @return the 'endof' pointer.
	 */
	FORCEINLINE T *EndOf()
	{
#if !defined(__NDS__)
		return endof(data);
#else
		return &data[len];
#endif
	}
};

/**
 * Base class that provides memory initialization on dynamically created objects.
 * All allocated memory will be zeroed.
 */
class ZeroedMemoryAllocator
{
public:
	ZeroedMemoryAllocator() {}
	virtual ~ZeroedMemoryAllocator() {}

	/**
	 * Memory allocator for a single class instance.
	 * @param size the amount of bytes to allocate.
	 * @return the given amounts of bytes zeroed.
	 */
	FORCEINLINE void *operator new(size_t size) { return CallocT<byte>(size); }

	/**
	 * Memory allocator for an array of class instances.
	 * @param size the amount of bytes to allocate.
	 * @return the given amounts of bytes zeroed.
	 */
	FORCEINLINE void *operator new[](size_t size) { return CallocT<byte>(size); }

	/**
	 * Memory release for a single class instance.
	 * @param ptr  the memory to free.
	 * @param size the amount of allocated memory (unused).
	 *
	 * @warning The value of the \a size parameter can only be trusted for
	 *          classes that have their own (virtual) destructor method.
	 */
	FORCEINLINE void operator delete(void *ptr, size_t size) { free(ptr); }

	/**
	 * Memory release for an array of class instances.
	 * @param ptr  the memory to free.
	 * @param size the amount of allocated memory (unused).
	 *
	 * @warning The value of the \a size parameter can only be trusted for
	 *          classes that have their own (virtual) destructor method.
	 */
	FORCEINLINE void operator delete[](void *ptr, size_t size) { free(ptr); }
};

#endif /* ALLOC_TYPE_HPP */