Last active
March 13, 2018 04:18
-
-
Save kunalkushwaha/2f7759a1900c442f4cf00ca4cf53f28e to your computer and use it in GitHub Desktop.
Memory allocator in blocks of GB
This file contains hidden or 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
| FROM scratch | |
| ADD high-mem-allocator / | |
| ENTRYPOINT ["/high-mem-allocator"] | |
| CMD ["1"] |
This file contains hidden or 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
| build: | |
| gcc -v -static mem-allocator.c -o high-mem-allocator | |
This file contains hidden or 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
| #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