From 003294e598ff3d72af7a493dd1596468f92d21cf Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 11 Apr 2020 12:27:07 +0200 Subject: [PATCH] - added printf.h header to avoid including more dirty ones for the console printing functions. --- src/dobject.h | 2 ++ src/doomtype.h | 37 +---------------------------- src/utility/m_alloc.cpp | 43 ++++----------------------------- src/utility/m_alloc.h | 8 +++++++ src/utility/matrix.cpp | 11 ++------- src/utility/matrix.h | 3 +-- src/utility/memarena.cpp | 5 ++-- src/utility/printf.h | 51 ++++++++++++++++++++++++++++++++++++++++ src/utility/vectors.h | 5 ++-- 9 files changed, 76 insertions(+), 89 deletions(-) create mode 100644 src/utility/printf.h diff --git a/src/dobject.h b/src/dobject.h index a4e16dc0b..071aa3220 100644 --- a/src/dobject.h +++ b/src/dobject.h @@ -265,12 +265,14 @@ private: void *operator new(size_t len, nonew&) { + GC::AllocBytes += len; return M_Malloc(len); } public: void operator delete (void *mem, nonew&) { + GC::AllocBytes -= _msize(mem); M_Free(mem); } diff --git a/src/doomtype.h b/src/doomtype.h index 3d8f4f81a..538d36b05 100644 --- a/src/doomtype.h +++ b/src/doomtype.h @@ -53,6 +53,7 @@ typedef TMap FClassMap; #endif #include "basics.h" +#include "printf.h" extern bool batchrun; @@ -65,42 +66,6 @@ enum BOXRIGHT }; // bbox coordinates - -// [RH] This gets used all over; define it here: -int Printf (int printlevel, const char *, ...) GCCPRINTF(2,3); -int Printf (const char *, ...) GCCPRINTF(1,2); - -// [RH] Same here: -int DPrintf (int level, const char *, ...) GCCPRINTF(2,3); - -extern "C" int mysnprintf(char *buffer, size_t count, const char *format, ...) GCCPRINTF(3,4); -extern "C" int myvsnprintf(char *buffer, size_t count, const char *format, va_list argptr) GCCFORMAT(3); - - -// game print flags -enum -{ - PRINT_LOW, // pickup messages - PRINT_MEDIUM, // death messages - PRINT_HIGH, // critical messages - PRINT_CHAT, // chat messages - PRINT_TEAMCHAT, // chat messages from a teammate - PRINT_LOG, // only to logfile - PRINT_BOLD = 200, // What Printf_Bold used - PRINT_TYPES = 1023, // Bitmask. - PRINT_NONOTIFY = 1024, // Flag - do not add to notify buffer - PRINT_NOLOG = 2048, // Flag - do not print to log file -}; - -enum -{ - DMSG_OFF, // no developer messages. - DMSG_ERROR, // general notification messages - DMSG_WARNING, // warnings - DMSG_NOTIFY, // general notification messages - DMSG_SPAMMY, // for those who want to see everything, regardless of its usefulness. -}; - #include "palentry.h" enum class ETextureType : uint8_t diff --git a/src/utility/m_alloc.cpp b/src/utility/m_alloc.cpp index 99f1b0657..214bba141 100644 --- a/src/utility/m_alloc.cpp +++ b/src/utility/m_alloc.cpp @@ -45,20 +45,13 @@ #endif #include "doomerrors.h" -#include "dobject.h" +#include "m_alloc.h" #ifndef _MSC_VER #define _NORMAL_BLOCK 0 #define _malloc_dbg(s,b,f,l) malloc(s) #define _realloc_dbg(p,s,b,f,l) realloc(p,s) #endif -#if defined(__APPLE__) -#define _msize(p) malloc_size(p) -#elif defined(__solaris__) || defined(__OpenBSD__) -#define _msize(p) (*((size_t*)(p)-1)) -#elif !defined(_WIN32) -#define _msize(p) malloc_usable_size(p) // from glibc/FreeBSD -#endif #ifndef _DEBUG #if !defined(__solaris__) && !defined(__OpenBSD__) @@ -69,22 +62,16 @@ void *M_Malloc(size_t size) if (block == NULL) I_FatalError("Could not malloc %zu bytes", size); - GC::AllocBytes += _msize(block); return block; } void *M_Realloc(void *memblock, size_t size) { - if (memblock != NULL) - { - GC::AllocBytes -= _msize(memblock); - } void *block = realloc(memblock, size); if (block == NULL) { I_FatalError("Could not realloc %zu bytes", size); } - GC::AllocBytes += _msize(block); return block; } #else @@ -99,7 +86,6 @@ void *M_Malloc(size_t size) *sizeStore = size; block = sizeStore+1; - GC::AllocBytes += _msize(block); return block; } @@ -108,10 +94,6 @@ void *M_Realloc(void *memblock, size_t size) if(memblock == NULL) return M_Malloc(size); - if (memblock != NULL) - { - GC::AllocBytes -= _msize(memblock); - } void *block = realloc(((size_t*) memblock)-1, size+sizeof(size_t)); if (block == NULL) { @@ -122,7 +104,6 @@ void *M_Realloc(void *memblock, size_t size) *sizeStore = size; block = sizeStore+1; - GC::AllocBytes += _msize(block); return block; } #endif @@ -137,24 +118,18 @@ void *M_Malloc_Dbg(size_t size, const char *file, int lineno) void *block = _malloc_dbg(size, _NORMAL_BLOCK, file, lineno); if (block == NULL) - I_FatalError("Could not malloc %zu bytes", size); + I_FatalError("Could not malloc %zu bytes in %s, line %d", size, file, lineno); - GC::AllocBytes += _msize(block); return block; } void *M_Realloc_Dbg(void *memblock, size_t size, const char *file, int lineno) { - if (memblock != NULL) - { - GC::AllocBytes -= _msize(memblock); - } void *block = _realloc_dbg(memblock, size, _NORMAL_BLOCK, file, lineno); if (block == NULL) { - I_FatalError("Could not realloc %zu bytes", size); + I_FatalError("Could not realloc %zu bytes in %s, line %d", size, file, lineno); } - GC::AllocBytes += _msize(block); return block; } #else @@ -163,13 +138,12 @@ void *M_Malloc_Dbg(size_t size, const char *file, int lineno) void *block = _malloc_dbg(size+sizeof(size_t), _NORMAL_BLOCK, file, lineno); if (block == NULL) - I_FatalError("Could not malloc %zu bytes", size); + I_FatalError("Could not malloc %zu bytes in %s, line %d", size, file, lineno); size_t *sizeStore = (size_t *) block; *sizeStore = size; block = sizeStore+1; - GC::AllocBytes += _msize(block); return block; } @@ -178,22 +152,17 @@ void *M_Realloc_Dbg(void *memblock, size_t size, const char *file, int lineno) if(memblock == NULL) return M_Malloc_Dbg(size, file, lineno); - if (memblock != NULL) - { - GC::AllocBytes -= _msize(memblock); - } void *block = _realloc_dbg(((size_t*) memblock)-1, size+sizeof(size_t), _NORMAL_BLOCK, file, lineno); if (block == NULL) { - I_FatalError("Could not realloc %zu bytes", size); + I_FatalError("Could not realloc %zu bytes in %s, line %d", size, file, lineno); } size_t *sizeStore = (size_t *) block; *sizeStore = size; block = sizeStore+1; - GC::AllocBytes += _msize(block); return block; } #endif @@ -204,7 +173,6 @@ void M_Free (void *block) { if (block != NULL) { - GC::AllocBytes -= _msize(block); free(block); } } @@ -213,7 +181,6 @@ void M_Free (void *block) { if(block != NULL) { - GC::AllocBytes -= _msize(block); free(((size_t*) block)-1); } } diff --git a/src/utility/m_alloc.h b/src/utility/m_alloc.h index c91983aaf..75c9d6cb9 100644 --- a/src/utility/m_alloc.h +++ b/src/utility/m_alloc.h @@ -36,6 +36,14 @@ #include +#if defined(__APPLE__) +#define _msize(p) malloc_size(p) +#elif defined(__solaris__) || defined(__OpenBSD__) +#define _msize(p) (*((size_t*)(p)-1)) +#elif !defined(_WIN32) +#define _msize(p) malloc_usable_size(p) // from glibc/FreeBSD +#endif + // These are the same as the same stdlib functions, // except they bomb out with a fatal error // when they can't get the memory. diff --git a/src/utility/matrix.cpp b/src/utility/matrix.cpp index 6b7e64df1..006a5fe5d 100644 --- a/src/utility/matrix.cpp +++ b/src/utility/matrix.cpp @@ -21,7 +21,7 @@ This is a simplified version of VSMatrix that has been adjusted for GZDoom's nee static inline FLOATTYPE DegToRad(FLOATTYPE degrees) { - return (FLOATTYPE)(degrees * (M_PI / 180.0f)); + return (FLOATTYPE)(degrees * (pi::pif() / 180.0f)); }; // sets the square matrix mat to the identity matrix, @@ -243,7 +243,7 @@ VSMatrix::lookAt(FLOATTYPE xPos, FLOATTYPE yPos, FLOATTYPE zPos, void VSMatrix::perspective(FLOATTYPE fov, FLOATTYPE ratio, FLOATTYPE nearp, FLOATTYPE farp) { - FLOATTYPE f = 1.0f / tan (fov * (M_PI / 360.0f)); + FLOATTYPE f = 1.0f / tan (fov * (pi::pif() / 360.0f)); loadIdentity(); mMatrix[0] = f / ratio; @@ -392,13 +392,6 @@ VSMatrix::length(const FLOATTYPE *a) { } -static inline int -M3(int i, int j) -{ - return (i*3+j); -}; - - // computes the derived normal matrix for the view matrix void diff --git a/src/utility/matrix.h b/src/utility/matrix.h index 22774845a..1c46c2397 100644 --- a/src/utility/matrix.h +++ b/src/utility/matrix.h @@ -22,7 +22,6 @@ #include #include "vectors.h" -#include "doomtype.h" #ifdef USE_DOUBLE typedef double FLOATTYPE; @@ -154,7 +153,7 @@ public: FVector3 axis(ax, ay, az); axis.MakeUnit(); - double c = cos(angle * M_PI/180.), s = sin(angle * M_PI/180.), t = 1 - c; + double c = cos(angle * pi::pi()/180.), s = sin(angle * pi::pi()/180.), t = 1 - c; double sx = s*axis.X, sy = s*axis.Y, sz = s*axis.Z; double tx, ty, txx, tyy, u, v; diff --git a/src/utility/memarena.cpp b/src/utility/memarena.cpp index f5544708d..b08828d73 100644 --- a/src/utility/memarena.cpp +++ b/src/utility/memarena.cpp @@ -37,9 +37,10 @@ ** with destructors). */ -#include "doomtype.h" +#include "basics.h" #include "memarena.h" -#include "c_dispatch.h" +#include "printf.h" +#include "cmdlib.h" struct FMemArena::Block { diff --git a/src/utility/printf.h b/src/utility/printf.h new file mode 100644 index 000000000..51f010443 --- /dev/null +++ b/src/utility/printf.h @@ -0,0 +1,51 @@ +#pragma once + +#if defined __GNUC__ || defined __clang__ +# define ATTRIBUTE(attrlist) __attribute__(attrlist) +#else +# define ATTRIBUTE(attrlist) +#endif + +// This header collects all things printf, so that this doesn't need to pull in other, far more dirty headers, just for outputting some text. + +extern "C" int mysnprintf(char* buffer, size_t count, const char* format, ...) ATTRIBUTE((format(printf, 3, 4))); +extern "C" int myvsnprintf(char* buffer, size_t count, const char* format, va_list argptr) ATTRIBUTE((format(printf, 3, 0))); + +// game print flags +enum +{ + PRINT_LOW, // pickup messages + PRINT_MEDIUM, // death messages + PRINT_HIGH, // critical messages + PRINT_CHAT, // chat messages + PRINT_TEAMCHAT, // chat messages from a teammate + PRINT_LOG, // only to logfile + PRINT_BOLD = 200, // What Printf_Bold used + PRINT_TYPES = 1023, // Bitmask. + PRINT_NONOTIFY = 1024, // Flag - do not add to notify buffer + PRINT_NOLOG = 2048, // Flag - do not print to log file +}; + +enum +{ + DMSG_OFF, // no developer messages. + DMSG_ERROR, // general notification messages + DMSG_WARNING, // warnings + DMSG_NOTIFY, // general notification messages + DMSG_SPAMMY, // for those who want to see everything, regardless of its usefulness. +}; + + +void I_Error(const char *fmt, ...) ATTRIBUTE((format(printf,1,2))); +void I_FatalError(const char* fmt, ...) ATTRIBUTE((format(printf, 1, 2))); + +// This really could need some cleanup - the main problem is that it'd create +// lots of potential for merge conflicts. + +int PrintString (int iprintlevel, const char *outline); +int VPrintf(int printlevel, const char* format, va_list parms); +int Printf (int printlevel, const char *format, ...) ATTRIBUTE((format(printf,2,3))); +int Printf (const char *format, ...) ATTRIBUTE((format(printf,1,2))); +int DPrintf (int level, const char *format, ...) ATTRIBUTE((format(printf,2,3))); + +void debugprintf(const char* f, ...); // Prints to the debugger's log. diff --git a/src/utility/vectors.h b/src/utility/vectors.h index ac4b97642..084e4b530 100644 --- a/src/utility/vectors.h +++ b/src/utility/vectors.h @@ -52,7 +52,8 @@ // make this a local inline function to avoid any dependencies on other headers and not pollute the global namespace namespace pi { - inline double pi() { return 3.14159265358979323846; } + inline constexpr double pi() { return 3.14159265358979323846; } + inline constexpr double pif() { return 3.14159265358979323846f; } } @@ -1356,7 +1357,7 @@ struct TAngle double Tan() const { - return g_tan(Degrees * (pi::pi() / 180.)); + return vec_t(g_tan(Radians())); } // This is for calculating vertical velocity. For high pitches the tangent will become too large to be useful.