small improvements; memory generated is now random

This commit is contained in:
ari melody 2025-08-04 20:55:37 +01:00
parent 3a6e764c0b
commit edb21f67a2
Signed by: ari
GPG key ID: CF99829C92678188

22
main.c
View file

@ -1,6 +1,5 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
@ -19,16 +18,16 @@
#define LOADS TiB * 9999
void print_help();
unsigned char* get_shorthand(unsigned long long int size);
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* 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;
char* filename = NULL;
unsigned char denominator = 0;
int arg;
@ -78,7 +77,7 @@ int main(int argc, char* argv[]) {
}
if (!(flags & BYTES_GIVEN)) {
fprintf(stderr, "argument -b is required!\n", optopt);
fprintf(stderr, "argument -b is required!\n");
print_help();
exit(1);
}
@ -86,7 +85,7 @@ int main(int argc, char* argv[]) {
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);
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);
@ -99,7 +98,7 @@ int main(int argc, char* argv[]) {
while (getchar() != '\n');
}
unsigned long long int* heap = allocate_heap(size, flags & SILENT);
void* heap = allocate_heap(size, flags & SILENT);
if (filename != NULL) save_heap(heap, size, filename, flags & SILENT);
if (!(flags & SILENT)) {
@ -132,14 +131,14 @@ void print_help() {
exit(0);
}
unsigned char* get_shorthand(unsigned long long int size) {
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);
char* buffer = malloc(sizeof(char) * 12);
if (size >= TiB) {
snprintf(buffer, 12, "%.2f TiB", (float) size / TiB);
@ -179,7 +178,7 @@ 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* allocate_heap(unsigned long long int size, char silent) {
if (!silent) {
printf("please wait.");
fflush(stdout);
@ -188,7 +187,8 @@ unsigned long long int* allocate_heap(unsigned long long int size, char silent)
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;
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);