فهرست منبع

Added bx::Location, and removed allocator macros.

Branimir Karadžić 2 سال پیش
والد
کامیت
a00ecbc9d5

+ 12 - 39
include/bx/allocator.h

@@ -9,28 +9,8 @@
 #include "bx.h"
 #include "uint32_t.h"
 
-#if BX_CONFIG_ALLOCATOR_DEBUG
-#	define BX_ALLOC(_allocator, _size)                         bx::alloc(_allocator, _size, 0, __FILE__, __LINE__)
-#	define BX_REALLOC(_allocator, _ptr, _size)                 bx::realloc(_allocator, _ptr, _size, 0, __FILE__, __LINE__)
-#	define BX_FREE(_allocator, _ptr)                           bx::free(_allocator, _ptr, 0, __FILE__, __LINE__)
-#	define BX_ALIGNED_ALLOC(_allocator, _size, _align)         bx::alloc(_allocator, _size, _align, __FILE__, __LINE__)
-#	define BX_ALIGNED_REALLOC(_allocator, _ptr, _size, _align) bx::realloc(_allocator, _ptr, _size, _align, __FILE__, __LINE__)
-#	define BX_ALIGNED_FREE(_allocator, _ptr, _align)           bx::free(_allocator, _ptr, _align, __FILE__, __LINE__)
-#	define BX_DELETE(_allocator, _ptr)                         bx::deleteObject(_allocator, _ptr, 0, __FILE__, __LINE__)
-#	define BX_ALIGNED_DELETE(_allocator, _ptr, _align)         bx::deleteObject(_allocator, _ptr, _align, __FILE__, __LINE__)
-#else
-#	define BX_ALLOC(_allocator, _size)                         bx::alloc(_allocator, _size, 0)
-#	define BX_REALLOC(_allocator, _ptr, _size)                 bx::realloc(_allocator, _ptr, _size, 0)
-#	define BX_FREE(_allocator, _ptr)                           bx::free(_allocator, _ptr, 0)
-#	define BX_ALIGNED_ALLOC(_allocator, _size, _align)         bx::alloc(_allocator, _size, _align)
-#	define BX_ALIGNED_REALLOC(_allocator, _ptr, _size, _align) bx::realloc(_allocator, _ptr, _size, _align)
-#	define BX_ALIGNED_FREE(_allocator, _ptr, _align)           bx::free(_allocator, _ptr, _align)
-#	define BX_DELETE(_allocator, _ptr)                         bx::deleteObject(_allocator, _ptr, 0)
-#	define BX_ALIGNED_DELETE(_allocator, _ptr, _align)         bx::deleteObject(_allocator, _ptr, _align)
-#endif // BX_CONFIG_DEBUG_ALLOC
-
-#define BX_NEW(_allocator, _type)                 BX_PLACEMENT_NEW(BX_ALLOC(_allocator, sizeof(_type) ), _type)
-#define BX_ALIGNED_NEW(_allocator, _type, _align) BX_PLACEMENT_NEW(BX_ALIGNED_ALLOC(_allocator, sizeof(_type), _align), _type)
+#define BX_NEW(_allocator, _type)                 BX_PLACEMENT_NEW(bx::alloc(_allocator, sizeof(_type) ), _type)
+#define BX_ALIGNED_NEW(_allocator, _type, _align) BX_PLACEMENT_NEW(bx::alignedAlloc(_allocator, sizeof(_type), _align), _type)
 #define BX_PLACEMENT_NEW(_ptr, _type)             ::new(bx::PlacementNew, _ptr) _type
 
 void* operator new(size_t, bx::PlacementNewTag, void* _ptr);
@@ -56,14 +36,14 @@ namespace bx
 		///   _size is not 0, memory block will be resized.
 		/// @param[in] _size If _ptr is set, and _size is 0, memory will be freed.
 		/// @param[in] _align Alignment.
