Commit b8387c87 by John Kessenich

PP: Non-functional: Remove custom allocator and related improvements.

Removed the preprocesser memory pool. Removed extra copies and unnecessary allocations of objects related to the ones that were using the pool. Replaced some allocated pointers with objects instead, generally using more modern techiques. There end up being fewer memory allocations/deletions to get right. Overall combined effect of all changes is to use slightly less memory and run slightly faster (< 1% for both, but noticable). As part of simplifying the code base, this change makes it easier to see PP symbol tracking, which I suspect has an even bigger run-time simplification to make.
parent bfff871d
...@@ -83,12 +83,11 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ...@@ -83,12 +83,11 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace glslang { namespace glslang {
TPpContext::TPpContext(TParseContextBase& pc, const std::string& rootFileName, TShader::Includer& inclr) : TPpContext::TPpContext(TParseContextBase& pc, const std::string& rootFileName, TShader::Includer& inclr) :
preamble(0), strings(0), parseContext(pc), includer(inclr), inComment(false), preamble(0), strings(0), previous_token('\n'), parseContext(pc), includer(inclr), inComment(false),
rootFileName(rootFileName), rootFileName(rootFileName),
currentSourceFile(rootFileName) currentSourceFile(rootFileName)
{ {
InitAtomTable(); InitAtomTable();
InitScanner();
ifdepth = 0; ifdepth = 0;
for (elsetracker = 0; elsetracker < maxIfNesting; elsetracker++) for (elsetracker = 0; elsetracker < maxIfNesting; elsetracker++)
...@@ -98,9 +97,6 @@ TPpContext::TPpContext(TParseContextBase& pc, const std::string& rootFileName, T ...@@ -98,9 +97,6 @@ TPpContext::TPpContext(TParseContextBase& pc, const std::string& rootFileName, T
TPpContext::~TPpContext() TPpContext::~TPpContext()
{ {
for (TSymbolMap::iterator it = symbols.begin(); it != symbols.end(); ++it)
delete it->second->mac.body;
mem_FreePool(pool);
delete [] preamble; delete [] preamble;
// free up the inputStack // free up the inputStack
......
...@@ -172,39 +172,27 @@ public: ...@@ -172,39 +172,27 @@ public:
size_t current; size_t current;
}; };
struct MemoryPool {
struct chunk *next;
uintptr_t free, end;
size_t chunksize;
uintptr_t alignmask;
};
// //
// From Pp.cpp // From Pp.cpp
// //
struct MacroSymbol { struct MacroSymbol {
MacroSymbol() : argc(0), args(0), body(0), busy(0), undef(0) { } MacroSymbol() : emptyArgs(0), busy(0), undef(0) { }
int argc; TVector<int> args;
int *args; TokenStream body;
TokenStream *body; unsigned emptyArgs : 1;
unsigned busy:1; unsigned busy : 1;
unsigned undef:1; unsigned undef : 1;
};
struct Symbol {
int atom;
MacroSymbol mac;
};
struct SymbolList {
struct SymbolList_Rec *next;
Symbol *symb;
}; };
MemoryPool *pool; typedef TMap<int, MacroSymbol> TSymbolMap;
typedef TMap<int, Symbol*> TSymbolMap; TSymbolMap macroDefs; // map atoms to macro definitions
TSymbolMap symbols; // this has light use... just defined macros MacroSymbol* lookupMacroDef(int atom)
{
auto existingMacroIt = macroDefs.find(atom);
return (existingMacroIt == macroDefs.end()) ? nullptr : &(existingMacroIt->second);
}
void addMacroDef(int atom, MacroSymbol& macroDef) { macroDefs[atom] = macroDef; }
protected: protected:
TPpContext(TPpContext&); TPpContext(TPpContext&);
...@@ -242,7 +230,6 @@ protected: ...@@ -242,7 +230,6 @@ protected:
bool peekPasting() { return !inputStack.empty() && inputStack.back()->peekPasting(); } bool peekPasting() { return !inputStack.empty() && inputStack.back()->peekPasting(); }
bool endOfReplacementList() { return inputStack.empty() || inputStack.back()->endOfReplacementList(); } bool endOfReplacementList() { return inputStack.empty() || inputStack.back()->endOfReplacementList(); }
static const int maxMacroArgs = 64;
static const int maxIfNesting = 64; static const int maxIfNesting = 64;
int ifdepth; // current #if-#else-#endif nesting in the cpp.c file (pre-processor) int ifdepth; // current #if-#else-#endif nesting in the cpp.c file (pre-processor)
...@@ -264,7 +251,7 @@ protected: ...@@ -264,7 +251,7 @@ protected:
virtual int getch() { assert(0); return EndOfInput; } virtual int getch() { assert(0); return EndOfInput; }
virtual void ungetch() { assert(0); } virtual void ungetch() { assert(0); }
bool peekPasting() override { return prepaste; } bool peekPasting() override { return prepaste; }
bool endOfReplacementList() override { return mac->body->current >= mac->body->data.size(); } bool endOfReplacementList() override { return mac->body.current >= mac->body.data.size(); }
MacroSymbol *mac; MacroSymbol *mac;
TVector<TokenStream*> args; TVector<TokenStream*> args;
...@@ -311,7 +298,6 @@ protected: ...@@ -311,7 +298,6 @@ protected:
// Used to obtain #include content. // Used to obtain #include content.
TShader::Includer& includer; TShader::Includer& includer;
int InitCPP();
int CPPdefine(TPpToken * ppToken); int CPPdefine(TPpToken * ppToken);
int CPPundef(TPpToken * ppToken); int CPPundef(TPpToken * ppToken);
int CPPelse(int matchelse, TPpToken * ppToken); int CPPelse(int matchelse, TPpToken * ppToken);
...@@ -327,27 +313,20 @@ protected: ...@@ -327,27 +313,20 @@ protected:
int CPPversion(TPpToken * ppToken); int CPPversion(TPpToken * ppToken);
int CPPextension(TPpToken * ppToken); int CPPextension(TPpToken * ppToken);
int readCPPline(TPpToken * ppToken); int readCPPline(TPpToken * ppToken);
TokenStream* PrescanMacroArg(TokenStream *a, TPpToken * ppToken, bool newLineOkay); TokenStream* PrescanMacroArg(TokenStream&, TPpToken*, bool newLineOkay);
int MacroExpand(int atom, TPpToken* ppToken, bool expandUndef, bool newLineOkay); int MacroExpand(int atom, TPpToken* ppToken, bool expandUndef, bool newLineOkay);
// //
// from PpSymbols.cpp
//
Symbol *NewSymbol(int name);
Symbol *AddSymbol(int atom);
Symbol *LookUpSymbol(int atom);
//
// From PpTokens.cpp // From PpTokens.cpp
// //
void lAddByte(TokenStream *fTok, unsigned char fVal); void lAddByte(TokenStream&, unsigned char fVal);
int lReadByte(TokenStream *pTok); int lReadByte(TokenStream&);
void lUnreadByte(TokenStream *pTok); void lUnreadByte(TokenStream&);
void RecordToken(TokenStream* pTok, int token, TPpToken* ppToken); void RecordToken(TokenStream&, int token, TPpToken* ppToken);
void RewindTokenStream(TokenStream *pTok); void RewindTokenStream(TokenStream&);
int ReadToken(TokenStream* pTok, TPpToken* ppToken); int ReadToken(TokenStream&, TPpToken*);
void pushTokenStreamInput(TokenStream *ts, bool pasting = false); void pushTokenStreamInput(TokenStream&, bool pasting = false);
void UngetToken(int token, TPpToken* ppToken); void UngetToken(int token, TPpToken*);
class tTokenInput : public tInput { class tTokenInput : public tInput {
public: public:
...@@ -357,7 +336,7 @@ protected: ...@@ -357,7 +336,7 @@ protected:
virtual void ungetch() { assert(0); } virtual void ungetch() { assert(0); }
virtual bool peekPasting() override; virtual bool peekPasting() override;
protected: protected:
TokenStream *tokens; TokenStream* tokens;
bool lastTokenPastes; // true if the last token in the input is to be pasted, rather than consumed as a token bool lastTokenPastes; // true if the last token in the input is to be pasted, rather than consumed as a token
}; };
...@@ -535,7 +514,6 @@ protected: ...@@ -535,7 +514,6 @@ protected:
tStringInput stringInput; tStringInput stringInput;
}; };
int InitScanner();
int ScanFromString(char* s); int ScanFromString(char* s);
void missingEndifCheck(); void missingEndifCheck();
int lFloatConst(int len, int ch, TPpToken* ppToken); int lFloatConst(int len, int ch, TPpToken* ppToken);
...@@ -576,14 +554,6 @@ protected: ...@@ -576,14 +554,6 @@ protected:
void AddAtomFixed(const char* s, int atom); void AddAtomFixed(const char* s, int atom);
int LookUpAddString(const char* s); int LookUpAddString(const char* s);
const char* GetAtomString(int atom); const char* GetAtomString(int atom);
//
// From PpMemory.cpp
//
MemoryPool *mem_CreatePool(size_t chunksize, unsigned align);
void mem_FreePool(MemoryPool*);
void *mem_Alloc(MemoryPool* p, size_t size);
int mem_AddCleanup(MemoryPool* p, void (*fn)(void *, void*), void* arg1, void* arg2);
}; };
} // end namespace glslang } // end namespace glslang
......
...@@ -76,86 +76,6 @@ TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF ...@@ -76,86 +76,6 @@ TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\****************************************************************************/ \****************************************************************************/
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include "PpContext.h"
// default alignment and chunksize, if called with 0 arguments
#define CHUNKSIZE (64*1024)
#define ALIGN 8
namespace glslang { namespace glslang {
struct chunk {
struct chunk *next;
};
TPpContext::MemoryPool* TPpContext::mem_CreatePool(size_t chunksize, unsigned int align)
{
if (align == 0)
align = ALIGN;
if (chunksize == 0)
chunksize = CHUNKSIZE;
if (align & (align - 1))
return nullptr;
if (chunksize < sizeof(MemoryPool))
return nullptr;
if (chunksize & (align - 1))
return nullptr;
MemoryPool *pool = (MemoryPool*)malloc(chunksize);
if (! pool)
return nullptr;
pool->next = 0;
pool->chunksize = chunksize;
pool->alignmask = (uintptr_t)(align) - 1;
pool->free = ((uintptr_t)(pool + 1) + pool->alignmask) & ~pool->alignmask;
pool->end = (uintptr_t)pool + chunksize;
return pool;
}
void TPpContext::mem_FreePool(MemoryPool *pool)
{
struct chunk *p, *next;
for (p = (struct chunk *)pool; p; p = next) {
next = p->next;
free(p);
}
}
void* TPpContext::mem_Alloc(MemoryPool *pool, size_t size)
{
struct chunk *ch;
void *rv = (void *)pool->free;
size = (size + pool->alignmask) & ~pool->alignmask;
if (size <= 0) size = pool->alignmask;
pool->free += size;
if (pool->free > pool->end || pool->free < (uintptr_t)rv) {
size_t minreq = (size + sizeof(struct chunk) + pool->alignmask) & ~pool->alignmask;
pool->free = (uintptr_t)rv;
if (minreq >= pool->chunksize) {
// request size is too big for the chunksize, so allocate it as
// a single chunk of the right size
ch = (struct chunk*)malloc(minreq);
if (! ch)
return nullptr;
} else {
ch = (struct chunk*)malloc(pool->chunksize);
if (! ch)
return nullptr;
pool->free = (uintptr_t)ch + minreq;
pool->end = (uintptr_t)ch + pool->chunksize;
}
ch->next = pool->next;
pool->next = ch;
rv = (void *)(((uintptr_t)(ch+1) + pool->alignmask) & ~pool->alignmask);
}
return rv;
}
} // end namespace glslang } // end namespace glslang
...@@ -90,17 +90,6 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ...@@ -90,17 +90,6 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace glslang { namespace glslang {
int TPpContext::InitScanner()
{
// Add various atoms needed by the CPP line scanner:
if (!InitCPP())
return 0;
previous_token = '\n';
return 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// Floating point constants: ///////////////////////////////// /////////////////////////////////// Floating point constants: /////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////
......
...@@ -91,44 +91,4 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ...@@ -91,44 +91,4 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace glslang { namespace glslang {
/*
* Allocate a new symbol node;
*
*/
TPpContext::Symbol* TPpContext::NewSymbol(int atom)
{
Symbol* lSymb;
char* pch;
size_t ii;
lSymb = (Symbol *) mem_Alloc(pool, sizeof(Symbol));
lSymb->atom = atom;
// Clear macro
pch = (char*) &lSymb->mac;
for (ii = 0; ii < sizeof(lSymb->mac); ii++)
*pch++ = 0;
return lSymb;
}
TPpContext::Symbol* TPpContext::AddSymbol(int atom)
{
Symbol *lSymb;
lSymb = NewSymbol(atom);
symbols[lSymb->atom] = lSymb;
return lSymb;
}
TPpContext::Symbol* TPpContext::LookUpSymbol(int atom)
{
TSymbolMap::iterator it = symbols.find(atom);
if (it == symbols.end())
return nullptr;
else
return it->second;
}
} // end namespace glslang } // end namespace glslang
...@@ -95,32 +95,32 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ...@@ -95,32 +95,32 @@ NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace glslang { namespace glslang {
void TPpContext::lAddByte(TokenStream *fTok, unsigned char fVal) void TPpContext::lAddByte(TokenStream& fTok, unsigned char fVal)
{ {
fTok->data.push_back(fVal); fTok.data.push_back(fVal);
} }
/* /*
* Get the next byte from a stream. * Get the next byte from a stream.
*/ */
int TPpContext::lReadByte(TokenStream *pTok) int TPpContext::lReadByte(TokenStream& pTok)
{ {
if (pTok->current < pTok->data.size()) if (pTok.current < pTok.data.size())
return pTok->data[pTok->current++]; return pTok.data[pTok.current++];
else else
return EndOfInput; return EndOfInput;
} }
void TPpContext::lUnreadByte(TokenStream *pTok) void TPpContext::lUnreadByte(TokenStream& pTok)
{ {
if (pTok->current > 0) if (pTok.current > 0)
--pTok->current; --pTok.current;
} }
/* /*
* Add a token to the end of a list for later playback. * Add a token to the end of a list for later playback.
*/ */
void TPpContext::RecordToken(TokenStream *pTok, int token, TPpToken* ppToken) void TPpContext::RecordToken(TokenStream& pTok, int token, TPpToken* ppToken)
{ {
const char* s; const char* s;
char* str = NULL; char* str = NULL;
...@@ -162,15 +162,15 @@ void TPpContext::RecordToken(TokenStream *pTok, int token, TPpToken* ppToken) ...@@ -162,15 +162,15 @@ void TPpContext::RecordToken(TokenStream *pTok, int token, TPpToken* ppToken)
/* /*
* Reset a token stream in preparation for reading. * Reset a token stream in preparation for reading.
*/ */
void TPpContext::RewindTokenStream(TokenStream *pTok) void TPpContext::RewindTokenStream(TokenStream& pTok)
{ {
pTok->current = 0; pTok.current = 0;
} }
/* /*
* Read the next token from a token stream (not the source stream, but stream used to hold a tokenized macro). * Read the next token from a token stream (not the source stream, but stream used to hold a tokenized macro).
*/ */
int TPpContext::ReadToken(TokenStream *pTok, TPpToken *ppToken) int TPpContext::ReadToken(TokenStream& pTok, TPpToken *ppToken)
{ {
char* tokenText = ppToken->name; char* tokenText = ppToken->name;
int ltoken, len; int ltoken, len;
...@@ -183,7 +183,7 @@ int TPpContext::ReadToken(TokenStream *pTok, TPpToken *ppToken) ...@@ -183,7 +183,7 @@ int TPpContext::ReadToken(TokenStream *pTok, TPpToken *ppToken)
switch (ltoken) { switch (ltoken) {
case '#': case '#':
// Check for ##, unless the current # is the last character // Check for ##, unless the current # is the last character
if (pTok->current < pTok->data.size()) { if (pTok.current < pTok.data.size()) {
if (lReadByte(pTok) == '#') { if (lReadByte(pTok) == '#') {
parseContext.requireProfile(ppToken->loc, ~EEsProfile, "token pasting (##)"); parseContext.requireProfile(ppToken->loc, ~EEsProfile, "token pasting (##)");
parseContext.profileRequires(ppToken->loc, ~EEsProfile, 130, 0, "token pasting (##)"); parseContext.profileRequires(ppToken->loc, ~EEsProfile, 130, 0, "token pasting (##)");
...@@ -274,7 +274,7 @@ int TPpContext::ReadToken(TokenStream *pTok, TPpToken *ppToken) ...@@ -274,7 +274,7 @@ int TPpContext::ReadToken(TokenStream *pTok, TPpToken *ppToken)
int TPpContext::tTokenInput::scan(TPpToken* ppToken) int TPpContext::tTokenInput::scan(TPpToken* ppToken)
{ {
return pp->ReadToken(tokens, ppToken); return pp->ReadToken(*tokens, ppToken);
} }
// We are pasting if the entire macro is preceding a pasting operator // We are pasting if the entire macro is preceding a pasting operator
...@@ -289,7 +289,7 @@ bool TPpContext::tTokenInput::peekPasting() ...@@ -289,7 +289,7 @@ bool TPpContext::tTokenInput::peekPasting()
size_t savePos = tokens->current; size_t savePos = tokens->current;
bool moreTokens = false; bool moreTokens = false;
do { do {
int byte = pp->lReadByte(tokens); int byte = pp->lReadByte(*tokens);
if (byte == EndOfInput) if (byte == EndOfInput)
break; break;
if (byte != ' ') { if (byte != ' ') {
...@@ -302,9 +302,9 @@ bool TPpContext::tTokenInput::peekPasting() ...@@ -302,9 +302,9 @@ bool TPpContext::tTokenInput::peekPasting()
return !moreTokens; return !moreTokens;
} }
void TPpContext::pushTokenStreamInput(TokenStream* ts, bool prepasting) void TPpContext::pushTokenStreamInput(TokenStream& ts, bool prepasting)
{ {
pushInput(new tTokenInput(this, ts, prepasting)); pushInput(new tTokenInput(this, &ts, prepasting));
RewindTokenStream(ts); RewindTokenStream(ts);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment