From 87e9f7379bee3a18c3984f8e1e33520aee2e1520 Mon Sep 17 00:00:00 2001 From: cuz Date: Wed, 16 May 2001 09:24:42 +0000 Subject: [PATCH] Use inline for better performance git-svn-id: svn://svn.cc65.org/cc65/trunk@726 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/common/check.c | 38 ++++--------- src/common/check.h | 31 ++++------- src/common/coll.c | 104 ---------------------------------- src/common/coll.h | 133 +++++++++++++++++++++++++++++++++++++++++--- src/common/inline.h | 59 ++++++++++++++++++++ 5 files changed, 204 insertions(+), 161 deletions(-) create mode 100644 src/common/inline.h diff --git a/src/common/check.c b/src/common/check.c index 52e24271f..d96d94a35 100644 --- a/src/common/check.c +++ b/src/common/check.c @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2001 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -55,10 +55,10 @@ const char* MsgProgramAborted = "Program aborted: "; static void DefaultCheckFailed (const char* msg, const char* cond, - int code, const char* file, unsigned line) + const char* file, unsigned line) attribute ((noreturn)); -void (*CheckFailed) (const char* Msg, const char* Cond, int Code, +void (*CheckFailed) (const char* Msg, const char* Cond, const char* File, unsigned Line) attribute ((noreturn)) = DefaultCheckFailed; /* Function pointer that is called from check if the condition code is true. */ @@ -66,34 +66,16 @@ void (*CheckFailed) (const char* Msg, const char* Cond, int Code, /*****************************************************************************/ -/* Code */ +/* Code */ /*****************************************************************************/ static void DefaultCheckFailed (const char* Msg, const char* Cond, - int Code, const char* File, unsigned Line) + const char* File, unsigned Line) { - /* Log the error */ - if (Code) { - AbEnd ("%s%s (= %d), file `%s', line %u", Msg, Cond, Code, File, Line); - } else { - AbEnd ("%s%s, file `%s', line %u", Msg, Cond, File, Line); - } -} - - - -void Check (const char* Msg, const char* Cond, int Code, - const char* File, unsigned Line) -/* This function is called from all check macros (see below). It checks, - * wether the given Code is true (!= 0). If so, it calls the CheckFailed - * vector with the given strings. If not, it simply returns. - */ -{ - if (Code != 0) { - CheckFailed (Msg, Cond, Code, File, Line); - } + /* Output a diagnostic and abort */ + AbEnd ("%s%s, file `%s', line %u", Msg, Cond, File, Line); } diff --git a/src/common/check.h b/src/common/check.h index 1049c4bf4..38b132438 100644 --- a/src/common/check.h +++ b/src/common/check.h @@ -6,10 +6,10 @@ /* */ /* */ /* */ -/* (C) 1998 Ullrich von Bassewitz */ -/* Wacholderweg 14 */ -/* D-70597 Stuttgart */ -/* EMail: uz@musoftware.de */ +/* (C) 1998-2001 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@cc65.org */ /* */ /* */ /* This software is provided 'as-is', without any expressed or implied */ @@ -56,7 +56,7 @@ extern const char* MsgProgramAborted; /* "Program aborted: " */ extern void (*CheckFailed) (const char* Msg, const char* Cond, - int Code, const char* File, unsigned Line) + const char* File, unsigned Line) attribute ((noreturn)); /* Function pointer that is called from check if the condition code is true. */ @@ -68,29 +68,20 @@ extern void (*CheckFailed) (const char* Msg, const char* Cond, -void Check (const char* Msg, const char* Cond, int Code, - const char* File, unsigned Line); -/* This function is called from all check macros (see below). It checks, - * wether the given Code is true (!= 0). If so, it calls the CheckFailed - * vector with the given strings. If not, it simply returns. - */ - - - -#define FAIL(s) CheckFailed (MsgInternalError, s, 0, __FILE__, __LINE__) +#define FAIL(s) CheckFailed (MsgInternalError, s, __FILE__, __LINE__) /* Fail macro. Is used if something evil happens, calls checkfailed directly. */ -#define ABORT(s) CheckFailed (MsgProgramAborted, s, 0, __FILE__, __LINE__) +#define ABORT(s) CheckFailed (MsgProgramAborted, s, __FILE__, __LINE__) /* Use this one instead of FAIL if there is no internal program error but an * error condition that is caused by the user or operating system (FAIL and * ABORT are essentially the same but the message differs). */ -#define PRECONDITION(c) Check (MsgPrecondition, #c, !(c), __FILE__, __LINE__) - -#define CHECK(c) Check (MsgCheckFailed, #c, !(c), __FILE__, __LINE__) +#define PRECONDITION(c) \ + { if (!(c)) CheckFailed (MsgPrecondition, #c, __FILE__, __LINE__); } -#define ZCHECK(c) Check (MsgCheckFailed, #c, c, __FILE__, __LINE__) +#define CHECK(c) \ + { if (!(c)) CheckFailed (MsgCheckFailed, #c, __FILE__, __LINE__); } diff --git a/src/common/coll.c b/src/common/coll.c index c552cbf0d..465a65987 100644 --- a/src/common/coll.c +++ b/src/common/coll.c @@ -97,14 +97,6 @@ void FreeCollection (Collection* C) -unsigned CollCount (const Collection* C) -/* Return the number of items in the collection */ -{ - return C->Count; -} - - - void CollInsert (Collection* C, void* Item, unsigned Index) /* Insert the data at the given position in the collection */ { @@ -138,77 +130,6 @@ void CollInsert (Collection* C, void* Item, unsigned Index) -void CollAppend (Collection* C, void* Item) -/* Append an item to the end of the collection */ -{ - /* Insert the item at the end of the current list */ - CollInsert (C, Item, C->Count); -} - - - -void* CollAt (Collection* C, unsigned Index) -/* Return the item at the given index */ -{ - /* Check the index */ - PRECONDITION (Index < C->Count); - - /* Return the element */ - return C->Items[Index]; -} - - - -const void* CollConstAt (const Collection* C, unsigned Index) -/* Return the item at the given index */ -{ - /* Check the index */ - PRECONDITION (Index < C->Count); - - /* Return the element */ - return C->Items[Index]; -} - - - -void* CollLast (Collection* C) -/* Return the last item in a collection */ -{ - /* We must have at least one entry */ - PRECONDITION (C->Count > 0); - - /* Return the element */ - return C->Items[C->Count-1]; -} - - - -const void* CollConstLast (const Collection* C) -/* Return the last item in a collection */ -{ - /* We must have at least one entry */ - PRECONDITION (C->Count > 0); - - /* Return the element */ - return C->Items[C->Count-1]; -} - - - -void* CollPop (Collection* C) -/* Remove the last segment from the stack and return it. Calls FAIL if the - * collection is empty. - */ -{ - /* We must have at least one entry */ - PRECONDITION (C->Count > 0); - - /* Return the element */ - return C->Items[--C->Count]; -} - - - int CollIndex (Collection* C, const void* Item) /* Return the index of the given item in the collection. Return -1 if the * item was not found in the collection. @@ -261,31 +182,6 @@ void CollDeleteItem (Collection* C, const void* Item) -void CollDeleteAll (Collection* C) -/* Delete all items from the given collection. This will not free the items - * itself, it will only remove the pointers. - */ -{ - /* This one is easy... */ - C->Count = 0; -} - - - -void CollReplace (Collection* C, void* Item, unsigned Index) -/* Replace the item at the given position. The old item will not be freed, - * just the pointer will et replaced. - */ -{ - /* Check the index */ - PRECONDITION (Index < C->Count); - - /* Replace the item pointer */ - C->Items[Index] = Item; -} - - - static void QuickSort (Collection* C, int Lo, int Hi, int (*Compare) (void*, const void*, const void*), void* Data) diff --git a/src/common/coll.h b/src/common/coll.h index 83d3e9d22..3a9796bd5 100644 --- a/src/common/coll.h +++ b/src/common/coll.h @@ -38,7 +38,10 @@ +/* common */ #include "attrib.h" +#include "cfeature.h" +#include "check.h" @@ -81,31 +84,122 @@ Collection* NewCollection (void); void FreeCollection (Collection* C); /* Free a collection */ -unsigned CollCount (const Collection* C); +#if defined(HAVE_INLINE) +INLINE unsigned CollCount (const Collection* C) /* Return the number of items in the collection */ +{ + return C->Count; +} +#else +# define CollCount(C) (C)->Count +#endif void CollInsert (Collection* C, void* Item, unsigned Index); /* Insert the data at the given position in the collection */ -void CollAppend (Collection* C, void* Item); +#if defined(HAVE_INLINE) +INLINE void CollAppend (Collection* C, void* Item) /* Append an item to the end of the collection */ +{ + /* Insert the item at the end of the current list */ + CollInsert (C, Item, C->Count); +} +#else +# define CollAppend(C, Item) CollInsert (C, Item, (C)->Count) +#endif + +#if defined(HAVE_INLINE) +INLINE void* CollAt (Collection* C, unsigned Index) +/* Return the item at the given index */ +{ + /* Check the index */ + PRECONDITION (Index < C->Count); + + /* Return the element */ + return C->Items[Index]; +} +#else +# define CollAt(C, Index) \ + (PRECONDITION ((Index) < (C)->Count), \ + (C)->Items[(Index)]) +#endif -void* CollAt (Collection* C, unsigned Index) attribute ((const)); +#if defined(HAVE_INLINE) +INLINE void* CollAtUnchecked (Collection* C, unsigned Index) /* Return the item at the given index */ +{ + /* Return the element */ + return C->Items[Index]; +} +#else +# define CollAtUnchecked(C, Index) ((C)->Items[(Index)]) +#endif -const void* CollConstAt (const Collection* C, unsigned Index) attribute ((const)); +#if defined(HAVE_INLINE) +INLINE const void* CollConstAt (const Collection* C, unsigned Index) /* Return the item at the given index */ +{ + /* Check the index */ + PRECONDITION (Index < C->Count); + + /* Return the element */ + return C->Items[Index]; +} +#else +# define CollConstAt(C, Index) \ + (PRECONDITION ((Index) < (C)->Count), \ + (C)->Items[(Index)]) +#endif -void* CollLast (Collection* C); +#if defined(HAVE_INLINE) +INLINE void* CollLast (Collection* C) /* Return the last item in a collection */ +{ + /* We must have at least one entry */ + PRECONDITION (C->Count > 0); + + /* Return the element */ + return C->Items[C->Count-1]; +} +#else +# define CollLast(C) \ + (PRECONDITION ((C)->Count > 0), \ + (C)->Items[(C)->Count-1]) +#endif -const void* CollConstLast (const Collection* C); +#if defined(HAVE_INLINE) +INLINE const void* CollConstLast (const Collection* C) /* Return the last item in a collection */ +{ + /* We must have at least one entry */ + PRECONDITION (C->Count > 0); + + /* Return the element */ + return C->Items[C->Count-1]; +} +#else +# define CollConstLast(C) \ + (PRECONDITION ((C)->Count > 0), \ + (C)->Items[(C)->Count-1]) +#endif -void* CollPop (Collection* C); +#if defined(HAVE_INLINE) +INLINE void* CollPop (Collection* C) /* Remove the last segment from the stack and return it. Calls FAIL if the * collection is empty. */ +{ + /* We must have at least one entry */ + PRECONDITION (C->Count > 0); + + /* Return the element */ + return C->Items[--C->Count]; +} +#else +# define CollPop(C) \ + (PRECONDITION ((C)->Count > 0), \ + (C)->Items[--(C)->Count]) +#endif int CollIndex (Collection* C, const void* Item); /* Return the index of the given item in the collection. Return -1 if the @@ -123,15 +217,36 @@ void CollDeleteItem (Collection* C, const void* Item); * collection, otherwise FAIL will be called. */ -void CollDeleteAll (Collection* C); +#if defined(HAVE_INLINE) +INLINE void CollDeleteAll (Collection* C) /* Delete all items from the given collection. This will not free the items * itself, it will only remove the pointers. */ +{ + /* This one is easy... */ + C->Count = 0; +} +#else +# define CollDeleteAll(C) ((C)->Count = 0) +#endif -void CollReplace (Collection* C, void* Item, unsigned Index); +#if defined(HAVE_INLINE) +INLINE void CollReplace (Collection* C, void* Item, unsigned Index) /* Replace the item at the given position. The old item will not be freed, * just the pointer will et replaced. */ +{ + /* Check the index */ + PRECONDITION (Index < C->Count); + + /* Replace the item pointer */ + C->Items[Index] = Item; +} +#else +# define CollReplace(C, Item, Index) \ + (PRECONDITION ((Index) < (C)->Count), \ + (C)->Items[(Index)] = (Item)) +#endif void CollSort (Collection* C, int (*Compare) (void*, const void*, const void*), diff --git a/src/common/inline.h b/src/common/inline.h new file mode 100644 index 000000000..cf8cecd95 --- /dev/null +++ b/src/common/inline.h @@ -0,0 +1,59 @@ +/*****************************************************************************/ +/* */ +/* cfeature.h */ +/* */ +/* Define compiler features */ +/* */ +/* */ +/* */ +/* (C) 2001 Ullrich von Bassewitz */ +/* Wacholderweg 14 */ +/* D-70597 Stuttgart */ +/* EMail: uz@cc65.org */ +/* */ +/* */ +/* This software is provided 'as-is', without any expressed or implied */ +/* warranty. In no event will the authors be held liable for any damages */ +/* arising from the use of this software. */ +/* */ +/* Permission is granted to anyone to use this software for any purpose, */ +/* including commercial applications, and to alter it and redistribute it */ +/* freely, subject to the following restrictions: */ +/* */ +/* 1. The origin of this software must not be misrepresented; you must not */ +/* claim that you wrote the original software. If you use this software */ +/* in a product, an acknowledgment in the product documentation would be */ +/* appreciated but is not required. */ +/* 2. Altered source versions must be plainly marked as such, and must not */ +/* be misrepresented as being the original software. */ +/* 3. This notice may not be removed or altered from any source */ +/* distribution. */ +/* */ +/*****************************************************************************/ + + + +#ifndef CFEATURE_H +#define CFEATURE_H + + + +/*****************************************************************************/ +/* Defines */ +/*****************************************************************************/ + + + +#if defined(__GNUC__) +# define HAVE_INLINE 1 +# define INLINE static __inline__ +#endif + + + +/* End of cfeature.h */ + +#endif + + + -- 2.39.5