Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
Dust1404 committed Aug 10, 2023
1 parent e831933 commit e2a7bc7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 51 deletions.
9 changes: 4 additions & 5 deletions unix/test/tle.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
int main()
{
while (1)
;
return 0;
int main() {
while (1)
;
return 0;
}
15 changes: 4 additions & 11 deletions unix/watcher_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,9 @@ template <typename Ehdr, typename Phdr> 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);
Expand All @@ -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; }
29 changes: 11 additions & 18 deletions unix/watcher_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<mach_header>(fd, sliceFileOffset);
} else if (slice->magic == MH_MAGIC_64) {
staticMemoryUsage = calculateStaticMemoryUsage<mach_header_64>(fd, sliceFileOffset);
} else {
staticMemoryUsage = -1;
}
if (slice->magic == MH_MAGIC) {
staticMemoryUsage = calculateStaticMemoryUsage<mach_header>(fd, sliceFileOffset);
} else if (slice->magic == MH_MAGIC_64) {
staticMemoryUsage = calculateStaticMemoryUsage<mach_header_64>(fd, sliceFileOffset);
} else {
staticMemoryUsage = -1;
}
});

if (rc != 0) {
Expand All @@ -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); }
34 changes: 17 additions & 17 deletions unix/watcher_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand All @@ -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();

Expand Down

0 comments on commit e2a7bc7

Please sign in to comment.