Compare commits
No commits in common. "af2492a8ce938490eadb0ec270700cc8aa8fb42a" and "3a6e764c0b654ea68087d1ff67f558b3ee1ef09b" have entirely different histories.
af2492a8ce
...
3a6e764c0b
1 changed files with 159 additions and 159 deletions
22
main.c
22
main.c
|
@ -1,5 +1,6 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
@ -18,16 +19,16 @@
|
||||||
#define LOADS TiB * 9999
|
#define LOADS TiB * 9999
|
||||||
|
|
||||||
void print_help();
|
void print_help();
|
||||||
char* get_shorthand(unsigned long long int size);
|
unsigned char* get_shorthand(unsigned long long int size);
|
||||||
void adjust_by_denomination(unsigned long long int* size, char denomination);
|
void adjust_by_denomination(unsigned long long int* size, char denomination);
|
||||||
void* allocate_heap(unsigned long long int size, char silent);
|
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);
|
void save_heap(unsigned long long int* heap, unsigned long long int size, char* filename, char silent);
|
||||||
|
|
||||||
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;
|
unsigned char* filename = NULL;
|
||||||
unsigned char denominator = 0;
|
unsigned char denominator = 0;
|
||||||
|
|
||||||
int arg;
|
int arg;
|
||||||
|
@ -77,7 +78,7 @@ int main(int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(flags & BYTES_GIVEN)) {
|
if (!(flags & BYTES_GIVEN)) {
|
||||||
fprintf(stderr, "argument -b is required!\n");
|
fprintf(stderr, "argument -b is required!\n", optopt);
|
||||||
print_help();
|
print_help();
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
@ -85,7 +86,7 @@ int main(int argc, char* argv[]) {
|
||||||
adjust_by_denomination(&size, denominator);
|
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.
|
// 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);
|
unsigned char* shorthand = get_shorthand(size);
|
||||||
if (!(flags & SILENT) && !(flags & BYPASS_WARN)) {
|
if (!(flags & SILENT) && !(flags & BYPASS_WARN)) {
|
||||||
if (shorthand) {
|
if (shorthand) {
|
||||||
printf("you are about to allocate %s (%llu bytes) of heap memory. are you sure? (y/N)\n> ", shorthand, size);
|
printf("you are about to allocate %s (%llu bytes) of heap memory. are you sure? (y/N)\n> ", shorthand, size);
|
||||||
|
@ -98,7 +99,7 @@ int main(int argc, char* argv[]) {
|
||||||
while (getchar() != '\n');
|
while (getchar() != '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
void* heap = allocate_heap(size, flags & SILENT);
|
unsigned long long int* heap = allocate_heap(size, flags & SILENT);
|
||||||
if (filename != NULL) save_heap(heap, size, filename, flags & SILENT);
|
if (filename != NULL) save_heap(heap, size, filename, flags & SILENT);
|
||||||
|
|
||||||
if (!(flags & SILENT)) {
|
if (!(flags & SILENT)) {
|
||||||
|
@ -131,14 +132,14 @@ void print_help() {
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
char* get_shorthand(unsigned long long int size) {
|
unsigned 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);
|
unsigned 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);
|
||||||
|
@ -178,7 +179,7 @@ void adjust_by_denomination(unsigned long long int* size, char denomination) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void* allocate_heap(unsigned long long int size, char silent) {
|
unsigned long long int* allocate_heap(unsigned long long int size, char silent) {
|
||||||
if (!silent) {
|
if (!silent) {
|
||||||
printf("please wait.");
|
printf("please wait.");
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
@ -187,8 +188,7 @@ void* allocate_heap(unsigned long long int size, char silent) {
|
||||||
unsigned long long int* heap = (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++) {
|
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();
|
heap[i] = i;
|
||||||
|
|
||||||
if (!silent && i > 0 && i % (1024 * 1024 * 1024 / sizeof(long long int)) == 0) {
|
if (!silent && i > 0 && i % (1024 * 1024 * 1024 / sizeof(long long int)) == 0) {
|
||||||
printf(".");
|
printf(".");
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue