MCPcopy Create free account
hub / github.com/PlatformLab/NanoLog / readDictionaryFragment

Method readDictionaryFragment

runtime/Log.cc:979–1064  ·  view source on GitHub ↗

* Reads a partial dictionary from the log file and adds it to the global * mapping of log identifiers to static log information. * * \param fd * File descriptor to read the mapping from * \return * true indicates success; false indicates error */

Source from the content-addressed store, hash-verified

977 * true indicates success; false indicates error
978 */
979bool
980Log::Decoder::readDictionaryFragment(FILE *fd) {
981 // These buffers start us off with some statically allocated space.
982 // Should we need more, we will malloc it.
983 size_t bufferSize = 10*1024;
984 char filenameBuffer[bufferSize];
985 char formatBuffer[bufferSize];
986
987 bool newBuffersAllocated = false;
988 char *filename = filenameBuffer;
989 char *format = formatBuffer;
990
991 DictionaryFragment df;
992 size_t bytesRead = fread(&df, 1, sizeof(DictionaryFragment), fd);
993 if (bytesRead != sizeof(DictionaryFragment)) {
994 fprintf(stderr, "Could not read entire dictionary fragment header\r\n");
995 return false;
996 }
997
998 assert(df.entryType == EntryType::LOG_MSGS_OR_DIC);
999
1000 while (bytesRead < df.newMetadataBytes && !feof(fd)) {
1001 CompressedLogInfo cli;
1002 size_t newBytesRead = 0;
1003 newBytesRead += fread(&cli, 1, sizeof(CompressedLogInfo), fd);
1004
1005 if (newBytesRead != sizeof(CompressedLogInfo)) {
1006 fprintf(stderr, "Could not read in log metadata\r\n");
1007 return false;
1008 }
1009
1010 if (bufferSize < cli.filenameLength ||
1011 bufferSize < cli.formatStringLength)
1012 {
1013 if (newBuffersAllocated) {
1014 free(filename);
1015 free(format);
1016 filename = format = nullptr;
1017 }
1018
1019 newBuffersAllocated = true;
1020 bufferSize = 2*std::max(cli.filenameLength, cli.formatStringLength);
1021 filename = static_cast<char*>(malloc(bufferSize));
1022 format = static_cast<char*>(malloc(bufferSize));
1023
1024 if (filename == nullptr || format == nullptr) {
1025 fprintf(stderr, "Internal Error: Could not allocate enough "
1026 "memory to store the filename/format strings "
1027 "in the dictionary. Tried to allocate %lu bytes "
1028 "to store the %u and %u byte filename and "
1029 "format string lengths respectively",
1030 bufferSize,
1031 cli.formatStringLength,
1032 cli.filenameLength);
1033 return false;
1034 }
1035 }
1036

Callers 1

TEST_FFunction · 0.80

Calls

no outgoing calls

Tested by 1

TEST_FFunction · 0.64