Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove grow() helper function #7563

Merged
merged 4 commits into from May 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 0 additions & 17 deletions src/core/smallvec_type.hpp
Expand Up @@ -52,21 +52,4 @@ int find_index(std::vector<T> const& vec, T const& item)
return -1;
}

/**
* Helper function to append N default-constructed elements and get a pointer to the first new element
* Consider using std::back_inserter in new code
*
* @param vec A reference to the vector to be extended
* @param num Number of elements to be default-constructed
*
* @return Pointer to the first new element
*/
template <typename T>
T* grow(std::vector<T>& vec, std::size_t num)
{
std::size_t const pos = vec.size();
vec.resize(pos + num);
return vec.data() + pos;
}

#endif /* SMALLVEC_TYPE_HPP */
42 changes: 26 additions & 16 deletions src/music/midifile.cpp
Expand Up @@ -113,7 +113,7 @@ class ByteBuffer {

/**
* Read bytes into a buffer.
* @param[out] dest buffer to copy info
* @param[out] dest buffer to copy into
* @param length number of bytes to read
* @return true if the requested number of bytes were available
*/
Expand All @@ -126,6 +126,21 @@ class ByteBuffer {
return true;
}

/**
* Read bytes into a MidiFile::DataBlock.
* @param[out] dest DataBlock to copy into
* @param length number of bytes to read
* @return true if the requested number of bytes were available
*/
bool ReadDataBlock(MidiFile::DataBlock *dest, size_t length)
{
if (this->IsEnd()) return false;
if (this->buflen - this->pos < length) return false;
dest->data.insert(dest->data.end(), this->buf + this->pos, this->buf + this->pos + length);
this->pos += length;
return true;
}

/**
* Skip over a number of bytes in the buffer.
* @param count number of bytes to skip over
Expand Down Expand Up @@ -208,28 +223,26 @@ static bool ReadTrackChunk(FILE *file, MidiFile &target)
/* Regular channel message */
last_status = status;
running_status:
byte *data;
switch (status & 0xF0) {
case MIDIST_NOTEOFF:
case MIDIST_NOTEON:
case MIDIST_POLYPRESS:
case MIDIST_CONTROLLER:
case MIDIST_PITCHBEND:
/* 3 byte messages */
data = grow(block->data, 3);
data[0] = status;
if (!chunk.ReadBuffer(&data[1], 2)) {
block->data.push_back(status);
if (!chunk.ReadDataBlock(block, 2)) {
return false;
}
break;
case MIDIST_PROGCHG:
case MIDIST_CHANPRESS:
/* 2 byte messages */
data = grow(block->data, 2);
data[0] = status;
if (!chunk.ReadByte(data[1])) {
block->data.push_back(status);
if (!chunk.ReadByte(buf[0])) {
return false;
}
block->data.push_back(buf[0]);
break;
default:
NOT_REACHED();
Expand Down Expand Up @@ -266,12 +279,11 @@ static bool ReadTrackChunk(FILE *file, MidiFile &target)
if (!chunk.ReadVariableLength(length)) {
return false;
}
byte *data = grow(block->data, length + 1);
data[0] = 0xF0;
if (!chunk.ReadBuffer(data + 1, length)) {
block->data.push_back(0xF0);
if (!chunk.ReadDataBlock(block, length)) {
return false;
}
if (data[length] != 0xF7) {
if (block->data.back() != 0xF7) {
/* Engage Casio weirdo mode - convert to normal sysex */
running_sysex = true;
block->data.push_back(0xF7);
Expand All @@ -284,8 +296,7 @@ static bool ReadTrackChunk(FILE *file, MidiFile &target)
if (!chunk.ReadVariableLength(length)) {
return false;
}
byte *data = grow(block->data, length);
if (!chunk.ReadBuffer(data, length)) {
if (!chunk.ReadDataBlock(block, length)) {
return false;
}
} else {
Expand Down Expand Up @@ -335,8 +346,7 @@ static bool FixupMidiData(MidiFile &target)
merged_blocks.push_back(block);
last_ticktime = block.ticktime;
} else {
byte *datadest = grow(merged_blocks.back().data, block.data.size());
memcpy(datadest, block.data.data(), block.data.size());
merged_blocks.back().data.insert(merged_blocks.back().data.end(), block.data.begin(), block.data.end());
}
}
std::swap(merged_blocks, target.blocks);
Expand Down
2 changes: 1 addition & 1 deletion src/network/network_content.cpp
Expand Up @@ -574,7 +574,7 @@ void ClientNetworkContentSocketHandler::OnReceiveData(const char *data, size_t l
if (this->http_response_index == -1) {
if (data != nullptr) {
/* Append the rest of the response. */
memcpy(grow(this->http_response, (uint)length), data, length);
this->http_response.insert(this->http_response.end(), data, data + length);
return;
} else {
/* Make sure the response is properly terminated. */
Expand Down
6 changes: 3 additions & 3 deletions src/newgrf_sound.cpp
Expand Up @@ -32,9 +32,9 @@ static std::vector<SoundEntry> _sounds;
*/
SoundEntry *AllocateSound(uint num)
{
SoundEntry *sound = grow(_sounds, num);
MemSetT(sound, 0, num);
return sound;
size_t pos = _sounds.size();
_sounds.insert(_sounds.end(), num, SoundEntry());
return &_sounds[pos];
}


Expand Down