1
0
Fork 0

(svn r18045) -Fix: GCC 4.5@HEAD not compiling OpenTTD anymore because of a "non-placement deallocation function [is] selected for placement delete", or in other words delete(void *, size_t) is 'magic'.

We implemented these delete(void *, size_t) operator functions because MSVC warned that "no matching operator delete found; memory will not be freed if initialization throws an exception" for new(size_t, size_t).
This disables MSVC warning about this because we do not use exceptions in the (constructors that use the) overridden allocation functions, as such they will never be called; delete(void *) remains necessary though.
release/1.0
rubidium 2009-11-11 21:15:58 +00:00
parent 285d25e01b
commit 485b5a9c2f
4 changed files with 46 additions and 44 deletions

View File

@ -118,20 +118,6 @@ struct Pool {
return Tpool->GetNew(size, index);
}
/**
* Deletes item with given index.
* @param p Titem memory to release
* @param index index of item
* @note never use this! Only for internal use
* (called automatically when constructor of Titem uses throw())
*/
FORCEINLINE void operator delete(void *p, size_t index)
{
assert(p == Tpool->Get(index));
Tpool->FreeItem(index);
}
/**
* Allocates space for new Titem at given memory address
* @param size size of Titem
@ -154,21 +140,6 @@ struct Pool {
return ptr;
}
/**
* Deletes item that was allocated by the function above
* @param p Titem memory to release
* @param ptr parameter given to operator new
* @note never use this! Only for internal use
* (called automatically when constructor of Titem uses throw())
*/
FORCEINLINE void operator delete(void *p, void *ptr)
{
assert(p == ptr);
for (size_t i = 0; i < Tpool->first_unused; i++) {
assert(ptr != Tpool->data[i]);
}
}
/** Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() */

View File

@ -125,24 +125,39 @@ public:
return new (strlen(text) + 1) GRFText(langid, text);
}
/**
* Helper allocation function to disallow something.
* Don't allow simple 'news'; they wouldn't have enough memory.
* @param size the amount of space not to allocate
*/
void *operator new(size_t size)
{
NOT_REACHED();
}
/**
* Free the memory we allocated
* @param p memory to free
*/
void operator delete(void *p)
{
free(p);
}
private:
GRFText(byte langid_, const char *text_) : next(NULL), langid(langid_)
{
strcpy(text, text_);
}
/**
* Allocate memory for this class.
* @param size the size of the instance
* @param extra the extra memory for the text
* @return the requested amount of memory for both the instance and the text
*/
void *operator new(size_t size, size_t extra)
{
return ::operator new(size + extra);
}
public:
/* dummy operator delete to silence VC8:
* 'void *GRFText::operator new(size_t,size_t)' : no matching operator delete found;
* memory will not be freed if initialization throws an exception */
void operator delete(void *p, size_t extra)
{
return ::operator delete(p);
return MallocT<byte>(size + extra);
}
public:

View File

@ -162,6 +162,7 @@
#if (_MSC_VER < 1400) // MSVC 2005 safety checks
#error "Only MSVC 2005 or higher are supported. MSVC 2003 and earlier are not! Upgrade your compiler."
#endif /* (_MSC_VER < 1400) */
#pragma warning(disable: 4291) // no matching operator delete found; memory will not be freed if initialization throws an exception (reason: our overloaded functions never throw an exception)
#pragma warning(disable: 4996) // 'strdup' was declared deprecated
#define _CRT_SECURE_NO_DEPRECATE // all deprecated 'unsafe string functions
#pragma warning(disable: 6308) // code analyzer: 'realloc' might return null pointer: assigning null pointer to 't_ptr', which is passed as an argument to 'realloc', will cause the original memory block to be leaked

View File

@ -345,11 +345,26 @@ public:
Window();
virtual ~Window();
/* Don't allow arrays; arrays of Windows are pointless as you need
* to destruct them all at the same time too, which is kinda hard. */
FORCEINLINE void *operator new[](size_t size) { NOT_REACHED(); }
/* Don't free the window directly; it corrupts the linked list when iterating */
FORCEINLINE void operator delete(void *ptr) {}
/**
* Helper allocation function to disallow something.
* Don't allow arrays; arrays of Windows are pointless as you need
* to destruct them all at the same time too, which is kinda hard.
* @param size the amount of space not to allocate
*/
FORCEINLINE void *operator new[](size_t size)
{
NOT_REACHED();
}
/**
* Helper allocation function to disallow something.
* Don't free the window directly; it corrupts the linked list when iterating
* @param ptr the pointer not to free
*/
FORCEINLINE void operator delete(void *ptr)
{
}
uint16 flags4; ///< Window flags, @see WindowFlags
WindowClass window_class; ///< Window class