Changeset - r17291:768e62fab51c
[Not reviewed]
master
0 3 0
smatz - 14 years ago 2011-02-09 18:55:51
smatz@openttd.org
(svn r22041) -Codechange: add a check that we called PoolItem::CanAllocateItem() before actually allocating it
3 files changed with 17 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/core/pool_func.hpp
Show inline comments
 
@@ -120,12 +120,16 @@ DEFINE_POOL_METHOD(inline void *)::Alloc
 
 * @note error() on failure! (no free item)
 
 */
 
DEFINE_POOL_METHOD(void *)::GetNew(size_t size)
 
{
 
	size_t index = this->FindFirstFree();
 

	
 
#ifdef OTTD_ASSERT
 
	assert(this->checked != 0);
 
	this->checked--;
 
#endif /* OTTD_ASSERT */
 
	if (index == NO_FREE_ITEM) {
 
		error("%s: no more free items", this->name);
 
	}
 

	
 
	this->first_free = index + 1;
 
	return this->AllocateItem(size, index);
src/core/pool_type.hpp
Show inline comments
 
@@ -29,13 +29,15 @@ struct Pool {
 
	const char * const name; ///< Name of this pool
 

	
 
	size_t size;         ///< Current allocated size
 
	size_t first_free;   ///< No item with index lower than this is free (doesn't say anything about this one!)
 
	size_t first_unused; ///< This and all higher indexes are free (doesn't say anything about first_unused-1 !)
 
	size_t items;        ///< Number of used indexes (non-NULL)
 

	
 
#ifdef OTTD_ASSERT
 
	size_t checked;      ///< Number of items we checked for
 
#endif /* OTTD_ASSERT */
 
	bool cleaning;       ///< True if cleaning pool (deleting all items)
 

	
 
	Titem **data;        ///< Pointer to array of pointers to Titem
 

	
 
	Pool(const char *name);
 
	void CleanPool();
 
@@ -66,13 +68,17 @@ struct Pool {
 
	 * Tests whether we can allocate 'n' items
 
	 * @param n number of items we want to allocate
 
	 * @return true if 'n' items can be allocated
 
	 */
 
	FORCEINLINE bool CanAllocate(size_t n = 1)
 
	{
 
		return this->items <= Tmax_size - n;
 
		bool ret = this->items <= Tmax_size - n;
 
#ifdef OTTD_ASSERT
 
		this->checked = ret ? n : 0;
 
#endif /* OTTD_ASSERT */
 
		return ret;
 
	}
 

	
 
	/**
 
	 * Base class for all PoolItems
 
	 * @tparam Tpool The pool this item is going to be part of
 
	 */
src/stdafx.h
Show inline comments
 
@@ -406,12 +406,17 @@ void NORETURN CDECL error(const char *st
 
 */
 
#if (defined(_MSC_VER) && defined(NDEBUG) && defined(WITH_ASSERT)) || (!defined(_MSC_VER) && !defined(NDEBUG) && !defined(_DEBUG))
 
	#undef assert
 
	#define assert(expression) if (!(expression)) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
 
#endif
 

	
 
/* Asserts are enabled if NDEBUG isn't defined, or if we are using MSVC and WITH_ASSERT is defined. */
 
#if !defined(NDEBUG) || (defined(_MSC_VER) && defined(WITH_ASSERT))
 
	#define OTTD_ASSERT
 
#endif
 

	
 
#if defined(MORPHOS) || defined(__NDS__) || defined(__DJGPP__)
 
	/* MorphOS and NDS don't have C++ conformant _stricmp... */
 
	#define _stricmp stricmp
 
#elif defined(OPENBSD)
 
	/* OpenBSD uses strcasecmp(3) */
 
	#define _stricmp strcasecmp
0 comments (0 inline, 0 general)