#error swr_assert.h should not be included directly, please include "common/os.h" instead.
#endif
+//=============================================================================
+//
+// MACROS defined in this file:
+//
+// - SWR_ASSUME(expression, ...): Tell compiler that the expression is true.
+// Helps with static code analysis as well.
+// DO NOT USE if code after this dynamically
+// checks for errors and handles them. The
+// compiler may optimize out the error check.
+//
+// - SWR_ASSERT(expression, ...): Inform the user is expression is false.
+// This check is only conditionally made,
+// usually only in debug mode.
+//
+// - SWR_REL_ASSERT(expression, ...): Unconditionally enabled version of SWR_ASSERT
+//
+// - SWR_ASSUME_ASSERT(expression, ...): Conditionally enabled SWR_ASSERT. Uses
+// SWR_ASSUME if SWR_ASSERT is disabled.
+// DO NOT USE in combination with actual
+// error checking (see SWR_ASSUME)
+//
+// - SWR_REL_ASSUME_ASSERT(expression, ...): Same as SWR_REL_ASSERT.
+//
+//=============================================================================
+
+#if defined(_WIN32)
+#define SWR_ASSUME(e, ...) __assume(e)
+#elif defined(__clang__)
+#define SWR_ASSUME(e, ...) __builtin_assume(e)
+#elif defined(__GNUC__)
+#define SWR_ASSUME(e, ...) ((e) ? ((void)0) : __builtin_unreachable())
+#else
+#define SWR_ASSUME(e, ...) ASSUME(e)
+#endif
+
#if !defined(SWR_ENABLE_ASSERTS)
#if !defined(NDEBUG)
}
#if SWR_ENABLE_ASSERTS
-#define SWR_ASSERT(e, ...) _SWR_ASSERT(true, e, ##__VA_ARGS__)
+#define SWR_ASSERT(e, ...) _SWR_ASSERT(true, e, ##__VA_ARGS__)
+#define SWR_ASSUME_ASSERT(e, ...) SWR_ASSERT(e, ##__VA_ARGS__)
#if defined(assert)
#undef assert
#endif
#define assert(exp) SWR_ASSERT(exp)
-#endif
+#endif // SWR_ENABLE_ASSERTS
#if SWR_ENABLE_REL_ASSERTS
-#define SWR_REL_ASSERT(e, ...) _SWR_ASSERT(false, e, ##__VA_ARGS__)
+#define SWR_REL_ASSERT(e, ...) _SWR_ASSERT(false, e, ##__VA_ARGS__)
+#define SWR_REL_ASSUME_ASSERT(e, ...) SWR_REL_ASSERT(e, ##__VA_ARGS__)
#endif
+
#endif // C++
#endif // SWR_ENABLE_ASSERTS || SWR_ENABLE_REL_ASSERTS
#if !SWR_ENABLE_ASSERTS
-#define SWR_ASSERT(e, ...) (void)(0)
+#define SWR_ASSERT(e, ...) (void)(0)
+#define SWR_ASSUME_ASSERT(e, ...) SWR_ASSUME(e, ##__VA_ARGS__)
#endif
#if !SWR_ENABLE_REL_ASSERTS
-#define SWR_REL_ASSERT(e, ...) (void)(0)
+#define SWR_REL_ASSERT(e, ...) (void)(0)
+#define SWR_REL_ASSUME_ASSERT(e, ...) SWR_ASSUME(e, ##__VA_ARGS__)
#endif
#define SWR_NOT_IMPL SWR_ASSERT(0, "%s not implemented", __FUNCTION__)
public:
ArenaBlock* AllocateAligned(size_t size, size_t align)
{
- SWR_ASSERT(size >= sizeof(ArenaBlock));
+ SWR_ASSUME_ASSERT(size >= sizeof(ArenaBlock));
ArenaBlock* p = new (_aligned_malloc(size, align)) ArenaBlock();
p->blockSize = size;
{
if (pMem)
{
- SWR_ASSERT(pMem->blockSize < size_t(0xdddddddd));
+ SWR_ASSUME_ASSERT(pMem->blockSize < size_t(0xdddddddd));
_aligned_free(pMem);
}
}
{
ArenaBlock* AllocateAligned(size_t size, size_t align)
{
- SWR_ASSERT(size >= sizeof(ArenaBlock));
- SWR_ASSERT(size <= uint32_t(-1));
+ SWR_ASSUME_ASSERT(size >= sizeof(ArenaBlock));
+ SWR_ASSUME_ASSERT(size <= uint32_t(-1));
uint32_t bucket = GetBucketId(size);
if (pBlock)
{
- SWR_ASSERT(pPrevBlock && pPrevBlock->pNext == pBlock);
+ SWR_ASSUME_ASSERT(pPrevBlock && pPrevBlock->pNext == pBlock);
pPrevBlock->pNext = pBlock->pNext;
pBlock->pNext = nullptr;
{
if (pMem)
{
- SWR_ASSERT(pMem->blockSize >= 0);
-
std::unique_lock<std::mutex> l(m_mutex);
InsertCachedBlock(GetBucketId(pMem->blockSize), pMem);
}
template <bool OldBlockT = false>
void InsertCachedBlock(uint32_t bucketId, ArenaBlock* pNewBlock)
{
- SWR_ASSERT(bucketId < CACHE_NUM_BUCKETS);
+ SWR_ASSUME_ASSERT(bucketId < CACHE_NUM_BUCKETS);
ArenaBlock* pPrevBlock = OldBlockT ? &m_oldCachedBlocks[bucketId] : &m_cachedBlocks[bucketId];
ArenaBlock* pBlock = pPrevBlock->pNext;
}
// Insert into list
- SWR_ASSERT(pPrevBlock);
+ SWR_ASSUME_ASSERT(pPrevBlock);
pPrevBlock->pNext = pNewBlock;
pNewBlock->pNext = pBlock;