xref: /aosp_15_r20/external/toybox/toys/other/memeater.c (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1 /* memeater.c - consume the specified amount of memory
2  *
3  * Copyright 2024 The Android Open Source Project
4 
5 USE_MEMEATER(NEWTOY(memeater, "<1>1M", TOYFLAG_USR|TOYFLAG_BIN))
6 
7 config MEMEATER
8   bool "memeater"
9   default y
10   help
11     usage: memeater [-M] BYTES
12 
13     Consume the specified amount of memory and wait to be killed.
14 
15     -M	Don't mlock() the memory (let it swap out).
16 */
17 
18 #define FOR_memeater
19 #include "toys.h"
20 
memeater_main(void)21 void memeater_main(void)
22 {
23   unsigned long size = atolx_range(*toys.optargs, 0, LONG_MAX), i,
24     *p = xmalloc(size);
25 
26   // Lock and dirty the physical pages.
27   if (!FLAG(M) && mlock(p, size)) perror_exit("mlock");
28   for (i = 0; i<size; i += 4096) p[i/sizeof(long)] = i;
29 
30   while (1) pause();
31 }
32