-		/// @param[in] _file Debug file path info.
+		/// @param[in] _filePath Debug file path info.
 		/// @param[in] _line Debug file line info.
 		///
 		virtual void* realloc(
 			  void* _ptr
 			, size_t _size
 			, size_t _align
-			, const char* _file
+			, const char* _filePath
 			, uint32_t _line
 			) = 0;
 	};
@@ -83,7 +63,7 @@ namespace bx
 			  void* _ptr
 			, size_t _size
 			, size_t _align
-			, const char* _file
+			, const char* _filePath
 			, uint32_t _line
 			) override;
 	};
@@ -100,8 +80,7 @@ namespace bx
 		  AllocatorI* _allocator
 		, size_t _size
 		, size_t _align = 0
-		, const char* _file = NULL
-		, uint32_t _line = 0
+		, const Location& _location = Location::current()
 		);
 
 	/// Free memory.
@@ -109,8 +88,7 @@ namespace bx
 		  AllocatorI* _allocator
 		, void* _ptr
 		, size_t _align = 0
-		, const char* _file = NULL
-		, uint32_t _line = 0
+		, const Location& _location = Location::current()
 		);
 
 	/// Resize memory block.
@@ -119,8 +97,7 @@ namespace bx
 		, void* _ptr
 		, size_t _size
 		, size_t _align = 0
-		, const char* _file = NULL
-		, uint32_t _line = 0
+		, const Location& _location = Location::current()
 		);
 
 	/// Allocate memory with specific alignment.
@@ -128,8 +105,7 @@ namespace bx
 		  AllocatorI* _allocator
 		, size_t _size
 		, size_t _align
-		, const char* _file = NULL
-		, uint32_t _line = 0
+		, const Location& _location = Location::current()
 		);
 
 	/// Free memory that was allocated with aligned allocator.
@@ -137,8 +113,7 @@ namespace bx
 		  AllocatorI* _allocator
 		, void* _ptr
 		, size_t /*_align*/
-		, const char* _file = NULL
-		, uint32_t _line = 0
+		, const Location& _location = Location::current()
 		);
 
 	/// Resize memory block that was allocated with aligned allocator.
@@ -147,8 +122,7 @@ namespace bx
 		, void* _ptr
 		, size_t _size
 		, size_t _align
-		, const char* _file = NULL
-		, uint32_t _line = 0
+		, const Location& _location = Location::current()
 		);
 
 	/// Delete object with specific allocator.
@@ -157,8 +131,7 @@ namespace bx
 		  AllocatorI* _allocator
 		, ObjectT* _object
 		, size_t _align = 0
-		, const char* _file = NULL
-		, uint32_t _line = 0
+		, const Location& _location = Location::current()
 		);
 
 } // namespace bx

+ 58 - 0
include/bx/bx.h

@@ -55,6 +55,64 @@ namespace bx
 	/// Fields are initialized to identity value.
 	BX_DECLARE_TAG(InitIdentity);
 
+	/// Source location with file path, and file line.
+	///
+	struct Location
+	{
+		/// Default constructor.
+		///
+		constexpr Location()
+			: filePath(""), line(0) {}
+
+		/// Constructor with specific file name, and line number.
+		///
+		constexpr Location(const char* _filePath, uint32_t _line)
+			: filePath(_filePath), line(_line) {}
+
+		/// Current source location.
+		///
+		static Location current(
+			  const char* _filePath = __builtin_FILE()
+			, uint32_t _line = __builtin_LINE()
+			);
+
+		const char* filePath; //!< File path.
+		uint32_t    line;     //!< File line.
+	};
+
+	/// Unknown source code location.
+	static constexpr Location kUnknownLocation("Unknown?", 0);
+
+	/// Source location with file path, file line, and function name.
+	///
+	struct LocationFull
+	{
+		/// Default constructor.
+		///
+		constexpr LocationFull()
+			: function(""), filePath(""), line(0) {}
+
+		/// Constructor with specific function name, file name, and line number.
+		///
+		constexpr LocationFull(const char* _function, const char* _filePath, uint32_t _line)
+			: function(_function), filePath(_filePath), line(_line) {}
+
+		/// Current source location.
+		///
+		static LocationFull current(
+			  const char* _function = __builtin_FUNCTION()
+			, const char* _filePath = __builtin_FILE()
+			, uint32_t _line = __builtin_LINE()
+			);
+
+		const char* function; //!< Function name.
+		const char* filePath; //!< File path.
+		uint32_t    line;     //!< File line.
+	};
+
+	/// Unknown source code location.
+	static constexpr LocationFull kUnknownLocationFull("Unknown?", "Unknown?", 0);
+
 	/// Arithmetic type `Ty` limits.
 	template<typename Ty, bool SignT = isSigned<Ty>()>
 	struct LimitsT;

