From e2a7bc75ef72c324c2333953333fea6b0878e750 Mon Sep 17 00:00:00 2001 From: Dust1404 <2477259579@qq.com> Date: Thu, 10 Aug 2023 22:22:42 +0800 Subject: [PATCH] format code --- unix/test/tle.c | 9 ++++----- unix/watcher_linux.cpp | 15 ++++----------- unix/watcher_macos.mm | 29 +++++++++++------------------ unix/watcher_unix.cpp | 34 +++++++++++++++++----------------- 4 files changed, 36 insertions(+), 51 deletions(-) diff --git a/unix/test/tle.c b/unix/test/tle.c index 7280c7f..36ac966 100644 --- a/unix/test/tle.c +++ b/unix/test/tle.c @@ -1,6 +1,5 @@ -int main() -{ - while (1) - ; - return 0; +int main() { + while (1) + ; + return 0; } \ No newline at end of file diff --git a/unix/watcher_linux.cpp b/unix/watcher_linux.cpp index 51945ee..cd113c8 100644 --- a/unix/watcher_linux.cpp +++ b/unix/watcher_linux.cpp @@ -68,12 +68,9 @@ template static auto calculateStaticMemoryUsage(i return res; } -void initWatcher() { - return; -} +void initWatcher() { return; } -ssize_t calculateStaticMemoryUsage(const std::string& fileName) -{ +ssize_t calculateStaticMemoryUsage(const std::string &fileName) { char e_ident[EI_NIDENT]; ssize_t staticMemoryUsage = 0; int fd = open(fileName.c_str(), O_RDONLY); @@ -96,10 +93,6 @@ ssize_t calculateStaticMemoryUsage(const std::string& fileName) return staticMemoryUsage; } -ssize_t getMemoryRLimit(ssize_t memoryLimitInMB) { - return memoryLimitInMB * 1024 * 1024; -} +ssize_t getMemoryRLimit(ssize_t memoryLimitInMB) { return memoryLimitInMB * 1024 * 1024; } -size_t getMaxRSSInByte(long ru_maxrss) { - return ru_maxrss * 1024; -} +size_t getMaxRSSInByte(long ru_maxrss) { return ru_maxrss * 1024; } diff --git a/unix/watcher_macos.mm b/unix/watcher_macos.mm index 213f73a..5e44709 100644 --- a/unix/watcher_macos.mm +++ b/unix/watcher_macos.mm @@ -102,12 +102,9 @@ int isAppleSilicon; -void initWatcher() { - isAppleSilicon = getCpuBrandString().find("Apple") != std::string::npos; -} +void initWatcher() { isAppleSilicon = getCpuBrandString().find("Apple") != std::string::npos; } -ssize_t calculateStaticMemoryUsage(const std::string& fileName) -{ +ssize_t calculateStaticMemoryUsage(const std::string &fileName) { uint32_t magic; ssize_t staticMemoryUsage = 0; int fd = open(fileName.c_str(), O_RDONLY); @@ -117,13 +114,13 @@ ssize_t calculateStaticMemoryUsage(const std::string& fileName) int rc = macho_best_slice_in_fd(fd, [&](const mach_header *slice, uint64_t sliceFileOffset, size_t sliceSize) { - if (slice->magic == MH_MAGIC) { - staticMemoryUsage = calculateStaticMemoryUsage(fd, sliceFileOffset); - } else if (slice->magic == MH_MAGIC_64) { - staticMemoryUsage = calculateStaticMemoryUsage(fd, sliceFileOffset); - } else { - staticMemoryUsage = -1; - } + if (slice->magic == MH_MAGIC) { + staticMemoryUsage = calculateStaticMemoryUsage(fd, sliceFileOffset); + } else if (slice->magic == MH_MAGIC_64) { + staticMemoryUsage = calculateStaticMemoryUsage(fd, sliceFileOffset); + } else { + staticMemoryUsage = -1; + } }); if (rc != 0) { @@ -147,10 +144,6 @@ ssize_t calculateStaticMemoryUsage(const std::string& fileName) return staticMemoryUsage; } -ssize_t getMemoryRLimit(ssize_t memoryLimitInMB) { - return memoryLimitInMB * 1024 * (isAppleSilicon ? 4 : 1); -} +ssize_t getMemoryRLimit(ssize_t memoryLimitInMB) { return memoryLimitInMB * 1024 * (isAppleSilicon ? 4 : 1); } -size_t getMaxRSSInByte(long ru_maxrss) { - return ru_maxrss / (isAppleSilicon ? 4 : 1); -} +size_t getMaxRSSInByte(long ru_maxrss) { return ru_maxrss / (isAppleSilicon ? 4 : 1); } diff --git a/unix/watcher_unix.cpp b/unix/watcher_unix.cpp index 5a20bc9..bffa505 100644 --- a/unix/watcher_unix.cpp +++ b/unix/watcher_unix.cpp @@ -27,7 +27,7 @@ void cleanUp(int /*dummy*/) { } extern void initWatcher(); -extern ssize_t calculateStaticMemoryUsage(const std::string&); +extern ssize_t calculateStaticMemoryUsage(const std::string &); extern ssize_t getMemoryRLimit(ssize_t memoryLimitInMB); extern size_t getMaxRSSInByte(long ru_maxrss); @@ -40,7 +40,7 @@ extern size_t getMaxRSSInByte(long ru_maxrss); * argv[6]: 空间限制(MB),若为负数表示无限制 */ auto main(int /*argc*/, char *argv[]) -> int { - initWatcher(); + initWatcher(); int timeLimit = 0; ssize_t memoryLimit = 0; @@ -49,24 +49,24 @@ auto main(int /*argc*/, char *argv[]) -> int { sscanf(argv[6], "%zd", &memoryLimit); /* check static memory usage */ - // 匹配 "" 来解析出文件名 + // 匹配 "" 来解析出文件名 std::string fileName(argv[1]); fileName = fileName.substr(1); fileName = fileName.substr(0, fileName.find("\"")); - // TODO: 处理符号链接 - - if (memoryLimit > 0) { - ssize_t staticMemoryUsage = calculateStaticMemoryUsage(fileName); - if (staticMemoryUsage == -1) { - return 1; - } - if (staticMemoryUsage > memoryLimit * 1024 * 1024) { - printf("0\n%zd\n", staticMemoryUsage); - return 0; - } - } - - memoryLimit = getMemoryRLimit(memoryLimit); + // TODO: 处理符号链接 + + if (memoryLimit > 0) { + ssize_t staticMemoryUsage = calculateStaticMemoryUsage(fileName); + if (staticMemoryUsage == -1) { + return 1; + } + if (staticMemoryUsage > memoryLimit * 1024 * 1024) { + printf("0\n%zd\n", staticMemoryUsage); + return 0; + } + } + + memoryLimit = getMemoryRLimit(memoryLimit); pid = fork();