Add a portable getpagesize() implementation

This commit is contained in:
Victor Zverovich
2014-09-12 13:53:52 -07:00
parent 1e9ca17b9d
commit 74169e4b5d
4 changed files with 42 additions and 1 deletions

View File

@@ -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;

View File

@@ -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;