mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-12 22:20:05 +00:00
Add a portable getpagesize() implementation
This commit is contained in:
parent
1e9ca17b9d
commit
74169e4b5d
13
posix.cc
13
posix.cc
@ -222,3 +222,16 @@ fmt::BufferedFile fmt::File::fdopen(const char *mode) {
|
||||
fd_ = -1;
|
||||
return file;
|
||||
}
|
||||
|
||||
long fmt::getpagesize() {
|
||||
#ifdef _WIN32
|
||||
SYSTEM_INFO si = {};
|
||||
GetSystemInfo(&si);
|
||||
return si.dwPageSize;
|
||||
#else
|
||||
long size = FMT_POSIX_CALL(sysconf(_SC_PAGESIZE));
|
||||
if (size < 0)
|
||||
throw SystemError(errno, "cannot get memory page size");
|
||||
return size;
|
||||
#endif
|
||||
}
|
||||
|
5
posix.h
5
posix.h
@ -320,7 +320,10 @@ class File {
|
||||
// this File object from the file.
|
||||
BufferedFile fdopen(const char *mode);
|
||||
};
|
||||
}
|
||||
|
||||
// Returns the memory page size.
|
||||
long getpagesize();
|
||||
} // namespace fmt
|
||||
|
||||
#if !FMT_USE_RVALUE_REFERENCES
|
||||
namespace std {
|
||||
|
@ -56,6 +56,7 @@ int fileno_count;
|
||||
std::size_t read_nbyte;
|
||||
std::size_t write_nbyte;
|
||||
bool increase_file_size;
|
||||
bool sysconf_error;
|
||||
}
|
||||
|
||||
#define EMULATE_EINTR(func, error_result) \
|
||||
@ -80,6 +81,15 @@ int test::fstat(int fd, struct stat *buf) {
|
||||
buf->st_size = max_file_size();
|
||||
return result;
|
||||
}
|
||||
|
||||
long test::sysconf(int name) {
|
||||
long result = ::sysconf(name);
|
||||
if (!sysconf_error)
|
||||
return result;
|
||||
// Simulate an error.
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
errno_t test::sopen_s(
|
||||
int* pfh, const char *filename, int oflag, int shflag, int pmode) {
|
||||
@ -184,6 +194,20 @@ TEST(UtilTest, StaticAssert) {
|
||||
// a compile-time error.
|
||||
}
|
||||
|
||||
TEST(UtilTest, GetPageSize) {
|
||||
#ifdef _WIN32
|
||||
SYSTEM_INFO si = {};
|
||||
GetSystemInfo(&si);
|
||||
EXPECT_EQ(si.dwPageSize, fmt::getpagesize());
|
||||
#else
|
||||
EXPECT_EQ(sysconf(_SC_PAGESIZE), fmt::getpagesize());
|
||||
sysconf_error = true;
|
||||
EXPECT_SYSTEM_ERROR(
|
||||
fmt::getpagesize(), EINVAL, "cannot get memory page size");
|
||||
sysconf_error = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(FileTest, OpenRetry) {
|
||||
write_file("test", "there must be something here");
|
||||
File *f = 0;
|
||||
|
@ -45,6 +45,7 @@ typedef size_t size_t;
|
||||
typedef ssize_t ssize_t;
|
||||
int open(const char *path, int oflag, int mode);
|
||||
int fstat(int fd, struct stat *buf);
|
||||
long sysconf(int name);
|
||||
#else
|
||||
typedef unsigned size_t;
|
||||
typedef int ssize_t;
|
||||
|
Loading…
Reference in New Issue
Block a user