Skip to content

Commit

Permalink
main: Don't strdup the inputFileName when storing a tag to the corkQueue
Browse files Browse the repository at this point in the history
When storing a tag entry to the corkQueue, the most of members
of the tag entry are duplicated for isolating the the stored tag
entry from the original parsing context.

In the most of all cases, we can expect all tag entries in the
corkQueue has the same value as inputFileName member. Strdup'ing
the value is waste of memory resource especially when the input
file name is long.

Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed Mar 28, 2023
1 parent 95f44c5 commit 9fd7417
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
28 changes: 24 additions & 4 deletions main/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -1059,11 +1059,14 @@ static tagEntryInfo *newNilTagEntry (unsigned int corkFlags)
x->corkIndex = CORK_NIL;
x->symtab = RB_ROOT;
x->slot.kindIndex = KIND_FILE_INDEX;
x->slot.inputFileName = getInputFileName ();
x->slot.inputFileName = eStrdup (x->slot.inputFileName);
return &(x->slot);
}

static tagEntryInfoX *copyTagEntry (const tagEntryInfo *const tag,
unsigned int corkFlags)
const char *shareInputFileName,
unsigned int corkFlags)
{
tagEntryInfoX *x = xMalloc (1, tagEntryInfoX);
x->symtab = RB_ROOT;
Expand All @@ -1075,7 +1078,17 @@ static tagEntryInfoX *copyTagEntry (const tagEntryInfo *const tag,
if (slot->pattern)
slot->pattern = eStrdup (slot->pattern);

slot->inputFileName = eStrdup (slot->inputFileName);
if (slot->inputFileName == getInputFileName ())
{
slot->inputFileName = shareInputFileName;
slot->isInputFileNameShared = 1;
}
else
{
slot->inputFileName = eStrdup (slot->inputFileName);
slot->isInputFileNameShared = 0;
}

slot->name = eStrdup (slot->name);
if (slot->extensionFields.access)
slot->extensionFields.access = eStrdup (slot->extensionFields.access);
Expand Down Expand Up @@ -1146,11 +1159,17 @@ static void deleteTagEnry (void *data)
tagEntryInfo *slot = data;

if (slot->kindIndex == KIND_FILE_INDEX)
{
eFree ((char *)slot->inputFileName);
goto out;
}

if (slot->pattern)
eFree ((char *)slot->pattern);
eFree ((char *)slot->inputFileName);

if (!slot->isInputFileNameShared)
eFree ((char *)slot->inputFileName);

eFree ((char *)slot->name);

if (slot->extensionFields.access)
Expand Down Expand Up @@ -1515,7 +1534,8 @@ static int queueTagEntry (const tagEntryInfo *const tag)
static bool warned;

int corkIndex;
tagEntryInfoX * entry = copyTagEntry (tag,
tagEntryInfo * nil = ptrArrayItem (TagFile.corkQueue, 0);
tagEntryInfoX * entry = copyTagEntry (tag, nil->inputFileName,
TagFile.corkFlags);

if (ptrArrayCount (TagFile.corkQueue) == (size_t)INT_MAX)
Expand Down
12 changes: 11 additions & 1 deletion main/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,24 @@ struct sTagEntryInfo {
unsigned int isPseudoTag:1; /* Used only in xref output.
If a tag is a pseudo, set this. */
unsigned int inCorkQueue:1;
unsigned int isInputFileNameShared: 1; /* shares the value for inputFileName.
* Set in the cork queue; don't touch this.*/

unsigned long lineNumber; /* line number of tag */
const char* pattern; /* pattern for locating input line
* (may be NULL if not present) *//* */
unsigned int boundaryInfo; /* info about nested input stream */
MIOPos filePosition; /* file position of line containing tag */
langType langType; /* language of input file */
const char *inputFileName; /* name of input file */
const char *inputFileName; /* name of input file.
You cannot modify the contents of buffer pointed
by this member of the tagEntryInfo returned from
getEntryInCorkQueue(). The buffer may be shared
between tag entries in the cork queue.
Further more, modifying this member of the
tagEntryInfo returned from getEntryInCorkQueue()
may cause a memory leak. */
const char *name; /* name of the tag */
int kindIndex; /* kind descriptor */
uint8_t extra[ ((XTAG_COUNT) / 8) + 1 ];
Expand Down

0 comments on commit 9fd7417

Please sign in to comment.