X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fcc65%2Flitpool.c;h=e5bc9b737a29ca5bb652a02d6379a76057ac2315;hb=4a667ead00c10797fb57298b3dd33c2efc6c8d3f;hp=e47ae2c9483f7a8a34c082feb756a3c1e4ca003e;hpb=2eab65ad24b672cf72499ad6002c05a3a173c17a;p=cc65 diff --git a/src/cc65/litpool.c b/src/cc65/litpool.c index e47ae2c94..e5bc9b737 100644 --- a/src/cc65/litpool.c +++ b/src/cc65/litpool.c @@ -38,7 +38,6 @@ /* common */ #include "check.h" #include "tgttrans.h" -#include "xmalloc.h" /* cc65 */ #include "asmlabel.h" @@ -50,15 +49,13 @@ /*****************************************************************************/ -/* 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; @@ -72,7 +69,7 @@ void InitLiteralPool (void) /* Initialize the literal pool */ { /* Get the pool label */ - LiteralPoolLabel = GetLabel (); + LiteralPoolLabel = GetLocalLabel (); } @@ -82,7 +79,7 @@ void TranslateLiteralPool (unsigned Offs) * charset. */ { - TgtTranslateBuf (LiteralPoolBuf + Offs, LiteralPoolOffs - Offs); + TgtTranslateBuf (SB_GetBuf (&LiteralPool) + Offs, SB_GetLen (&LiteralPool) - Offs); } @@ -91,7 +88,7 @@ void DumpLiteralPool (void) /* Dump the literal pool */ { /* If nothing there, exit... */ - if (LiteralPoolOffs == 0) { + if (SB_GetLen (&LiteralPool) == 0) { return; } @@ -103,16 +100,13 @@ void DumpLiteralPool (void) } /* Define the label */ - g_defloclabel (LiteralPoolLabel); + g_defdatalabel (LiteralPoolLabel); /* Translate the buffer contents into the target charset */ TranslateLiteralPool (0); /* Output the buffer data */ - g_defbytes (LiteralPoolBuf, LiteralPoolOffs); - - /* Switch back to the code segment */ - g_usecode (); + g_defbytes (SB_GetConstBuf (&LiteralPool), SB_GetLen (&LiteralPool)); } @@ -120,7 +114,7 @@ void DumpLiteralPool (void) unsigned GetLiteralPoolOffs (void) /* Return the current offset into the literal pool */ { - return LiteralPoolOffs; + return SB_GetLen (&LiteralPool); } @@ -130,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); } @@ -139,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); } @@ -162,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; @@ -178,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); } @@ -187,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)); }