(svn r22041) -Codechange: add a check that we called PoolItem::CanAllocateItem() before actually allocating it

This commit is contained in:
smatz 2011-02-09 18:55:51 +00:00
parent 67cbee4f64
commit fbfb0ffbf2
3 changed files with 17 additions and 2 deletions

View File

@ -123,6 +123,10 @@ DEFINE_POOL_METHOD(void *)::GetNew(size_t size)
{ {
size_t index = this->FindFirstFree(); size_t index = this->FindFirstFree();
#ifdef OTTD_ASSERT
assert(this->checked != 0);
this->checked--;
#endif /* OTTD_ASSERT */
if (index == NO_FREE_ITEM) { if (index == NO_FREE_ITEM) {
error("%s: no more free items", this->name); error("%s: no more free items", this->name);
} }

View File

@ -32,7 +32,9 @@ struct Pool {
size_t first_free; ///< No item with index lower than this is free (doesn't say anything about this one!) 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 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) 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) bool cleaning; ///< True if cleaning pool (deleting all items)
Titem **data; ///< Pointer to array of pointers to Titem Titem **data; ///< Pointer to array of pointers to Titem
@ -69,7 +71,11 @@ struct Pool {
*/ */
FORCEINLINE bool CanAllocate(size_t n = 1) 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;
} }
/** /**

View File

@ -409,6 +409,11 @@ void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2);
#define assert(expression) if (!(expression)) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression); #define assert(expression) if (!(expression)) error("Assertion failed at line %i of %s: %s", __LINE__, __FILE__, #expression);
#endif #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__) #if defined(MORPHOS) || defined(__NDS__) || defined(__DJGPP__)
/* MorphOS and NDS don't have C++ conformant _stricmp... */ /* MorphOS and NDS don't have C++ conformant _stricmp... */
#define _stricmp stricmp #define _stricmp stricmp