+ 15 - 15
include/bx/inline/allocator.inl

@@ -32,46 +32,46 @@ namespace bx
 		return un.ptr;
 	}
 
-	inline void* alloc(AllocatorI* _allocator, size_t _size, size_t _align, const char* _file, uint32_t _line)
+	inline void* alloc(AllocatorI* _allocator, size_t _size, size_t _align, const Location& _location)
 	{
-		return _allocator->realloc(NULL, _size, _align, _file, _line);
+		return _allocator->realloc(NULL, _size, _align, _location.filePath, _location.line);
 	}
 
-	inline void free(AllocatorI* _allocator, void* _ptr, size_t _align, const char* _file, uint32_t _line)
+	inline void free(AllocatorI* _allocator, void* _ptr, size_t _align, const Location& _location)
 	{
-		_allocator->realloc(_ptr, 0, _align, _file, _line);
+		_allocator->realloc(_ptr, 0, _align, _location.filePath, _location.line);
 	}
 
-	inline void* realloc(AllocatorI* _allocator, void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line)
+	inline void* realloc(AllocatorI* _allocator, void* _ptr, size_t _size, size_t _align, const Location& _location)
 	{
-		return _allocator->realloc(_ptr, _size, _align, _file, _line);
+		return _allocator->realloc(_ptr, _size, _align, _location.filePath, _location.line);
 	}
 
-	inline void* alignedAlloc(AllocatorI* _allocator, size_t _size, size_t _align, const char* _file, uint32_t _line)
+	inline void* alignedAlloc(AllocatorI* _allocator, size_t _size, size_t _align, const Location& _location)
 	{
 		const size_t align = max(_align, sizeof(uint32_t) );
 		const size_t total = _size + align;
-		uint8_t* ptr = (uint8_t*)alloc(_allocator, total, 0, _file, _line);
+		uint8_t* ptr = (uint8_t*)alloc(_allocator, total, 0, _location);
 		uint8_t* aligned = (uint8_t*)alignPtr(ptr, sizeof(uint32_t), align);
 		uint32_t* header = (uint32_t*)aligned - 1;
 		*header = uint32_t(aligned - ptr);
 		return aligned;
 	}
 
-	inline void alignedFree(AllocatorI* _allocator, void* _ptr, size_t _align, const char* _file, uint32_t _line)
+	inline void alignedFree(AllocatorI* _allocator, void* _ptr, size_t _align, const Location& _location)
 	{
 		BX_UNUSED(_align);
 		uint8_t* aligned = (uint8_t*)_ptr;
 		uint32_t* header = (uint32_t*)aligned - 1;
 		uint8_t* ptr = aligned - *header;
-		free(_allocator, ptr, 0, _file, _line);
+		free(_allocator, ptr, 0, _location);
 	}
 
