From 247a66cad18cee22ae5d069ffd9c5791edc090eb Mon Sep 17 00:00:00 2001 From: ari melody Date: Mon, 12 Feb 2024 01:40:58 +0000 Subject: [PATCH 01/15] updated readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c58f4e5..54e05a2 100644 --- a/README.md +++ b/README.md @@ -5,18 +5,18 @@ it allocates memory *(in gibibytes (GiB))* ## how to use - run the executable -- enter how many gibibytes you want (max is how many gibibytes of memory you have) +- enter how many gibibytes you want (max is how many gibibytes of memory you have...or more!) - confirm (y) - wait for the program to allocate the memory - your memory is allocated -- press any key or CTRL+C to free the memory and exit the program +- press ENTER or CTRL+C to free the memory and exit the program - ??? - profit ## how to compile - get a c compiler (i use gcc) -- `gcc -o ./allocatememory.exe ./main.c` +- `gcc -o ./allocatememory ./main.c` - ??? - profit From a258b4bbea7b955321ad124aa5f2708e71fe5fc7 Mon Sep 17 00:00:00 2001 From: ari melody Date: Mon, 12 Feb 2024 02:14:41 +0000 Subject: [PATCH 02/15] flush console to ensure progress output --- main.c | 71 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/main.c b/main.c index 3353cae..960eb30 100644 --- a/main.c +++ b/main.c @@ -3,46 +3,51 @@ int main() { - unsigned long long int size = 0; - unsigned int gib = 0; - - printf("please enter the number of gibibytes (GiB) you would like to allocate (1 GiB = 1073741824 bytes):\n> "); - unsigned int digit = 0; - while (1) { + unsigned long long int size = 0; + unsigned int gib = 0; + + printf("please enter the number of gibibytes (GiB) you would like to allocate (1 GiB = 1073741824 bytes):\n> "); + unsigned int digit = 0; + while (1) { + unsigned char input = getchar(); + if (input == '\n') break; + if (input < '0' || input > '9') continue; + + if (digit > 0) gib = gib * 10; + + gib = gib + (unsigned int) input - 48; + digit++; + } + + size = (unsigned long long int) gib * 1024 * 1024 * 1024; + + printf("you are about to allocate %d GiB (%llu bytes) of heap memory. are you sure? (y/n)\n> ", gib, size); + unsigned char input = getchar(); - if (input == '\n') break; - if (input < '0' || input > '9') continue; + if (input != 'y') return 0; + while (getchar() != '\n'); - if (digit > 0) gib = gib * 10; + printf("please wait"); + fflush(stdout); - gib = gib + (unsigned int) input - 48; - digit++; - } + unsigned long long int* wtf = (unsigned long long int*) malloc(size); - size = (unsigned long long int) gib * 1024 * 1024 * 1024; + for (unsigned long long int i = 0; i < size / sizeof(long long int); i++) { + wtf[i] = 0x80085; + if (i > 0 && i % (1024 * 1024 * 1024 / sizeof(long long int)) == 0) { + printf("."); + fflush(stdout); + } + } + printf("\n"); - printf("you are about to allocate %d GiB (%llu bytes) of heap memory. are you sure? (y/n)\n> ", gib, size); + printf("%d GiB (%llu bytes) of heap memory has been allocated. you are insane.\n", gib, size); + printf("press ENTER to release this memory, or CTRL+C to exit the program.\n"); - unsigned char input = getchar(); - if (input != 'y') return 0; - while (getchar() != '\n'); + getchar(); - printf("please wait"); - unsigned long long int* wtf = (unsigned long long int*) malloc(size); + free(wtf); - for (unsigned long long int i = 0; i < size / sizeof(long long int); i++) { - wtf[i] = 0x80085; - if (i > 0 && i % (1024 * 1024 * 1024 / sizeof(long long int)) == 0) printf("."); - } - printf("\n"); - - printf("%d GiB (%llu bytes) of heap memory has been allocated. you are insane.\n", gib, size); - printf("press ENTER to release this memory, or CTRL+C to exit the program.\n"); - - getchar(); - - free(wtf); - - return 0; + return 0; } From aeb84dc19a6537b3e23d975efa2cd51f9a7f78a8 Mon Sep 17 00:00:00 2001 From: ari melody Date: Mon, 12 Feb 2024 02:18:29 +0000 Subject: [PATCH 03/15] use unsigned long long int to prevent OS memory optimisation :3 --- main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 960eb30..a23db0f 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,6 @@ #include #include +#include int main() { @@ -33,7 +34,7 @@ int main() { unsigned long long int* wtf = (unsigned long long int*) malloc(size); for (unsigned long long int i = 0; i < size / sizeof(long long int); i++) { - wtf[i] = 0x80085; + wtf[i] = ULLONG_MAX; if (i > 0 && i % (1024 * 1024 * 1024 / sizeof(long long int)) == 0) { printf("."); fflush(stdout); From 358e1ea2ffd9bdc04e5cd94a0c7935f7caf0c800 Mon Sep 17 00:00:00 2001 From: ari melody Date: Mon, 12 Feb 2024 17:35:23 +0000 Subject: [PATCH 04/15] allow for all denominations up to TiB --- main.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 72 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index a23db0f..5825165 100644 --- a/main.c +++ b/main.c @@ -1,28 +1,88 @@ #include #include +#include +#include #include +#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; + } +} + int main() { unsigned long long int size = 0; - unsigned int gib = 0; - printf("please enter the number of gibibytes (GiB) you would like to allocate (1 GiB = 1073741824 bytes):\n> "); + printf("please enter the amount of memory you would like to allocate (\"1G\" = 1 GiB = 1073741824 bytes):\n> "); unsigned int digit = 0; + unsigned char last_char = 0; while (1) { unsigned char input = getchar(); if (input == '\n') break; + last_char = input; + if (input < '0' || input > '9') continue; - if (digit > 0) gib = gib * 10; + if (digit > 0) size *= 10; - gib = gib + (unsigned int) input - 48; + size += (unsigned int) input - 48; digit++; } - size = (unsigned long long int) gib * 1024 * 1024 * 1024; + size = adjust_by_denomination(size, last_char); - printf("you are about to allocate %d GiB (%llu bytes) of heap memory. are you sure? (y/n)\n> ", gib, size); + // 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); + + 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'); + } unsigned char input = getchar(); if (input != 'y') return 0; @@ -42,7 +102,12 @@ int main() { } printf("\n"); - printf("%d GiB (%llu bytes) of heap memory has been allocated. you are insane.\n", gib, size); + if (shorthand) { + printf("%s (%llu bytes) of heap memory has been allocated. you are insane.\n", shorthand, size); + } else { + printf("%llu byte%c of heap memory have been allocated. you are insane.\n", size, size == 1 ? 0 : 's'); + } + free(shorthand); printf("press ENTER to release this memory, or CTRL+C to exit the program.\n"); getchar(); From a8f3cccbd4e436cb71c38885da265c5861521357 Mon Sep 17 00:00:00 2001 From: ari melody Date: Mon, 12 Feb 2024 17:38:54 +0000 Subject: [PATCH 05/15] fix grammatical error when allocating 1 byte --- main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.c b/main.c index 5825165..f640d29 100644 --- a/main.c +++ b/main.c @@ -105,7 +105,7 @@ int main() { if (shorthand) { printf("%s (%llu bytes) of heap memory has been allocated. you are insane.\n", shorthand, size); } else { - printf("%llu byte%c of heap memory have been allocated. you are insane.\n", size, size == 1 ? 0 : 's'); + printf("%llu byte%c of heap memory ha%s been allocated. you are insane.\n", size, size == 1 ? 0 : 's', size == 1 ? "s" : "ve"); } free(shorthand); printf("press ENTER to release this memory, or CTRL+C to exit the program.\n"); From f127a1a249670172e81e03ed2ec60ba01db4dc9e Mon Sep 17 00:00:00 2001 From: ari melody Date: Mon, 12 Feb 2024 17:41:49 +0000 Subject: [PATCH 06/15] fix progress indicator not showing period for all GiB allocated --- main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.c b/main.c index f640d29..63568ee 100644 --- a/main.c +++ b/main.c @@ -88,7 +88,7 @@ int main() { if (input != 'y') return 0; while (getchar() != '\n'); - printf("please wait"); + printf("please wait."); fflush(stdout); unsigned long long int* wtf = (unsigned long long int*) malloc(size); From 431c198138f25df9a468781273df1d0cf82c5546 Mon Sep 17 00:00:00 2001 From: ari melody Date: Mon, 12 Feb 2024 17:43:19 +0000 Subject: [PATCH 07/15] shift lowercase input chars to uppercase --- main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/main.c b/main.c index 63568ee..f5e8101 100644 --- a/main.c +++ b/main.c @@ -63,6 +63,7 @@ int main() { while (1) { unsigned char input = getchar(); if (input == '\n') break; + if (input >= 'a' && input <= 'z') input -= 32; last_char = input; if (input < '0' || input > '9') continue; From 62044194078a0059df0528857c9db8aea51e6f6e Mon Sep 17 00:00:00 2001 From: ari melody Date: Mon, 12 Feb 2024 17:47:33 +0000 Subject: [PATCH 08/15] update readme --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 54e05a2..fc17ff6 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,13 @@ # memory allocator -it allocates memory *(in gibibytes (GiB))* +it allocates memory ## how to use - run the executable -- enter how many gibibytes you want (max is how many gibibytes of memory you have...or more!) +- enter how much memory you want to allocate + - i.e. "1g" = 1 GiB = 1073741824 bytes + - the limit is how much memory you have...or more. your funeral. - confirm (y) - wait for the program to allocate the memory - your memory is allocated @@ -16,7 +18,7 @@ it allocates memory *(in gibibytes (GiB))* ## how to compile - get a c compiler (i use gcc) -- `gcc -o ./allocatememory ./main.c` +- `gcc -o ./allocatememory -O2 ./main.c` - ??? - profit From 0bebd05e0e84b7fa81b732ac1877f17e085c6b93 Mon Sep 17 00:00:00 2001 From: ari melody Date: Mon, 12 Feb 2024 17:49:50 +0000 Subject: [PATCH 09/15] redundant return to appease the compiler. all hail mighty compiler --- main.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.c b/main.c index f5e8101..90f3eb0 100644 --- a/main.c +++ b/main.c @@ -36,6 +36,8 @@ char* get_shorthand(unsigned long long int size) { snprintf(buffer, 12, "%.2f KiB", (float) size / KiB); return buffer; } + + return 0; } unsigned long long int adjust_by_denomination(unsigned long long int size, char denomination) { From de270552cb66d1d0f38959c160001bd226bf4708 Mon Sep 17 00:00:00 2001 From: ari melody Date: Mon, 12 Feb 2024 21:55:39 +0000 Subject: [PATCH 10/15] option to save memory to file --- main.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/main.c b/main.c index f5e8101..7ae5970 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #define KiB (unsigned long long int) 0x400 @@ -53,7 +52,11 @@ unsigned long long int adjust_by_denomination(unsigned long long int size, char } } -int main() { +int main(int argc, char* argv[]) { + + for (int i = 0; i < argc; i++) { + printf("%s\n", argv[i]); + } unsigned long long int size = 0; @@ -95,7 +98,7 @@ int main() { unsigned long long int* wtf = (unsigned long long int*) malloc(size); for (unsigned long long int i = 0; i < size / sizeof(long long int); i++) { - wtf[i] = ULLONG_MAX; + wtf[i] = i; if (i > 0 && i % (1024 * 1024 * 1024 / sizeof(long long int)) == 0) { printf("."); fflush(stdout); @@ -109,6 +112,52 @@ int main() { printf("%llu byte%c of heap memory ha%s been allocated. you are insane.\n", size, size == 1 ? 0 : 's', size == 1 ? "s" : "ve"); } free(shorthand); + + 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'); + } + printf("press ENTER to release this memory, or CTRL+C to exit the program.\n"); getchar(); From ca9d63c3ebada5822a1c71d4c6c8cb455675ba06 Mon Sep 17 00:00:00 2001 From: ari melody Date: Tue, 13 Feb 2024 02:41:49 +0000 Subject: [PATCH 11/15] cli args in place of stdin and code separation --- main.c | 287 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 172 insertions(+), 115 deletions(-) diff --git a/main.c b/main.c index 7ae5970..de56995 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,15 @@ #include #include #include -#include +#include +#include +#include +#include + +#define BYTES_GIVEN 1 +#define FILENAME_GIVEN 2 +#define SILENT 4 +#define BYPASS_WARN 8 #define KiB (unsigned long long int) 0x400 #define MiB (unsigned long long int) 0x100000 @@ -10,14 +18,131 @@ #define LOADS TiB * 9999 -char* get_shorthand(unsigned long long int size) { +void print_help(); +unsigned char* get_shorthand(unsigned long long int size); +void adjust_by_denomination(unsigned long long int* size, char denomination); +unsigned long long int* allocate_heap(unsigned long long int size, char silent); +void save_heap(unsigned long long int* heap, unsigned long long int size, char* filename, char silent); + +int main(int argc, char* argv[]) { + + unsigned char flags = 0; + unsigned long long int size = 0; + unsigned char* filename = NULL; + unsigned char denominator = 0; + + int arg; + while ((arg = getopt(argc, argv, "hb:o:sw")) != -1) { + switch (arg) { + case 'h': + print_help(); + exit(0); + case 'b': + size = strtoull(optarg, NULL, 10); + if (errno == EINVAL) { + fprintf(stderr, "argument -b used with invalid value '%s'!\n", optarg); + exit(0); + } + if (errno == ERANGE) { + fprintf(stderr, "byte argument ''%s' out of range!\n", optarg); + exit(0); + } + denominator = optarg[strlen(optarg) - 1]; + flags |= BYTES_GIVEN; + break; + case 'o': + filename = optarg; + flags |= FILENAME_GIVEN; + break; + case 's': + if (size == 0 || filename == NULL) { + fprintf(stderr, "cannot run silently without -b and -o set!\n"); + print_help(); + } + flags |= SILENT; + break; + case 'w': + flags |= BYPASS_WARN; + break; + case '?': + if (optopt == 'b' || optopt == 'o') + fprintf(stderr, "argument -%c requires an argument!\n", optopt); + else if (isprint(optopt)) + fprintf(stderr, "unknown argument '-%c'.\n", optopt); + else + fprintf(stderr, "unknown argument character '\\x%x'.\n", optopt); + exit(1); + default: + exit(1); + } + } + + if (!(flags & BYTES_GIVEN)) { + fprintf(stderr, "argument -b is required!\n", optopt); + print_help(); + exit(1); + } + + adjust_by_denomination(&size, denominator); + + // even if the user doesn't specify a denomination, it still would be nice to display a truncated amount if available. + unsigned char* shorthand = get_shorthand(size); + if (!(flags & SILENT) && !(flags & BYPASS_WARN)) { + 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'); + } + + unsigned char input = getchar(); + if (input != 'y') exit(0); + while (getchar() != '\n'); + } + + unsigned long long int* heap = allocate_heap(size, flags & SILENT); + + if (!(flags & SILENT)) { + if (shorthand) { + printf("%s (%llu bytes) of heap memory has been allocated. you are insane.\n", shorthand, size); + } else { + printf("%llu byte%c of heap memory ha%s been allocated. you are insane.\n", size, size == 1 ? 0 : 's', size == 1 ? "s" : "ve"); + } + free(shorthand); + + printf("press ENTER to release this memory, or CTRL+C to exit the program.\n"); + getchar(); + free(heap); + exit(0); + } + + if (filename != NULL) save_heap(heap, size, filename, SILENT); + + if (!(flags & SILENT)) printf("done!\n"); + + free(heap); + return 0; + +} + +void print_help() { + printf("usage: allocatememory -b [-o ] [-s]\n\n"); + printf("OPTIONS\n"); + printf(" -h\tdisplays help\n"); + printf(" -b\tnumber of bytes to allocate (e.g. 1024, 1g, 64K)\n"); + printf(" -o\tfile to output to\n"); + printf(" -s\trun silently (requires -b and -o)\n"); + printf(" -w\tdisable warnings (\"i know what i'm doing!\")\n"); + exit(0); +} + +unsigned 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); + unsigned char* buffer = malloc(sizeof(char) * 12); if (size >= TiB) { snprintf(buffer, 12, "%.2f TiB", (float) size / TiB); @@ -37,133 +162,65 @@ char* get_shorthand(unsigned long long int size) { } } -unsigned long long int adjust_by_denomination(unsigned long long int size, char denomination) { +void adjust_by_denomination(unsigned long long int* size, char denomination) { + if (denomination >= 'a' && denomination <= 'z') denomination -= 32; switch (denomination) { case 'K': - return size * KiB; + *size *= KiB; + return; case 'M': - return size * MiB; + *size *= MiB; + return; case 'G': - return size * GiB; + *size *= GiB; + return; case 'T': - return size * TiB; - default: - return size; + *size *= TiB; + return; } } -int main(int argc, char* argv[]) { - - for (int i = 0; i < argc; i++) { - printf("%s\n", argv[i]); +unsigned long long int* allocate_heap(unsigned long long int size, char silent) { + if (!silent) { + printf("please wait."); + fflush(stdout); } - unsigned long long int size = 0; - - printf("please enter the amount of memory you would like to allocate (\"1G\" = 1 GiB = 1073741824 bytes):\n> "); - unsigned int digit = 0; - unsigned char last_char = 0; - while (1) { - unsigned char input = getchar(); - if (input == '\n') break; - if (input >= 'a' && input <= 'z') input -= 32; - last_char = input; - - if (input < '0' || input > '9') continue; - - if (digit > 0) size *= 10; - - size += (unsigned int) input - 48; - digit++; - } - - 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); - - 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'); - } - - unsigned char input = getchar(); - if (input != 'y') return 0; - while (getchar() != '\n'); - - printf("please wait."); - fflush(stdout); - - unsigned long long int* wtf = (unsigned long long int*) malloc(size); + unsigned long long int* heap = (unsigned long long int*) malloc(size); for (unsigned long long int i = 0; i < size / sizeof(long long int); i++) { - wtf[i] = i; - if (i > 0 && i % (1024 * 1024 * 1024 / sizeof(long long int)) == 0) { + heap[i] = i; + if (!silent && i > 0 && i % (1024 * 1024 * 1024 / sizeof(long long int)) == 0) { printf("."); fflush(stdout); } } - printf("\n"); - - if (shorthand) { - printf("%s (%llu bytes) of heap memory has been allocated. you are insane.\n", shorthand, size); - } else { - printf("%llu byte%c of heap memory ha%s been allocated. you are insane.\n", size, size == 1 ? 0 : 's', size == 1 ? "s" : "ve"); - } - free(shorthand); - - 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'); - } - - printf("press ENTER to release this memory, or CTRL+C to exit the program.\n"); - - getchar(); - - free(wtf); - - return 0; + if (!silent) printf("\n"); + return heap; +} + +void save_heap(unsigned long long int* heap, unsigned long long int size, char* filename, char silent) { + FILE* file = fopen(filename, "wb"); + if (file == NULL) { + printf("failed to open %s!", filename); + exit(1); + } + + if (!silent) printf("please wait...\n"); + /* + for (unsigned long long int i = 0; i < size / sizeof(unsigned long long int); i++) { + for (int p = 0; p < 8; p++) { + fputc(*(&heap[i] + p), file); + } + if (i > 0 && i % (1024 * 1024 * 1024 / sizeof(long long int)) == 0) { + if (!silent) printf("."); + fflush(stdout); + } + } + if (!silent) printf("\n"); + */ + fwrite(heap, sizeof(char), size, file); + fputs("\n", file); + fclose(file); } From aa8d58a5dbdb0ba714ee55ad26c28b66c94d6295 Mon Sep 17 00:00:00 2001 From: ari melody Date: Tue, 13 Feb 2024 03:19:14 +0000 Subject: [PATCH 12/15] fixed file output not working unless silent --- main.c | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/main.c b/main.c index c6c3801..317d945 100644 --- a/main.c +++ b/main.c @@ -7,7 +7,7 @@ #include #define BYTES_GIVEN 1 -#define FILENAME_GIVEN 2 +#define OUTPUT_FILE 2 #define SILENT 4 #define BYPASS_WARN 8 @@ -52,7 +52,7 @@ int main(int argc, char* argv[]) { break; case 'o': filename = optarg; - flags |= FILENAME_GIVEN; + flags |= OUTPUT_FILE; break; case 's': if (size == 0 || filename == NULL) { @@ -100,6 +100,7 @@ int main(int argc, char* argv[]) { } unsigned long long int* heap = allocate_heap(size, flags & SILENT); + if (filename != NULL) save_heap(heap, size, filename, flags & SILENT); if (!(flags & SILENT)) { if (shorthand) { @@ -115,10 +116,6 @@ int main(int argc, char* argv[]) { exit(0); } - if (filename != NULL) save_heap(heap, size, filename, SILENT); - - if (!(flags & SILENT)) printf("done!\n"); - free(heap); return 0; @@ -208,20 +205,6 @@ void save_heap(unsigned long long int* heap, unsigned long long int size, char* printf("failed to open %s!", filename); exit(1); } - - if (!silent) printf("please wait...\n"); - /* - for (unsigned long long int i = 0; i < size / sizeof(unsigned long long int); i++) { - for (int p = 0; p < 8; p++) { - fputc(*(&heap[i] + p), file); - } - if (i > 0 && i % (1024 * 1024 * 1024 / sizeof(long long int)) == 0) { - if (!silent) printf("."); - fflush(stdout); - } - } - if (!silent) printf("\n"); - */ fwrite(heap, sizeof(char), size, file); fputs("\n", file); fclose(file); From cda29dd3173e4963dd96a4a9558d90f6b9bdae2c Mon Sep 17 00:00:00 2001 From: ari melody Date: Tue, 13 Feb 2024 03:19:25 +0000 Subject: [PATCH 13/15] update readme --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index fc17ff6..f235539 100644 --- a/README.md +++ b/README.md @@ -4,20 +4,20 @@ it allocates memory ## how to use -- run the executable -- enter how much memory you want to allocate - - i.e. "1g" = 1 GiB = 1073741824 bytes - - the limit is how much memory you have...or more. your funeral. -- confirm (y) -- wait for the program to allocate the memory -- your memory is allocated -- press ENTER or CTRL+C to free the memory and exit the program -- ??? -- profit +``` +usage: allocatememory -b [-o ] [-s] + +OPTIONS + -h displays help + -b number of bytes to allocate (e.g. 1024, 1g, 64K) + -o file to output to + -s run silently (requires -b and -o) + -w disable warnings ("i know what i'm doing!") +``` ## how to compile -- get a c compiler (i use gcc) +- get a c compiler (i use gcc or clang) - `gcc -o ./allocatememory -O2 ./main.c` - ??? - profit From 665cc326ae1edcc5ea11b0508fa899ea2865c09c Mon Sep 17 00:00:00 2001 From: ari melody Date: Mon, 24 Mar 2025 23:58:18 +0000 Subject: [PATCH 14/15] add makefile --- .gitignore | 1 + Makefile | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index e058d00..ebc3ca4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.exe **/.DS_Store bin/ +melody-allocator diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8054e0c --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +EXEC = "melody-allocator" + +$(EXEC): + gcc -o ./$(EXEC) -O2 ./main.c + +install: $(EXEC) + cp $(EXEC) /usr/local/bin/ + chmod +x /usr/local/bin/$(EXEC) + +clean: + rm ./$(EXEC) From 3a6e764c0b654ea68087d1ff67f558b3ee1ef09b Mon Sep 17 00:00:00 2001 From: ari melody Date: Tue, 25 Mar 2025 00:07:21 +0000 Subject: [PATCH 15/15] update readme --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f235539..69e6af0 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,9 @@ OPTIONS ## how to compile -- get a c compiler (i use gcc or clang) -- `gcc -o ./allocatememory -O2 ./main.c` -- ??? -- profit +- `make` +- (optional) `sudo make install` +- yeag ## why