From: cuz Date: Thu, 11 Oct 2001 21:54:25 +0000 (+0000) Subject: Fixed problems with the inline macros X-Git-Tag: V2.12.0~2555 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=de7da529f05047509119f730d400c9753e3b2332;p=cc65 Fixed problems with the inline macros git-svn-id: svn://svn.cc65.org/cc65/trunk@1040 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- diff --git a/src/common/coll.c b/src/common/coll.c index ffae7f608..a5eaac527 100644 --- a/src/common/coll.c +++ b/src/common/coll.c @@ -116,22 +116,22 @@ void CollInsert (Collection* C, void* Item, unsigned Index) /* Grow the array if necessary */ if (C->Count >= C->Size) { - /* Must grow */ + /* Must grow */ void** NewItems; - if (C->Size > 0) { - C->Size *= 2; - } else { - C->Size = 8; - } - NewItems = xmalloc (C->Size * sizeof (void*)); - memcpy (NewItems, C->Items, C->Count * sizeof (void*)); - xfree (C->Items); - C->Items = NewItems; + if (C->Size > 0) { + C->Size *= 2; + } else { + C->Size = 8; + } + NewItems = xmalloc (C->Size * sizeof (void*)); + memcpy (NewItems, C->Items, C->Count * sizeof (void*)); + xfree (C->Items); + C->Items = NewItems; } /* Move the existing elements if needed */ if (C->Count != Index) { - memmove (C->Items+Index+1, C->Items+Index, (C->Count-Index) * sizeof (void*)); + memmove (C->Items+Index+1, C->Items+Index, (C->Count-Index) * sizeof (void*)); } ++C->Count; @@ -141,6 +141,89 @@ void CollInsert (Collection* C, void* Item, unsigned Index) +#if !defined(HAVE_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); +} +#endif + + + +#if !defined(HAVE_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]; +} +#endif + + + +#if !defined(HAVE_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]; +} +#endif + + + +#if !defined(HAVE_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]; +} +#endif + + + +#if !defined(HAVE_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]; +} +#endif + + + +#if !defined(HAVE_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]; +} +#endif + + + 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. @@ -149,9 +232,9 @@ int CollIndex (Collection* C, const void* Item) /* Linear search */ unsigned I; for (I = 0; I < C->Count; ++I) { - if (Item == C->Items[I]) { - /* Found */ - return (int)I; + if (Item == C->Items[I]) { + /* Found */ + return (int)I; } } @@ -193,6 +276,22 @@ void CollDeleteItem (Collection* C, const void* Item) +#if !defined(HAVE_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 get replaced. + */ +{ + /* Check the index */ + PRECONDITION (Index < C->Count); + + /* Replace the item pointer */ + C->Items[Index] = Item; +} +#endif + + + void CollMove (Collection* C, unsigned OldIndex, unsigned NewIndex) /* Move an item from one position in the collection to another. OldIndex * is the current position of the item, NewIndex is the new index after diff --git a/src/common/coll.h b/src/common/coll.h index 9f5539afc..470c54235 100644 --- a/src/common/coll.h +++ b/src/common/coll.h @@ -111,7 +111,8 @@ INLINE void CollAppend (Collection* C, void* Item) CollInsert (C, Item, C->Count); } #else -# define CollAppend(C, Item) CollInsert (C, Item, (C)->Count) +void CollAppend (Collection* C, void* Item); +/* Append an item to the end of the collection */ #endif #if defined(HAVE_INLINE) @@ -125,9 +126,8 @@ INLINE void* CollAt (Collection* C, unsigned Index) return C->Items[Index]; } #else -# define CollAt(C, Index) \ - (PRECONDITION ((Index) < (C)->Count), \ - (C)->Items[(Index)]) +void* CollAt (Collection* C, unsigned Index); +/* Return the item at the given index */ #endif #if defined(HAVE_INLINE) @@ -152,9 +152,8 @@ INLINE const void* CollConstAt (const Collection* C, unsigned Index) return C->Items[Index]; } #else -# define CollConstAt(C, Index) \ - (PRECONDITION ((Index) < (C)->Count), \ - (C)->Items[(Index)]) +const void* CollConstAt (const Collection* C, unsigned Index); +/* Return the item at the given index */ #endif #if defined(HAVE_INLINE) @@ -168,9 +167,8 @@ INLINE void* CollLast (Collection* C) return C->Items[C->Count-1]; } #else -# define CollLast(C) \ - (PRECONDITION ((C)->Count > 0), \ - (C)->Items[(C)->Count-1]) +void* CollLast (Collection* C); +/* Return the last item in a collection */ #endif #if defined(HAVE_INLINE) @@ -184,9 +182,8 @@ INLINE const void* CollConstLast (const Collection* C) return C->Items[C->Count-1]; } #else -# define CollConstLast(C) \ - (PRECONDITION ((C)->Count > 0), \ - (C)->Items[(C)->Count-1]) +const void* CollConstLast (const Collection* C); +/* Return the last item in a collection */ #endif #if defined(HAVE_INLINE) @@ -202,9 +199,10 @@ INLINE void* CollPop (Collection* C) return C->Items[--C->Count]; } #else -# define CollPop(C) \ - (PRECONDITION ((C)->Count > 0), \ - (C)->Items[--(C)->Count]) +void* CollPop (Collection* C); +/* Remove the last segment from the stack and return it. Calls FAIL if the + * collection is empty. + */ #endif int CollIndex (Collection* C, const void* Item); @@ -239,7 +237,7 @@ INLINE void CollDeleteAll (Collection* C) #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. + * just the pointer will get replaced. */ { /* Check the index */ @@ -249,9 +247,10 @@ INLINE void CollReplace (Collection* C, void* Item, unsigned Index) C->Items[Index] = Item; } #else -# define CollReplace(C, Item, Index) \ - (PRECONDITION ((Index) < (C)->Count), \ - (C)->Items[(Index)] = (Item)) +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 get replaced. + */ #endif void CollMove (Collection* C, unsigned OldIndex, unsigned NewIndex); @@ -280,10 +279,10 @@ void CollSort (Collection* C, -/* End of exprlist.h */ +/* End of coll.h */ #endif - + diff --git a/src/common/strbuf.c b/src/common/strbuf.c index f20b4cdc6..4d13c8645 100644 --- a/src/common/strbuf.c +++ b/src/common/strbuf.c @@ -124,6 +124,17 @@ void SB_Realloc (StrBuf* B, unsigned NewSize) +#if !defined(HAVE_INLINE) +char SB_At (const StrBuf* B, unsigned Index) +/* Get a character from the buffer */ +{ + PRECONDITION (Index < B->Len); + return B->Buf[Index]; +} +#endif + + + void SB_Terminate (StrBuf* B) /* Zero terminate the given string buffer. NOTE: The terminating zero is not * accounted for in B->Len, if you want that, you have to use AppendChar! @@ -150,6 +161,26 @@ void SB_CopyBuf (StrBuf* Target, const char* Buf, unsigned Size) +#if !defined(HAVE_INLINE) +void SB_CopyStr (StrBuf* Target, const char* S) +/* Copy S to Target, discarding the old contents of Target */ +{ + SB_CopyBuf (Target, S, strlen (S)); +} +#endif + + + +#if !defined(HAVE_INLINE) +void SB_Copy (StrBuf* Target, const StrBuf* Source) +/* Copy Source to Target, discarding the old contents of Target */ +{ + SB_CopyBuf (Target, Source->Buf, Source->Len); +} +#endif + + + void SB_AppendChar (StrBuf* B, char C) /* Append a character to a string buffer */ { @@ -176,6 +207,40 @@ void SB_AppendBuf (StrBuf* B, const char* S, unsigned Size) +#if !defined(HAVE_INLINE) +void SB_AppendStr (StrBuf* B, const char* S) +/* Append a string to the end of the string buffer */ +{ + SB_AppendBuf (B, S, strlen (S)); +} +#endif + + + +#if !defined(HAVE_INLINE) +void SB_Append (StrBuf* Target, const StrBuf* Source) +/* Append the contents of Source to Target */ +{ + SB_AppendBuf (Target, Source->Buf, Source->Len); +} +#endif + + + +#if !defined(HAVE_INLINE) +void SB_Cut (StrBuf* B, unsigned Len) +/* Cut the contents of B at the given length. If the current length of the + * buffer is smaller than Len, nothing will happen. + */ +{ + if (Len < B->Len) { + B->Len = Len; + } +} +#endif + + + void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len) /* Copy a slice from Source into Target. The current contents of Target are * destroyed. If Start is greater than the length of Source, or if Len diff --git a/src/common/strbuf.h b/src/common/strbuf.h index 1c92e0fee..fa9b74111 100644 --- a/src/common/strbuf.h +++ b/src/common/strbuf.h @@ -132,9 +132,8 @@ INLINE char SB_At (const StrBuf* B, unsigned Index) return B->Buf[Index]; } #else -# define SB_At(B, Index) \ - (PRECONDITION ((Index) < (B)->Len), \ - (B)->Buf[Index]) +char SB_At (const StrBuf* B, unsigned Index); +/* Get a character from the buffer */ #endif #if defined(HAVE_INLINE) @@ -182,7 +181,8 @@ INLINE void SB_CopyStr (StrBuf* Target, const char* S) SB_CopyBuf (Target, S, strlen (S)); } #else -# define SB_CopyStr(Target, S) SB_CopyBuf (Target, S, strlen (S)) +void SB_CopyStr (StrBuf* Target, const char* S); +/* Copy S to Target, discarding the old contents of Target */ #endif #if defined(HAVE_INLINE) @@ -192,7 +192,8 @@ INLINE void SB_Copy (StrBuf* Target, const StrBuf* Source) SB_CopyBuf (Target, Source->Buf, Source->Len); } #else -# define SB_Copy(Target, Source) SB_CopyBuf (Target, (Source)->Buf, (Source)->Len) +void SB_Copy (StrBuf* Target, const StrBuf* Source); +/* Copy Source to Target, discarding the old contents of Target */ #endif void SB_AppendChar (StrBuf* B, char C); @@ -208,7 +209,8 @@ INLINE void SB_AppendStr (StrBuf* B, const char* S) SB_AppendBuf (B, S, strlen (S)); } #else -# define SB_AppendStr(B, S) SB_AppendBuf (B, S, strlen (S)) +void SB_AppendStr (StrBuf* B, const char* S); +/* Append a string to the end of the string buffer */ #endif #if defined(HAVE_INLINE) @@ -218,7 +220,8 @@ INLINE void SB_Append (StrBuf* Target, const StrBuf* Source) SB_AppendBuf (Target, Source->Buf, Source->Len); } #else -# define SB_Append(Target, Source) SB_AppendBuf (Target, (Source)->Buf, (Source)->Len) +void SB_Append (StrBuf* Target, const StrBuf* Source); +/* Append the contents of Source to Target */ #endif #if defined(HAVE_INLINE) @@ -232,7 +235,10 @@ INLINE void SB_Cut (StrBuf* B, unsigned Len) } } #else -# define SB_Cut(B, L) if ((L) < (B)->Len) { (B)->Len = (L); } +void SB_Cut (StrBuf* B, unsigned Len); +/* Cut the contents of B at the given length. If the current length of the + * buffer is smaller than Len, nothing will happen. + */ #endif void SB_Slice (StrBuf* Target, const StrBuf* Source, unsigned Start, unsigned Len);