disable smatch_containers.c
[smatch.git] / smatch_oom.c
blob0917421ca66a61f5cb40688eaa4fe8ab92679f75
1 /*
2 * sparse/smatch_oom.c
4 * Copyright (C) 2009 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <string.h>
16 int option_oom_kb = 800000; // 800 megs
18 static long get_mem_used()
20 static char filename[64];
21 static char buf[4096];
22 int fd;
23 char *line;
25 if (!filename[0]) {
26 snprintf(filename, 63, "/proc/%d/status", getpid());
29 fd = open(filename, O_RDONLY);
30 if (fd < 0)
31 return -1;
32 if (read(fd, buf, 4096) < 0) {
33 close(fd);
34 return -1;
36 close(fd);
38 line = buf;
39 while ((line = strchr(line, 'V')) > 0) {
40 if (!strncmp(line, "VmSize:", 7))
41 return atol(line + 8);
42 line++;
44 return -1;
47 int out_of_memory()
49 if (get_mem_used() > option_oom_kb)
50 return 1;
51 return 0;