Skip to content

Commit

Permalink
Use calloc for anonymous mapping on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenneth Heafield committed Jan 20, 2012
1 parent 60c7551 commit b162dd9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
4 changes: 2 additions & 2 deletions lm/binary_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ uint8_t *SetupJustVocab(const Config &config, uint8_t order, std::size_t memory_
strncpy(reinterpret_cast<char*>(backing.vocab.get()), kMagicIncomplete, TotalHeaderSize(order));
return reinterpret_cast<uint8_t*>(backing.vocab.get()) + TotalHeaderSize(order);
} else {
backing.vocab.reset(util::MapAnonymous(memory_size), memory_size, util::scoped_memory::MMAP_ALLOCATED);
util::MapAnonymous(memory_size, backing.vocab);
return reinterpret_cast<uint8_t*>(backing.vocab.get());
}
}
Expand All @@ -110,7 +110,7 @@ uint8_t *GrowForSearch(const Config &config, std::size_t vocab_pad, std::size_t

return reinterpret_cast<uint8_t*>(backing.search.get()) + alignment_cruft;
} else {
backing.search.reset(util::MapAnonymous(memory_size), memory_size, util::scoped_memory::MMAP_ALLOCATED);
util::MapAnonymous(memory_size, backing.search);
return reinterpret_cast<uint8_t*>(backing.search.get());
}
}
Expand Down
15 changes: 6 additions & 9 deletions util/mmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,19 @@ void MapRead(LoadMethod method, int fd, uint64_t offset, std::size_t size, scope
}
}

void *MapAnonymous(std::size_t size) {
// Allocates zeroed memory in to.
void MapAnonymous(std::size_t size, util::scoped_memory &to) {
to.reset();
#if defined(_WIN32) || defined(_WIN64)
HANDLE hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, size >> 32, static_cast<DWORD>(size), NULL);
UTIL_THROW_IF(!hMapping, ErrnoException, "Anonymous CreateFileMapping failed for size" << size);
LPVOID ret = MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, size);
CloseHandle(hMapping);
UTIL_THROW_IF(!ret, ErrnoException, "MapViewOfFile failed");
return ret;
to.reset(calloc(1, size), size, scoped_memory::MALLOC_ALLOCATED);
#else
return MapOrThrow(size, true,
to.reset(MapOrThrow(size, true,
# if defined(MAP_ANONYMOUS)
MAP_ANONYMOUS | MAP_PRIVATE // Linux
# else
MAP_ANON | MAP_PRIVATE // BSD
# endif
, false, -1, 0);
, false, -1, 0), size, scoped_memory::MMAP_ALLOCATED);
#endif
}

Expand Down
2 changes: 1 addition & 1 deletion util/mmap.hh
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void *MapOrThrow(std::size_t size, bool for_write, int flags, bool prefault, int

void MapRead(LoadMethod method, int fd, uint64_t offset, std::size_t size, scoped_memory &out);

void *MapAnonymous(std::size_t size);
void MapAnonymous(std::size_t size, scoped_memory &to);

// Open file name with mmap of size bytes, all of which are initially zero.
void *MapZeroedWrite(int fd, std::size_t size);
Expand Down

0 comments on commit b162dd9

Please sign in to comment.