Changeset View
Changeset View
Standalone View
Standalone View
source/lib/code_generation.h
Show First 20 Lines • Show All 95 Lines • ▼ Show 20 Lines | |||||
#define SAFE_NEW(type, ptr)\ | #define SAFE_NEW(type, ptr)\ | ||||
type* ptr;\ | type* ptr;\ | ||||
try\ | try\ | ||||
{\ | {\ | ||||
ptr = new type();\ | ptr = new type();\ | ||||
}\ | }\ | ||||
catch(std::bad_alloc&)\ | catch(std::bad_alloc&)\ | ||||
{\ | {\ | ||||
ptr = 0;\ | ptr = nullptr;\ | ||||
} | } | ||||
/** | /** | ||||
* delete memory ensuing from new and set the pointer to zero | * delete memory ensuing from new and set the pointer to nullptr | ||||
* (thus making double-frees safe / a no-op) | * (thus making double-frees safe / a no-op) | ||||
**/ | **/ | ||||
#define SAFE_DELETE(p)\ | #define SAFE_DELETE(p)\ | ||||
STMT(\ | STMT(\ | ||||
delete (p); /* if p == 0, delete is a no-op */ \ | delete (p); /* if p == nullptr, delete is a no-op */ \ | ||||
(p) = 0;\ | (p) = nullptr;\ | ||||
) | ) | ||||
/** | /** | ||||
* delete memory ensuing from new[] and set the pointer to zero | * delete memory ensuing from new[] and set the pointer to nullptr | ||||
* (thus making double-frees safe / a no-op) | * (thus making double-frees safe / a no-op) | ||||
**/ | **/ | ||||
#define SAFE_ARRAY_DELETE(p)\ | #define SAFE_ARRAY_DELETE(p)\ | ||||
STMT(\ | STMT(\ | ||||
delete[] (p); /* if p == 0, delete is a no-op */ \ | delete[] (p); /* if p == nullptr, delete is a no-op */ \ | ||||
(p) = 0;\ | (p) = nullptr;\ | ||||
) | ) | ||||
/** | /** | ||||
* free memory ensuing from malloc and set the pointer to zero | * free memory ensuing from malloc and set the pointer to nullptr | ||||
* (thus making double-frees safe / a no-op) | * (thus making double-frees safe / a no-op) | ||||
**/ | **/ | ||||
#define SAFE_FREE(p)\ | #define SAFE_FREE(p)\ | ||||
STMT(\ | STMT(\ | ||||
free((void*)p); /* if p == 0, free is a no-op */ \ | free((void*)p); /* if p == nullptr, free is a no-op */ \ | ||||
(p) = 0;\ | (p) = nullptr;\ | ||||
) | ) | ||||
#endif // #ifndef INCLUDED_CODE_GENERATION | #endif // #ifndef INCLUDED_CODE_GENERATION |
Wildfire Games · Phabricator