Compare commits
16 commits
Author | SHA1 | Date | |
---|---|---|---|
ari melody | cda29dd317 | ||
ari melody | aa8d58a5db | ||
ari melody | 1c720eaca3 | ||
ari melody | ca9d63c3eb | ||
ari melody | de270552cb | ||
ari melody | 0bebd05e0e | ||
ari melody | 6204419407 | ||
ari melody | 431c198138 | ||
ari melody | f127a1a249 | ||
ari melody | a8f3cccbd4 | ||
ari melody | 358e1ea2ff | ||
ari melody | aeb84dc19a | ||
ari melody | a258b4bbea | ||
ari melody | 247a66cad1 | ||
ari melody | 548be13ede | ||
ari melody | ea68d67619 |
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
*.exe
|
*.exe
|
||||||
**/.DS_Store
|
**/.DS_Store
|
||||||
|
bin/
|
||||||
|
|
24
README.md
24
README.md
|
@ -1,22 +1,24 @@
|
||||||
# memory allocator
|
# memory allocator
|
||||||
|
|
||||||
it allocates memory *(in gibibytes (GiB))*
|
it allocates memory
|
||||||
|
|
||||||
## how to use
|
## how to use
|
||||||
|
|
||||||
- run the executable
|
```
|
||||||
- enter how many gibibytes you want (max is how many gibibytes of memory you have)
|
usage: allocatememory -b <bytes> [-o <output file>] [-s]
|
||||||
- confirm (y)
|
|
||||||
- wait for the program to allocate the memory
|
OPTIONS
|
||||||
- your memory is allocated
|
-h displays help
|
||||||
- press any key or CTRL+C to free the memory and exit the program
|
-b number of bytes to allocate (e.g. 1024, 1g, 64K)
|
||||||
- ???
|
-o file to output to
|
||||||
- profit
|
-s run silently (requires -b and -o)
|
||||||
|
-w disable warnings ("i know what i'm doing!")
|
||||||
|
```
|
||||||
|
|
||||||
## how to compile
|
## how to compile
|
||||||
|
|
||||||
- get a c compiler (i use gcc)
|
- get a c compiler (i use gcc or clang)
|
||||||
- `gcc -o ./allocatememory.exe ./main.c`
|
- `gcc -o ./allocatememory -O2 ./main.c`
|
||||||
- ???
|
- ???
|
||||||
- profit
|
- profit
|
||||||
|
|
||||||
|
|
222
main.c
222
main.c
|
@ -1,47 +1,211 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
int main() {
|
#define BYTES_GIVEN 1
|
||||||
|
#define OUTPUT_FILE 2
|
||||||
|
#define SILENT 4
|
||||||
|
#define BYPASS_WARN 8
|
||||||
|
|
||||||
long long unsigned int size = 0;
|
#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
|
||||||
|
|
||||||
printf("please enter the number of bytes (GiB) you would like to allocate (i.e. 4 = 4GiB):\n> ");
|
#define LOADS TiB * 9999
|
||||||
int digit = 0;
|
|
||||||
while (1) {
|
|
||||||
char input = getchar();
|
|
||||||
if (input == '\n') break;
|
|
||||||
if (input < '0' || input > '9') continue;
|
|
||||||
|
|
||||||
char number = input - 48;
|
void print_help();
|
||||||
if (digit > 0) number = number * 10 * digit;
|
unsigned char* get_shorthand(unsigned long long int size);
|
||||||
if (input == '0') number = 10 * digit;
|
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);
|
||||||
|
|
||||||
size = size + number;
|
int main(int argc, char* argv[]) {
|
||||||
digit++;
|
|
||||||
}
|
|
||||||
|
|
||||||
size = size * 1024 * 1024 * 1024;
|
unsigned char flags = 0;
|
||||||
|
unsigned long long int size = 0;
|
||||||
|
unsigned char* filename = NULL;
|
||||||
|
unsigned char denominator = 0;
|
||||||
|
|
||||||
printf("you are about to allocate %llu bytes of heap memory. are you sure? (y/n)\n> ", size);
|
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 |= OUTPUT_FILE;
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
char input = getchar();
|
if (!(flags & BYTES_GIVEN)) {
|
||||||
if (input != 'y') return 0;
|
fprintf(stderr, "argument -b is required!\n", optopt);
|
||||||
while (getchar() != '\n');
|
print_help();
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
printf("please wait...\n");
|
adjust_by_denomination(&size, denominator);
|
||||||
char* wtf = (char*) malloc(size * sizeof(char));
|
|
||||||
|
|
||||||
for (long long unsigned int i = 0; i < size; i++) {
|
// even if the user doesn't specify a denomination, it still would be nice to display a truncated amount if available.
|
||||||
wtf[i] = 255;
|
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');
|
||||||
|
}
|
||||||
|
|
||||||
printf("%llu bytes of heap memory has been allocated. you are insane.\n", size);
|
unsigned char input = getchar();
|
||||||
printf("press any key to release this memory, or CTRL+C to exit the program.\n");
|
if (input != 'y') exit(0);
|
||||||
|
while (getchar() != '\n');
|
||||||
|
}
|
||||||
|
|
||||||
getchar();
|
unsigned long long int* heap = allocate_heap(size, flags & SILENT);
|
||||||
|
if (filename != NULL) save_heap(heap, size, filename, flags & SILENT);
|
||||||
|
|
||||||
free(wtf);
|
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);
|
||||||
|
|
||||||
return 0;
|
printf("press ENTER to release this memory, or CTRL+C to exit the program.\n");
|
||||||
|
getchar();
|
||||||
|
free(heap);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(heap);
|
||||||
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void print_help() {
|
||||||
|
printf("usage: allocatememory -b <bytes> [-o <output file>] [-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";
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void adjust_by_denomination(unsigned long long int* size, char denomination) {
|
||||||
|
if (denomination >= 'a' && denomination <= 'z') denomination -= 32;
|
||||||
|
switch (denomination) {
|
||||||
|
case 'K':
|
||||||
|
*size *= KiB;
|
||||||
|
return;
|
||||||
|
case 'M':
|
||||||
|
*size *= MiB;
|
||||||
|
return;
|
||||||
|
case 'G':
|
||||||
|
*size *= GiB;
|
||||||
|
return;
|
||||||
|
case 'T':
|
||||||
|
*size *= TiB;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long long int* allocate_heap(unsigned long long int size, char silent) {
|
||||||
|
if (!silent) {
|
||||||
|
printf("please wait.");
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
|
||||||
|
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++) {
|
||||||
|
heap[i] = i;
|
||||||
|
if (!silent && i > 0 && i % (1024 * 1024 * 1024 / sizeof(long long int)) == 0) {
|
||||||
|
printf(".");
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
fwrite(heap, sizeof(char), size, file);
|
||||||
|
fputs("\n", file);
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue