replace lame soy 8-space indentation with based chad 4-space indentation
This commit is contained in:
parent
edb21f67a2
commit
af2492a8ce
1 changed files with 147 additions and 147 deletions
294
main.c
294
main.c
|
@ -25,187 +25,187 @@ void save_heap(unsigned long long int* heap, unsigned long long int size, char*
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
|
||||||
unsigned char flags = 0;
|
unsigned char flags = 0;
|
||||||
unsigned long long int size = 0;
|
unsigned long long int size = 0;
|
||||||
char* filename = NULL;
|
char* filename = NULL;
|
||||||
unsigned char denominator = 0;
|
unsigned char denominator = 0;
|
||||||
|
|
||||||
int arg;
|
int arg;
|
||||||
while ((arg = getopt(argc, argv, "hb:o:sw")) != -1) {
|
while ((arg = getopt(argc, argv, "hb:o:sw")) != -1) {
|
||||||
switch (arg) {
|
switch (arg) {
|
||||||
case 'h':
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(flags & BYTES_GIVEN)) {
|
|
||||||
fprintf(stderr, "argument -b is required!\n");
|
|
||||||
print_help();
|
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);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
adjust_by_denomination(&size, denominator);
|
if (!(flags & BYTES_GIVEN)) {
|
||||||
|
fprintf(stderr, "argument -b is required!\n");
|
||||||
|
print_help();
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
// even if the user doesn't specify a denomination, it still would be nice to display a truncated amount if available.
|
adjust_by_denomination(&size, denominator);
|
||||||
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();
|
// even if the user doesn't specify a denomination, it still would be nice to display a truncated amount if available.
|
||||||
if (input != 'y') exit(0);
|
char* shorthand = get_shorthand(size);
|
||||||
while (getchar() != '\n');
|
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');
|
||||||
}
|
}
|
||||||
|
|
||||||
void* heap = allocate_heap(size, flags & SILENT);
|
unsigned char input = getchar();
|
||||||
if (filename != NULL) save_heap(heap, size, filename, flags & SILENT);
|
if (input != 'y') exit(0);
|
||||||
|
while (getchar() != '\n');
|
||||||
|
}
|
||||||
|
|
||||||
if (!(flags & SILENT)) {
|
void* heap = allocate_heap(size, flags & SILENT);
|
||||||
if (shorthand) {
|
if (filename != NULL) save_heap(heap, size, filename, flags & SILENT);
|
||||||
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");
|
if (!(flags & SILENT)) {
|
||||||
getchar();
|
if (shorthand) {
|
||||||
free(heap);
|
printf("%s (%llu bytes) of heap memory has been allocated. you are insane.\n", shorthand, size);
|
||||||
exit(0);
|
} 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);
|
free(heap);
|
||||||
return 0;
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(heap);
|
||||||
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_help() {
|
void print_help() {
|
||||||
printf("usage: allocatememory -b <bytes> [-o <output file>] [-s]\n\n");
|
printf("usage: allocatememory -b <bytes> [-o <output file>] [-s]\n\n");
|
||||||
printf("OPTIONS\n");
|
printf("OPTIONS\n");
|
||||||
printf(" -h\tdisplays help\n");
|
printf(" -h\tdisplays help\n");
|
||||||
printf(" -b\tnumber of bytes to allocate (e.g. 1024, 1g, 64K)\n");
|
printf(" -b\tnumber of bytes to allocate (e.g. 1024, 1g, 64K)\n");
|
||||||
printf(" -o\tfile to output to\n");
|
printf(" -o\tfile to output to\n");
|
||||||
printf(" -s\trun silently (requires -b and -o)\n");
|
printf(" -s\trun silently (requires -b and -o)\n");
|
||||||
printf(" -w\tdisable warnings (\"i know what i'm doing!\")\n");
|
printf(" -w\tdisable warnings (\"i know what i'm doing!\")\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
char* get_shorthand(unsigned long long int size) {
|
char* get_shorthand(unsigned long long int size) {
|
||||||
if (size < KiB) return 0;
|
if (size < KiB) return 0;
|
||||||
|
|
||||||
if (size > LOADS) {
|
if (size > LOADS) {
|
||||||
return ">9999 TiB";
|
return ">9999 TiB";
|
||||||
}
|
}
|
||||||
|
|
||||||
char* buffer = malloc(sizeof(char) * 12);
|
char* buffer = malloc(sizeof(char) * 12);
|
||||||
|
|
||||||
if (size >= TiB) {
|
if (size >= TiB) {
|
||||||
snprintf(buffer, 12, "%.2f TiB", (float) size / TiB);
|
snprintf(buffer, 12, "%.2f TiB", (float) size / TiB);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
if (size >= GiB) {
|
if (size >= GiB) {
|
||||||
snprintf(buffer, 12, "%.2f GiB", (float) size / GiB);
|
snprintf(buffer, 12, "%.2f GiB", (float) size / GiB);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
if (size >= MiB) {
|
if (size >= MiB) {
|
||||||
snprintf(buffer, 12, "%.2f MiB", (float) size / MiB);
|
snprintf(buffer, 12, "%.2f MiB", (float) size / MiB);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
if (size >= KiB) {
|
if (size >= KiB) {
|
||||||
snprintf(buffer, 12, "%.2f KiB", (float) size / KiB);
|
snprintf(buffer, 12, "%.2f KiB", (float) size / KiB);
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void 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;
|
if (denomination >= 'a' && denomination <= 'z') denomination -= 32;
|
||||||
switch (denomination) {
|
switch (denomination) {
|
||||||
case 'K':
|
case 'K':
|
||||||
*size *= KiB;
|
*size *= KiB;
|
||||||
return;
|
return;
|
||||||
case 'M':
|
case 'M':
|
||||||
*size *= MiB;
|
*size *= MiB;
|
||||||
return;
|
return;
|
||||||
case 'G':
|
case 'G':
|
||||||
*size *= GiB;
|
*size *= GiB;
|
||||||
return;
|
return;
|
||||||
case 'T':
|
case 'T':
|
||||||
*size *= TiB;
|
*size *= TiB;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void* allocate_heap(unsigned long long int size, char silent) {
|
void* allocate_heap(unsigned long long int size, char silent) {
|
||||||
if (!silent) {
|
if (!silent) {
|
||||||
printf("please wait.");
|
printf("please wait.");
|
||||||
fflush(stdout);
|
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] = ((unsigned long long int)rand() << 32) + (unsigned long long int)rand();
|
||||||
|
|
||||||
|
if (!silent && i > 0 && i % (1024 * 1024 * 1024 / sizeof(long long int)) == 0) {
|
||||||
|
printf(".");
|
||||||
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (!silent) printf("\n");
|
||||||
|
|
||||||
unsigned long long int* heap = (unsigned long long int*) malloc(size);
|
return heap;
|
||||||
|
|
||||||
for (unsigned long long int i = 0; i < size / sizeof(long long int); i++) {
|
|
||||||
heap[i] = ((unsigned long long int)rand() << 32) + (unsigned long long int)rand();
|
|
||||||
|
|
||||||
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) {
|
void save_heap(unsigned long long int* heap, unsigned long long int size, char* filename, char silent) {
|
||||||
FILE* file = fopen(filename, "wb");
|
FILE* file = fopen(filename, "wb");
|
||||||
if (file == NULL) {
|
if (file == NULL) {
|
||||||
printf("failed to open %s!", filename);
|
printf("failed to open %s!", filename);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
fwrite(heap, sizeof(char), size, file);
|
fwrite(heap, sizeof(char), size, file);
|
||||||
fputs("\n", file);
|
fputs("\n", file);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue