2024-02-11 00:36:54 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2024-02-12 17:35:23 +00:00
|
|
|
#include <math.h>
|
2024-02-12 02:18:29 +00:00
|
|
|
#include <limits.h>
|
2024-02-11 00:36:54 +00:00
|
|
|
|
2024-02-12 17:35:23 +00:00
|
|
|
#define KiB (unsigned long long int) 0x400
|
|
|
|
#define MiB (unsigned long long int) 0x100000
|
|
|
|
#define GiB (unsigned long long int) 0x40000000
|
|
|
|
#define TiB (unsigned long long int) 0x10000000000
|
|
|
|
|
|
|
|
#define LOADS TiB * 9999
|
|
|
|
|
|
|
|
char* get_shorthand(unsigned long long int size) {
|
|
|
|
if (size < KiB) return 0;
|
|
|
|
|
|
|
|
if (size > LOADS) {
|
|
|
|
return ">9999 TiB";
|
|
|
|
}
|
|
|
|
|
|
|
|
char* buffer = malloc(sizeof(char) * 12);
|
|
|
|
|
|
|
|
if (size >= TiB) {
|
|
|
|
snprintf(buffer, 12, "%.2f TiB", (float) size / TiB);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
if (size >= GiB) {
|
|
|
|
snprintf(buffer, 12, "%.2f GiB", (float) size / GiB);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
if (size >= MiB) {
|
|
|
|
snprintf(buffer, 12, "%.2f MiB", (float) size / MiB);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
if (size >= KiB) {
|
|
|
|
snprintf(buffer, 12, "%.2f KiB", (float) size / KiB);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long long int adjust_by_denomination(unsigned long long int size, char denomination) {
|
|
|
|
switch (denomination) {
|
|
|
|
case 'K':
|
|
|
|
return size * KiB;
|
|
|
|
case 'M':
|
|
|
|
return size * MiB;
|
|
|
|
case 'G':
|
|
|
|
return size * GiB;
|
|
|
|
case 'T':
|
|
|
|
return size * TiB;
|
|
|
|
default:
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-12 21:55:39 +00:00
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
|
|
printf("%s\n", argv[i]);
|
|
|
|
}
|
2024-02-11 00:36:54 +00:00
|
|
|
|
2024-02-12 02:14:41 +00:00
|
|
|
unsigned long long int size = 0;
|
|
|
|
|
2024-02-12 17:35:23 +00:00
|
|
|
printf("please enter the amount of memory you would like to allocate (\"1G\" = 1 GiB = 1073741824 bytes):\n> ");
|
2024-02-12 02:14:41 +00:00
|
|
|
unsigned int digit = 0;
|
2024-02-12 17:35:23 +00:00
|
|
|
unsigned char last_char = 0;
|
2024-02-12 02:14:41 +00:00
|
|
|
while (1) {
|
|
|
|
unsigned char input = getchar();
|
|
|
|
if (input == '\n') break;
|
2024-02-12 17:43:19 +00:00
|
|
|
if (input >= 'a' && input <= 'z') input -= 32;
|
2024-02-12 17:35:23 +00:00
|
|
|
last_char = input;
|
|
|
|
|
2024-02-12 02:14:41 +00:00
|
|
|
if (input < '0' || input > '9') continue;
|
2024-02-11 00:36:54 +00:00
|
|
|
|
2024-02-12 17:35:23 +00:00
|
|
|
if (digit > 0) size *= 10;
|
2024-02-11 00:36:54 +00:00
|
|
|
|
2024-02-12 17:35:23 +00:00
|
|
|
size += (unsigned int) input - 48;
|
2024-02-12 02:14:41 +00:00
|
|
|
digit++;
|
|
|
|
}
|
2024-02-11 00:36:54 +00:00
|
|
|
|
2024-02-12 17:35:23 +00:00
|
|
|
size = adjust_by_denomination(size, last_char);
|
|
|
|
|
|
|
|
// even if the user doesn't specify a denomination, it still would be nice to display a truncated amount if available.
|
|
|
|
char* shorthand = get_shorthand(size);
|
2024-02-11 00:36:54 +00:00
|
|
|
|
2024-02-12 17:35:23 +00:00
|
|
|
if (shorthand) {
|
|
|
|
printf("you are about to allocate %s (%llu bytes) of heap memory. are you sure? (y/n)\n> ", shorthand, size);
|
|
|
|
} else {
|
|
|
|
printf("you are about to allocate %llu byte%c of heap memory. are you sure? (y/n)\n> ", size, size == 1 ? 0 : 's');
|
|
|
|
}
|
2024-02-12 02:14:41 +00:00
|
|
|
|
|
|
|
unsigned char input = getchar();
|
|
|
|
if (input != 'y') return 0;
|
|
|
|
while (getchar() != '\n');
|
2024-02-11 00:36:54 +00:00
|
|
|
|
2024-02-12 17:41:49 +00:00
|
|
|
printf("please wait.");
|
2024-02-12 02:14:41 +00:00
|
|
|
fflush(stdout);
|
2024-02-11 00:36:54 +00:00
|
|
|
|
2024-02-12 02:14:41 +00:00
|
|
|
unsigned long long int* wtf = (unsigned long long int*) malloc(size);
|
2024-02-11 00:36:54 +00:00
|
|
|
|
2024-02-12 02:14:41 +00:00
|
|
|
for (unsigned long long int i = 0; i < size / sizeof(long long int); i++) {
|
2024-02-12 21:55:39 +00:00
|
|
|
wtf[i] = i;
|
2024-02-12 02:14:41 +00:00
|
|
|
if (i > 0 && i % (1024 * 1024 * 1024 / sizeof(long long int)) == 0) {
|
|
|
|
printf(".");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
2024-02-11 00:36:54 +00:00
|
|
|
|
2024-02-12 17:35:23 +00:00
|
|
|
if (shorthand) {
|
|
|
|
printf("%s (%llu bytes) of heap memory has been allocated. you are insane.\n", shorthand, size);
|
|
|
|
} else {
|
2024-02-12 17:38:54 +00:00
|
|
|
printf("%llu byte%c of heap memory ha%s been allocated. you are insane.\n", size, size == 1 ? 0 : 's', size == 1 ? "s" : "ve");
|
2024-02-12 17:35:23 +00:00
|
|
|
}
|
|
|
|
free(shorthand);
|
2024-02-12 21:55:39 +00:00
|
|
|
|
|
|
|
printf("would you like to save this abomination to disk? (y/n)\n> ");
|
|
|
|
input = getchar();
|
|
|
|
if (input == 'y') {
|
|
|
|
while (getchar() != '\n');
|
|
|
|
|
|
|
|
printf("please enter a filename (relative to current directory)\n> ");
|
|
|
|
char filename[32];
|
|
|
|
unsigned char filename_length = 0;
|
|
|
|
while (1) {
|
|
|
|
input = getchar();
|
|
|
|
if (input == '\n' || filename_length == 31) {
|
|
|
|
filename[filename_length] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
filename[filename_length] = input;
|
|
|
|
filename_length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("saving to %s.\n", filename);
|
|
|
|
|
|
|
|
FILE* file = fopen(filename, "w");
|
|
|
|
if (file == NULL) {
|
|
|
|
printf("failed to open %s!", filename);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("please wait.");
|
|
|
|
for (unsigned long long int i = 0; i < size / sizeof(long long int); i++) {
|
|
|
|
for (int p = 0; p < 8; p++) {
|
|
|
|
fputc(*(&wtf[i] + p), file);
|
|
|
|
}
|
|
|
|
if (i > 0 && i % (1024 * 1024 * 1024 / sizeof(long long int)) == 0) {
|
|
|
|
printf(".");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
fputs("\n", file);
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
printf("done!\n");
|
|
|
|
} else {
|
|
|
|
while (getchar() != '\n');
|
|
|
|
}
|
|
|
|
|
2024-02-12 02:14:41 +00:00
|
|
|
printf("press ENTER to release this memory, or CTRL+C to exit the program.\n");
|
2024-02-11 00:36:54 +00:00
|
|
|
|
2024-02-12 02:14:41 +00:00
|
|
|
getchar();
|
2024-02-11 00:36:54 +00:00
|
|
|
|
2024-02-12 02:14:41 +00:00
|
|
|
free(wtf);
|
2024-02-11 00:36:54 +00:00
|
|
|
|
2024-02-12 02:14:41 +00:00
|
|
|
return 0;
|
2024-02-11 00:36:54 +00:00
|
|
|
|
|
|
|
}
|