Last active
August 29, 2015 14:19
-
-
Save floriandejonckheere/b686d33f53e45a7823bd to your computer and use it in GitHub Desktop.
allocbot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* allocbot.c - mindlessly allocate memory | |
* | |
* Florian Dejonckheere <[email protected] | |
* | |
* */ | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <strings.h> | |
#define ALLOC (10 * 1024 * 1024) | |
void main() { | |
while(1) { | |
// Allocate 10 MiB | |
void *ptr = malloc(ALLOC); | |
bzero(ptr, ALLOC); | |
printf("Allocated 10 MiB\n"); | |
usleep(100000); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* allocbot2.c - allocate or deallocate memory on demand | |
* | |
* Florian Dejonckheere <[email protected] | |
* | |
* Usage: press '+' or '-' and enter to enter allocating | |
* or deallocating mode. Press enter to execute action. | |
* | |
* */ | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <termios.h> | |
#include <signal.h> | |
// 10 MiB | |
#define ALLOC (10 * 1024 * 1024) | |
// Mode: 0 = allocate, 1 = free | |
int mode = 0; | |
// Stack | |
typedef struct Stack { | |
void *mem; | |
struct Stack *next; | |
} Stack; | |
unsigned long total = 0; | |
Stack *stack = NULL; | |
struct termios new_kbd_mode; | |
struct termios g_old_kbd_mode; | |
char* readable_fs(double size/*in bytes*/, char *buf) { | |
int i = 0; | |
const char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}; | |
while(size > 1024) { | |
size /= 1024; | |
i++; | |
} | |
sprintf(buf, "%.*f %s", i, size, units[i]); | |
return buf; | |
} | |
void cleanup() { | |
while(stack != NULL) { | |
Stack *next = stack->next; | |
free(stack->mem); | |
free(stack); | |
stack = next; | |
} | |
char buf[10]; | |
printf("De-allocated %s, exiting...\n", readable_fs((total * ALLOC), buf)); | |
// Put keyboard back in old mode | |
tcsetattr (0, TCSANOW, &g_old_kbd_mode); | |
exit(0); | |
} | |
void main() { | |
signal(SIGINT, cleanup); | |
// Put keyboard (stdin) in raw, unbuffered mode | |
tcgetattr (0, &g_old_kbd_mode); | |
memcpy (&new_kbd_mode, &g_old_kbd_mode, sizeof (struct termios)); | |
new_kbd_mode.c_lflag &= ~(ICANON | ECHO); | |
new_kbd_mode.c_cc[VTIME] = 0; | |
new_kbd_mode.c_cc[VMIN] = 1; | |
tcsetattr (0, TCSANOW, &new_kbd_mode); | |
char c; | |
while(c = getchar()) { | |
if(c == '+') { | |
mode = 0; | |
printf("Entering allocation mode\n"); | |
} else if(c == '-') { | |
mode = 1; | |
printf("Entering de-allocation mode\n"); | |
} else if(c == '\n') { | |
if(mode) { | |
// De-allocate | |
if(stack != NULL) { | |
Stack *next = stack->next; | |
free(stack->mem); | |
free(stack); | |
stack = next; | |
total--; | |
char buf[10]; | |
printf("De-allocated %d bytes, %s total\n", ALLOC, readable_fs((total * ALLOC), buf)); | |
} else printf("Nothing to deallocate\n"); | |
} else { | |
// Allocate | |
Stack *top = malloc(sizeof(Stack)); | |
top->mem = malloc(ALLOC); | |
bzero(top->mem, ALLOC); | |
top->next = stack; | |
stack = top; | |
total++; | |
char buf[10]; | |
printf("Allocated %d bytes, %s total\n", ALLOC, readable_fs((total * ALLOC), buf)); | |
} | |
} | |
} | |
// Put keyboard back in old mode | |
tcsetattr (0, TCSANOW, &g_old_kbd_mode); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment