Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add assume.c/.h, convert assert-like error checks into assume statements #216

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions include/ace/utils/assume.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef _ACE_UTILS_ASSUME_H_
#define _ACE_UTILS_ASSUME_H_

#include <ace/types.h>
#include <ace/managers/system.h>
#include <ace/managers/log.h>

#ifdef __cplusplus
extern "C" {
#endif

#if defined(ACE_DEBUG)

#define assume(cond) _assume(cond, 0, __FILE__, __LINE__)
#define assumeMsg(cond, szErrorMsg) _assume(cond, szErrorMsg, __FILE__, __LINE__)
#define assumeNotNull(ptr) assumeMsg(ptr != 0, "Null pointer: " #ptr);

void _assume(ULONG ulExprValue, const char *szErrorMsg, const char *szFile, ULONG ulLine);

#else

#if __GNUC__ >= 13
#define assume(cond) __attribute__((__assume__(cond)))
#else
// https://stackoverflow.com/questions/25667901/ - this isn't optimized away!
// #define assume(cond) do { if (!(cond)) __builtin_unreachable(); } while (0)
// https://stackoverflow.com/questions/30919802/ - no footprint on code
#define assume(cond) ((void) sizeof(cond))
#endif
#define assumeMsg(cond, szErrorMsg) assume(cond)
#define assumeNotNull(ptr) assume(ptr != 0)

#endif

#ifdef __cplusplus
}
#endif

#endif // _ACE_UTILS_ASSUME_H_
9 changes: 5 additions & 4 deletions src/ace/managers/blit.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <ace/managers/blit.h>
#include <ace/managers/system.h>
#include <ace/utils/assume.h>

#define BLIT_LINE_OR ((ABC | ABNC | NABC | NANBC) | (SRCA | SRCC | DEST))
#define BLIT_LINE_XOR ((ABNC | NABC | NANBC) | (SRCA | SRCC | DEST))
Expand Down Expand Up @@ -313,10 +314,10 @@ UBYTE blitSafeCopyAligned(
tBitMap *pDst, WORD wDstX, WORD wDstY, WORD wWidth, WORD wHeight,
UWORD uwLine, const char *szFile
) {
if((wSrcX | wDstX | wWidth) & 0x000F) {
logWrite("ERR: Dimensions are not divisible by 16\n");
return 0;
}
assumeMsg((wSrcX & 0xF) == 0, "wSrcX isn't divisible by 16");
assumeMsg((wDstX & 0xF) == 0, "wDstX isn't divisible by 16");
assumeMsg((wWidth & 0xF) == 0, "wWidth isn't divisible by 16");

if(!blitCheck(
pSrc, wSrcX, wSrcY, pDst, wDstX, wDstY, wWidth, wHeight, uwLine, szFile
)) {
Expand Down
17 changes: 4 additions & 13 deletions src/ace/managers/bob.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <ace/managers/system.h>
#include <ace/managers/blit.h>
#include <ace/utils/custom.h>
#include <ace/utils/assume.h>

#if !defined(ACE_NO_BOB_WRAP_Y)
// Enables support for Y-wrapping of bobs. Required for scroll- and tileBuffer.
Expand Down Expand Up @@ -90,13 +91,8 @@ void bobManagerCreate(
pFront, pBack, uwAvailHeight
);

if(!bitmapIsInterleaved(pFront)) {
logWrite("ERR: front buffer bitmap %p isn't interleaved\n", pFront);
}

if(!bitmapIsInterleaved(pBack)) {
logWrite("ERR: back buffer bitmap %p isn't interleaved\n", pBack);
}
assumeMsg(bitmapIsInterleaved(pFront), "Front buffer bitmap isn't interleaved");
assumeMsg(bitmapIsInterleaved(pBack), "Back buffer bitmap isn't interleaved");

s_ubBpp = pFront->Depth;
s_pQueues[0].pDst = pBack;
Expand Down Expand Up @@ -427,12 +423,7 @@ void bobBegin(tBitMap *pBuffer) {
}
#ifdef GAME_DEBUG
UWORD uwDrawLimit = s_pQueues[0].pBg->Rows * s_pQueues[0].pBg->Depth;
if(uwDrawnHeight > uwDrawLimit) {
logWrite(
"ERR: BG restore out of bounds: used %hu, limit: %hu",
uwDrawnHeight, uwDrawLimit
);
}
assumeMsg(uwDrawnHeight <= uwDrawLimit, "BG restore out of bounds");
#endif
s_ubBobsSaved = 0;
s_ubBobsDrawn = 0;
Expand Down
23 changes: 5 additions & 18 deletions src/ace/managers/copper.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include <ace/managers/copper.h>
#ifdef AMIGA
#include <stdarg.h>
#include <ace/managers/system.h>
#include <limits.h>
#include <ace/managers/system.h>
#include <ace/utils/assume.h>
#include <proto/exec.h>

tCopManager g_sCopManager;
Expand Down Expand Up @@ -203,17 +204,9 @@ tCopList *copListCreate(void *pTagList, ...) {
ULONG ulListSize = tagGet(
pTagList, vaTags, TAG_COPPER_RAW_COUNT, ulInvalidSize
);
if(ulListSize == ulInvalidSize) {
logWrite("ERR: no size specified for raw list\n");
goto fail;
}
if(ulListSize > USHRT_MAX) {
logWrite(
"ERR: raw copperlist size too big: %lu, max is %u\n",
ulListSize, USHRT_MAX
);
goto fail;
}
assumeMsg(ulListSize != ulInvalidSize, "No size specified for raw list");
assumeMsg(ulListSize <= USHRT_MAX, "Raw copperlist size is too big");

logWrite("RAW mode, size: %lu + WAIT(0xFFFF)\n", ulListSize);
// Front bfr
pCopList->pFrontBfr->uwCmdCount = ulListSize+1;
Expand All @@ -233,12 +226,6 @@ tCopList *copListCreate(void *pTagList, ...) {
logBlockEnd("copListCreate()");
va_end(vaTags);
return pCopList;

fail:
va_end(vaTags);
copListDestroy(pCopList);
logBlockEnd("copListCreate()");
return 0;
}

void copListDestroy(tCopList *pCopList) {
Expand Down
13 changes: 5 additions & 8 deletions src/ace/managers/joy.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <ace/managers/log.h>
#include <ace/managers/system.h>
#include <ace/utils/custom.h>
#include <ace/utils/assume.h>

#if defined ACE_DEBUG
static UBYTE s_bInitCount = 0;
Expand All @@ -34,19 +35,15 @@ static inline const char *myAllocMiscResource(

void joyOpen(void) {
#if defined(ACE_DEBUG)
if(s_bInitCount++ != 0) {
// You should call keyCreate() only once
logWrite("ERR: Joy already initialized!\n");
}
assumeMsg(s_bInitCount == 0, "Joy already initialized");
++s_bInitCount;
#endif
}

void joyClose(void) {
#if defined(ACE_DEBUG)
if(s_bInitCount-- != 1) {
// You should call joyClose() only once for each joyCreate()
logWrite("ERR: Joy was initialized multiple times!\n");
}
--s_bInitCount;
assumeMsg(s_bInitCount == 0, "Joy was initialized multiple times");
#endif
joyDisableParallel();
}
Expand Down
13 changes: 5 additions & 8 deletions src/ace/managers/key.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <ace/managers/memory.h>
#include <ace/managers/system.h>
#include <ace/utils/custom.h>
#include <ace/utils/assume.h>
#include <hardware/intbits.h> // INTB_PORTS
#define KEY_RELEASED_BIT 1

Expand Down Expand Up @@ -94,10 +95,8 @@ const UBYTE g_pToAscii[] = {
void keyCreate(void) {
logBlockBegin("keyCreate()");
#if defined(ACE_DEBUG)
if(s_bInitCount++ != 0) {
// You should call keyCreate() only once
logWrite("ERR: Keyboard already initialized!\n");
}
assumeMsg(s_bInitCount == 0, "Keyboard already initialized");
++s_bInitCount;
#endif
systemSetCiaInt(CIA_A, CIAICRB_SERIAL, keyIntServer, &g_sKeyManager);
logBlockEnd("keyCreate()");
Expand All @@ -106,10 +105,8 @@ void keyCreate(void) {
void keyDestroy(void) {
logBlockBegin("keyDestroy()");
#if defined(ACE_DEBUG)
if(s_bInitCount-- != 1) {
// You should call keyDestroy() only once for each keyCreate()
logWrite("ERR: Keyboard was initialized multiple times!\n");
}
--s_bInitCount;
assumeMsg(s_bInitCount == 0, "Keyboard was initialized multiple times");
#endif
systemSetCiaInt(CIA_A, CIAICRB_SERIAL, 0, 0);
logBlockEnd("keyDestroy()");
Expand Down
6 changes: 3 additions & 3 deletions src/ace/managers/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string.h>
#include <ace/macros.h>
#include <ace/managers/system.h>
#include <ace/utils/assume.h>
#ifdef ACE_DEBUG

// Globals
Expand Down Expand Up @@ -243,9 +244,8 @@ void _logPushInt(void) {
}

void _logPopInt(void) {
if(--g_sLogManager.wInterruptDepth < 0) {
logWrite("ERR: INT DEPTH NEGATIVE!\n");
}
--g_sLogManager.wInterruptDepth;
assumeMsg(g_sLogManager.wInterruptDepth >= 0, "INT DEPTH NEGATIVE");
}

#endif // ACE_DEBUG
Loading