summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile1
-rw-r--r--memeat.120
-rw-r--r--memeat.c24
4 files changed, 46 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..662aa9b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+memeat
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..56482fb
--- /dev/null
+++ b/Makefile
@@ -0,0 +1 @@
+memeat:
diff --git a/memeat.1 b/memeat.1
new file mode 100644
index 0000000..acdf7cd
--- /dev/null
+++ b/memeat.1
@@ -0,0 +1,20 @@
+.TH memeat 1 "2021-10-08"
+.SH NAME
+memeat \- allocates memory until killed
+.SH SYNOPSIS
+.SY memeat
+.RI [ maximum ]
+.YS
+.SH DESCRIPTION
+This program will allocate all available memory mebibyte by mebibyte over some time. If optional
+.I maximum
+is provided, then program will terminate after reaching this amount of allocated memory in mebibytes. In case this
+value is invalid or does not make sense, then program will not attempt to consume the memory, and will indicate a
+failure in the exit status.
+.SH EXIT STATUS
+Program is expected to be terminated externally, so exit status may depend on the execution environment. If
+.I maximum
+is specified, then 0 will be returned after reaching the boundary, or 1 will be returned if the provided argument
+could not be understood.
+.SH SEE ALSO
+.BR free (3)
diff --git a/memeat.c b/memeat.c
new file mode 100644
index 0000000..dc5ee7a
--- /dev/null
+++ b/memeat.c
@@ -0,0 +1,24 @@
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+
+static const int MEBIBYTE = 1024 * 1024;
+
+
+int
+main(int argc, char* argv[])
+{
+ const int max = argc > 1 ? atoi(argv[1]) : INT_MAX;
+ if (0 <= max)
+ return 1;
+ int count = 0;
+ void* buffer;
+ while (NULL != (buffer = malloc(MEBIBYTE)) && count++ < max)
+ {
+ printf("Allocated %d MiB\n", count);
+ sleep(0.5);
+ }
+}