]> git.ipfire.org Git - pakfire.git/commitdiff
tests: command: All option to exhaust all memory
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 21 Jul 2022 10:12:59 +0000 (10:12 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 21 Jul 2022 10:12:59 +0000 (10:12 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/stub/command.c

index 74b1384642301f5e37f3f9b9d2c0800172a15f02..c6c54b26ec98f9321feea6d7235bdc730f038ac1 100644 (file)
@@ -65,6 +65,23 @@ static int echo_environ(int argc, char* argv[]) {
        return 0;
 }
 
+static int exhaust_memory(int argc, char* argv[]) {
+       const size_t block = 1048576; // 1 MiB
+       size_t bytes = 0;
+
+       while (1) {
+               char* p = malloc(block);
+               if (!p)
+                       break;
+
+               bytes += block;
+       }
+
+       printf("Successfully allocated %f MiB\n", (double)bytes / 1048576);
+
+       return 0;
+}
+
 static int exit_with_code(int argc, char* argv[]) {
        if (argc < 1) {
                fprintf(stderr, "exit-with-code requires an argument\n");
@@ -129,6 +146,10 @@ int main(int argc, char* argv[]) {
        else if (strcmp(command, "echo-environ") == 0)
                callback = echo_environ;
 
+       // Exhaust memory
+       else if (strcmp(command, "exhaust-memory") == 0)
+               callback = exhaust_memory;
+
        // Exit with code
        else if (strcmp(command, "exit-with-code") == 0)
                callback = exit_with_code;