-	inline void* alignedRealloc(AllocatorI* _allocator, void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line)
+	inline void* alignedRealloc(AllocatorI* _allocator, void* _ptr, size_t _size, size_t _align, const Location& _location)
 	{
 		if (NULL == _ptr)
 		{
-			return alignedAlloc(_allocator, _size, _align, _file, _line);
+			return alignedAlloc(_allocator, _size, _align, _location);
 		}
 
 		uint8_t* aligned = (uint8_t*)_ptr;
@@ -80,7 +80,7 @@ namespace bx
 
 		const size_t align = max(_align, sizeof(uint32_t) );;
 		const size_t total = _size + align;
-		ptr = (uint8_t*)realloc(_allocator, ptr, total, 0, _file, _line);
+		ptr = (uint8_t*)realloc(_allocator, ptr, total, 0, _location);
 		uint8_t* newAligned = (uint8_t*)alignPtr(ptr, sizeof(uint32_t), align);
 
 		if (newAligned == aligned)
@@ -96,12 +96,12 @@ namespace bx
 	}
 
 	template <typename ObjectT>
-	inline void deleteObject(AllocatorI* _allocator, ObjectT* _object, size_t _align, const char* _file, uint32_t _line)
+	inline void deleteObject(AllocatorI* _allocator, ObjectT* _object, size_t _align, const Location& _location)
 	{
 		if (NULL != _object)
 		{
 			_object->~ObjectT();
-			free(_allocator, _object, _align, _file, _line);
+			free(_allocator, _object, _align, _location);
 		}
 	}
 

+ 2 - 2
include/bx/inline/handlealloc.inl

@@ -103,14 +103,14 @@ namespace bx
 
 	inline HandleAlloc* createHandleAlloc(AllocatorI* _allocator, uint16_t _maxHandles)
 	{
-		uint8_t* ptr = (uint8_t*)BX_ALLOC(_allocator, sizeof(HandleAlloc) + 2*_maxHandles*sizeof(uint16_t) );
+		uint8_t* ptr = (uint8_t*)bx::alloc(_allocator, sizeof(HandleAlloc) + 2*_maxHandles*sizeof(uint16_t) );
 		return BX_PLACEMENT_NEW(ptr, HandleAlloc)(_maxHandles);
 	}
 
 	inline void destroyHandleAlloc(AllocatorI* _allocator, HandleAlloc* _handleAlloc)
 	{
 		_handleAlloc->~HandleAlloc();
-		BX_FREE(_allocator, _handleAlloc);
+		bx::free(_allocator, _handleAlloc);
 	}
 
 	template <uint16_t MaxHandlesT>

+ 2 - 2
include/bx/inline/readerwriter.inl

@@ -67,7 +67,7 @@ namespace bx
 
 	inline MemoryBlock::~MemoryBlock()
 	{
-		BX_FREE(m_allocator, m_data);
+		bx::free(m_allocator, m_data);
 	}
 
 	inline void* MemoryBlock::more(uint32_t _size)
@@ -75,7 +75,7 @@ namespace bx
 		if (0 < _size)
 		{
 			m_size += _size;
-			m_data = BX_REALLOC(m_allocator, m_data, m_size);
+			m_data = bx::realloc(m_allocator, m_data, m_size);
 		}
 
 		return m_data;

+ 2 - 2
include/bx/inline/spscqueue.inl

@@ -27,7 +27,7 @@ namespace bx
 		{
 			Node* node = m_first;
 			m_first = node->m_next;
-			BX_DELETE(m_allocator, node);
+			bx::deleteObject(m_allocator, node);
 		}
 	}
 
@@ -39,7 +39,7 @@ namespace bx
 		{
 			Node* node = m_first;
 			m_first = m_first->m_next;
-			BX_DELETE(m_allocator, node);
+			bx::deleteObject(m_allocator, node);
 		}
 	}
 

+ 2 - 2
include/bx/inline/string.inl

