mirror of
https://github.com/facebook/sapling.git
synced 2024-10-09 16:31:02 +03:00
be7a6b5cb3
Summary: While hacking on some code, I ran into a situation where some zero-initialized stat structs weren't actually being zeroed. This was either a compiler bug or a situation where the build system was not correctly rebuilding everything after my changes, and I did not have enough disassembly available to investigate. Either way, since this code assumes zero bits in some nonobvious ways, explicitly assert they are. Reviewed By: xavierd Differential Revision: D23644819 fbshipit-source-id: eb6bff9ff997379113db1e1bf9d6a0a538f10f0b
43 lines
1011 B
C++
43 lines
1011 B
C++
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This software may be used and distributed according to the terms of the
|
|
* GNU General Public License version 2.
|
|
*/
|
|
|
|
#pragma once
|
|
#include <folly/FBString.h>
|
|
#include <string>
|
|
|
|
namespace facebook::eden {
|
|
|
|
/**
|
|
* Asserts the specified memory consists entirely of zeroes, and aborts the
|
|
* process if not.
|
|
*/
|
|
void assertZeroBits(const void* memory, size_t size);
|
|
|
|
template <typename T>
|
|
void assertZeroBits(const T& value) {
|
|
assertZeroBits(&value, sizeof(T));
|
|
}
|
|
|
|
template <typename StringType>
|
|
bool isStringStorageEmbedded(const StringType& t) {
|
|
const void* tbegin = &t;
|
|
const void* tend = &t + 1;
|
|
return std::less_equal<const void*>{}(tbegin, t.data()) &&
|
|
std::less<const void*>{}(t.data(), tend);
|
|
}
|
|
|
|
template <typename StringType>
|
|
size_t estimateIndirectMemoryUsage(const StringType& s) {
|
|
if (isStringStorageEmbedded(s)) {
|
|
return 0;
|
|
} else {
|
|
return folly::goodMallocSize(s.capacity());
|
|
}
|
|
}
|
|
|
|
} // namespace facebook::eden
|