]> git.sur5r.net Git - cc65/blobdiff - src/cc65/litpool.c
Cleanup. Added a few general purpose functions.
[cc65] / src / cc65 / litpool.c
index f57e9441330e4b4ca5d7891fb70ebd5ce2d57bc9..e026db0d49af72c85687d75068e9ef46c7bfa785 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998     Ullrich von Bassewitz                                        */
-/*              Wacholderweg 14                                              */
-/*              D-70597 Stuttgart                                            */
-/* EMail:       uz@musoftware.de                                             */
+/* (C) 1998-2004 Ullrich von Bassewitz                                        */
+/*               Römerstraße 52                                              */
+/*               D-70794 Filderstadt                                         */
+/* EMail:        uz@cc65.org                                                 */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -38,7 +38,6 @@
 /* common */
 #include "check.h"
 #include "tgttrans.h"
-#include "xmalloc.h"
 
 /* cc65 */
 #include "asmlabel.h"
 
 
 /*****************************************************************************/
-/*                                  Data                                    */
+/*                                  Data                                    */
 /*****************************************************************************/
 
 
 
-static unsigned char* LiteralPoolBuf   = 0;    /* Pointer to buffer */
-static unsigned       LiteralPoolSize  = 0;    /* Size of pool */
-static unsigned              LiteralPoolOffs   = 0;    /* Current offset into pool */
 unsigned             LiteralPoolLabel  = 0;    /* Pool asm label */
+static StrBuf         LiteralPool       = STATIC_STRBUF_INITIALIZER;
 
 
 
@@ -82,7 +79,7 @@ void TranslateLiteralPool (unsigned Offs)
  * charset.
  */
 {
-    TgtTranslateBuf (LiteralPoolBuf + Offs, LiteralPoolOffs - Offs);
+    TgtTranslateBuf (SB_GetBuf (&LiteralPool) + Offs, SB_GetLen (&LiteralPool) - Offs);
 }
 
 
@@ -91,12 +88,12 @@ void DumpLiteralPool (void)
 /* Dump the literal pool */
 {
     /* If nothing there, exit... */
-    if (LiteralPoolOffs == 0) {
+    if (SB_GetLen (&LiteralPool) == 0) {
        return;
     }
 
     /* Switch to the data segment */
-    if (WriteableStrings) {
+    if (IS_Get (&WritableStrings)) {
        g_usedata ();
     } else {
                g_userodata ();
@@ -109,7 +106,7 @@ void DumpLiteralPool (void)
     TranslateLiteralPool (0);
 
     /* Output the buffer data */
-    g_defbytes (LiteralPoolBuf, LiteralPoolOffs);
+    g_defbytes (SB_GetConstBuf (&LiteralPool), SB_GetLen (&LiteralPool));
 }
 
 
@@ -117,7 +114,7 @@ void DumpLiteralPool (void)
 unsigned GetLiteralPoolOffs (void)
 /* Return the current offset into the literal pool */
 {
-    return LiteralPoolOffs;
+    return SB_GetLen (&LiteralPool);
 }
 
 
@@ -127,8 +124,8 @@ void ResetLiteralPoolOffs (unsigned Offs)
  * removing values from the pool.
  */
 {
-    CHECK (Offs <= LiteralPoolOffs);
-    LiteralPoolOffs = Offs;
+    CHECK (Offs <= SB_GetLen (&LiteralPool));
+    SB_Cut (&LiteralPool, Offs);
 }
 
 
@@ -136,19 +133,7 @@ void ResetLiteralPoolOffs (unsigned Offs)
 void AddLiteralChar (char C)
 /* Add one character to the literal pool */
 {
-    /* Grow the buffer if needed */
-    if (LiteralPoolOffs >= LiteralPoolSize) {
-       if (LiteralPoolSize == 0) {
-           /* First call */
-           LiteralPoolSize = 256;
-       } else {
-           LiteralPoolSize *= 2;
-       }
-               LiteralPoolBuf = xrealloc (LiteralPoolBuf, LiteralPoolSize);
-    }
-
-    /* Store the character */
-    LiteralPoolBuf[LiteralPoolOffs++] = C;
+    SB_AppendChar (&LiteralPool, C);
 }
 
 
@@ -159,12 +144,10 @@ unsigned AddLiteral (const char* S)
  */
 {
     /* Remember the starting offset */
-    unsigned Start = LiteralPoolOffs;
+    unsigned Start = SB_GetLen (&LiteralPool);
 
     /* Copy the string including the terminator growing the buffer if needed */
-    do {
-       AddLiteralChar (*S);
-    } while (*S++);
+    SB_AppendBuf (&LiteralPool, S, strlen (S) + 1);
 
     /* Return the starting offset */
     return Start;
@@ -175,8 +158,19 @@ unsigned AddLiteral (const char* S)
 const char* GetLiteral (unsigned Offs)
 /* Get a pointer to the literal with the given offset in the pool */
 {
-    CHECK (Offs < LiteralPoolOffs);
-    return (const char*) &LiteralPoolBuf[Offs];
+    CHECK (Offs < SB_GetLen (&LiteralPool));
+    return SB_GetConstBuf (&LiteralPool) + Offs;
+}
+
+
+
+void GetLiteralStrBuf (StrBuf* Target, unsigned Offs)
+/* Copy the string starting at Offs and lasting to the end of the buffer
+ * into Target.
+ */
+{
+    CHECK (Offs <= SB_GetLen (&LiteralPool));
+    SB_Slice (Target, &LiteralPool, Offs, SB_GetLen (&LiteralPool) - Offs);
 }
 
 
@@ -184,7 +178,7 @@ const char* GetLiteral (unsigned Offs)
 void PrintLiteralPoolStats (FILE* F)
 /* Print statistics about the literal space used */
 {
-    fprintf (F, "Literal space used: %u bytes\n", LiteralPoolOffs);
+    fprintf (F, "Literal space used: %u bytes\n", SB_GetLen (&LiteralPool));
 }