@@ -192,7 +192,7 @@ namespace bx
 			if (len+1 > m_capacity)
 			{
 				const int32_t capacity = alignUp(len+1, 256);
-				ptr = (char*)BX_REALLOC(*AllocatorT, 0 != m_capacity ? ptr : NULL, capacity);
+				ptr = (char*)bx::realloc(*AllocatorT, 0 != m_capacity ? ptr : NULL, capacity);
 
 				*const_cast<char**>(&m_ptr) = ptr;
 				m_capacity = capacity;
@@ -216,7 +216,7 @@ namespace bx
 
 		if (0 != m_capacity)
 		{
-			BX_FREE(*AllocatorT, const_cast<char*>(m_ptr) );
+			bx::free(*AllocatorT, const_cast<char*>(m_ptr) );
 
 			StringView::clear();
 			m_capacity = 0;

+ 46 - 0
include/bx/macros.h

@@ -244,6 +244,14 @@
 #	endif // BX_CONFIG_DEBUG
 #endif // BX_ASSERT
 
+#ifndef BX_ASSERT_LOC
+#	if BX_CONFIG_DEBUG
+#		define BX_ASSERT_LOC _BX_ASSERT_LOC
+#	else
+#		define BX_ASSERT_LOC(...) BX_NOOP()
+#	endif // BX_CONFIG_DEBUG
+#endif // BX_ASSERT_LOC
+
 #ifndef BX_TRACE
 #	if BX_CONFIG_DEBUG
 #		define BX_TRACE _BX_TRACE
@@ -252,6 +260,14 @@
 #	endif // BX_CONFIG_DEBUG
 #endif // BX_TRACE
 
+#ifndef BX_TRACE_LOC
+#	if BX_CONFIG_DEBUG
+#		define BX_TRACE_LOC _BX_TRACE_LOC
+#	else
+#		define BX_TRACE_LOC(...) BX_NOOP()
+#	endif // BX_CONFIG_DEBUG
+#endif // BX_TRACE_LOC
+
 #ifndef BX_WARN
 #	if BX_CONFIG_DEBUG
 #		define BX_WARN _BX_WARN
@@ -260,11 +276,24 @@
 #	endif // BX_CONFIG_DEBUG
 #endif // BX_ASSERT
 
+#ifndef BX_WARN_LOC
+#	if BX_CONFIG_DEBUG
+#		define BX_WARN_LOC _BX_WARN_LOC
+#	else
+#		define BX_WARN_LOC(...) BX_NOOP()
+#	endif // BX_CONFIG_DEBUG
+#endif // BX_WARN_LOC
+
 #define _BX_TRACE(_format, ...)                                                                    \
 	BX_MACRO_BLOCK_BEGIN                                                                           \
 		bx::debugPrintf(__FILE__ "(" BX_STRINGIZE(__LINE__) "): BX " _format "\n", ##__VA_ARGS__); \
 	BX_MACRO_BLOCK_END
 
+#define _BX_TRACE_LOC(_location, _format, ...)                                                          \
+	BX_MACRO_BLOCK_BEGIN                                                                                \
+		bx::debugPrintf("%s(%d): BX " _format "\n", _location.filePath, _location.line, ##__VA_ARGS__); \
+	BX_MACRO_BLOCK_END
+
 #define _BX_WARN(_condition, _format, ...)            \
 	BX_MACRO_BLOCK_BEGIN                              \
 		if (!BX_IGNORE_C4127(_condition) )            \
@@ -282,6 +311,23 @@
 		}                                               \
 	BX_MACRO_BLOCK_END
 
+#define _BX_ASSERT_LOC(_location, _condition, _format, ...)             \
+	BX_MACRO_BLOCK_BEGIN                                                \
+		if (!BX_IGNORE_C4127(_condition) )                              \
+		{                                                               \
+			_BX_TRACE_LOC(_location, "ASSERT " _format, ##__VA_ARGS__); \
+			bx::debugBreak();                                           \
+		}                                                               \
+	BX_MACRO_BLOCK_END
+
+#define _BX_WARN_LOC(_location, _condition, _format, ...)             \
+	BX_MACRO_BLOCK_BEGIN                                              \
+		if (!BX_IGNORE_C4127(_condition) )                            \
+		{                                                             \
+			_BX_TRACE_LOC(_location, "WARN " _format, ##__VA_ARGS__); \
+		}                                                             \
+	BX_MACRO_BLOCK_END
+
 // static_assert sometimes causes unused-local-typedef...
 BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG("-Wunused-local-typedef")
 

+ 4 - 4
src/allocator.cpp

@@ -21,7 +21,7 @@ namespace bx
 	{
 	}
 
-	void* DefaultAllocator::realloc(void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line)
+	void* DefaultAllocator::realloc(void* _ptr, size_t _size, size_t _align, const char* _filePath, uint32_t _line)
 	{
 		if (0 == _size)
 		{
@@ -34,7 +34,7 @@ namespace bx
 				}
 
 #	if BX_COMPILER_MSVC
-				BX_UNUSED(_file, _line);
+				BX_UNUSED(_filePath, _line);
 				_aligned_free(_ptr);
 #	else
 				alignedFree(this, _ptr, _align, _file, _line);
@@ -51,7 +51,7 @@ namespace bx
 			}
 
 #	if BX_COMPILER_MSVC
-			BX_UNUSED(_file, _line);
+			BX_UNUSED(_filePath, _line);
 			return _aligned_malloc(_size, _align);
 #	else
 			return alignedAlloc(this, _size, _align, _file, _line);
@@ -64,7 +64,7 @@ namespace bx
 		}
 
 #	if BX_COMPILER_MSVC
-		BX_UNUSED(_file, _line);
+		BX_UNUSED(_filePath, _line);
 		return _aligned_realloc(_ptr, _size, _align);
 #	else
 		return alignedRealloc(this, _ptr, _size, _align, _file, _line);

+ 10 - 0
src/bx.cpp

@@ -12,6 +12,16 @@
 
 namespace bx
 {
+	Location Location::current(const char* _filePath, uint32_t _line)
+	{
+		return Location(_filePath, _line);
+	}
+
+	LocationFull LocationFull::current(const char* _function, const char* _filePath, uint32_t _line)
+	{
+		return LocationFull(_function, _filePath, _line);
+	}
+
 	void swap(void* _a, void* _b, size_t _numBytes)
 	{
 		uint8_t* lhs = (uint8_t*)_a;

+ 6 - 6
src/settings.cpp

@@ -7,8 +7,8 @@
 
 namespace
 {
-#define INI_MALLOC(_ctx, _size)        (BX_ALLOC(reinterpret_cast<bx::AllocatorI*>(_ctx), _size) )
-#define INI_FREE(_ctx, _ptr)           (BX_FREE(reinterpret_cast<bx::AllocatorI*>(_ctx), _ptr) )
+#define INI_MALLOC(_ctx, _size)        (bx::alloc(reinterpret_cast<bx::AllocatorI*>(_ctx), _size) )
+#define INI_FREE(_ctx, _ptr)           (bx::free(reinterpret_cast<bx::AllocatorI*>(_ctx), _ptr) )
 #define INI_MEMCPY(_dst, _src, _count) (bx::memCopy(_dst, _src, _count) )
 #define INI_STRLEN(_str)               (bx::strLen(_str) )
 #define INI_STRNICMP(_s1, _s2, _len)   (bx::strCmpI(_s1, _s2, _len) )
@@ -170,12 +170,12 @@ int32_t Settings::read(ReaderSeekerI* _reader, Error* _err)
 {
 	int32_t size = int32_t(getRemain(_reader) );
 
-	void* data = BX_ALLOC(m_allocator, size);
+	void* data = bx::alloc(m_allocator, size);
 
 	int32_t total = bx::read(_reader, data, size, _err);
 	load(data, size);
 
-	BX_FREE(m_allocator, data);
+	bx::free(m_allocator, data);
 
 	return total;
 }
@@ -185,12 +185,12 @@ int32_t Settings::write(WriterI* _writer, Error* _err) const
 	ini_t* ini = INI_T(m_ini);
 
 	int32_t size = ini_save(ini, NULL, 0);
-	void* data = BX_ALLOC(m_allocator, size);
+	void* data = bx::alloc(m_allocator, size);
 
 	ini_save(ini, (char*)data, size);
 	int32_t total = bx::write(_writer, data, size-1, _err);
 
-	BX_FREE(m_allocator, data);
+	bx::free(m_allocator, data);
 
 	return total;
 }