Skip to content

Commit

Permalink
MemoryBlock take the array ownership from SimProc
Browse files Browse the repository at this point in the history
to avoid the memory double and copy

Signed-off-by: Xiang Xiao <[email protected]>
  • Loading branch information
xiaoxiang781216 committed Jan 3, 2022
1 parent eb59a03 commit 9cc7539
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
18 changes: 6 additions & 12 deletions src/MemoryBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,18 @@

//! @param[in] _nextBlock pointer to the next memory block (NULL if none)
//! @param[in] _startAddr start address of the region of memory represented.
//! @param[in] numWords Number of words in the array supplied
//! @param[in] wordArray Array of words with which to initialize the memory.
//! @param[in] numBytes Number of bytes in the array supplied
//! @param[in] byteArray Array of bytes with which to initialize the memory.
//-----------------------------------------------------------------------------
MemoryBlock::MemoryBlock (MemoryBlock *_nextBlock,
uint32_t _startAddr,
int numBytes,
uint8_t *byteArray) :
nextBlock (_nextBlock),
startAddr (_startAddr),
byteSize (numBytes)
byteSize (numBytes),
memory (byteArray)
{
memory = new uint8_t [byteSize];

for (int b = 0 ; b < numBytes ; b++ )
{
memory[b] = byteArray[b];
}
} // MemoryBlock ()


Expand All @@ -78,10 +73,9 @@ MemoryBlock::MemoryBlock (MemoryBlock *_nextBlock,
bool targetIsLittleEndianP) :
nextBlock (_nextBlock),
startAddr (_startAddr),
byteSize (numWords * BYTES_PER_WORD)
byteSize (numWords * BYTES_PER_WORD),
memory (reinterpret_cast<uint8_t *>(wordArray))
{
memory = new uint8_t [byteSize];

for (int w = 0 ; w < numWords ; w++ )
{
int b = w * BYTES_PER_WORD;
Expand Down
14 changes: 6 additions & 8 deletions src/SimProc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@ SimProc::parseSpecificMemoryClause (ScannerObject memoryType)
else
{
// Error recovery time. We should never get here, so if we do scan until
// we find either the start ofa new memory clause or EOF.
// we find either the start of a new memory clause or EOF.
parseError ("Memory specification expected, but not found. Skipping.");
skipBlock ();
return;
Expand Down Expand Up @@ -1068,18 +1068,16 @@ SimProc::parseSpecificMemoryClause (ScannerObject memoryType)
if (SO_BYTE == memoryType)
{
uint8_t *byteArray = new uint8_t [byteSize];
parseMemoryValues (memoryType, (int)byteSize, (void *)byteArray);
parseMemoryValues (memoryType, byteSize, (void *)byteArray);
memList = new MemoryBlock (memList, baseAddr, byteSize, byteArray);
delete [] byteArray;
}
else
{
uint32_t wordSize = byteSize / BYTES_PER_WORD;
uint32_t *wordArray = new uint32_t [wordSize];
parseMemoryValues (memoryType, (int)wordSize, (void *)wordArray);
parseMemoryValues (memoryType, wordSize, (void *)wordArray);
memList = new MemoryBlock (memList, baseAddr, wordSize, wordArray,
isLittleEndianP);
delete [] wordArray;
}
}
} // parseSpecificMemoryClause ()
Expand Down Expand Up @@ -1192,13 +1190,13 @@ SimProc::parseMemoryParams (uint32_t &baseAddr,
//-----------------------------------------------------------------------------
void
SimProc::parseMemoryValues (ScannerObject memoryType,
int arraySize,
uint32_t arraySize,
void *memArray)
{
uint8_t *byteArray = (uint8_t *)memArray;
uint32_t *wordArray = (uint32_t *)memArray;

int nextMem = 0;
uint32_t nextMem = 0;

#ifdef PARSE_DEBUG
cout << "-- parseMemoryvalues" << endl;
Expand All @@ -1216,7 +1214,7 @@ SimProc::parseMemoryValues (ScannerObject memoryType,
{
if (SO_BYTE == memoryType)
{
byteArray[nextMem] = (uint32_t)numberLval;
byteArray[nextMem] = (uint8_t)numberLval;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/SimProc.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class SimProc
void parseMemoryParams (uint32_t &baseAddr,
uint32_t &byteSize);
void parseMemoryValues (ScannerObject memoryType,
int arraySize,
uint32_t arraySize,
void *memArray);

// Parser support functions
Expand Down

0 comments on commit 9cc7539

Please sign in to comment.