Skip to content

Instantly share code, notes, and snippets.

@kunalkushwaha
Last active March 13, 2018 04:18
Show Gist options
  • Select an option

  • Save kunalkushwaha/2f7759a1900c442f4cf00ca4cf53f28e to your computer and use it in GitHub Desktop.

Select an option

Save kunalkushwaha/2f7759a1900c442f4cf00ca4cf53f28e to your computer and use it in GitHub Desktop.
Memory allocator in blocks of GB
FROM scratch
ADD high-mem-allocator /
ENTRYPOINT ["/high-mem-allocator"]
CMD ["1"]
build:
gcc -v -static mem-allocator.c -o high-mem-allocator
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int GB=1024*1024*1024;
int main(int argc, char *argv[]) {
char* high_mem[12];
if (argc<2) {
printf("Allocates memory blocks in GB \nUSAGE: \n$ high-mem-allocator <Unit>\n");
return 1;
}
int multiplier = atoi(argv[1]);
for (int i=0; i<multiplier ;i++) {
high_mem[i] = malloc(1*GB);
if (high_mem[i] == NULL) {
printf("Error while allocating memory\n");
return 1;
}
memset(high_mem[i],'0',GB);
}
printf("Total memory allocated: %d GB\n\n", multiplier);
printf("<Press Enter key to exit>");
getchar();
for (int i=0; i<multiplier ;i++) {
free(high_mem[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment