├── css.h ├── Course └── LinuxMemoryManager │ ├── css.h │ ├── UdemyCourse │ ├── css.h │ ├── uapi_mm.h │ ├── testapp.c │ ├── compilation Steps │ ├── gluethread │ │ ├── glthread.h │ │ └── glthread.c │ ├── block_split_ass.c │ ├── mm.h │ ├── mm_ass.c │ └── mm.c │ ├── Makefile │ ├── testapp.c │ ├── uapi_mm_old.h │ ├── uapi_mm.h │ ├── uapi_mm_new.h │ ├── gluethread │ ├── test.c │ ├── glthread.h │ └── glthread.c │ ├── mm.h │ └── mm.c ├── Makefile ├── .gitignore ├── README.md ├── testapp.c ├── uapi_mm_new.h ├── uapi_mm_old.h ├── uapi_mm.h ├── gluethread ├── test.c ├── glthread.h └── glthread.c ├── mm.h ├── mm.c └── LICENSE /css.h: -------------------------------------------------------------------------------- 1 | #ifndef __CSS__ 2 | #define __CSS__ 3 | 4 | #define ANSI_COLOR_RED "\x1b[31m" 5 | #define ANSI_COLOR_GREEN "\x1b[32m" 6 | #define ANSI_COLOR_YELLOW "\x1b[33m" 7 | #define ANSI_COLOR_BLUE "\x1b[34m" 8 | #define ANSI_COLOR_MAGENTA "\x1b[35m" 9 | #define ANSI_COLOR_CYAN "\x1b[36m" 10 | #define ANSI_COLOR_RESET "\x1b[0m" 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/css.h: -------------------------------------------------------------------------------- 1 | #ifndef __CSS__ 2 | #define __CSS__ 3 | 4 | #define ANSI_COLOR_RED "\x1b[31m" 5 | #define ANSI_COLOR_GREEN "\x1b[32m" 6 | #define ANSI_COLOR_YELLOW "\x1b[33m" 7 | #define ANSI_COLOR_BLUE "\x1b[34m" 8 | #define ANSI_COLOR_MAGENTA "\x1b[35m" 9 | #define ANSI_COLOR_CYAN "\x1b[36m" 10 | #define ANSI_COLOR_RESET "\x1b[0m" 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/UdemyCourse/css.h: -------------------------------------------------------------------------------- 1 | #ifndef __CSS__ 2 | #define __CSS__ 3 | 4 | #define ANSI_COLOR_RED "\x1b[31m" 5 | #define ANSI_COLOR_GREEN "\x1b[32m" 6 | #define ANSI_COLOR_YELLOW "\x1b[33m" 7 | #define ANSI_COLOR_BLUE "\x1b[34m" 8 | #define ANSI_COLOR_MAGENTA "\x1b[35m" 9 | #define ANSI_COLOR_CYAN "\x1b[36m" 10 | #define ANSI_COLOR_RESET "\x1b[0m" 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-g 3 | TARGET:testapp.exe libmm.a 4 | OUTFILES=testapp.exe libmm.a 5 | EXTERNAL_LIBS= 6 | OBJS=gluethread/glthread.o mm.o 7 | 8 | testapp.exe:testapp.o ${OBJS} 9 | ${CC} ${CFLAGS} testapp.o ${OBJS} -o testapp.exe ${EXTERNAL_LIBS} 10 | testapp.o:testapp.c 11 | ${CC} ${CFLAGS} -c testapp.c -o testapp.o 12 | gluethread/glthread.o:gluethread/glthread.c 13 | ${CC} ${CFLAGS} -c -I gluethread gluethread/glthread.c -o gluethread/glthread.o 14 | mm.o:mm.c 15 | ${CC} ${CFLAGS} -c mm.c -o mm.o 16 | libmm.a:${OBJ} 17 | ar rs libmm.a ${OBJ} 18 | clean: 19 | rm -f testapp.o 20 | rm -f ${OUTFILES} 21 | rm -f ${OBJS} 22 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-g 3 | TARGET:testapp.exe libmm.a 4 | OUTFILES=testapp.exe libmm.a 5 | EXTERNAL_LIBS= 6 | OBJS=gluethread/glthread.o mm.o 7 | 8 | testapp.exe:testapp.o ${OBJS} 9 | ${CC} ${CFLAGS} testapp.o ${OBJS} -o testapp.exe ${EXTERNAL_LIBS} 10 | testapp.o:testapp.c 11 | ${CC} ${CFLAGS} -c testapp.c -o testapp.o 12 | gluethread/glthread.o:gluethread/glthread.c 13 | ${CC} ${CFLAGS} -c -I gluethread gluethread/glthread.c -o gluethread/glthread.o 14 | mm.o:mm.c 15 | ${CC} ${CFLAGS} -c mm.c -o mm.o 16 | libmm.a:${OBJ} 17 | ar rs libmm.a ${OBJ} 18 | clean: 19 | rm -f testapp.o 20 | rm -f ${OUTFILES} 21 | rm -f ${OBJS} 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/UdemyCourse/uapi_mm.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef __UAPI_MM__ 3 | #define __UAPI_MM__ 4 | 5 | 6 | #include 7 | 8 | void * 9 | xcalloc(char *struct_name, int units); 10 | void xfree(void *ptr); 11 | 12 | #define XCALLOC(units, struct_name) \ 13 | (xcalloc(#struct_name, units)) 14 | 15 | #define XFREE(ptr) \ 16 | (xfree(ptr)) 17 | 18 | /*Initialization Functions*/ 19 | void 20 | mm_init(); 21 | 22 | /*Registration function*/ 23 | void 24 | mm_instantiate_new_page_family( 25 | char *struct_name, 26 | uint32_t struct_size); 27 | 28 | #define MM_REG_STRUCT(struct_name) \ 29 | (mm_instantiate_new_page_family(#struct_name, sizeof(struct_name))) 30 | 31 | void mm_print_memory_usage(char *struct_name); 32 | void mm_print_registered_page_families(); 33 | void mm_print_block_usage(); 34 | 35 | #endif /* __UAPI_MM__ */ 36 | 37 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/UdemyCourse/testapp.c: -------------------------------------------------------------------------------- 1 | #include "uapi_mm.h" 2 | #include 3 | 4 | typedef struct emp_ { 5 | 6 | char name[32]; 7 | uint32_t emp_id; 8 | } emp_t; 9 | 10 | typedef struct student_ { 11 | 12 | char name[32]; 13 | uint32_t rollno; 14 | uint32_t marks_phys; 15 | uint32_t marks_chem; 16 | uint32_t marks_maths; 17 | struct student_ *next; 18 | } student_t; 19 | 20 | int 21 | main(int argc, char **argv){ 22 | 23 | int wait; 24 | mm_init(); 25 | MM_REG_STRUCT(emp_t); 26 | MM_REG_STRUCT(student_t); 27 | mm_print_registered_page_families(); 28 | 29 | emp_t *emp1 = XCALLOC(1, emp_t); 30 | emp_t *emp2 = XCALLOC(1, emp_t); 31 | emp_t *emp3 = XCALLOC(1, emp_t); 32 | 33 | student_t *stud1 = XCALLOC(1, student_t); 34 | student_t *stud2 = XCALLOC(1, student_t); 35 | 36 | printf(" \nSCENARIO 1 : *********** \n"); 37 | mm_print_memory_usage(0); 38 | mm_print_block_usage(); 39 | 40 | 41 | scanf("%d", &wait); 42 | 43 | XFREE(emp1); 44 | XFREE(emp3); 45 | XFREE(stud2); 46 | printf(" \nSCENARIO 2 : *********** \n"); 47 | mm_print_memory_usage(0); 48 | mm_print_block_usage(); 49 | 50 | 51 | scanf("%d", &wait); 52 | 53 | XFREE(emp2); 54 | XFREE(stud1); 55 | printf(" \nSCENARIO 3 : *********** \n"); 56 | mm_print_memory_usage(0); 57 | mm_print_block_usage(); 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LinuxMemoryManager 2 | This projects is regarding designing own Heap memory manager which will manage the process's memory requirement. Memory manager will get rid Or minimize the internal and external fragmentation problems, and boost performance of the process by preventing unnecessary system calls for allocating/releasing the memory. Not only that, LMM can display the user with the statistics regarding the memory usage by each structure of the process. From this stats, application memory usage can be analyzed and can provide hints to further optimize the memory requirements of the process. Memory leak can also be discovered using LMM. 3 | 4 | LMM request and release memmory from kernel space on behalf of application in units of Virtual Memory Pages. It used mmap() and munmap() system calls for this purpose. It caches the VM pages and use it as reservoir for future memory requests issued by the application, until the VM page is fully exhausted. VM page is released back to kernel if application has freed enough Memory such that VM page has no region occupied/allocated to the application for use. 5 | 6 | 7 | Future Enhancement : 8 | A GUI tool can be developed which can fetch the Memory usage stats from LMM and display graphically in real time. 9 | 10 | 11 | Algorithms used : 12 | = = = = = = = = = 13 | Algorithms for Block Splitting and Merging 14 | Doubly linked list for maintaining free and allocated blocks 15 | Largest fit Algorithms using priority Queue Data Structure for allocating memory to the process 16 | 17 | 18 | Compilations: 19 | gcc -g -c testapp.c -o testapp.o 20 | gcc -g -c mm.c -o mm.o 21 | gcc -g -c gluethread/glthread.c -o gluethread/glthread.o 22 | gcc -g testapp.o mm.o gluethread/glthread.o -o exe 23 | -------------------------------------------------------------------------------- /testapp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "uapi_mm.h" 3 | #include 4 | 5 | typedef struct emp_ { 6 | 7 | char name[32]; 8 | uint32_t emp_id; 9 | } emp_t; 10 | 11 | typedef struct student_ { 12 | 13 | char name[32]; 14 | uint32_t rollno; 15 | uint32_t marks_phys; 16 | uint32_t marks_chem; 17 | uint32_t marks_maths; 18 | struct student_ *next; 19 | } student_t; 20 | 21 | int 22 | main(int argc, char **argv){ 23 | 24 | mm_init(); 25 | MM_REG_STRUCT(emp_t); 26 | MM_REG_STRUCT(student_t); 27 | #if 0 28 | emp_t *emp1 = xcalloc("emp_t", 1); 29 | emp_t *emp2 = xcalloc("emp_t", 1); 30 | emp_t *emp3 = xcalloc("emp_t", 1); 31 | emp_t *emp4 = xcalloc("emp_t", 3); 32 | student_t *stud1 = xcalloc("student_t", 1); 33 | student_t *stud2 = xcalloc("student_t", 2); 34 | student_t *stud3 = xcalloc("student_t", 1); 35 | mm_print_memory_usage(); 36 | xfree(emp1); 37 | mm_print_memory_usage(); 38 | xfree(emp2); 39 | mm_print_memory_usage(); 40 | xfree(emp3); 41 | mm_print_memory_usage(); 42 | xfree(emp4); 43 | mm_print_memory_usage(); 44 | xfree(stud1); 45 | mm_print_memory_usage(); 46 | xfree(stud2); 47 | mm_print_memory_usage(); 48 | xfree(stud3); 49 | mm_print_memory_usage(); 50 | #endif 51 | int i = 0; 52 | student_t *stud = NULL, *prev = NULL; 53 | student_t *first = NULL; 54 | for( ; i < 120; i++){ 55 | stud = xcalloc("student_t", 1); 56 | if(i == 0) 57 | first = stud; 58 | assert(stud); 59 | if(prev){ 60 | prev->next = stud; 61 | } 62 | prev = stud; 63 | } 64 | mm_print_memory_usage(0); 65 | mm_print_block_usage(); 66 | #if 1 67 | i = 0; 68 | student_t *next = NULL; 69 | for( ; first; first = next){ 70 | next = first->next; 71 | if(1 || i%4 == 0) 72 | xfree(first); 73 | i++; 74 | } 75 | mm_print_memory_usage(0); 76 | mm_print_block_usage(); 77 | #endif 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /uapi_mm_new.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: uapi_mm.h 5 | * 6 | * Description: This Header file ocntains public APIs to be used by the application 7 | * 8 | * Version: 1.0 9 | * Created: 02/01/2020 10:00:27 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Juniper Networks (https://csepracticals.wixsite.com/csepracticals), sachinites@gmail.com 14 | * Company: Juniper Networks 15 | * 16 | * This file is part of the Linux Memory Manager distribution (https://github.com/sachinites) 17 | * Copyright (c) 2019 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General 19 | * Public License as published by the Free Software Foundation, version 3. 20 | * 21 | * This program is distributed in the hope that it will be useful, but 22 | * WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 | * General Public License for more details. 25 | * 26 | * visit website : https://csepracticals.wixsite.com/csepracticals for more courses and projects 27 | * 28 | * ===================================================================================== 29 | */ 30 | 31 | #ifndef __UAPI_MM__ 32 | #define __UAPI_MM__ 33 | 34 | #include 35 | 36 | void * 37 | xcalloc(char *struct_name, int units); 38 | 39 | void 40 | xfree(void *app_ptr); 41 | 42 | /*Printing Functions*/ 43 | void mm_print_memory_usage(); 44 | void mm_print_block_usage(); 45 | 46 | /*Initialization Functions*/ 47 | void 48 | mm_init(); 49 | 50 | /*Registration function*/ 51 | void 52 | mm_instantiate_new_page_family( 53 | char *struct_name, 54 | uint32_t struct_size); 55 | 56 | #define XCALLOC(units, struct_name) \ 57 | (xcalloc(#struct_name, units)) 58 | 59 | #define MM_REG_STRUCT(struct_name) \ 60 | (mm_instantiate_new_page_family(#struct_name, sizeof(struct_name))) 61 | 62 | #define XFREE(ptr) \ 63 | xfree(ptr) 64 | 65 | #endif /* __UAPI_MM__ */ 66 | -------------------------------------------------------------------------------- /uapi_mm_old.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: uapi_mm.h 5 | * 6 | * Description: This Header file ocntains public APIs to be used by the application 7 | * 8 | * Version: 1.0 9 | * Created: 02/01/2020 10:00:27 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Juniper Networks (https://csepracticals.wixsite.com/csepracticals), sachinites@gmail.com 14 | * Company: Juniper Networks 15 | * 16 | * This file is part of the Linux Memory Manager distribution (https://github.com/sachinites) 17 | * Copyright (c) 2019 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General 19 | * Public License as published by the Free Software Foundation, version 3. 20 | * 21 | * This program is distributed in the hope that it will be useful, but 22 | * WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 | * General Public License for more details. 25 | * 26 | * visit website : https://csepracticals.wixsite.com/csepracticals for more courses and projects 27 | * 28 | * ===================================================================================== 29 | */ 30 | 31 | #ifndef __UAPI_MM__ 32 | #define __UAPI_MM__ 33 | 34 | #include 35 | 36 | void * 37 | xcalloc(char *struct_name, int units); 38 | 39 | void 40 | xfree(void *app_ptr); 41 | 42 | /*Printing Functions*/ 43 | void mm_print_memory_usage(); 44 | void mm_print_block_usage(); 45 | 46 | /*Initialization Functions*/ 47 | void 48 | mm_init(); 49 | 50 | /*Registration function*/ 51 | void 52 | mm_instantiate_new_page_family( 53 | char *struct_name, 54 | uint32_t struct_size); 55 | 56 | #define XCALLOC(units, struct_name) \ 57 | (calloc(units, sizeof(struct_name))) 58 | 59 | #define MM_REG_STRUCT(struct_name) \ 60 | (mm_instantiate_new_page_family(#struct_name, sizeof(struct_name))) 61 | 62 | #define XFREE(ptr) \ 63 | free(ptr) 64 | 65 | #endif /* __UAPI_MM__ */ 66 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/UdemyCourse/compilation Steps: -------------------------------------------------------------------------------- 1 | vm@ubuntu:~/src/LinuxMemoryManager/Course/LinuxMemoryManager/UdemyCourse$ gcc -g -c testapp.c -o testapp.o 2 | vm@ubuntu:~/src/LinuxMemoryManager/Course/LinuxMemoryManager/UdemyCourse$ gcc -g -c mm.c -o mm.o 3 | vm@ubuntu:~/src/LinuxMemoryManager/Course/LinuxMemoryManager/UdemyCourse$ gcc -g -c gluethread/glthread.c -o gluethread/glthread.o 4 | vm@ubuntu:~/src/LinuxMemoryManager/Course/LinuxMemoryManager/UdemyCourse$ gcc -g gluethread/glthread.o mm.o testapp.o -o test.exe 5 | vm@ubuntu:~/src/LinuxMemoryManager/Course/LinuxMemoryManager/UdemyCourse$ ./test.exe 6 | Page Family : emp_t, Size = 36 7 | Page Family : student_t, Size = 56 8 | 9 | SCENARIO 1 : *********** 10 | 11 | Page Size = 4096 Bytes 12 | vm_page_family : emp_t, struct size = 36 13 | next = (nil), prev = (nil) 14 | page family = emp_t 15 | 0x7f6bbb5ad018 Block 0 ALLOCATED block_size = 36 offset = 24 prev = (nil) next = 0x7f6bbb5ad06c 16 | 0x7f6bbb5ad06c Block 1 ALLOCATED block_size = 36 offset = 108 prev = 0x7f6bbb5ad018 next = 0x7f6bbb5ad0c0 17 | 0x7f6bbb5ad0c0 Block 2 ALLOCATED block_size = 36 offset = 192 prev = 0x7f6bbb5ad06c next = 0x7f6bbb5ad114 18 | 0x7f6bbb5ad114 Block 3 F R E E D block_size = 3772 offset = 276 prev = 0x7f6bbb5ad0c0 next = (nil) 19 | 20 | vm_page_family : student_t, struct size = 56 21 | next = (nil), prev = (nil) 22 | page family = student_t 23 | 0x7f6bbb5ac018 Block 0 ALLOCATED block_size = 56 offset = 24 prev = (nil) next = 0x7f6bbb5ac080 24 | 0x7f6bbb5ac080 Block 1 ALLOCATED block_size = 56 offset = 128 prev = 0x7f6bbb5ac018 next = 0x7f6bbb5ac0e8 25 | 0x7f6bbb5ac0e8 Block 2 F R E E D block_size = 3816 offset = 232 prev = 0x7f6bbb5ac080 next = (nil) 26 | 27 | # Of VM Pages in Use : 2 (8192 Bytes) 28 | Total Memory being used by Memory Manager = 8192 Bytes 29 | emp_t TBC : 4 FBC : 1 OBC : 3 AppMemUsage : 252 30 | student_t TBC : 3 FBC : 1 OBC : 2 AppMemUsage : 208 31 | -------------------------------------------------------------------------------- /uapi_mm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: uapi_mm.h 5 | * 6 | * Description: This Header file ocntains public APIs to be used by the application 7 | * 8 | * Version: 1.0 9 | * Created: 02/01/2020 10:00:27 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Juniper Networks (https://csepracticals.wixsite.com/csepracticals), sachinites@gmail.com 14 | * Company: Juniper Networks 15 | * 16 | * This file is part of the Linux Memory Manager distribution (https://github.com/sachinites) 17 | * Copyright (c) 2019 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General 19 | * Public License as published by the Free Software Foundation, version 3. 20 | * 21 | * This program is distributed in the hope that it will be useful, but 22 | * WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 | * General Public License for more details. 25 | * 26 | * visit website : https://csepracticals.wixsite.com/csepracticals for more courses and projects 27 | * 28 | * ===================================================================================== 29 | */ 30 | 31 | #ifndef __UAPI_MM__ 32 | #define __UAPI_MM__ 33 | 34 | #include 35 | 36 | void * 37 | xcalloc(char *struct_name, int units); 38 | 39 | void 40 | xfree(void *app_ptr); 41 | 42 | void 43 | mm_instantiate_new_page_family( 44 | char *struct_name, 45 | uint32_t struct_size); 46 | 47 | 48 | /* 49 | * Public APIs Exposed to the Application using Memory Manager 50 | */ 51 | 52 | /*Printing Functions*/ 53 | void mm_print_memory_usage(char *struct_name); 54 | void mm_print_block_usage(); 55 | 56 | /*Initialization Functions*/ 57 | void 58 | mm_init(); 59 | 60 | /*Registration function*/ 61 | #define MM_REG_STRUCT(struct_name) \ 62 | (mm_instantiate_new_page_family(#struct_name, sizeof(struct_name))) 63 | 64 | /*Allocators and De-Allocators*/ 65 | #define XCALLOC(units, struct_name) \ 66 | (xcalloc(#struct_name, units)) 67 | 68 | #define XFREE(ptr) \ 69 | xfree(ptr) 70 | 71 | #endif /* __UAPI_MM__ */ 72 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/testapp.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "uapi_mm.h" 5 | 6 | typedef struct emp_ { 7 | 8 | char name[32]; 9 | uint32_t emp_id; 10 | } emp_t; 11 | 12 | typedef struct student_ { 13 | 14 | char name[32]; 15 | uint32_t rollno; 16 | uint32_t marks_phys; 17 | uint32_t marks_chem; 18 | uint32_t marks_maths; 19 | struct student_ *next; 20 | } student_t; 21 | 22 | int 23 | main(int argc, char **argv){ 24 | 25 | mm_init(); 26 | MM_REG_STRUCT(0, emp_t); 27 | MM_REG_STRUCT(0, student_t); 28 | mm_print_registered_page_families(0); 29 | 30 | mm_print_memory_usage(0, 0); 31 | mm_print_block_usage(0); 32 | 33 | char *buff1 = XCALLOC_BUFF(0, 32); 34 | char *buff2 = XCALLOC_BUFF(0, 32); 35 | assert(buff1); 36 | assert(buff2); 37 | mm_print_variable_buffers(0); 38 | xfree(buff1); 39 | xfree(buff2); 40 | mm_print_variable_buffers(0); 41 | #if 0 42 | mm_print_memory_usage(0); 43 | mm_print_block_usage(); 44 | 45 | 46 | emp_t *emp1 = xcalloc("emp_t", 1); 47 | emp_t *emp2 = xcalloc("emp_t", 1); 48 | emp_t *emp3 = xcalloc("emp_t", 1); 49 | emp_t *emp4 = xcalloc("emp_t", 3); 50 | student_t *stud1 = xcalloc("student_t", 1); 51 | student_t *stud2 = xcalloc("student_t", 2); 52 | student_t *stud3 = xcalloc("student_t", 1); 53 | mm_print_memory_usage(); 54 | xfree(emp1); 55 | mm_print_memory_usage(); 56 | xfree(emp2); 57 | mm_print_memory_usage(); 58 | xfree(emp3); 59 | mm_print_memory_usage(); 60 | xfree(emp4); 61 | mm_print_memory_usage(); 62 | xfree(stud1); 63 | mm_print_memory_usage(); 64 | xfree(stud2); 65 | mm_print_memory_usage(); 66 | xfree(stud3); 67 | mm_print_memory_usage(); 68 | 69 | int i = 0; 70 | student_t *stud = NULL, *prev = NULL; 71 | student_t *first = NULL; 72 | for( ; i < 120; i++){ 73 | stud = xcalloc("student_t", 1); 74 | if(i == 0) 75 | first = stud; 76 | assert(stud); 77 | if(prev){ 78 | prev->next = stud; 79 | } 80 | prev = stud; 81 | } 82 | mm_print_memory_usage(0); 83 | mm_print_block_usage(); 84 | #endif 85 | #if 0 86 | i = 0; 87 | student_t *next = NULL; 88 | for( ; first; first = next){ 89 | next = first->next; 90 | if(1 || i%4 == 0) 91 | xfree(first); 92 | i++; 93 | } 94 | mm_print_memory_usage(0); 95 | mm_print_block_usage(); 96 | #endif 97 | return 0; 98 | } 99 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/uapi_mm_old.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: uapi_mm.h 5 | * 6 | * Description: This Header file ocntains public APIs to be used by the application 7 | * 8 | * Version: 1.0 9 | * Created: 02/01/2020 10:00:27 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Juniper Networks (https://csepracticals.wixsite.com/csepracticals), sachinites@gmail.com 14 | * Company: Juniper Networks 15 | * 16 | * This file is part of the Linux Memory Manager distribution (https://github.com/sachinites) 17 | * Copyright (c) 2019 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General 19 | * Public License as published by the Free Software Foundation, version 3. 20 | * 21 | * This program is distributed in the hope that it will be useful, but 22 | * WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 | * General Public License for more details. 25 | * 26 | * visit website : https://csepracticals.wixsite.com/csepracticals for more courses and projects 27 | * 28 | * ===================================================================================== 29 | */ 30 | 31 | #ifndef __UAPI_MM__ 32 | #define __UAPI_MM__ 33 | 34 | #include 35 | typedef struct mm_instance_ mm_instance_t; 36 | 37 | void * 38 | xcalloc(mm_instance_t *mm_inst, char *struct_name, int units); 39 | void * 40 | xcalloc_buff(mm_instance_t *mm_inst, uint32_t bytes) ; 41 | void 42 | xfree(void *app_ptr); 43 | 44 | /*Printing Functions*/ 45 | void mm_print_memory_usage(mm_instance_t *mm_inst, char *struct_name); 46 | void mm_print_block_usage(mm_instance_t *mm_inst); 47 | void mm_print_registered_page_families(mm_instance_t *mm_inst); 48 | void mm_print_variable_buffers(mm_instance_t *mm_inst); 49 | 50 | /*Initialization Functions*/ 51 | void 52 | mm_init(); 53 | 54 | mm_instance_t * 55 | mm_init_new_instance(); 56 | 57 | /*Registration function*/ 58 | void 59 | mm_instantiate_new_page_family( 60 | mm_instance_t *mm_inst, 61 | char *struct_name, 62 | uint32_t struct_size); 63 | 64 | #define XCALLOC(mm_inst, units, struct_name) \ 65 | (calloc(units, sizeof(struct_name))) 66 | 67 | #define XCALLOC_BUFF(mm_inst, size_in_bytes) \ 68 | (calloc(1, size_in_bytes)) 69 | 70 | #define MM_REG_STRUCT(mm_inst, struct_name) \ 71 | (mm_instantiate_new_page_family(mm_inst, #struct_name, sizeof(struct_name))) 72 | 73 | #define XFREE(ptr) \ 74 | free(ptr) 75 | 76 | #endif /* __UAPI_MM__ */ 77 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/uapi_mm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: uapi_mm.h 5 | * 6 | * Description: This Header file ocntains public APIs to be used by the application 7 | * 8 | * Version: 1.0 9 | * Created: 02/01/2020 10:00:27 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Juniper Networks (https://csepracticals.wixsite.com/csepracticals), sachinites@gmail.com 14 | * Company: Juniper Networks 15 | * 16 | * This file is part of the Linux Memory Manager distribution (https://github.com/sachinites) 17 | * Copyright (c) 2019 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General 19 | * Public License as published by the Free Software Foundation, version 3. 20 | * 21 | * This program is distributed in the hope that it will be useful, but 22 | * WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 | * General Public License for more details. 25 | * 26 | * visit website : https://csepracticals.wixsite.com/csepracticals for more courses and projects 27 | * 28 | * ===================================================================================== 29 | */ 30 | 31 | #ifndef __UAPI_MM__ 32 | #define __UAPI_MM__ 33 | 34 | #include 35 | typedef struct mm_instance_ mm_instance_t; 36 | 37 | void * 38 | xcalloc(mm_instance_t *mm_inst, char *struct_name, int units); 39 | void * 40 | xcalloc_buff(mm_instance_t *mm_inst, uint32_t bytes) ; 41 | void 42 | xfree(void *app_ptr); 43 | 44 | /*Printing Functions*/ 45 | void mm_print_memory_usage(mm_instance_t *mm_inst, char *struct_name); 46 | void mm_print_block_usage(mm_instance_t *mm_inst); 47 | void mm_print_registered_page_families(mm_instance_t *mm_inst); 48 | void mm_print_variable_buffers(mm_instance_t *mm_inst); 49 | 50 | /*Initialization Functions*/ 51 | void 52 | mm_init(); 53 | 54 | mm_instance_t * 55 | mm_init_new_instance(); 56 | 57 | /*Registration function*/ 58 | void 59 | mm_instantiate_new_page_family( 60 | mm_instance_t *mm_inst, 61 | char *struct_name, 62 | uint32_t struct_size); 63 | 64 | #define XCALLOC(mm_inst, units, struct_name) \ 65 | (xcalloc(mm_inst, #struct_name, units)) 66 | 67 | #define XCALLOC_BUFF(mm_inst, size_in_bytes) \ 68 | (xcalloc_buff(mm_inst, size_in_bytes) ) 69 | 70 | #define MM_REG_STRUCT(mm_inst, struct_name) \ 71 | (mm_instantiate_new_page_family(mm_inst, #struct_name, sizeof(struct_name))) 72 | 73 | #define XFREE(ptr) \ 74 | xfree(ptr) 75 | 76 | #endif /* __UAPI_MM__ */ 77 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/uapi_mm_new.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: uapi_mm.h 5 | * 6 | * Description: This Header file ocntains public APIs to be used by the application 7 | * 8 | * Version: 1.0 9 | * Created: 02/01/2020 10:00:27 PM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Juniper Networks (https://csepracticals.wixsite.com/csepracticals), sachinites@gmail.com 14 | * Company: Juniper Networks 15 | * 16 | * This file is part of the Linux Memory Manager distribution (https://github.com/sachinites) 17 | * Copyright (c) 2019 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General 19 | * Public License as published by the Free Software Foundation, version 3. 20 | * 21 | * This program is distributed in the hope that it will be useful, but 22 | * WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 | * General Public License for more details. 25 | * 26 | * visit website : https://csepracticals.wixsite.com/csepracticals for more courses and projects 27 | * 28 | * ===================================================================================== 29 | */ 30 | 31 | #ifndef __UAPI_MM__ 32 | #define __UAPI_MM__ 33 | 34 | #include 35 | typedef struct mm_instance_ mm_instance_t; 36 | 37 | void * 38 | xcalloc(mm_instance_t *mm_inst, char *struct_name, int units); 39 | void * 40 | xcalloc_buff(mm_instance_t *mm_inst, uint32_t bytes) ; 41 | void 42 | xfree(void *app_ptr); 43 | 44 | /*Printing Functions*/ 45 | void mm_print_memory_usage(mm_instance_t *mm_inst, char *struct_name); 46 | void mm_print_block_usage(mm_instance_t *mm_inst); 47 | void mm_print_registered_page_families(mm_instance_t *mm_inst); 48 | void mm_print_variable_buffers(mm_instance_t *mm_inst); 49 | 50 | /*Initialization Functions*/ 51 | void 52 | mm_init(); 53 | 54 | mm_instance_t * 55 | mm_init_new_instance(); 56 | 57 | /*Registration function*/ 58 | void 59 | mm_instantiate_new_page_family( 60 | mm_instance_t *mm_inst, 61 | char *struct_name, 62 | uint32_t struct_size); 63 | 64 | #define XCALLOC(mm_inst, units, struct_name) \ 65 | (xcalloc(mm_inst, #struct_name, units)) 66 | 67 | #define XCALLOC_BUFF(mm_inst, size_in_bytes) \ 68 | (xcalloc_buff(mm_inst, size_in_bytes) ) 69 | 70 | #define MM_REG_STRUCT(mm_inst, struct_name) \ 71 | (mm_instantiate_new_page_family(mm_inst, #struct_name, sizeof(struct_name))) 72 | 73 | #define XFREE(ptr) \ 74 | xfree(ptr) 75 | 76 | #endif /* __UAPI_MM__ */ 77 | -------------------------------------------------------------------------------- /gluethread/test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: test.c 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: Monday 12 March 2018 02:15:28 IST 10 | * Revision: 1.0 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Networking Developer (AS), sachinites@gmail.com 14 | * Company: Brocade Communications(Jul 2012- Mar 2016), Current : Juniper Networks(Apr 2017 - Present) 15 | * 16 | * This file is part of the XXX distribution (https://github.com/sachinites). 17 | * Copyright (c) 2017 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify 19 | * it under the terms of the GNU General Public License as published by 20 | * the Free Software Foundation, version 3. 21 | * 22 | * This program is distributed in the hope that it will be useful, but 23 | * WITHOUT ANY WARRANTY; without even the implied warranty of 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 25 | * General Public License for more details. 26 | * 27 | * You should have received a copy of the GNU General Public License 28 | * along with this program. If not, see . 29 | * 30 | * ===================================================================================== 31 | */ 32 | 33 | #include "glthread.h" 34 | #include 35 | #include 36 | #include 37 | 38 | typedef struct _person{ 39 | 40 | int age; 41 | int weight; 42 | glthread_t glthread; 43 | } person_t ; 44 | 45 | int 46 | senior_citizen(person_t *p1, person_t *p2){ 47 | 48 | if(p1->age == p2->age) return 0; 49 | if(p1->age < p2->age) return 1; 50 | return -1; 51 | } 52 | 53 | #define offset(struct_name, fld_name) \ 54 | (unsigned int)&(((struct_name *)0)->fld_name) 55 | 56 | GLTHREAD_TO_STRUCT(thread_to_person, person_t, glthread, glthreadptr); 57 | 58 | int main(int argc, char **argv){ 59 | 60 | person_t person[5]; 61 | memset(person, 0, sizeof(person_t) * 5); 62 | person[0].age = 1; 63 | person[0].weight = 2; 64 | person[1].age = 3; 65 | person[1].weight = 4; 66 | person[2].age = 5; 67 | person[2].weight = 6; 68 | person[3].age = 7; 69 | person[3].weight = 8; 70 | person[4].age = 9; 71 | person[4].weight = 10; 72 | 73 | glthread_t base_glthread; 74 | init_glthread(&base_glthread); 75 | 76 | glthread_priority_insert(&base_glthread, &person[4].glthread, senior_citizen, offset(person_t, glthread)); 77 | glthread_priority_insert(&base_glthread, &person[3].glthread, senior_citizen, offset(person_t, glthread)); 78 | glthread_priority_insert(&base_glthread, &person[2].glthread, senior_citizen, offset(person_t, glthread)); 79 | glthread_priority_insert(&base_glthread, &person[1].glthread, senior_citizen, offset(person_t, glthread)); 80 | glthread_priority_insert(&base_glthread, &person[0].glthread, senior_citizen, offset(person_t, glthread)); 81 | 82 | glthread_t *curr = NULL; 83 | ITERATE_GLTHREAD_BEGIN(&base_glthread, curr){ 84 | 85 | person_t *p = thread_to_person(curr); 86 | printf("Age = %d\n", p->age); 87 | } ITERATE_GLTHREAD_END(&base_glthread, curr); 88 | 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/gluethread/test.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: test.c 5 | * 6 | * Description: 7 | * 8 | * Version: 1.0 9 | * Created: Monday 12 March 2018 02:15:28 IST 10 | * Revision: 1.0 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Networking Developer (AS), sachinites@gmail.com 14 | * Company: Brocade Communications(Jul 2012- Mar 2016), Current : Juniper Networks(Apr 2017 - Present) 15 | * 16 | * This file is part of the XXX distribution (https://github.com/sachinites). 17 | * Copyright (c) 2017 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify 19 | * it under the terms of the GNU General Public License as published by 20 | * the Free Software Foundation, version 3. 21 | * 22 | * This program is distributed in the hope that it will be useful, but 23 | * WITHOUT ANY WARRANTY; without even the implied warranty of 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 25 | * General Public License for more details. 26 | * 27 | * You should have received a copy of the GNU General Public License 28 | * along with this program. If not, see . 29 | * 30 | * ===================================================================================== 31 | */ 32 | 33 | #include "glthread.h" 34 | #include 35 | #include 36 | #include 37 | 38 | typedef struct _person{ 39 | 40 | int age; 41 | int weight; 42 | glthread_t glthread; 43 | } person_t ; 44 | 45 | int 46 | senior_citizen(person_t *p1, person_t *p2){ 47 | 48 | if(p1->age == p2->age) return 0; 49 | if(p1->age < p2->age) return 1; 50 | return -1; 51 | } 52 | 53 | #define offset(struct_name, fld_name) \ 54 | (unsigned int)&(((struct_name *)0)->fld_name) 55 | 56 | GLTHREAD_TO_STRUCT(thread_to_person, person_t, glthread); 57 | 58 | int main(int argc, char **argv){ 59 | 60 | person_t person[5]; 61 | memset(person, 0, sizeof(person_t) * 5); 62 | person[0].age = 1; 63 | person[0].weight = 2; 64 | person[1].age = 3; 65 | person[1].weight = 4; 66 | person[2].age = 5; 67 | person[2].weight = 6; 68 | person[3].age = 7; 69 | person[3].weight = 8; 70 | person[4].age = 9; 71 | person[4].weight = 10; 72 | 73 | glthread_t base_glthread; 74 | init_glthread(&base_glthread); 75 | 76 | glthread_priority_insert(&base_glthread, &person[4].glthread, senior_citizen, offset(person_t, glthread)); 77 | glthread_priority_insert(&base_glthread, &person[3].glthread, senior_citizen, offset(person_t, glthread)); 78 | glthread_priority_insert(&base_glthread, &person[2].glthread, senior_citizen, offset(person_t, glthread)); 79 | glthread_priority_insert(&base_glthread, &person[1].glthread, senior_citizen, offset(person_t, glthread)); 80 | glthread_priority_insert(&base_glthread, &person[0].glthread, senior_citizen, offset(person_t, glthread)); 81 | 82 | glthread_t *curr = NULL; 83 | ITERATE_GLTHREAD_BEGIN(&base_glthread, curr){ 84 | 85 | person_t *p = thread_to_person(curr); 86 | printf("Age = %d\n", p->age); 87 | } ITERATE_GLTHREAD_END(&base_glthread, curr); 88 | 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /gluethread/glthread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: glthread.h 5 | * 6 | * Description: This file defines the Data structure and APIs for Glue thread 7 | * 8 | * Version: 1.0 9 | * Created: Monday 12 March 2018 02:01:51 IST 10 | * Revision: 1.0 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Networking Developer (AS), sachinites@gmail.com 14 | * Company: Brocade Communications(Jul 2012- Mar 2016), Current : Juniper Networks(Apr 2017 - Present) 15 | * 16 | * This file is part of the SPFComputation distribution (https://github.com/sachinites). 17 | * Copyright (c) 2017 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify 19 | * it under the terms of the GNU General Public License as published by 20 | * the Free Software Foundation, version 3. 21 | * 22 | * This program is distributed in the hope that it will be useful, but 23 | * WITHOUT ANY WARRANTY; without even the implied warranty of 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 25 | * General Public License for more details. 26 | * 27 | * You should have received a copy of the GNU General Public License 28 | * along with this program. If not, see . 29 | * 30 | * ===================================================================================== 31 | */ 32 | 33 | #ifndef __GLUETHREAD__ 34 | #define __GLUETHREAD__ 35 | 36 | typedef struct _glthread{ 37 | 38 | struct _glthread *left; 39 | struct _glthread *right; 40 | } glthread_t; 41 | 42 | void 43 | glthread_add_next(glthread_t *base_glthread, glthread_t *new_glthread); 44 | 45 | void 46 | glthread_add_before(glthread_t *base_glthread, glthread_t *new_glthread); 47 | 48 | void 49 | remove_glthread(glthread_t *glthread); 50 | 51 | void 52 | init_glthread(glthread_t *glthread); 53 | 54 | void 55 | glthread_add_last(glthread_t *base_glthread, glthread_t *new_glthread); 56 | 57 | #define IS_GLTHREAD_LIST_EMPTY(glthreadptr) \ 58 | ((glthreadptr)->right == 0 && (glthreadptr)->left == 0) 59 | 60 | #define GLTHREAD_TO_STRUCT(fn_name, structure_name, field_name, glthreadptr) \ 61 | static inline structure_name * fn_name(glthread_t *glthreadptr){ \ 62 | return (structure_name *)((char *)(glthreadptr) - (char *)&(((structure_name *)0)->field_name)); \ 63 | } 64 | 65 | /* delete safe loop*/ 66 | /*Normal continue and break can be used with this loop macro*/ 67 | 68 | #define BASE(glthreadptr) ((glthreadptr)->right) 69 | 70 | #define ITERATE_GLTHREAD_BEGIN(glthreadptrstart, glthreadptr) \ 71 | { \ 72 | glthread_t *_glthread_ptr = NULL; \ 73 | glthreadptr = BASE(glthreadptrstart); \ 74 | for(; glthreadptr!= NULL; glthreadptr = _glthread_ptr){ \ 75 | _glthread_ptr = (glthreadptr)->right; 76 | 77 | #define ITERATE_GLTHREAD_END(glthreadptrstart, glthreadptr) \ 78 | }} 79 | 80 | #define GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthreadptr, offset) \ 81 | (void *)((char *)(glthreadptr) - offset) 82 | 83 | void 84 | delete_glthread_list(glthread_t *base_glthread); 85 | 86 | unsigned int 87 | get_glthread_list_count(glthread_t *base_glthread); 88 | 89 | void 90 | glthread_priority_insert(glthread_t *base_glthread, 91 | glthread_t *glthread, 92 | int (*comp_fn)(void *, void *), 93 | int offset); 94 | 95 | 96 | #if 0 97 | void * 98 | gl_thread_search(glthread_t *base_glthread, 99 | void *(*thread_to_struct_fn)(glthread_t *), 100 | void *key, 101 | int (*comparison_fn)(void *, void *)); 102 | 103 | #endif 104 | #endif /* __GLUETHREAD__ */ 105 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/UdemyCourse/gluethread/glthread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: glthread.h 5 | * 6 | * Description: This file defines the Data structure and APIs for Glue thread 7 | * 8 | * Version: 1.0 9 | * Created: Monday 12 March 2018 02:01:51 IST 10 | * Revision: 1.0 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Networking Developer (AS), sachinites@gmail.com 14 | * Company: Brocade Communications(Jul 2012- Mar 2016), Current : Juniper Networks(Apr 2017 - Present) 15 | * 16 | * This file is part of the SPFComputation distribution (https://github.com/sachinites). 17 | * Copyright (c) 2017 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify 19 | * it under the terms of the GNU General Public License as published by 20 | * the Free Software Foundation, version 3. 21 | * 22 | * This program is distributed in the hope that it will be useful, but 23 | * WITHOUT ANY WARRANTY; without even the implied warranty of 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 25 | * General Public License for more details. 26 | * 27 | * You should have received a copy of the GNU General Public License 28 | * along with this program. If not, see . 29 | * 30 | * ===================================================================================== 31 | */ 32 | 33 | #ifndef __GLUETHREAD__ 34 | #define __GLUETHREAD__ 35 | 36 | typedef struct _glthread{ 37 | 38 | struct _glthread *left; 39 | struct _glthread *right; 40 | } glthread_t; 41 | 42 | void 43 | glthread_add_next(glthread_t *base_glthread, glthread_t *new_glthread); 44 | 45 | void 46 | glthread_add_before(glthread_t *base_glthread, glthread_t *new_glthread); 47 | 48 | void 49 | remove_glthread(glthread_t *glthread); 50 | 51 | void 52 | init_glthread(glthread_t *glthread); 53 | 54 | void 55 | glthread_add_last(glthread_t *base_glthread, glthread_t *new_glthread); 56 | 57 | #define IS_GLTHREAD_LIST_EMPTY(glthreadptr) \ 58 | ((glthreadptr)->right == 0 && (glthreadptr)->left == 0) 59 | 60 | #define GLTHREAD_TO_STRUCT(fn_name, structure_name, field_name, glthreadptr) \ 61 | static inline structure_name * fn_name(glthread_t *glthreadptr){ \ 62 | return (structure_name *)((char *)(glthreadptr) - (char *)&(((structure_name *)0)->field_name)); \ 63 | } 64 | 65 | /* delete safe loop*/ 66 | /*Normal continue and break can be used with this loop macro*/ 67 | 68 | #define BASE(glthreadptr) ((glthreadptr)->right) 69 | 70 | #define ITERATE_GLTHREAD_BEGIN(glthreadptrstart, glthreadptr) \ 71 | { \ 72 | glthread_t *_glthread_ptr = NULL; \ 73 | glthreadptr = BASE(glthreadptrstart); \ 74 | for(; glthreadptr!= NULL; glthreadptr = _glthread_ptr){ \ 75 | _glthread_ptr = (glthreadptr)->right; 76 | 77 | #define ITERATE_GLTHREAD_END(glthreadptrstart, glthreadptr) \ 78 | }} 79 | 80 | #define GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthreadptr, offset) \ 81 | (void *)((char *)(glthreadptr) - offset) 82 | 83 | void 84 | delete_glthread_list(glthread_t *base_glthread); 85 | 86 | unsigned int 87 | get_glthread_list_count(glthread_t *base_glthread); 88 | 89 | void 90 | glthread_priority_insert(glthread_t *base_glthread, 91 | glthread_t *glthread, 92 | int (*comp_fn)(void *, void *), 93 | int offset); 94 | 95 | 96 | #if 0 97 | void * 98 | gl_thread_search(glthread_t *base_glthread, 99 | void *(*thread_to_struct_fn)(glthread_t *), 100 | void *key, 101 | int (*comparison_fn)(void *, void *)); 102 | 103 | #endif 104 | #endif /* __GLUETHREAD__ */ 105 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/gluethread/glthread.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: glthread.h 5 | * 6 | * Description: This file defines the Data structure and APIs for Glue thread 7 | * 8 | * Version: 1.0 9 | * Created: Monday 12 March 2018 02:01:51 IST 10 | * Revision: 1.0 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Networking Developer (AS), sachinites@gmail.com 14 | * Company: Brocade Communications(Jul 2012- Mar 2016), Current : Juniper Networks(Apr 2017 - Present) 15 | * 16 | * This file is part of the SPFComputation distribution (https://github.com/sachinites). 17 | * Copyright (c) 2017 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify 19 | * it under the terms of the GNU General Public License as published by 20 | * the Free Software Foundation, version 3. 21 | * 22 | * This program is distributed in the hope that it will be useful, but 23 | * WITHOUT ANY WARRANTY; without even the implied warranty of 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 25 | * General Public License for more details. 26 | * 27 | * You should have received a copy of the GNU General Public License 28 | * along with this program. If not, see . 29 | * 30 | * ===================================================================================== 31 | */ 32 | 33 | #ifndef __GLUETHREAD__ 34 | #define __GLUETHREAD__ 35 | 36 | typedef struct _glthread{ 37 | 38 | struct _glthread *left; 39 | struct _glthread *right; 40 | } glthread_t; 41 | 42 | void 43 | glthread_add_next(glthread_t *base_glthread, glthread_t *new_glthread); 44 | 45 | void 46 | glthread_add_before(glthread_t *base_glthread, glthread_t *new_glthread); 47 | 48 | void 49 | remove_glthread(glthread_t *glthread); 50 | 51 | void 52 | init_glthread(glthread_t *glthread); 53 | 54 | void 55 | glthread_add_last(glthread_t *base_glthread, glthread_t *new_glthread); 56 | 57 | #define IS_QUEUED_UP_IN_THREAD(glthreadptr) \ 58 | (!((glthreadptr)->right == 0 && (glthreadptr)->left == 0)) 59 | 60 | #define IS_GLTHREAD_LIST_EMPTY(glthreadptr) \ 61 | ((glthreadptr)->right == 0 && (glthreadptr)->left == 0) 62 | 63 | #define GLTHREAD_TO_STRUCT(fn_name, structure_name, field_name) \ 64 | static inline structure_name * fn_name(glthread_t *glthreadptr){ \ 65 | return (structure_name *)((char *)(glthreadptr) - (char *)&(((structure_name *)0)->field_name)); \ 66 | } 67 | 68 | /* delete safe loop*/ 69 | /*Normal continue and break can be used with this loop macro*/ 70 | 71 | #define BASE(glthreadptr) ((glthreadptr)->right) 72 | 73 | #define ITERATE_GLTHREAD_BEGIN(glthreadptrstart, glthreadptr) \ 74 | { \ 75 | glthread_t *_glthread_ptr = NULL; \ 76 | glthreadptr = BASE(glthreadptrstart); \ 77 | for(; glthreadptr!= NULL; glthreadptr = _glthread_ptr){ \ 78 | _glthread_ptr = (glthreadptr)->right; 79 | 80 | #define ITERATE_GLTHREAD_END(glthreadptrstart, glthreadptr) \ 81 | }} 82 | 83 | #define GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthreadptr, offset) \ 84 | (void *)((char *)(glthreadptr) - offset) 85 | 86 | void 87 | delete_glthread_list(glthread_t *base_glthread); 88 | 89 | unsigned int 90 | get_glthread_list_count(glthread_t *base_glthread); 91 | 92 | void 93 | glthread_priority_insert(glthread_t *base_glthread, 94 | glthread_t *glthread, 95 | int (*comp_fn)(void *, void *), 96 | int offset); 97 | 98 | glthread_t * 99 | dequeue_glthread_first(glthread_t *base_glthread); 100 | 101 | #if 0 102 | void * 103 | gl_thread_search(glthread_t *base_glthread, 104 | void *(*thread_to_struct_fn)(glthread_t *), 105 | void *key, 106 | int (*comparison_fn)(void *, void *)); 107 | 108 | #endif 109 | #endif /* __GLUETHREAD__ */ 110 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/UdemyCourse/block_split_ass.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | void 9 | print_meta_block_stats(block_meta_data_t *first_block_meta_data); 10 | 11 | static size_t SYSTEM_PAGE_SIZE = 0; 12 | 13 | typedef enum{ 14 | 15 | MM_FALSE, 16 | MM_TRUE 17 | } vm_bool_t; 18 | 19 | typedef struct block_meta_data_{ 20 | 21 | vm_bool_t is_free; 22 | uint32_t block_size; 23 | uint32_t offset; /*offset from the start of the page*/ 24 | struct block_meta_data_ *prev_block; 25 | struct block_meta_data_ *next_block; 26 | } block_meta_data_t; 27 | 28 | /*Function to request VM page from kernel*/ 29 | static void * 30 | mm_get_new_vm_page_from_kernel(int units){ 31 | 32 | char *vm_page = mmap( 33 | 0, 34 | units * SYSTEM_PAGE_SIZE, 35 | PROT_READ|PROT_WRITE|PROT_EXEC, 36 | MAP_ANON|MAP_PRIVATE, 37 | 0, 0); 38 | 39 | if(vm_page == MAP_FAILED){ 40 | printf("Error : VM Page allocation Failed\n"); 41 | return NULL; 42 | } 43 | memset(vm_page, 0, units * SYSTEM_PAGE_SIZE); 44 | return (void *)vm_page; 45 | } 46 | 47 | static void 48 | split_blocks(block_meta_data_t *block_meta_data){ 49 | 50 | /* 51 | This function fragments the VM page of size 4096B 52 | as per the below diagram 53 | +------------------+ 54 | | | 55 | | | 56 | | | 57 | | DB4 | 58 | | | 59 | | | 60 | | | 61 | +------------------+ 62 | | MB4 | 63 | | NF, 3264, 804 | 64 | +------------------+ 65 | | | 66 | | DB3 | 67 | | | 68 | +------------------+ 69 | | MB3 | 70 | |F, 220, 556 | 71 | +------------------+ 72 | | | 73 | | DB2 | 74 | | | 75 | | | 76 | +------------------+ 77 | | MB2 | 78 | | NF, 300, 228 | 79 | +------------------+ 80 | | | 81 | | DB1 | 82 | | | 83 | | | 84 | | | 85 | +------------------+ 86 | | MB1 | 87 | | F, 200, 0 | 88 | +------------------+ 89 | 90 | */ 91 | 92 | block_meta_data_t *MB1 = block_meta_data; 93 | MB1->is_free = MM_TRUE; 94 | MB1->block_size = 200; 95 | MB1->offset = 0; 96 | 97 | block_meta_data_t *MB2 = NEXT_META_BLOCK_BY_SIZE(MB1); 98 | MB2->is_free = MM_FALSE; 99 | MB2->block_size = 300; 100 | MB2->offset = MB1->offset + sizeof(block_meta_data_t) + MB1->block_size; 101 | 102 | mm_bind_blocks_for_allocation(MB1, MB2); 103 | 104 | block_meta_data_t *MB3 = NEXT_META_BLOCK_BY_SIZE(MB2); 105 | MB3->is_free = MM_FALSE; 106 | MB3->block_size = 220; 107 | MB3->offset = MB2->offset + sizeof(block_meta_data_t) + MB2->block_size; 108 | 109 | mm_bind_blocks_for_allocation(MB2, MB3); 110 | 111 | block_meta_data_t *MB4 = NEXT_META_BLOCK_BY_SIZE(MB3); 112 | MB4->is_free = MM_FALSE; 113 | MB4->block_size = 3264; 114 | MB4->offset = MB3->offset + sizeof(block_meta_data_t) + MB3->block_size; 115 | 116 | mm_bind_blocks_for_allocation(MB3, MB4); 117 | 118 | } 119 | 120 | static void 121 | print_one_meta_block_stats(block_meta_data_t *block_meta_data){ 122 | 123 | printf("is_free = %s size = %-5d offset = %-5d prev = %p next = %p\n", 124 | block_meta_data->is_free ? "TRUE", "FALSE", 125 | block_meta_data->block_size, 126 | block_meta_data->offset, 127 | block_meta_data->prev_block, 128 | block_meta_data->next_block); 129 | 130 | } 131 | 132 | static void 133 | print_all_meta_blocks(block_meta_data_t *first_meta_block){ 134 | 135 | 136 | while(first_meta_block){ 137 | 138 | print_one_meta_block_stats(block_meta_data); 139 | first_meta_block = first_meta_block->next_block; 140 | } 141 | } 142 | 143 | 144 | int 145 | main(int argc, char **argv){ 146 | 147 | SYSTEM_PAGE_SIZE = getpagesize(); 148 | 149 | void *vm_page = mm_get_new_vm_page_from_kernel(1); 150 | if(!vm_page){ 151 | printf("Error : Page allocation failed\n"); 152 | return 0; 153 | } 154 | 155 | /*Covert VM page into 1 single Free data block*/ 156 | block_meta_data_t *first_block_meta_data = 157 | (block_meta_data_t *)vm_page; 158 | 159 | first_block_meta_data->is_free = MM_TRUE; 160 | first_block_meta_data->block_size = SYSTEM_PAGE_SIZE - \ 161 | sizeof(block_meta_data_t); 162 | first_block_meta_data->offset = 0; 163 | first_block_meta_data->prev_block = 0; 164 | first_block_meta_data->next_block = 0; 165 | split_blocks(first_block_meta_data); 166 | print_all_meta_blocks(first_block_meta_data); 167 | return 0; 168 | } 169 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/UdemyCourse/mm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: mm.h 5 | * 6 | * Description: This file defines the public APIs and Data structures used for Memory Manager 7 | * 8 | * Version: 1.0 9 | * Created: 01/30/2020 10:11:20 AM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Juniper Networks (https://csepracticals.wixsite.com/csepracticals), sachinites@gmail.com 14 | * Company: Juniper Networks 15 | * 16 | * This file is part of the Linux Memory Manager distribution (https://github.com/sachinites) 17 | * Copyright (c) 2019 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General 19 | * Public License as published by the Free Software Foundation, version 3. 20 | * 21 | * This program is distributed in the hope that it will be useful, but 22 | * WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 | * General Public License for more details. 25 | * 26 | * visit website : https://csepracticals.wixsite.com/csepracticals for more courses and projects 27 | * 28 | * ===================================================================================== 29 | */ 30 | 31 | #ifndef __MM__ 32 | #define __MM__ 33 | 34 | #include "gluethread/glthread.h" 35 | #include /*uint32_t*/ 36 | 37 | typedef enum{ 38 | 39 | MM_FALSE, 40 | MM_TRUE 41 | } vm_bool_t; 42 | 43 | typedef struct block_meta_data_{ 44 | 45 | vm_bool_t is_free; 46 | uint32_t block_size; 47 | uint32_t offset; /*offset from the start of the page*/ 48 | glthread_t priority_thread_glue; 49 | struct block_meta_data_ *prev_block; 50 | struct block_meta_data_ *next_block; 51 | } block_meta_data_t; 52 | GLTHREAD_TO_STRUCT(glthread_to_block_meta_data, 53 | block_meta_data_t, priority_thread_glue, glthread_ptr); 54 | 55 | #define offset_of(container_structure, field_name) \ 56 | ((size_t)&(((container_structure *)0)->field_name)) 57 | 58 | /*Forward Declaration*/ 59 | struct vm_page_family_; 60 | 61 | typedef struct vm_page_{ 62 | struct vm_page_ *next; 63 | struct vm_page_ *prev; 64 | struct vm_page_family_ *pg_family; /*back pointer*/ 65 | block_meta_data_t block_meta_data; 66 | char page_memory[0]; 67 | } vm_page_t; 68 | 69 | #define MM_GET_PAGE_FROM_META_BLOCK(block_meta_data_ptr) \ 70 | ((void * )((char *)block_meta_data_ptr - block_meta_data_ptr->offset)) 71 | 72 | #define NEXT_META_BLOCK(block_meta_data_ptr) \ 73 | (block_meta_data_ptr->next_block) 74 | 75 | #define NEXT_META_BLOCK_BY_SIZE(block_meta_data_ptr) \ 76 | (block_meta_data_t *)((char *)(block_meta_data_ptr + 1) \ 77 | + block_meta_data_ptr->block_size) 78 | 79 | #define PREV_META_BLOCK(block_meta_data_ptr) \ 80 | (block_meta_data_ptr->prev_block) 81 | 82 | #define mm_bind_blocks_for_allocation(allocated_meta_block, free_meta_block) \ 83 | free_meta_block->prev_block = allocated_meta_block; \ 84 | free_meta_block->next_block = allocated_meta_block->next_block; \ 85 | allocated_meta_block->next_block = free_meta_block; \ 86 | if (free_meta_block->next_block) \ 87 | free_meta_block->next_block->prev_block = free_meta_block 88 | 89 | vm_bool_t 90 | mm_is_vm_page_empty(vm_page_t *vm_page); 91 | 92 | #define MM_MAX_STRUCT_NAME 32 93 | typedef struct vm_page_family_{ 94 | 95 | char struct_name[MM_MAX_STRUCT_NAME]; 96 | uint32_t struct_size; 97 | vm_page_t *first_page; 98 | glthread_t free_block_priority_list_head; 99 | } vm_page_family_t; 100 | 101 | typedef struct vm_page_for_families_{ 102 | 103 | struct vm_page_for_families_ *next; 104 | vm_page_family_t vm_page_family[0]; 105 | } vm_page_for_families_t; 106 | 107 | #define MAX_FAMILIES_PER_VM_PAGE \ 108 | ((SYSTEM_PAGE_SIZE - sizeof(vm_page_for_families_t *))/sizeof(vm_page_family_t)) 109 | 110 | static inline block_meta_data_t * 111 | mm_get_biggest_free_block_page_family( 112 | vm_page_family_t *vm_page_family){ 113 | 114 | glthread_t *biggest_free_block_glue = 115 | vm_page_family->free_block_priority_list_head.right; 116 | 117 | if(biggest_free_block_glue) 118 | return glthread_to_block_meta_data(biggest_free_block_glue); 119 | 120 | return NULL; 121 | } 122 | 123 | vm_page_t * 124 | allocate_vm_page(); 125 | 126 | #define MARK_VM_PAGE_EMPTY(vm_page_t_ptr) \ 127 | vm_page_t_ptr->block_meta_data.next_block = NULL; \ 128 | vm_page_t_ptr->block_meta_data.prev_block = NULL; \ 129 | vm_page_t_ptr->block_meta_data.is_free = MM_TRUE 130 | 131 | #define ITERATE_VM_PAGE_BEGIN(vm_page_family_ptr, curr) \ 132 | { \ 133 | curr = vm_page_family_ptr->first_page; \ 134 | vm_page_t *next = NULL; \ 135 | for(; curr; curr = next){ \ 136 | next = curr->next; 137 | 138 | #define ITERATE_VM_PAGE_END(vm_page_family_ptr, curr) \ 139 | }} 140 | 141 | #define ITERATE_VM_PAGE_ALL_BLOCKS_BEGIN(vm_page_ptr, curr) \ 142 | { \ 143 | curr = &vm_page_ptr->block_meta_data; \ 144 | block_meta_data_t *next = NULL; \ 145 | for( ; curr; curr = next){ \ 146 | next = NEXT_META_BLOCK(curr); 147 | 148 | #define ITERATE_VM_PAGE_ALL_BLOCKS_END(vm_page_ptr, curr) \ 149 | }} 150 | 151 | #define ITERATE_PAGE_FAMILIES_BEGIN(vm_page_for_families_ptr, curr) \ 152 | { \ 153 | uint32_t _count = 0; \ 154 | for(curr = (vm_page_family_t *)&vm_page_for_families_ptr->vm_page_family[0]; \ 155 | curr->struct_size && _count < MAX_FAMILIES_PER_VM_PAGE; \ 156 | curr++,_count++){ 157 | 158 | #define ITERATE_PAGE_FAMILIES_END(vm_page_for_families_ptr, curr) }} 159 | 160 | vm_page_family_t * 161 | lookup_page_family_by_name(char *struct_name); 162 | 163 | void mm_vm_page_delete_and_free(vm_page_t *vm_page); 164 | #endif /**/ 165 | -------------------------------------------------------------------------------- /gluethread/glthread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: glthread.c 5 | * 6 | * Description: Implementation of glthread Library 7 | * 8 | * Version: 1.0 9 | * Created: Monday 12 March 2018 02:13:36 IST 10 | * Revision: 1.0 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Networking Developer (AS), sachinites@gmail.com 14 | * Company: Brocade Communications(Jul 2012- Mar 2016), Current : Juniper Networks(Apr 2017 - Present) 15 | * 16 | * This file is part of the SPFComputation distribution (https://github.com/sachinites). 17 | * Copyright (c) 2017 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify 19 | * it under the terms of the GNU General Public License as published by 20 | * the Free Software Foundation, version 3. 21 | * 22 | * This program is distributed in the hope that it will be useful, but 23 | * WITHOUT ANY WARRANTY; without even the implied warranty of 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 25 | * General Public License for more details. 26 | * 27 | * You should have received a copy of the GNU General Public License 28 | * along with this program. If not, see . 29 | * 30 | * ===================================================================================== 31 | */ 32 | 33 | 34 | #include "glthread.h" 35 | #include 36 | 37 | void 38 | init_glthread(glthread_t *glthread){ 39 | 40 | glthread->left = NULL; 41 | glthread->right = NULL; 42 | } 43 | 44 | void 45 | glthread_add_next(glthread_t *curr_glthread, glthread_t *new_glthread){ 46 | 47 | if(!curr_glthread->right){ 48 | curr_glthread->right = new_glthread; 49 | new_glthread->left = curr_glthread; 50 | return; 51 | } 52 | 53 | glthread_t *temp = curr_glthread->right; 54 | curr_glthread->right = new_glthread; 55 | new_glthread->left = curr_glthread; 56 | new_glthread->right = temp; 57 | temp->left = new_glthread; 58 | } 59 | 60 | void 61 | glthread_add_before(glthread_t *curr_glthread, glthread_t *new_glthread){ 62 | 63 | if(!curr_glthread->left){ 64 | new_glthread->left = NULL; 65 | new_glthread->right = curr_glthread; 66 | curr_glthread->left = new_glthread; 67 | return; 68 | } 69 | 70 | glthread_t *temp = curr_glthread->left; 71 | temp->right = new_glthread; 72 | new_glthread->left = temp; 73 | new_glthread->right = curr_glthread; 74 | curr_glthread->left = new_glthread; 75 | } 76 | 77 | void 78 | remove_glthread(glthread_t *curr_glthread){ 79 | 80 | if(!curr_glthread->left){ 81 | if(curr_glthread->right){ 82 | curr_glthread->right->left = NULL; 83 | curr_glthread->right = 0; 84 | return; 85 | } 86 | return; 87 | } 88 | if(!curr_glthread->right){ 89 | curr_glthread->left->right = NULL; 90 | curr_glthread->left = NULL; 91 | return; 92 | } 93 | 94 | curr_glthread->left->right = curr_glthread->right; 95 | curr_glthread->right->left = curr_glthread->left; 96 | curr_glthread->left = 0; 97 | curr_glthread->right = 0; 98 | } 99 | 100 | void 101 | delete_glthread_list(glthread_t *base_glthread){ 102 | 103 | glthread_t *glthreadptr = NULL; 104 | 105 | ITERATE_GLTHREAD_BEGIN(base_glthread, glthreadptr){ 106 | remove_glthread(glthreadptr); 107 | } ITERATE_GLTHREAD_END(base_glthread, glthreadptr); 108 | } 109 | 110 | void 111 | glthread_add_last(glthread_t *base_glthread, glthread_t *new_glthread){ 112 | 113 | glthread_t *glthreadptr = NULL, 114 | *prevglthreadptr = NULL; 115 | 116 | ITERATE_GLTHREAD_BEGIN(base_glthread, glthreadptr){ 117 | prevglthreadptr = glthreadptr; 118 | } ITERATE_GLTHREAD_END(base_glthread, glthreadptr); 119 | 120 | if(prevglthreadptr) 121 | glthread_add_next(prevglthreadptr, new_glthread); 122 | else 123 | glthread_add_next(base_glthread, new_glthread); 124 | } 125 | 126 | unsigned int 127 | get_glthread_list_count(glthread_t *base_glthread){ 128 | 129 | unsigned int count = 0; 130 | glthread_t *glthreadptr = NULL; 131 | 132 | ITERATE_GLTHREAD_BEGIN(base_glthread, glthreadptr){ 133 | count++; 134 | } ITERATE_GLTHREAD_END(base_glthread, glthreadptr); 135 | return count; 136 | } 137 | 138 | 139 | void 140 | glthread_priority_insert(glthread_t *base_glthread, 141 | glthread_t *glthread, 142 | int (*comp_fn)(void *, void *), 143 | int offset){ 144 | 145 | 146 | glthread_t *curr = NULL, 147 | *prev = NULL; 148 | 149 | init_glthread(glthread); 150 | 151 | if(IS_GLTHREAD_LIST_EMPTY(base_glthread)){ 152 | glthread_add_next(base_glthread, glthread); 153 | return; 154 | } 155 | 156 | /* Only one node*/ 157 | if(base_glthread->right && !base_glthread->right->right){ 158 | if(comp_fn(GLTHREAD_GET_USER_DATA_FROM_OFFSET(base_glthread->right, offset), 159 | GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthread, offset)) == -1){ 160 | glthread_add_next(base_glthread->right, glthread); 161 | } 162 | else{ 163 | glthread_add_next(base_glthread, glthread); 164 | } 165 | return; 166 | } 167 | 168 | if(comp_fn(GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthread, offset), 169 | GLTHREAD_GET_USER_DATA_FROM_OFFSET(base_glthread->right, offset)) == -1){ 170 | glthread_add_next(base_glthread, glthread); 171 | return; 172 | } 173 | 174 | ITERATE_GLTHREAD_BEGIN(base_glthread, curr){ 175 | 176 | if(comp_fn(GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthread, offset), 177 | GLTHREAD_GET_USER_DATA_FROM_OFFSET(curr, offset)) != -1){ 178 | prev = curr; 179 | continue; 180 | } 181 | 182 | glthread_add_next(curr, glthread); 183 | return; 184 | 185 | }ITERATE_GLTHREAD_END(base_glthread, curr); 186 | 187 | /*Add in the end*/ 188 | glthread_add_next(prev, glthread); 189 | } 190 | 191 | #if 0 192 | void * 193 | gl_thread_search(glthread_t *base_glthread, 194 | void *(*thread_to_struct_fn)(glthread_t *), 195 | void *key, 196 | int (*comparison_fn)(void *, void *)){ 197 | 198 | return NULL; 199 | } 200 | #endif 201 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/UdemyCourse/gluethread/glthread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: glthread.c 5 | * 6 | * Description: Implementation of glthread Library 7 | * 8 | * Version: 1.0 9 | * Created: Monday 12 March 2018 02:13:36 IST 10 | * Revision: 1.0 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Networking Developer (AS), sachinites@gmail.com 14 | * Company: Brocade Communications(Jul 2012- Mar 2016), Current : Juniper Networks(Apr 2017 - Present) 15 | * 16 | * This file is part of the SPFComputation distribution (https://github.com/sachinites). 17 | * Copyright (c) 2017 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify 19 | * it under the terms of the GNU General Public License as published by 20 | * the Free Software Foundation, version 3. 21 | * 22 | * This program is distributed in the hope that it will be useful, but 23 | * WITHOUT ANY WARRANTY; without even the implied warranty of 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 25 | * General Public License for more details. 26 | * 27 | * You should have received a copy of the GNU General Public License 28 | * along with this program. If not, see . 29 | * 30 | * ===================================================================================== 31 | */ 32 | 33 | 34 | #include "glthread.h" 35 | #include 36 | 37 | void 38 | init_glthread(glthread_t *glthread){ 39 | 40 | glthread->left = NULL; 41 | glthread->right = NULL; 42 | } 43 | 44 | void 45 | glthread_add_next(glthread_t *curr_glthread, glthread_t *new_glthread){ 46 | 47 | if(!curr_glthread->right){ 48 | curr_glthread->right = new_glthread; 49 | new_glthread->left = curr_glthread; 50 | return; 51 | } 52 | 53 | glthread_t *temp = curr_glthread->right; 54 | curr_glthread->right = new_glthread; 55 | new_glthread->left = curr_glthread; 56 | new_glthread->right = temp; 57 | temp->left = new_glthread; 58 | } 59 | 60 | void 61 | glthread_add_before(glthread_t *curr_glthread, glthread_t *new_glthread){ 62 | 63 | if(!curr_glthread->left){ 64 | new_glthread->left = NULL; 65 | new_glthread->right = curr_glthread; 66 | curr_glthread->left = new_glthread; 67 | return; 68 | } 69 | 70 | glthread_t *temp = curr_glthread->left; 71 | temp->right = new_glthread; 72 | new_glthread->left = temp; 73 | new_glthread->right = curr_glthread; 74 | curr_glthread->left = new_glthread; 75 | } 76 | 77 | void 78 | remove_glthread(glthread_t *curr_glthread){ 79 | 80 | if(!curr_glthread->left){ 81 | if(curr_glthread->right){ 82 | curr_glthread->right->left = NULL; 83 | curr_glthread->right = 0; 84 | return; 85 | } 86 | return; 87 | } 88 | if(!curr_glthread->right){ 89 | curr_glthread->left->right = NULL; 90 | curr_glthread->left = NULL; 91 | return; 92 | } 93 | 94 | curr_glthread->left->right = curr_glthread->right; 95 | curr_glthread->right->left = curr_glthread->left; 96 | curr_glthread->left = 0; 97 | curr_glthread->right = 0; 98 | } 99 | 100 | void 101 | delete_glthread_list(glthread_t *base_glthread){ 102 | 103 | glthread_t *glthreadptr = NULL; 104 | 105 | ITERATE_GLTHREAD_BEGIN(base_glthread, glthreadptr){ 106 | remove_glthread(glthreadptr); 107 | } ITERATE_GLTHREAD_END(base_glthread, glthreadptr); 108 | } 109 | 110 | void 111 | glthread_add_last(glthread_t *base_glthread, glthread_t *new_glthread){ 112 | 113 | glthread_t *glthreadptr = NULL, 114 | *prevglthreadptr = NULL; 115 | 116 | ITERATE_GLTHREAD_BEGIN(base_glthread, glthreadptr){ 117 | prevglthreadptr = glthreadptr; 118 | } ITERATE_GLTHREAD_END(base_glthread, glthreadptr); 119 | 120 | if(prevglthreadptr) 121 | glthread_add_next(prevglthreadptr, new_glthread); 122 | else 123 | glthread_add_next(base_glthread, new_glthread); 124 | } 125 | 126 | unsigned int 127 | get_glthread_list_count(glthread_t *base_glthread){ 128 | 129 | unsigned int count = 0; 130 | glthread_t *glthreadptr = NULL; 131 | 132 | ITERATE_GLTHREAD_BEGIN(base_glthread, glthreadptr){ 133 | count++; 134 | } ITERATE_GLTHREAD_END(base_glthread, glthreadptr); 135 | return count; 136 | } 137 | 138 | 139 | void 140 | glthread_priority_insert(glthread_t *base_glthread, 141 | glthread_t *glthread, 142 | int (*comp_fn)(void *, void *), 143 | int offset){ 144 | 145 | 146 | glthread_t *curr = NULL, 147 | *prev = NULL; 148 | 149 | init_glthread(glthread); 150 | 151 | if(IS_GLTHREAD_LIST_EMPTY(base_glthread)){ 152 | glthread_add_next(base_glthread, glthread); 153 | return; 154 | } 155 | 156 | /* Only one node*/ 157 | if(base_glthread->right && !base_glthread->right->right){ 158 | if(comp_fn(GLTHREAD_GET_USER_DATA_FROM_OFFSET(base_glthread->right, offset), 159 | GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthread, offset)) == -1){ 160 | glthread_add_next(base_glthread->right, glthread); 161 | } 162 | else{ 163 | glthread_add_next(base_glthread, glthread); 164 | } 165 | return; 166 | } 167 | 168 | if(comp_fn(GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthread, offset), 169 | GLTHREAD_GET_USER_DATA_FROM_OFFSET(base_glthread->right, offset)) == -1){ 170 | glthread_add_next(base_glthread, glthread); 171 | return; 172 | } 173 | 174 | ITERATE_GLTHREAD_BEGIN(base_glthread, curr){ 175 | 176 | if(comp_fn(GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthread, offset), 177 | GLTHREAD_GET_USER_DATA_FROM_OFFSET(curr, offset)) != -1){ 178 | prev = curr; 179 | continue; 180 | } 181 | 182 | glthread_add_next(curr, glthread); 183 | return; 184 | 185 | }ITERATE_GLTHREAD_END(base_glthread, curr); 186 | 187 | /*Add in the end*/ 188 | glthread_add_next(prev, glthread); 189 | } 190 | 191 | #if 0 192 | void * 193 | gl_thread_search(glthread_t *base_glthread, 194 | void *(*thread_to_struct_fn)(glthread_t *), 195 | void *key, 196 | int (*comparison_fn)(void *, void *)){ 197 | 198 | return NULL; 199 | } 200 | #endif 201 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/gluethread/glthread.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: glthread.c 5 | * 6 | * Description: Implementation of glthread Library 7 | * 8 | * Version: 1.0 9 | * Created: Monday 12 March 2018 02:13:36 IST 10 | * Revision: 1.0 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Networking Developer (AS), sachinites@gmail.com 14 | * Company: Brocade Communications(Jul 2012- Mar 2016), Current : Juniper Networks(Apr 2017 - Present) 15 | * 16 | * This file is part of the SPFComputation distribution (https://github.com/sachinites). 17 | * Copyright (c) 2017 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify 19 | * it under the terms of the GNU General Public License as published by 20 | * the Free Software Foundation, version 3. 21 | * 22 | * This program is distributed in the hope that it will be useful, but 23 | * WITHOUT ANY WARRANTY; without even the implied warranty of 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 25 | * General Public License for more details. 26 | * 27 | * You should have received a copy of the GNU General Public License 28 | * along with this program. If not, see . 29 | * 30 | * ===================================================================================== 31 | */ 32 | 33 | 34 | #include "glthread.h" 35 | #include 36 | 37 | void 38 | init_glthread(glthread_t *glthread){ 39 | 40 | glthread->left = NULL; 41 | glthread->right = NULL; 42 | } 43 | 44 | void 45 | glthread_add_next(glthread_t *curr_glthread, glthread_t *new_glthread){ 46 | 47 | if(!curr_glthread->right){ 48 | curr_glthread->right = new_glthread; 49 | new_glthread->left = curr_glthread; 50 | return; 51 | } 52 | 53 | glthread_t *temp = curr_glthread->right; 54 | curr_glthread->right = new_glthread; 55 | new_glthread->left = curr_glthread; 56 | new_glthread->right = temp; 57 | temp->left = new_glthread; 58 | } 59 | 60 | void 61 | glthread_add_before(glthread_t *curr_glthread, glthread_t *new_glthread){ 62 | 63 | if(!curr_glthread->left){ 64 | new_glthread->left = NULL; 65 | new_glthread->right = curr_glthread; 66 | curr_glthread->left = new_glthread; 67 | return; 68 | } 69 | 70 | glthread_t *temp = curr_glthread->left; 71 | temp->right = new_glthread; 72 | new_glthread->left = temp; 73 | new_glthread->right = curr_glthread; 74 | curr_glthread->left = new_glthread; 75 | } 76 | 77 | void 78 | remove_glthread(glthread_t *curr_glthread){ 79 | 80 | if(!curr_glthread->left){ 81 | if(curr_glthread->right){ 82 | curr_glthread->right->left = NULL; 83 | curr_glthread->right = 0; 84 | return; 85 | } 86 | return; 87 | } 88 | if(!curr_glthread->right){ 89 | curr_glthread->left->right = NULL; 90 | curr_glthread->left = NULL; 91 | return; 92 | } 93 | 94 | curr_glthread->left->right = curr_glthread->right; 95 | curr_glthread->right->left = curr_glthread->left; 96 | curr_glthread->left = 0; 97 | curr_glthread->right = 0; 98 | } 99 | 100 | void 101 | delete_glthread_list(glthread_t *glthread_head){ 102 | 103 | glthread_t *glthreadptr = NULL; 104 | 105 | ITERATE_GLTHREAD_BEGIN(glthread_head, glthreadptr){ 106 | remove_glthread(glthreadptr); 107 | } ITERATE_GLTHREAD_END(glthread_head, glthreadptr); 108 | } 109 | 110 | void 111 | glthread_add_last(glthread_t *glthread_head, glthread_t *new_glthread){ 112 | 113 | glthread_t *glthreadptr = NULL, 114 | *prevglthreadptr = NULL; 115 | 116 | ITERATE_GLTHREAD_BEGIN(glthread_head, glthreadptr){ 117 | prevglthreadptr = glthreadptr; 118 | } ITERATE_GLTHREAD_END(glthread_head, glthreadptr); 119 | 120 | if(prevglthreadptr) 121 | glthread_add_next(prevglthreadptr, new_glthread); 122 | else 123 | glthread_add_next(glthread_head, new_glthread); 124 | } 125 | 126 | unsigned int 127 | get_glthread_list_count(glthread_t *glthread_head){ 128 | 129 | unsigned int count = 0; 130 | glthread_t *glthreadptr = NULL; 131 | 132 | ITERATE_GLTHREAD_BEGIN(glthread_head, glthreadptr){ 133 | count++; 134 | } ITERATE_GLTHREAD_END(glthread_head, glthreadptr); 135 | return count; 136 | } 137 | 138 | 139 | void 140 | glthread_priority_insert(glthread_t *glthread_head, 141 | glthread_t *glthread, 142 | int (*comp_fn)(void *, void *), 143 | int offset){ 144 | 145 | 146 | glthread_t *curr = NULL, 147 | *prev = NULL; 148 | 149 | init_glthread(glthread); 150 | 151 | if(IS_GLTHREAD_LIST_EMPTY(glthread_head)){ 152 | glthread_add_next(glthread_head, glthread); 153 | return; 154 | } 155 | 156 | /* Only one node*/ 157 | if(glthread_head->right && !glthread_head->right->right){ 158 | if(comp_fn(GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthread, offset), 159 | GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthread_head->right, offset)) == -1){ 160 | glthread_add_next(glthread_head, glthread); 161 | } 162 | else{ 163 | glthread_add_next(glthread_head->right, glthread); 164 | } 165 | return; 166 | } 167 | 168 | ITERATE_GLTHREAD_BEGIN(glthread_head, curr){ 169 | 170 | if(comp_fn(GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthread, offset), 171 | GLTHREAD_GET_USER_DATA_FROM_OFFSET(curr, offset)) != -1){ 172 | prev = curr; 173 | continue; 174 | } 175 | 176 | if(!prev) 177 | glthread_add_next(glthread_head, glthread); 178 | else 179 | glthread_add_next(prev, glthread); 180 | 181 | return; 182 | 183 | }ITERATE_GLTHREAD_END(glthread_head, curr); 184 | 185 | /*Add in the end*/ 186 | glthread_add_next(prev, glthread); 187 | } 188 | 189 | glthread_t * 190 | dequeue_glthread_first(glthread_t *base_glthread){ 191 | 192 | glthread_t *temp; 193 | if(!base_glthread->right) 194 | return NULL; 195 | temp = base_glthread->right; 196 | remove_glthread(temp); 197 | return temp; 198 | } 199 | 200 | #if 0 201 | void * 202 | gl_thread_search(glthread_t *glthread_head, 203 | void *(*thread_to_struct_fn)(glthread_t *), 204 | void *key, 205 | int (*comparison_fn)(void *, void *)){ 206 | 207 | return NULL; 208 | } 209 | #endif 210 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/UdemyCourse/mm_ass.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include /*for getpagesize*/ 4 | #include /*For using mmap()*/ 5 | #include 6 | #include "mm.h" 7 | #include 8 | 9 | static vm_page_for_families_t *first_vm_page_for_families = NULL; 10 | static size_t SYSTEM_PAGE_SIZE = 0; 11 | 12 | void 13 | mm_init(){ 14 | 15 | SYSTEM_PAGE_SIZE = getpagesize(); 16 | } 17 | 18 | /*Function to request VM page from kernel*/ 19 | static void * 20 | mm_get_new_vm_page_from_kernel(int units){ 21 | 22 | char *vm_page = mmap( 23 | 0, 24 | units * SYSTEM_PAGE_SIZE, 25 | PROT_READ|PROT_WRITE|PROT_EXEC, 26 | MAP_ANON|MAP_PRIVATE, 27 | 0, 0); 28 | 29 | if(vm_page == MAP_FAILED){ 30 | printf("Error : VM Page allocation Failed\n"); 31 | return NULL; 32 | } 33 | memset(vm_page, 0, units * SYSTEM_PAGE_SIZE); 34 | return (void *)vm_page; 35 | } 36 | 37 | /*Function to return a page to kernel*/ 38 | 39 | static void 40 | mm_return_vm_page_to_kernel (void *vm_page, int units){ 41 | 42 | if(munmap(vm_page, units * SYSTEM_PAGE_SIZE)){ 43 | printf("Error : Could not munmap VM page to kernel"); 44 | } 45 | } 46 | 47 | static void 48 | mm_union_free_blocks(block_meta_data_t *first, 49 | block_meta_data_t *second){ 50 | 51 | assert(first->is_free == MM_TRUE && 52 | second->is_free == MM_TRUE); 53 | 54 | first->block_size += sizeof(block_meta_data_t) + 55 | second->block_size; 56 | 57 | first->next_block = second->next_block; 58 | 59 | if(second->next_block) 60 | second->next_block->prev_block = first; 61 | } 62 | 63 | static inline uint32_t 64 | mm_max_page_allocatable_memory (int units){ 65 | 66 | return (uint32_t) 67 | ((SYSTEM_PAGE_SIZE * units) - \ 68 | offset_of(vm_page_t, page_memory)); 69 | } 70 | 71 | vm_page_t * 72 | allocate_vm_page(vm_page_family_t *vm_page_family){ 73 | 74 | vm_page_t *vm_page = mm_get_new_vm_page_from_kernel(1); 75 | 76 | /*Initialize lower most Meta block of the VM page*/ 77 | MARK_VM_PAGE_EMPTY(vm_page); 78 | 79 | vm_page->block_meta_data.block_size = 80 | mm_max_page_allocatable_memory(1); 81 | vm_page->block_meta_data.offset = 82 | offset_of(vm_page_t, block_meta_data); 83 | 84 | vm_page->next = NULL; 85 | vm_page->prev = NULL; 86 | 87 | /*Set the back pointer to page family*/ 88 | vm_page->pg_family = vm_page_family; 89 | 90 | /*If it is a first VM data page for a given 91 | * page family*/ 92 | if(!vm_page_family->first_page){ 93 | vm_page_family->first_page = vm_page; 94 | return vm_page; 95 | } 96 | 97 | /* Insert new VM page to the head of the linked 98 | * list*/ 99 | vm_page->next = vm_page_family->first_page; 100 | vm_page_family->first_page->prev = vm_page; 101 | vm_page_family->first_page = vm_page; 102 | return vm_page; 103 | } 104 | 105 | void 106 | mm_vm_page_delete_and_free( 107 | vm_page_t *vm_page){ 108 | 109 | vm_page_family_t *vm_page_family = 110 | vm_page->pg_family; 111 | 112 | /*If the page being deleted is the head of the linked 113 | * list*/ 114 | if(vm_page_family->first_page == vm_page){ 115 | vm_page_family->first_page = vm_page->next; 116 | if(vm_page->next) 117 | vm_page->next->prev = NULL; 118 | vm_page->next = NULL; 119 | vm_page->prev = NULL; 120 | mm_return_vm_page_to_kernel((void *)vm_page, 1); 121 | return; 122 | } 123 | 124 | /*If we are deleting the page from middle or end of 125 | * linked list*/ 126 | if(vm_page->next) 127 | vm_page->next->prev = vm_page->prev; 128 | vm_page->prev->next = vm_page->next; 129 | mm_return_vm_page_to_kernel((void *)vm_page, 1); 130 | } 131 | 132 | void 133 | mm_print_vm_page_details(vm_page_t *vm_page){ 134 | 135 | printf("\t\t next = %p, prev = %p\n", vm_page->next, vm_page->prev); 136 | printf("\t\t page family = %s\n", vm_page->pg_family->struct_name); 137 | 138 | uint32_t j = 0; 139 | block_meta_data_t *curr; 140 | ITERATE_VM_PAGE_ALL_BLOCKS_BEGIN(vm_page, curr){ 141 | 142 | printf("\t\t\t%-14p Block %-3u %s block_size = %-6u " 143 | "offset = %-6u prev = %-14p next = %p\n", 144 | curr, 145 | j++, curr->is_free ? "F R E E D" : "ALLOCATED", 146 | curr->block_size, curr->offset, 147 | curr->prev_block, 148 | curr->next_block); 149 | } ITERATE_VM_PAGE_ALL_BLOCKS_END(vm_page, curr); 150 | } 151 | 152 | 153 | 154 | 155 | void 156 | mm_instantiate_new_page_family( 157 | char *struct_name, 158 | uint32_t struct_size){ 159 | 160 | 161 | vm_page_family_t *vm_page_family_curr = NULL; 162 | vm_page_for_families_t *new_vm_page_for_families = NULL; 163 | 164 | if(struct_size > SYSTEM_PAGE_SIZE){ 165 | 166 | printf("Error : %s() Structure %s Size exceeds system page size\n", 167 | __FUNCTION__, struct_name); 168 | return; 169 | } 170 | 171 | if(!first_vm_page_for_families){ 172 | 173 | first_vm_page_for_families = 174 | (vm_page_for_families_t *)mm_get_new_vm_page_from_kernel(1); 175 | first_vm_page_for_families->next = NULL; 176 | strncpy(first_vm_page_for_families->vm_page_family[0].struct_name, 177 | struct_name, MM_MAX_STRUCT_NAME); 178 | first_vm_page_for_families->vm_page_family[0].struct_size = struct_size; 179 | return; 180 | } 181 | 182 | uint32_t count = 0; 183 | 184 | ITERATE_PAGE_FAMILIES_BEGIN(first_vm_page_for_families, vm_page_family_curr){ 185 | 186 | if(strncmp(vm_page_family_curr->struct_name, 187 | struct_name,MM_MAX_STRUCT_NAME) != 0){ 188 | count++; 189 | continue; 190 | } 191 | 192 | assert(0); 193 | 194 | } ITERATE_PAGE_FAMILIES_END(first_vm_page_for_families, vm_page_family_curr); 195 | 196 | if(count == MAX_FAMILIES_PER_VM_PAGE){ 197 | 198 | new_vm_page_for_families = 199 | (vm_page_for_families_t *)mm_get_new_vm_page_from_kernel(1); 200 | new_vm_page_for_families->next = first_vm_page_for_families; 201 | first_vm_page_for_families = new_vm_page_for_families; 202 | vm_page_family_curr = &first_vm_page_for_families->vm_page_family[0]; 203 | } 204 | 205 | strncpy(vm_page_family_curr->struct_name, struct_name, 206 | MM_MAX_STRUCT_NAME); 207 | vm_page_family_curr->struct_size = struct_size; 208 | } 209 | 210 | void 211 | mm_print_registered_page_families(){ 212 | 213 | vm_page_family_t *vm_page_family_curr = NULL; 214 | vm_page_for_families_t *vm_page_for_families_curr = NULL; 215 | 216 | for(vm_page_for_families_curr = first_vm_page_for_families; 217 | vm_page_for_families_curr; 218 | vm_page_for_families_curr = vm_page_for_families_curr->next){ 219 | 220 | ITERATE_PAGE_FAMILIES_BEGIN(vm_page_for_families_curr, 221 | vm_page_family_curr){ 222 | 223 | printf("Page Family : %s, Size = %u\n", 224 | vm_page_family_curr->struct_name, 225 | vm_page_family_curr->struct_size); 226 | 227 | } ITERATE_PAGE_FAMILIES_END(vm_page_for_families_curr, 228 | vm_page_family_curr); 229 | } 230 | } 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | -------------------------------------------------------------------------------- /mm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: mm.h 5 | * 6 | * Description: This file defines the public APIs and Data structures used for Memory Manager 7 | * 8 | * Version: 1.0 9 | * Created: 01/30/2020 10:11:20 AM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Juniper Networks (https://csepracticals.wixsite.com/csepracticals), sachinites@gmail.com 14 | * Company: Juniper Networks 15 | * 16 | * This file is part of the Linux Memory Manager distribution (https://github.com/sachinites) 17 | * Copyright (c) 2019 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General 19 | * Public License as published by the Free Software Foundation, version 3. 20 | * 21 | * This program is distributed in the hope that it will be useful, but 22 | * WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 | * General Public License for more details. 25 | * 26 | * visit website : https://csepracticals.wixsite.com/csepracticals for more courses and projects 27 | * 28 | * ===================================================================================== 29 | */ 30 | 31 | #ifndef __MM__ 32 | #define __MM__ 33 | 34 | #include 35 | #include "gluethread/glthread.h" 36 | #include /*for size_t*/ 37 | 38 | 39 | typedef enum{ 40 | 41 | MM_FALSE, 42 | MM_TRUE 43 | } vm_bool_t; 44 | 45 | typedef struct block_meta_data_{ 46 | 47 | vm_bool_t is_free; 48 | uint32_t block_size; 49 | uint32_t offset; /*offset from the start of the page*/ 50 | glthread_t priority_thread_glue; 51 | struct block_meta_data_ *prev_block; 52 | struct block_meta_data_ *next_block; 53 | } block_meta_data_t; 54 | GLTHREAD_TO_STRUCT(glthread_to_block_meta_data, 55 | block_meta_data_t, priority_thread_glue, glthread_ptr); 56 | 57 | #define offset_of(container_structure, field_name) \ 58 | ((size_t)&(((container_structure *)0)->field_name)) 59 | 60 | /*Forward Declaration*/ 61 | struct vm_page_family_; 62 | 63 | typedef struct vm_page_{ 64 | struct vm_page_ *next; 65 | struct vm_page_ *prev; 66 | struct vm_page_family_ *pg_family; /*back pointer*/ 67 | uint32_t page_index; 68 | block_meta_data_t block_meta_data; 69 | char page_memory[0]; 70 | } vm_page_t; 71 | 72 | #define MM_GET_PAGE_FROM_META_BLOCK(block_meta_data_ptr) \ 73 | ((vm_page_t *)((char *)block_meta_data_ptr - block_meta_data_ptr->offset)) 74 | 75 | #define NEXT_META_BLOCK(block_meta_data_ptr) \ 76 | (block_meta_data_ptr->next_block) 77 | 78 | #define NEXT_META_BLOCK_BY_SIZE(block_meta_data_ptr) \ 79 | (block_meta_data_t *)((char *)(block_meta_data_ptr + 1) \ 80 | + block_meta_data_ptr->block_size) 81 | 82 | #define PREV_META_BLOCK(block_meta_data_ptr) \ 83 | (block_meta_data_ptr->prev_block) 84 | 85 | #define mm_bind_blocks_for_allocation(allocated_meta_block, free_meta_block) \ 86 | free_meta_block->prev_block = allocated_meta_block; \ 87 | free_meta_block->next_block = allocated_meta_block->next_block; \ 88 | allocated_meta_block->next_block = free_meta_block; \ 89 | if (free_meta_block->next_block)\ 90 | free_meta_block->next_block->prev_block = free_meta_block 91 | 92 | #define mm_bind_blocks_for_deallocation(freed_meta_block_top, freed_meta_block_down) \ 93 | freed_meta_block_top->next_block = freed_meta_block_down->next_block; \ 94 | if(freed_meta_block_down->next_block) \ 95 | freed_meta_block_down->next_block->prev_block = freed_meta_block_top 96 | 97 | vm_bool_t 98 | mm_is_vm_page_empty(vm_page_t *vm_page); 99 | 100 | #define MM_MAX_STRUCT_NAME 32 101 | typedef struct vm_page_family_{ 102 | 103 | char struct_name[MM_MAX_STRUCT_NAME]; 104 | uint32_t struct_size; 105 | vm_page_t *first_page; 106 | glthread_t free_block_priority_list_head; 107 | 108 | /*Statistics*/ 109 | uint32_t total_memory_in_use_by_app; 110 | uint32_t no_of_system_calls_to_alloc_dealloc_vm_pages; 111 | } vm_page_family_t; 112 | 113 | static inline block_meta_data_t * 114 | mm_get_biggest_free_block_page_family( 115 | vm_page_family_t *vm_page_family){ 116 | 117 | glthread_t *biggest_free_block_glue = 118 | vm_page_family->free_block_priority_list_head.right; 119 | 120 | if(biggest_free_block_glue) 121 | return glthread_to_block_meta_data(biggest_free_block_glue); 122 | 123 | return NULL; 124 | } 125 | 126 | vm_page_t * 127 | allocate_vm_page(); 128 | 129 | #define MARK_VM_PAGE_EMPTY(vm_page_t_ptr) \ 130 | vm_page_t_ptr->block_meta_data.next_block = NULL; \ 131 | vm_page_t_ptr->block_meta_data.prev_block = NULL; \ 132 | vm_page_t_ptr->block_meta_data.is_free = MM_TRUE 133 | 134 | #define MM_GET_NEXT_PAGE_IN_HEAP_SEGMENT(vm_page_t_ptr, incr) \ 135 | ((incr == '+') ? ((vm_page_t *)((char *)vm_page_t_ptr + GB_SYSTEM_PAGE_SIZE)): \ 136 | ((vm_page_t *)((char *)vm_page_t_ptr - GB_SYSTEM_PAGE_SIZE))) 137 | 138 | void 139 | mm_init(); 140 | 141 | #define N_PAGE_FAMILY_PER_VM_PAGE \ 142 | (GB_SYSTEM_PAGE_SIZE/sizeof(vm_page_family_t)) 143 | 144 | #define VM_PAGE_FAMILY_RESIDUAL_SPACE \ 145 | (GB_SYSTEM_PAGE_SIZE - (N_PAGE_FAMILY_PER_VM_PAGE * sizeof(vm_page_family_t))) 146 | 147 | #define ITERATE_PAGE_FAMILIES_BEGIN(first_vm_page_family_ptr, curr) \ 148 | { \ 149 | uint32_t count = 0; curr = NULL; \ 150 | for(curr = (vm_page_family_t *)first_vm_page_family_ptr; \ 151 | count != gb_no_of_vm_families_registered; \ 152 | count++, curr++){ \ 153 | if(count == N_PAGE_FAMILY_PER_VM_PAGE){ \ 154 | curr = (vm_page_family_t *)((char *)curr + \ 155 | VM_PAGE_FAMILY_RESIDUAL_SPACE); \ 156 | count = 0; \ 157 | } 158 | 159 | #define ITERATE_PAGE_FAMILIES_END(first_vm_page_family_ptr, curr) \ 160 | }} 161 | 162 | vm_page_family_t * 163 | lookup_page_family_by_name(char *struct_name); 164 | 165 | #define ITERATE_VM_PAGE_PER_FAMILY_BEGIN(vm_page_family_ptr, curr) \ 166 | { \ 167 | curr = vm_page_family_ptr->first_page; \ 168 | vm_page_t *next = NULL; \ 169 | for(; curr; curr = next){ \ 170 | next = curr->next; 171 | 172 | #define ITERATE_VM_PAGE_PER_FAMILY_END(vm_page_family_ptr, curr) \ 173 | }} 174 | 175 | #define ITERATE_VM_PAGE_ALL_BLOCKS_BEGIN(vm_page_ptr, curr) \ 176 | {\ 177 | curr = &vm_page_ptr->block_meta_data;\ 178 | block_meta_data_t *next = NULL;\ 179 | for( ; curr; curr = next){\ 180 | next = NEXT_META_BLOCK(curr); 181 | 182 | #define ITERATE_VM_PAGE_ALL_BLOCKS_END(vm_page_ptr, curr) \ 183 | }} 184 | 185 | #define ITERATE_HEAP_SEGMENT_PAGE_WISE_BEGIN(vm_page_begin_ptr, curr) \ 186 | { \ 187 | void *heap_segment_end = sbrk(0); \ 188 | for(curr = (vm_page_t *)vm_page_begin_ptr; \ 189 | (void *)curr != heap_segment_end; \ 190 | curr = MM_GET_NEXT_PAGE_IN_HEAP_SEGMENT(curr, '+')){ \ 191 | 192 | #define ITERATE_HEAP_SEGMENT_PAGE_WISE_END(vm_page_begin_ptr, curr) \ 193 | }} 194 | 195 | void mm_vm_page_delete_and_free(vm_page_t *vm_page); 196 | #endif /**/ 197 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/mm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: mm.h 5 | * 6 | * Description: This file defines the public APIs and Data structures used for Memory Manager 7 | * 8 | * Version: 1.0 9 | * Created: 01/30/2020 10:11:20 AM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Juniper Networks (https://csepracticals.wixsite.com/csepracticals), sachinites@gmail.com 14 | * Company: Juniper Networks 15 | * 16 | * This file is part of the Linux Memory Manager distribution (https://github.com/sachinites) 17 | * Copyright (c) 2019 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General 19 | * Public License as published by the Free Software Foundation, version 3. 20 | * 21 | * This program is distributed in the hope that it will be useful, but 22 | * WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 | * General Public License for more details. 25 | * 26 | * visit website : https://csepracticals.wixsite.com/csepracticals for more courses and projects 27 | * 28 | * ===================================================================================== 29 | */ 30 | 31 | #ifndef __MM__ 32 | #define __MM__ 33 | 34 | #include /*for size_t*/ 35 | #include 36 | #include "gluethread/glthread.h" 37 | 38 | 39 | typedef enum{ 40 | 41 | MM_FALSE, 42 | MM_TRUE 43 | } vm_bool_t; 44 | 45 | typedef struct block_meta_data_{ 46 | 47 | vm_bool_t is_free; 48 | uint32_t block_size; 49 | uint32_t offset; /*offset from the start of the page*/ 50 | glthread_t priority_thread_glue; 51 | struct block_meta_data_ *prev_block; 52 | struct block_meta_data_ *next_block; 53 | } block_meta_data_t; 54 | GLTHREAD_TO_STRUCT(glthread_to_block_meta_data, 55 | block_meta_data_t, priority_thread_glue); 56 | 57 | #define offset_of(container_structure, field_name) \ 58 | ((size_t)&(((container_structure *)0)->field_name)) 59 | 60 | /*Forward Declaration*/ 61 | struct vm_page_family_; 62 | 63 | typedef struct vm_page_{ 64 | struct vm_page_ *next; 65 | struct vm_page_ *prev; 66 | struct vm_page_family_ *pg_family; /*back pointer*/ 67 | uint32_t page_index; 68 | uint32_t page_size; 69 | block_meta_data_t block_meta_data; 70 | char page_memory[0]; 71 | } vm_page_t; 72 | 73 | #define MM_GET_PAGE_FROM_META_BLOCK(block_meta_data_ptr) \ 74 | ((vm_page_t *)((char *)block_meta_data_ptr - block_meta_data_ptr->offset)) 75 | 76 | #define NEXT_META_BLOCK(block_meta_data_ptr) \ 77 | (block_meta_data_ptr->next_block) 78 | 79 | #define NEXT_META_BLOCK_BY_SIZE(block_meta_data_ptr) \ 80 | (block_meta_data_t *)((char *)(block_meta_data_ptr + 1) \ 81 | + block_meta_data_ptr->block_size) 82 | 83 | #define PREV_META_BLOCK(block_meta_data_ptr) \ 84 | (block_meta_data_ptr->prev_block) 85 | 86 | #define mm_bind_blocks_for_allocation(allocated_meta_block, free_meta_block) \ 87 | free_meta_block->prev_block = allocated_meta_block; \ 88 | free_meta_block->next_block = allocated_meta_block->next_block; \ 89 | allocated_meta_block->next_block = free_meta_block; \ 90 | if (free_meta_block->next_block)\ 91 | free_meta_block->next_block->prev_block = free_meta_block 92 | 93 | #define mm_bind_blocks_for_deallocation(freed_meta_block_down, freed_meta_block_top) \ 94 | freed_meta_block_down->next_block = freed_meta_block_top->next_block; \ 95 | if(freed_meta_block_top->next_block) \ 96 | freed_meta_block_top->next_block->prev_block = freed_meta_block_down 97 | 98 | vm_bool_t 99 | mm_is_vm_page_empty(vm_page_t *vm_page); 100 | 101 | #define MM_MAX_STRUCT_NAME 32 102 | typedef struct vm_page_family_{ 103 | 104 | char struct_name[MM_MAX_STRUCT_NAME]; 105 | uint32_t struct_size; 106 | vm_page_t *first_page; 107 | glthread_t free_block_priority_list_head; 108 | /*Statistics*/ 109 | uint32_t total_memory_in_use_by_app; 110 | uint32_t no_of_system_calls_to_alloc_dealloc_vm_pages; 111 | } vm_page_family_t; 112 | 113 | typedef struct vm_page_for_families_{ 114 | 115 | struct vm_page_for_families_ *next; 116 | vm_page_family_t vm_page_family[0]; 117 | } vm_page_for_families_t; 118 | 119 | typedef struct mm_instance_ { 120 | 121 | vm_page_for_families_t *first_vm_page_for_families; 122 | vm_page_family_t misc_vm_page_family; 123 | void *gb_hsba; 124 | } mm_instance_t; 125 | 126 | #define MAX_FAMILIES_PER_VM_PAGE \ 127 | ((SYSTEM_PAGE_SIZE - sizeof(vm_page_for_families_t *))/sizeof(vm_page_family_t)) 128 | 129 | 130 | static inline block_meta_data_t * 131 | mm_get_biggest_free_block_page_family( 132 | vm_page_family_t *vm_page_family){ 133 | 134 | glthread_t *biggest_free_block_glue = 135 | vm_page_family->free_block_priority_list_head.right; 136 | 137 | if(biggest_free_block_glue) 138 | return glthread_to_block_meta_data(biggest_free_block_glue); 139 | 140 | return NULL; 141 | } 142 | 143 | vm_page_t * 144 | allocate_vm_page(); 145 | 146 | #define MARK_VM_PAGE_EMPTY(vm_page_t_ptr) \ 147 | vm_page_t_ptr->block_meta_data.next_block = NULL; \ 148 | vm_page_t_ptr->block_meta_data.prev_block = NULL; \ 149 | vm_page_t_ptr->block_meta_data.is_free = MM_TRUE 150 | 151 | #define MM_GET_NEXT_CONTIGUOUS_PAGE_IN_HEAP_SEGMENT(vm_page_t_ptr, incr) \ 152 | ((incr == '+') ? ((vm_page_t *)((char *)vm_page_t_ptr + SYSTEM_PAGE_SIZE)): \ 153 | ((vm_page_t *)((char *)vm_page_t_ptr - SYSTEM_PAGE_SIZE))) 154 | 155 | 156 | #define ITERATE_PAGE_FAMILIES_BEGIN(vm_page_for_families_ptr, curr) \ 157 | { \ 158 | uint32_t _count = 0; \ 159 | for(curr = (vm_page_family_t *)&vm_page_for_families_ptr->vm_page_family[0]; \ 160 | curr->struct_size && _count < MAX_FAMILIES_PER_VM_PAGE; \ 161 | curr++,_count++){ 162 | 163 | #define ITERATE_PAGE_FAMILIES_END(vm_page_for_families_ptr, curr) }} 164 | 165 | vm_page_family_t * 166 | lookup_page_family_by_name(mm_instance_t *mm_inst, char *struct_name); 167 | 168 | 169 | #define ITERATE_VM_PAGE_BEGIN(vm_page_family_ptr, curr) \ 170 | { \ 171 | curr = vm_page_family_ptr->first_page; \ 172 | vm_page_t *next = NULL; \ 173 | for(; curr; curr = next){ \ 174 | next = curr->next; 175 | 176 | #define ITERATE_VM_PAGE_END(vm_page_family_ptr, curr) \ 177 | }} 178 | 179 | #define ITERATE_VM_PAGE_ALL_BLOCKS_BEGIN(vm_page_ptr, curr) \ 180 | { \ 181 | curr = &vm_page_ptr->block_meta_data; \ 182 | block_meta_data_t *next = NULL; \ 183 | for( ; curr; curr = next){ \ 184 | next = NEXT_META_BLOCK(curr); 185 | 186 | #define ITERATE_VM_PAGE_ALL_BLOCKS_END(vm_page_ptr, curr) \ 187 | }} 188 | 189 | #define ITERATE_HEAP_SEGMENT_PAGE_WISE_BEGIN(vm_page_begin_ptr, curr) \ 190 | { \ 191 | void *heap_segment_end = sbrk(0); \ 192 | for(curr = (vm_page_t *)vm_page_begin_ptr; \ 193 | (void *)curr != heap_segment_end; \ 194 | curr = MM_GET_NEXT_CONTIGUOUS_PAGE_IN_HEAP_SEGMENT(curr, '+')){ \ 195 | 196 | #define ITERATE_HEAP_SEGMENT_PAGE_WISE_END(vm_page_begin_ptr, curr) \ 197 | }} 198 | 199 | void mm_vm_page_delete_and_free(vm_page_t *vm_page); 200 | #endif /**/ 201 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/UdemyCourse/mm.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include /*for getpagesize*/ 4 | #include /*For using mmap()*/ 5 | #include 6 | #include "mm.h" 7 | #include 8 | #include "css.h" 9 | 10 | static vm_page_for_families_t *first_vm_page_for_families = NULL; 11 | static size_t SYSTEM_PAGE_SIZE = 0; 12 | 13 | void 14 | mm_init(){ 15 | 16 | SYSTEM_PAGE_SIZE = getpagesize(); 17 | } 18 | 19 | static inline uint32_t 20 | mm_max_page_allocatable_memory(int units){ 21 | 22 | return (uint32_t) 23 | ((SYSTEM_PAGE_SIZE * units) - offset_of(vm_page_t, page_memory)); 24 | } 25 | 26 | #define MAX_PAGE_ALLOCATABLE_MEMORY(units) \ 27 | (mm_max_page_allocatable_memory(units)) 28 | 29 | 30 | /*Function to request VM page from kernel*/ 31 | static void * 32 | mm_get_new_vm_page_from_kernel(int units){ 33 | 34 | char *vm_page = mmap( 35 | 0, 36 | units * SYSTEM_PAGE_SIZE, 37 | PROT_READ|PROT_WRITE|PROT_EXEC, 38 | MAP_ANON|MAP_PRIVATE, 39 | 0, 0); 40 | 41 | if(vm_page == MAP_FAILED){ 42 | printf("Error : VM Page allocation Failed\n"); 43 | return NULL; 44 | } 45 | memset(vm_page, 0, units * SYSTEM_PAGE_SIZE); 46 | return (void *)vm_page; 47 | } 48 | 49 | /*Function to return a page to kernel*/ 50 | 51 | static void 52 | mm_return_vm_page_to_kernel (void *vm_page, int units){ 53 | 54 | if(munmap(vm_page, units * SYSTEM_PAGE_SIZE)){ 55 | printf("Error : Could not munmap VM page to kernel"); 56 | } 57 | } 58 | 59 | static int 60 | mm_get_hard_internal_memory_frag_size( 61 | block_meta_data_t *first, 62 | block_meta_data_t *second){ 63 | 64 | block_meta_data_t *next_block = NEXT_META_BLOCK_BY_SIZE(first); 65 | return (int)((unsigned long)second - (unsigned long)(next_block)); 66 | } 67 | 68 | 69 | static void 70 | mm_union_free_blocks(block_meta_data_t *first, 71 | block_meta_data_t *second){ 72 | 73 | assert(first->is_free == MM_TRUE && 74 | second->is_free == MM_TRUE); 75 | 76 | first->block_size += sizeof(block_meta_data_t) + 77 | second->block_size; 78 | 79 | first->next_block = second->next_block; 80 | 81 | if(second->next_block) 82 | second->next_block->prev_block = first; 83 | } 84 | 85 | vm_page_t * 86 | allocate_vm_page(vm_page_family_t *vm_page_family){ 87 | 88 | vm_page_t *vm_page = mm_get_new_vm_page_from_kernel(1); 89 | 90 | /*Initialize lower most Meta block of the VM page*/ 91 | MARK_VM_PAGE_EMPTY(vm_page); 92 | 93 | vm_page->block_meta_data.block_size = 94 | mm_max_page_allocatable_memory(1); 95 | vm_page->block_meta_data.offset = 96 | offset_of(vm_page_t, block_meta_data); 97 | init_glthread(&vm_page->block_meta_data.priority_thread_glue); 98 | vm_page->next = NULL; 99 | vm_page->prev = NULL; 100 | 101 | /*Set the back pointer to page family*/ 102 | vm_page->pg_family = vm_page_family; 103 | 104 | /*If it is a first VM data page for a given 105 | * page family*/ 106 | if(!vm_page_family->first_page){ 107 | vm_page_family->first_page = vm_page; 108 | return vm_page; 109 | } 110 | 111 | /* Insert new VM page to the head of the linked 112 | * list*/ 113 | vm_page->next = vm_page_family->first_page; 114 | vm_page_family->first_page->prev = vm_page; 115 | vm_page_family->first_page = vm_page; 116 | return vm_page; 117 | } 118 | 119 | void 120 | mm_vm_page_delete_and_free( 121 | vm_page_t *vm_page){ 122 | 123 | vm_page_family_t *vm_page_family = 124 | vm_page->pg_family; 125 | 126 | /*If the page being deleted is the head of the linked 127 | * list*/ 128 | if(vm_page_family->first_page == vm_page){ 129 | vm_page_family->first_page = vm_page->next; 130 | if(vm_page->next) 131 | vm_page->next->prev = NULL; 132 | vm_page->next = NULL; 133 | vm_page->prev = NULL; 134 | mm_return_vm_page_to_kernel((void *)vm_page, 1); 135 | return; 136 | } 137 | 138 | /*If we are deleting the page from middle or end of 139 | * linked list*/ 140 | if(vm_page->next) 141 | vm_page->next->prev = vm_page->prev; 142 | vm_page->prev->next = vm_page->next; 143 | mm_return_vm_page_to_kernel((void *)vm_page, 1); 144 | } 145 | 146 | void 147 | mm_print_vm_page_details(vm_page_t *vm_page){ 148 | 149 | printf("\t\t next = %p, prev = %p\n", vm_page->next, vm_page->prev); 150 | printf("\t\t page family = %s\n", vm_page->pg_family->struct_name); 151 | 152 | uint32_t j = 0; 153 | block_meta_data_t *curr; 154 | ITERATE_VM_PAGE_ALL_BLOCKS_BEGIN(vm_page, curr){ 155 | 156 | printf("\t\t\t%-14p Block %-3u %s block_size = %-6u " 157 | "offset = %-6u prev = %-14p next = %p\n", 158 | curr, 159 | j++, curr->is_free ? "F R E E D" : "ALLOCATED", 160 | curr->block_size, curr->offset, 161 | curr->prev_block, 162 | curr->next_block); 163 | } ITERATE_VM_PAGE_ALL_BLOCKS_END(vm_page, curr); 164 | } 165 | 166 | 167 | 168 | 169 | void 170 | mm_instantiate_new_page_family( 171 | char *struct_name, 172 | uint32_t struct_size){ 173 | 174 | 175 | vm_page_family_t *vm_page_family_curr = NULL; 176 | vm_page_for_families_t *new_vm_page_for_families = NULL; 177 | 178 | if(struct_size > SYSTEM_PAGE_SIZE){ 179 | 180 | printf("Error : %s() Structure %s Size exceeds system page size\n", 181 | __FUNCTION__, struct_name); 182 | return; 183 | } 184 | 185 | if(!first_vm_page_for_families){ 186 | 187 | first_vm_page_for_families = 188 | (vm_page_for_families_t *)mm_get_new_vm_page_from_kernel(1); 189 | first_vm_page_for_families->next = NULL; 190 | strncpy(first_vm_page_for_families->vm_page_family[0].struct_name, 191 | struct_name, MM_MAX_STRUCT_NAME); 192 | first_vm_page_for_families->vm_page_family[0].struct_size = struct_size; 193 | first_vm_page_for_families->vm_page_family[0].first_page = NULL; 194 | init_glthread(&first_vm_page_for_families->vm_page_family[0].free_block_priority_list_head); 195 | return; 196 | } 197 | 198 | vm_page_family_curr = lookup_page_family_by_name(struct_name); 199 | 200 | if(vm_page_family_curr) { 201 | assert(0); 202 | } 203 | 204 | uint32_t count = 0; 205 | 206 | ITERATE_PAGE_FAMILIES_BEGIN(first_vm_page_for_families, vm_page_family_curr){ 207 | 208 | count++; 209 | 210 | } ITERATE_PAGE_FAMILIES_END(first_vm_page_for_families, vm_page_family_curr); 211 | 212 | if(count == MAX_FAMILIES_PER_VM_PAGE){ 213 | 214 | new_vm_page_for_families = 215 | (vm_page_for_families_t *)mm_get_new_vm_page_from_kernel(1); 216 | new_vm_page_for_families->next = first_vm_page_for_families; 217 | first_vm_page_for_families = new_vm_page_for_families; 218 | } 219 | 220 | strncpy(vm_page_family_curr->struct_name, struct_name, 221 | MM_MAX_STRUCT_NAME); 222 | vm_page_family_curr->struct_size = struct_size; 223 | vm_page_family_curr->first_page = NULL; 224 | init_glthread(&vm_page_family_curr->free_block_priority_list_head); 225 | } 226 | 227 | void 228 | mm_print_registered_page_families(){ 229 | 230 | vm_page_family_t *vm_page_family_curr = NULL; 231 | vm_page_for_families_t *vm_page_for_families_curr = NULL; 232 | 233 | for(vm_page_for_families_curr = first_vm_page_for_families; 234 | vm_page_for_families_curr; 235 | vm_page_for_families_curr = vm_page_for_families_curr->next){ 236 | 237 | ITERATE_PAGE_FAMILIES_BEGIN(vm_page_for_families_curr, 238 | vm_page_family_curr){ 239 | 240 | printf("Page Family : %s, Size = %u\n", 241 | vm_page_family_curr->struct_name, 242 | vm_page_family_curr->struct_size); 243 | 244 | } ITERATE_PAGE_FAMILIES_END(vm_page_for_families_curr, 245 | vm_page_family_curr); 246 | } 247 | } 248 | 249 | static int 250 | free_blocks_comparison_function( 251 | void *_block_meta_data1, 252 | void *_block_meta_data2){ 253 | 254 | block_meta_data_t *block_meta_data1 = 255 | (block_meta_data_t *)_block_meta_data1; 256 | 257 | block_meta_data_t *block_meta_data2 = 258 | (block_meta_data_t *)_block_meta_data2; 259 | 260 | if(block_meta_data1->block_size > block_meta_data2->block_size) 261 | return -1; 262 | else if(block_meta_data1->block_size < block_meta_data2->block_size) 263 | return 1; 264 | return 0; 265 | } 266 | 267 | static void 268 | mm_add_free_block_meta_data_to_free_block_list( 269 | vm_page_family_t *vm_page_family, 270 | block_meta_data_t *free_block){ 271 | 272 | assert(free_block->is_free == MM_TRUE); 273 | glthread_priority_insert(&vm_page_family->free_block_priority_list_head, 274 | &free_block->priority_thread_glue, 275 | free_blocks_comparison_function, 276 | offset_of(block_meta_data_t, priority_thread_glue)); 277 | } 278 | 279 | static vm_page_t * 280 | mm_family_new_page_add(vm_page_family_t *vm_page_family){ 281 | 282 | vm_page_t *vm_page = allocate_vm_page(vm_page_family); 283 | 284 | if(!vm_page) 285 | return NULL; 286 | 287 | /* The new page is like one free block, add it to the 288 | * free block list*/ 289 | mm_add_free_block_meta_data_to_free_block_list( 290 | vm_page_family, &vm_page->block_meta_data); 291 | 292 | return vm_page; 293 | } 294 | 295 | /* Fn to mark block_meta_data as being Allocated for 296 | * 'size' bytes of application data. Return TRUE if 297 | * block allocation succeeds*/ 298 | static vm_bool_t 299 | mm_split_free_data_block_for_allocation( 300 | vm_page_family_t *vm_page_family, 301 | block_meta_data_t *block_meta_data, 302 | uint32_t size){ 303 | 304 | block_meta_data_t *next_block_meta_data = NULL; 305 | 306 | assert(block_meta_data->is_free == MM_TRUE); 307 | 308 | if(block_meta_data->block_size < size){ 309 | return MM_FALSE; 310 | } 311 | 312 | uint32_t remaining_size = 313 | block_meta_data->block_size - size; 314 | 315 | block_meta_data->is_free = MM_FALSE; 316 | block_meta_data->block_size = size; 317 | remove_glthread(&block_meta_data->priority_thread_glue); 318 | /*block_meta_data->offset = ??*/ 319 | 320 | /*Case 1 : No Split*/ 321 | if(!remaining_size){ 322 | return MM_TRUE; 323 | } 324 | 325 | /*Case 3 : Partial Split : Soft Internal Fragmentation*/ 326 | else if(sizeof(block_meta_data_t) < remaining_size && 327 | remaining_size < (sizeof(block_meta_data_t) + vm_page_family->struct_size)){ 328 | /*New Meta block is to be created*/ 329 | next_block_meta_data = NEXT_META_BLOCK_BY_SIZE(block_meta_data); 330 | next_block_meta_data->is_free = MM_TRUE; 331 | next_block_meta_data->block_size = 332 | remaining_size - sizeof(block_meta_data_t); 333 | next_block_meta_data->offset = block_meta_data->offset + 334 | sizeof(block_meta_data_t) + block_meta_data->block_size; 335 | init_glthread(&next_block_meta_data->priority_thread_glue); 336 | mm_add_free_block_meta_data_to_free_block_list( 337 | vm_page_family, next_block_meta_data); 338 | mm_bind_blocks_for_allocation(block_meta_data, next_block_meta_data); 339 | } 340 | 341 | /*Case 3 : Partial Split : Hard Internal Fragmentation*/ 342 | else if(remaining_size < sizeof(block_meta_data_t)){ 343 | /*No need to do anything !!*/ 344 | } 345 | 346 | /*Case 2 : Full Split : New Meta block is Created*/ 347 | else { 348 | /*New Meta block is to be created*/ 349 | next_block_meta_data = NEXT_META_BLOCK_BY_SIZE(block_meta_data); 350 | next_block_meta_data->is_free = MM_TRUE; 351 | next_block_meta_data->block_size = 352 | remaining_size - sizeof(block_meta_data_t); 353 | next_block_meta_data->offset = block_meta_data->offset + 354 | sizeof(block_meta_data_t) + block_meta_data->block_size; 355 | init_glthread(&next_block_meta_data->priority_thread_glue); 356 | mm_add_free_block_meta_data_to_free_block_list( 357 | vm_page_family, next_block_meta_data); 358 | mm_bind_blocks_for_allocation(block_meta_data, next_block_meta_data); 359 | } 360 | 361 | return MM_TRUE; 362 | 363 | } 364 | 365 | 366 | 367 | static block_meta_data_t * 368 | mm_allocate_free_data_block( 369 | vm_page_family_t *vm_page_family, 370 | uint32_t req_size){ 371 | 372 | vm_bool_t status = MM_FALSE; 373 | vm_page_t *vm_page = NULL; 374 | block_meta_data_t *block_meta_data = NULL; 375 | 376 | block_meta_data_t *biggest_block_meta_data = 377 | mm_get_biggest_free_block_page_family(vm_page_family); 378 | 379 | if(!biggest_block_meta_data || 380 | biggest_block_meta_data->block_size < req_size){ 381 | 382 | /*Time to add a new page to Page family to satisfy the request*/ 383 | vm_page = mm_family_new_page_add(vm_page_family); 384 | 385 | /*Allocate the free block from this page now*/ 386 | status = mm_split_free_data_block_for_allocation(vm_page_family, 387 | &vm_page->block_meta_data, req_size); 388 | 389 | if(status) 390 | return &vm_page->block_meta_data; 391 | 392 | return NULL; 393 | } 394 | /*The biggest block meta data can satisfy the request*/ 395 | if(biggest_block_meta_data){ 396 | status = mm_split_free_data_block_for_allocation(vm_page_family, 397 | biggest_block_meta_data, req_size); 398 | } 399 | 400 | if(status) 401 | return biggest_block_meta_data; 402 | 403 | return NULL; 404 | } 405 | 406 | vm_page_family_t * 407 | lookup_page_family_by_name(char *struct_name){ 408 | 409 | vm_page_family_t *vm_page_family_curr = NULL; 410 | vm_page_for_families_t *vm_page_for_families_curr = NULL; 411 | 412 | for(vm_page_for_families_curr = first_vm_page_for_families; 413 | vm_page_for_families_curr; 414 | vm_page_for_families_curr = vm_page_for_families_curr->next){ 415 | 416 | ITERATE_PAGE_FAMILIES_BEGIN(vm_page_for_families_curr, vm_page_family_curr){ 417 | 418 | if(strncmp(vm_page_family_curr->struct_name, 419 | struct_name, 420 | MM_MAX_STRUCT_NAME) == 0){ 421 | 422 | return vm_page_family_curr; 423 | } 424 | } ITERATE_PAGE_FAMILIES_END(vm_page_for_families_curr, vm_page_family_curr); 425 | } 426 | return NULL; 427 | } 428 | 429 | 430 | /* The public fn to be invoked by the application for Dynamic 431 | * Memory Allocations.*/ 432 | void * 433 | xcalloc(char *struct_name, int units){ 434 | 435 | /*Step 1*/ 436 | vm_page_family_t *pg_family = 437 | lookup_page_family_by_name(struct_name); 438 | 439 | if(!pg_family){ 440 | 441 | printf("Error : Structure %s not registered with Memory Manager\n", 442 | struct_name); 443 | return NULL; 444 | } 445 | 446 | if(units * pg_family->struct_size > MAX_PAGE_ALLOCATABLE_MEMORY(1)){ 447 | 448 | printf("Error : Memory Requested Exceeds Page Size\n"); 449 | return NULL; 450 | } 451 | 452 | /*Find the page which can satisfy the request*/ 453 | block_meta_data_t *free_block_meta_data = NULL; 454 | 455 | free_block_meta_data = mm_allocate_free_data_block( 456 | pg_family, units * pg_family->struct_size); 457 | 458 | if(free_block_meta_data){ 459 | memset((char *)(free_block_meta_data + 1), 0, 460 | free_block_meta_data->block_size); 461 | return (void *)(free_block_meta_data + 1); 462 | } 463 | 464 | return NULL; 465 | } 466 | 467 | static block_meta_data_t * 468 | mm_free_blocks(block_meta_data_t *to_be_free_block){ 469 | 470 | block_meta_data_t *return_block = NULL; 471 | 472 | assert(to_be_free_block->is_free == MM_FALSE); 473 | 474 | vm_page_t *hosting_page = 475 | MM_GET_PAGE_FROM_META_BLOCK(to_be_free_block); 476 | 477 | vm_page_family_t *vm_page_family = hosting_page->pg_family; 478 | 479 | return_block = to_be_free_block; 480 | 481 | to_be_free_block->is_free = MM_TRUE; 482 | 483 | block_meta_data_t *next_block = NEXT_META_BLOCK(to_be_free_block); 484 | 485 | /*Handling Hard IF memory*/ 486 | if(next_block){ 487 | /* Scenario 1 : When data block to be freed is not the last 488 | * upper most meta block in a VM data page*/ 489 | to_be_free_block->block_size += 490 | mm_get_hard_internal_memory_frag_size (to_be_free_block, next_block); 491 | } 492 | else { 493 | /* Scenario 2: Page Boundry condition*/ 494 | /* Block being freed is the upper most free data block 495 | * in a VM data page, check of hard internal fragmented 496 | * memory and merge*/ 497 | char *end_address_of_vm_page = (char *)((char *)hosting_page + SYSTEM_PAGE_SIZE); 498 | char *end_address_of_free_data_block = 499 | (char *)(to_be_free_block + 1) + to_be_free_block->block_size; 500 | int internal_mem_fragmentation = (int)((unsigned long)end_address_of_vm_page - 501 | (unsigned long)end_address_of_free_data_block); 502 | to_be_free_block->block_size += internal_mem_fragmentation; 503 | } 504 | 505 | /*Now perform Merging*/ 506 | if(next_block && next_block->is_free == MM_TRUE){ 507 | /*Union two free blocks*/ 508 | mm_union_free_blocks(to_be_free_block, next_block); 509 | return_block = to_be_free_block; 510 | } 511 | /*Check the previous block if it was free*/ 512 | block_meta_data_t *prev_block = PREV_META_BLOCK(to_be_free_block); 513 | 514 | if(prev_block && prev_block->is_free){ 515 | mm_union_free_blocks(prev_block, to_be_free_block); 516 | return_block = prev_block; 517 | } 518 | 519 | if(mm_is_vm_page_empty(hosting_page)){ 520 | mm_vm_page_delete_and_free(hosting_page); 521 | return NULL; 522 | } 523 | mm_add_free_block_meta_data_to_free_block_list( 524 | hosting_page->pg_family, return_block); 525 | 526 | return return_block; 527 | } 528 | 529 | 530 | 531 | void 532 | xfree(void *app_data){ 533 | 534 | block_meta_data_t *block_meta_data = 535 | (block_meta_data_t *)((char *)app_data - sizeof(block_meta_data_t)); 536 | 537 | assert(block_meta_data->is_free == MM_FALSE); 538 | mm_free_blocks(block_meta_data); 539 | } 540 | 541 | vm_bool_t 542 | mm_is_vm_page_empty(vm_page_t *vm_page){ 543 | 544 | if(vm_page->block_meta_data.next_block == NULL && 545 | vm_page->block_meta_data.prev_block == NULL && 546 | vm_page->block_meta_data.is_free == MM_TRUE){ 547 | 548 | return MM_TRUE; 549 | } 550 | return MM_FALSE; 551 | } 552 | 553 | 554 | 555 | 556 | void 557 | mm_print_block_usage(){ 558 | 559 | vm_page_t *vm_page_curr; 560 | vm_page_family_t *vm_page_family_curr; 561 | block_meta_data_t *block_meta_data_curr; 562 | uint32_t total_block_count, free_block_count, 563 | occupied_block_count; 564 | uint32_t application_memory_usage; 565 | 566 | ITERATE_PAGE_FAMILIES_BEGIN(first_vm_page_for_families, vm_page_family_curr){ 567 | 568 | total_block_count = 0; 569 | free_block_count = 0; 570 | application_memory_usage = 0; 571 | occupied_block_count = 0; 572 | ITERATE_VM_PAGE_BEGIN(vm_page_family_curr, vm_page_curr){ 573 | 574 | ITERATE_VM_PAGE_ALL_BLOCKS_BEGIN(vm_page_curr, block_meta_data_curr){ 575 | 576 | total_block_count++; 577 | 578 | /*Sanity Checks*/ 579 | if(block_meta_data_curr->is_free == MM_FALSE){ 580 | assert(IS_GLTHREAD_LIST_EMPTY(&block_meta_data_curr->\ 581 | priority_thread_glue)); 582 | } 583 | if(block_meta_data_curr->is_free == MM_TRUE){ 584 | assert(!IS_GLTHREAD_LIST_EMPTY(&block_meta_data_curr->\ 585 | priority_thread_glue)); 586 | } 587 | 588 | if(block_meta_data_curr->is_free == MM_TRUE){ 589 | free_block_count++; 590 | } 591 | else{ 592 | application_memory_usage += 593 | block_meta_data_curr->block_size + \ 594 | sizeof(block_meta_data_t); 595 | occupied_block_count++; 596 | } 597 | } ITERATE_VM_PAGE_ALL_BLOCKS_END(vm_page_curr, block_meta_data_curr); 598 | } ITERATE_VM_PAGE_END(vm_page_family_curr, vm_page_curr); 599 | 600 | printf("%-20s TBC : %-4u FBC : %-4u OBC : %-4u AppMemUsage : %u\n", 601 | vm_page_family_curr->struct_name, total_block_count, 602 | free_block_count, occupied_block_count, application_memory_usage); 603 | 604 | } ITERATE_PAGE_FAMILIES_END(first_vm_page_for_families, vm_page_family_curr); 605 | } 606 | 607 | 608 | void 609 | mm_print_memory_usage(char *struct_name){ 610 | 611 | uint32_t i = 0; 612 | vm_page_t *vm_page = NULL; 613 | vm_page_family_t *vm_page_family_curr; 614 | uint32_t number_of_struct_families = 0; 615 | uint32_t cumulative_vm_pages_claimed_from_kernel = 0; 616 | 617 | printf("\nPage Size = %zu Bytes\n", SYSTEM_PAGE_SIZE); 618 | 619 | ITERATE_PAGE_FAMILIES_BEGIN(first_vm_page_for_families, vm_page_family_curr){ 620 | 621 | if(struct_name){ 622 | if(strncmp(struct_name, vm_page_family_curr->struct_name, 623 | strlen(vm_page_family_curr->struct_name))){ 624 | continue; 625 | } 626 | } 627 | 628 | number_of_struct_families++; 629 | 630 | printf(ANSI_COLOR_GREEN "vm_page_family : %s, struct size = %u\n" 631 | ANSI_COLOR_RESET, 632 | vm_page_family_curr->struct_name, 633 | vm_page_family_curr->struct_size); 634 | i = 0; 635 | 636 | ITERATE_VM_PAGE_BEGIN(vm_page_family_curr, vm_page){ 637 | 638 | cumulative_vm_pages_claimed_from_kernel++; 639 | mm_print_vm_page_details(vm_page); 640 | 641 | } ITERATE_VM_PAGE_END(vm_page_family_curr, vm_page); 642 | printf("\n"); 643 | } ITERATE_PAGE_FAMILIES_END(first_vm_page_for_families, vm_page_family_curr); 644 | 645 | printf(ANSI_COLOR_MAGENTA "# Of VM Pages in Use : %u (%lu Bytes)\n" \ 646 | ANSI_COLOR_RESET, 647 | cumulative_vm_pages_claimed_from_kernel, 648 | SYSTEM_PAGE_SIZE * cumulative_vm_pages_claimed_from_kernel); 649 | 650 | float memory_app_use_to_total_memory_ratio = 0.0; 651 | 652 | printf("Total Memory being used by Memory Manager = %lu Bytes\n", 653 | cumulative_vm_pages_claimed_from_kernel * SYSTEM_PAGE_SIZE); 654 | } 655 | 656 | 657 | -------------------------------------------------------------------------------- /mm.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: mm.c 5 | * 6 | * Description: This file implements the routine for Memory Manager 7 | * 8 | * Version: 1.0 9 | * Created: 01/30/2020 10:31:41 AM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Juniper Networks (https://csepracticals.wixsite.com/csepracticals), sachinites@gmail.com 14 | * Company: Juniper Networks 15 | * 16 | * This file is part of the Linux Memory Manager distribution (https://github.com/sachinites) 17 | * Copyright (c) 2019 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General 19 | * Public License as published by the Free Software Foundation, version 3. 20 | * 21 | * This program is distributed in the hope that it will be useful, but 22 | * WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 | * General Public License for more details. 25 | * 26 | * visit website : https://csepracticals.wixsite.com/csepracticals for more courses and projects 27 | * 28 | * ===================================================================================== 29 | */ 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include /*for getpagesize, brk(), sbrk()*/ 36 | #include 37 | #include "css.h" 38 | #include "mm.h" 39 | 40 | /*Library Globals*/ 41 | void *gb_heap_segment_start = NULL; 42 | size_t GB_SYSTEM_PAGE_SIZE = 0; 43 | uint32_t gb_no_of_vm_families_registered = 0; 44 | void *gb_hsba = NULL; /*Heap Segment Start for Block Allocation*/ 45 | 46 | void 47 | mm_init(){ 48 | 49 | GB_SYSTEM_PAGE_SIZE = getpagesize(); 50 | gb_heap_segment_start = sbrk(0); 51 | if(!gb_heap_segment_start){ 52 | printf("Heap Memory Instantiation Failed\n"); 53 | assert(0); 54 | } 55 | } 56 | #if 1 57 | vm_page_t * 58 | mm_get_available_page_from_heap_segment(){ 59 | 60 | vm_page_t *vm_page_curr = NULL; 61 | #if 1 62 | vm_page_t *first_vm_page = (vm_page_t *)gb_hsba; 63 | 64 | ITERATE_HEAP_SEGMENT_PAGE_WISE_BEGIN(first_vm_page, vm_page_curr){ 65 | if(mm_is_vm_page_empty(vm_page_curr)){ 66 | return vm_page_curr; 67 | } 68 | }ITERATE_HEAP_SEGMENT_PAGE_WISE_END(first_vm_page, vm_page_curr); 69 | #endif 70 | /*No free Page could be found, expand heap segment*/ 71 | 72 | vm_page_curr = (vm_page_t *)sbrk(GB_SYSTEM_PAGE_SIZE); 73 | 74 | if(!vm_page_curr){ 75 | printf("Error : Heap Segment Expansion Failed, error no = %d\n", errno); 76 | return 0; 77 | } 78 | #if 0 79 | printf("Heap Segment Expanded. New diff = %lu, page units from gb_hsba = %ld, new sbrk = %p\n", 80 | (unsigned long)sbrk(0) - (unsigned long)initial_break, 81 | ((unsigned long)sbrk(0) - (unsigned long)gb_hsba)/GB_SYSTEM_PAGE_SIZE, 82 | sbrk(0)); 83 | #endif 84 | return vm_page_curr; 85 | } 86 | #endif 87 | 88 | #if 0 89 | vm_page_t * 90 | mm_get_available_page_from_heap_segment(){ 91 | 92 | vm_page_t *vm_page_curr = NULL; 93 | /*No free Page could be found, expand heap segment*/ 94 | vm_page_curr = (vm_page_t *)sbrk(GB_SYSTEM_PAGE_SIZE); 95 | return vm_page_curr; 96 | } 97 | #endif 98 | 99 | static inline uint32_t 100 | mm_max_page_allocatable_memory(){ 101 | 102 | return (uint32_t) 103 | (GB_SYSTEM_PAGE_SIZE - offset_of(vm_page_t, page_memory)); 104 | } 105 | 106 | #define MAX_PAGE_ALLOCATABLE_MEMORY \ 107 | (mm_max_page_allocatable_memory()) 108 | 109 | static vm_page_t * 110 | mm_get_available_page_index(vm_page_family_t *vm_page_family){ 111 | 112 | vm_page_t *curr, *prev; 113 | int page_index = -1; 114 | 115 | if(!vm_page_family->first_page) 116 | return NULL; 117 | 118 | ITERATE_VM_PAGE_PER_FAMILY_BEGIN(vm_page_family, curr){ 119 | 120 | if((int)(curr->page_index) == page_index + 1){ 121 | page_index++; 122 | prev = curr; 123 | continue; 124 | } 125 | return curr->prev; 126 | } ITERATE_VM_PAGE_PER_FAMILY_END(vm_page_family, curr) 127 | 128 | return prev; 129 | } 130 | 131 | /*Return a fresh new virtual page*/ 132 | vm_page_t * 133 | allocate_vm_page(vm_page_family_t *vm_page_family){ 134 | 135 | vm_page_t *prev_page = 136 | mm_get_available_page_index(vm_page_family); 137 | 138 | vm_page_t *vm_page = 139 | mm_get_available_page_from_heap_segment(); 140 | 141 | vm_page->block_meta_data.is_free = MM_TRUE; 142 | vm_page->block_meta_data.block_size = 143 | MAX_PAGE_ALLOCATABLE_MEMORY; 144 | vm_page->block_meta_data.offset = 145 | offset_of(vm_page_t, block_meta_data); 146 | init_glthread(&vm_page->block_meta_data.priority_thread_glue); 147 | vm_page->block_meta_data.prev_block = NULL; 148 | vm_page->block_meta_data.next_block = NULL; 149 | vm_page->next = NULL; 150 | vm_page->prev = NULL; 151 | vm_page_family->no_of_system_calls_to_alloc_dealloc_vm_pages++; 152 | vm_page->pg_family = vm_page_family; 153 | 154 | if(!prev_page){ 155 | vm_page->page_index = 0; 156 | vm_page->next = vm_page_family->first_page; 157 | if(vm_page_family->first_page) 158 | vm_page_family->first_page->prev = vm_page; 159 | vm_page_family->first_page = vm_page; 160 | return vm_page; 161 | } 162 | 163 | vm_page->next = prev_page->next; 164 | vm_page->prev = prev_page; 165 | if(vm_page->next) 166 | vm_page->next->prev = vm_page; 167 | prev_page->next = vm_page; 168 | vm_page->page_index = prev_page->page_index + 1; 169 | return vm_page; 170 | } 171 | void 172 | mm_instantiate_new_page_family( 173 | char *struct_name, 174 | uint32_t struct_size){ 175 | 176 | vm_page_family_t *vm_page_family = NULL; 177 | static size_t remaining_bytes_in_a_page = 0; 178 | 179 | if(struct_size > GB_SYSTEM_PAGE_SIZE){ 180 | printf("Error : %s() Structure Size exceeds system page size\n", 181 | __FUNCTION__); 182 | return; 183 | } 184 | 185 | if(!gb_no_of_vm_families_registered){ 186 | 187 | vm_page_family = (vm_page_family_t *)sbrk(GB_SYSTEM_PAGE_SIZE); 188 | remaining_bytes_in_a_page = GB_SYSTEM_PAGE_SIZE - sizeof(vm_page_family_t); 189 | gb_hsba = (void *) 190 | ((char *)vm_page_family + GB_SYSTEM_PAGE_SIZE); 191 | } 192 | else if(remaining_bytes_in_a_page >= sizeof(vm_page_family_t)){ 193 | 194 | vm_page_family = (vm_page_family_t *)((char *)gb_heap_segment_start + \ 195 | GB_SYSTEM_PAGE_SIZE - remaining_bytes_in_a_page); 196 | 197 | remaining_bytes_in_a_page -= sizeof(vm_page_family_t); 198 | } 199 | else{ 200 | vm_page_family = (vm_page_family_t *)sbrk(GB_SYSTEM_PAGE_SIZE); 201 | remaining_bytes_in_a_page = GB_SYSTEM_PAGE_SIZE - sizeof(vm_page_family_t); 202 | gb_hsba = (void *) 203 | ((char *)vm_page_family + GB_SYSTEM_PAGE_SIZE); 204 | } 205 | gb_no_of_vm_families_registered++; 206 | strncpy(vm_page_family->struct_name, struct_name, MM_MAX_STRUCT_NAME); 207 | vm_page_family->struct_size = struct_size; 208 | vm_page_family->first_page = NULL; 209 | init_glthread(&vm_page_family->free_block_priority_list_head); 210 | } 211 | 212 | vm_page_family_t * 213 | lookup_page_family_by_name(char *struct_name){ 214 | 215 | vm_page_family_t *vm_page_family_curr; 216 | 217 | ITERATE_PAGE_FAMILIES_BEGIN(gb_heap_segment_start, 218 | vm_page_family_curr){ 219 | 220 | if(strncmp(vm_page_family_curr->struct_name, 221 | struct_name, 222 | MM_MAX_STRUCT_NAME) == 0){ 223 | 224 | return vm_page_family_curr; 225 | } 226 | }ITERATE_PAGE_FAMILIES_END(gb_heap_segment_start, vm_page_family_curr); 227 | return NULL; 228 | } 229 | 230 | static int 231 | free_blocks_comparison_function( 232 | void *_block_meta_data1, 233 | void *_block_meta_data2){ 234 | 235 | block_meta_data_t *block_meta_data1 = 236 | (block_meta_data_t *)_block_meta_data1; 237 | 238 | block_meta_data_t *block_meta_data2 = 239 | (block_meta_data_t *)_block_meta_data2; 240 | 241 | if(block_meta_data1->block_size > block_meta_data2->block_size) 242 | return -1; 243 | else if(block_meta_data1->block_size < block_meta_data2->block_size) 244 | return 1; 245 | return 0; 246 | } 247 | 248 | static void 249 | mm_add_free_block_meta_data_to_free_block_list( 250 | vm_page_family_t *vm_page_family, 251 | block_meta_data_t *free_block){ 252 | 253 | assert(free_block->is_free == MM_TRUE); 254 | glthread_priority_insert(&vm_page_family->free_block_priority_list_head, 255 | &free_block->priority_thread_glue, 256 | free_blocks_comparison_function, 257 | offset_of(block_meta_data_t, priority_thread_glue)); 258 | } 259 | 260 | static vm_page_t * 261 | mm_family_new_page_add(vm_page_family_t *vm_page_family){ 262 | 263 | vm_page_t *vm_page = allocate_vm_page(vm_page_family); 264 | 265 | if(!vm_page) 266 | return NULL; 267 | 268 | /* The new page is like one free block, add it to the 269 | * free block list*/ 270 | mm_add_free_block_meta_data_to_free_block_list( 271 | vm_page_family, &vm_page->block_meta_data); 272 | 273 | return vm_page; 274 | } 275 | 276 | /* Fn to mark block_meta_data as being Allocated for 277 | * 'size' bytes of application data. Return TRUE if 278 | * block allocation succeeds*/ 279 | static vm_bool_t 280 | mm_allocate_free_block( 281 | vm_page_family_t *vm_page_family, 282 | block_meta_data_t *block_meta_data, 283 | uint32_t size){ 284 | 285 | assert(block_meta_data->is_free == MM_TRUE); 286 | 287 | assert(block_meta_data->block_size >= size); 288 | 289 | uint32_t remaining_size = 290 | block_meta_data->block_size - size; 291 | 292 | block_meta_data->is_free = MM_FALSE; 293 | block_meta_data->block_size = size; 294 | 295 | /*Unchanged*/ 296 | //block_meta_data->offset = ?? 297 | 298 | /* Since this block of memory is not allocated, remove it from 299 | * priority list of free blocks*/ 300 | remove_glthread(&block_meta_data->priority_thread_glue); 301 | 302 | vm_page_family->total_memory_in_use_by_app += 303 | sizeof(block_meta_data_t) + size; 304 | /* No need to do anything else if this block is completely used 305 | * to satisfy memory request*/ 306 | if(!remaining_size) 307 | return MM_TRUE; 308 | 309 | /* If the remaining memory chunk in this free block is unusable 310 | * because of fragmentation - however this should not be possible 311 | * except the boundry condition*/ 312 | if(remaining_size < 313 | (sizeof(block_meta_data_t) + vm_page_family->struct_size)){ 314 | /*printf("Warning : %uB Memory Unusable at page bottom\n", 315 | remaining_size);*/ 316 | return MM_TRUE; 317 | } 318 | 319 | block_meta_data_t *next_block_meta_data = NULL; 320 | 321 | next_block_meta_data = NEXT_META_BLOCK_BY_SIZE(block_meta_data); 322 | 323 | next_block_meta_data->is_free = MM_TRUE; 324 | 325 | next_block_meta_data->block_size = 326 | remaining_size - sizeof(block_meta_data_t); 327 | 328 | next_block_meta_data->offset = block_meta_data->offset + 329 | sizeof(block_meta_data_t) + block_meta_data->block_size; 330 | 331 | init_glthread(&next_block_meta_data->priority_thread_glue); 332 | 333 | mm_bind_blocks_for_allocation(block_meta_data, next_block_meta_data); 334 | 335 | mm_add_free_block_meta_data_to_free_block_list( 336 | vm_page_family, next_block_meta_data); 337 | 338 | return MM_TRUE; 339 | } 340 | 341 | static vm_page_t * 342 | mm_get_page_satisfying_request( 343 | vm_page_family_t *vm_page_family, 344 | uint32_t req_size, 345 | block_meta_data_t **block_meta_data/*O/P*/){ 346 | 347 | vm_bool_t status = MM_FALSE; 348 | vm_page_t *vm_page = NULL; 349 | 350 | block_meta_data_t *biggest_block_meta_data = 351 | mm_get_biggest_free_block_page_family(vm_page_family); 352 | 353 | if(!biggest_block_meta_data || 354 | biggest_block_meta_data->block_size < req_size){ 355 | 356 | /*Time to add a new page to Page family to satisfy the request*/ 357 | vm_page = mm_family_new_page_add(vm_page_family); 358 | /*Allocate the free block from this page now*/ 359 | status = mm_allocate_free_block(vm_page_family, 360 | &vm_page->block_meta_data, req_size); 361 | 362 | if(status == MM_FALSE){ 363 | *block_meta_data = NULL; 364 | mm_vm_page_delete_and_free(vm_page); 365 | return NULL; 366 | } 367 | 368 | *block_meta_data = &vm_page->block_meta_data; 369 | return vm_page; 370 | } 371 | /*The biggest block meta data can satisfy the request*/ 372 | status = mm_allocate_free_block(vm_page_family, 373 | biggest_block_meta_data, req_size); 374 | 375 | if(status == MM_FALSE){ 376 | *block_meta_data = NULL; 377 | return NULL; 378 | } 379 | 380 | *block_meta_data = biggest_block_meta_data; 381 | 382 | return MM_GET_PAGE_FROM_META_BLOCK(biggest_block_meta_data); 383 | } 384 | 385 | /* The public fn to be invoked by the application for Dynamic 386 | * Memory Allocations.*/ 387 | void * 388 | xcalloc(char *struct_name, int units){ 389 | 390 | void *result = NULL; 391 | 392 | vm_page_family_t *pg_family = 393 | lookup_page_family_by_name(struct_name); 394 | 395 | if(!pg_family){ 396 | 397 | printf("Error : Structure %s not registered with Memory Manager\n", 398 | struct_name); 399 | return NULL; 400 | } 401 | 402 | if(units * pg_family->struct_size > MAX_PAGE_ALLOCATABLE_MEMORY){ 403 | 404 | printf("Error : Memory Requested Exceeds Page Size\n"); 405 | return NULL; 406 | } 407 | 408 | if(!pg_family->first_page){ 409 | 410 | pg_family->first_page = mm_family_new_page_add(pg_family); 411 | 412 | if(mm_allocate_free_block(pg_family, 413 | &pg_family->first_page->block_meta_data, 414 | units * pg_family->struct_size)){ 415 | memset((char *)pg_family->first_page->page_memory, 0, 416 | units * pg_family->struct_size); 417 | return (void *)pg_family->first_page->page_memory; 418 | } 419 | } 420 | 421 | /*Find the page which can satisfy the request*/ 422 | block_meta_data_t *free_block_meta_data; 423 | 424 | vm_page_t *vm_page_curr = mm_get_page_satisfying_request( 425 | pg_family, units * pg_family->struct_size, 426 | &free_block_meta_data); 427 | 428 | if(free_block_meta_data){ 429 | /*Sanity Checks*/ 430 | if(free_block_meta_data->is_free == MM_TRUE || 431 | !IS_GLTHREAD_LIST_EMPTY(&free_block_meta_data->priority_thread_glue)){ 432 | assert(0); 433 | } 434 | memset((char *)(free_block_meta_data + 1), 0, 435 | free_block_meta_data->block_size); 436 | return (void *)(free_block_meta_data + 1); 437 | } 438 | 439 | return NULL; 440 | } 441 | 442 | static void 443 | mm_union_free_blocks(block_meta_data_t *first, 444 | block_meta_data_t *second){ 445 | 446 | assert(first->is_free == MM_TRUE && 447 | second->is_free == MM_TRUE); 448 | 449 | first->block_size += sizeof(block_meta_data_t) + 450 | second->block_size; 451 | remove_glthread(&first->priority_thread_glue); 452 | remove_glthread(&second->priority_thread_glue); 453 | mm_bind_blocks_for_deallocation(first, second); 454 | } 455 | 456 | static void 457 | mm_return_vm_page_to_heap_segment(vm_page_t *vm_page){ 458 | 459 | MARK_VM_PAGE_EMPTY(vm_page); 460 | 461 | /* If this VM page is the top-most page of Heap Memory 462 | * Segment, then lower down the heap memory segment. 463 | * Note that, once you lower down the heap memory segment 464 | * this page shall be out of allotted valid virtual address 465 | * of a process, and any access to it shall result in 466 | * segmentation fault*/ 467 | /* Also note that, if the VM page is the top-most page of Heap Memory 468 | * then it could be possible there are free contiguous pages below 469 | * this VM page. We need to lowered down break pointer freeing all 470 | * contiguous VM pages lying below this VM page*/ 471 | if((void *)vm_page != 472 | (void *)((char *)sbrk(0) - GB_SYSTEM_PAGE_SIZE)){ 473 | return; 474 | } 475 | 476 | vm_page_t *bottom_most_free_page = NULL; 477 | 478 | for(bottom_most_free_page = 479 | MM_GET_NEXT_PAGE_IN_HEAP_SEGMENT(vm_page, '-'); 480 | mm_is_vm_page_empty(bottom_most_free_page); 481 | bottom_most_free_page = 482 | MM_GET_NEXT_PAGE_IN_HEAP_SEGMENT(bottom_most_free_page, '-')){ 483 | 484 | if((void *)bottom_most_free_page == gb_hsba) 485 | break; 486 | } 487 | 488 | if((void *)bottom_most_free_page != gb_hsba){ 489 | bottom_most_free_page = 490 | MM_GET_NEXT_PAGE_IN_HEAP_SEGMENT(bottom_most_free_page, '+'); 491 | } 492 | #if 0 493 | printf("No of Contiguous pages to be freed from Heap Segment = %lu\n", 494 | (((char *)vm_page - (char *)bottom_most_free_page)/GB_SYSTEM_PAGE_SIZE)+ 1); 495 | #endif 496 | /*Now lower down the break pointer*/ 497 | assert(!brk((void *)bottom_most_free_page)); 498 | } 499 | 500 | void 501 | mm_vm_page_delete_and_free( 502 | vm_page_t *vm_page){ 503 | 504 | vm_page_family_t *vm_page_family = 505 | vm_page->pg_family; 506 | 507 | assert(vm_page_family->first_page); 508 | 509 | if(vm_page_family->first_page == vm_page){ 510 | vm_page_family->first_page = vm_page->next; 511 | if(vm_page->next) 512 | vm_page->next->prev = NULL; 513 | vm_page_family->no_of_system_calls_to_alloc_dealloc_vm_pages++; 514 | mm_return_vm_page_to_heap_segment(vm_page); 515 | return; 516 | } 517 | 518 | if(vm_page->next) 519 | vm_page->next->prev = vm_page->prev; 520 | vm_page->prev->next = vm_page->next; 521 | vm_page_family->no_of_system_calls_to_alloc_dealloc_vm_pages++; 522 | mm_return_vm_page_to_heap_segment(vm_page); 523 | } 524 | 525 | static block_meta_data_t * 526 | mm_free_blocks(block_meta_data_t *to_be_free_block){ 527 | 528 | block_meta_data_t *return_block = NULL; 529 | 530 | assert(to_be_free_block->is_free == MM_FALSE); 531 | 532 | vm_page_t *hosting_page = 533 | MM_GET_PAGE_FROM_META_BLOCK(to_be_free_block); 534 | 535 | vm_page_family_t *vm_page_family = hosting_page->pg_family; 536 | 537 | vm_page_family->total_memory_in_use_by_app -= 538 | sizeof(block_meta_data_t) + to_be_free_block->block_size; 539 | 540 | to_be_free_block->is_free = MM_TRUE; 541 | 542 | return_block = to_be_free_block; 543 | 544 | block_meta_data_t *next_block = NEXT_META_BLOCK(to_be_free_block); 545 | 546 | if(next_block && next_block->is_free == MM_TRUE){ 547 | /*Union two free blocks*/ 548 | mm_union_free_blocks(to_be_free_block, next_block); 549 | return_block = to_be_free_block; 550 | } 551 | /*Check the previous block if it was free*/ 552 | block_meta_data_t *prev_block = PREV_META_BLOCK(to_be_free_block); 553 | 554 | if(prev_block && prev_block->is_free){ 555 | mm_union_free_blocks(prev_block, to_be_free_block); 556 | return_block = prev_block; 557 | } 558 | 559 | if(mm_is_vm_page_empty(hosting_page)){ 560 | mm_vm_page_delete_and_free(hosting_page); 561 | return NULL; 562 | } 563 | mm_add_free_block_meta_data_to_free_block_list( 564 | hosting_page->pg_family, return_block); 565 | 566 | return return_block; 567 | } 568 | 569 | void 570 | xfree(void *app_data){ 571 | 572 | block_meta_data_t *block_meta_data = 573 | (block_meta_data_t *)((char *)app_data - sizeof(block_meta_data_t)); 574 | 575 | if(block_meta_data->is_free == MM_TRUE){ 576 | printf("!Double Free detected\n"); 577 | assert(0); 578 | } 579 | mm_free_blocks(block_meta_data); 580 | } 581 | 582 | vm_bool_t 583 | mm_is_vm_page_empty(vm_page_t *vm_page){ 584 | 585 | if(vm_page->block_meta_data.next_block == NULL && 586 | vm_page->block_meta_data.prev_block == NULL && 587 | vm_page->block_meta_data.is_free == MM_TRUE){ 588 | 589 | return MM_TRUE; 590 | } 591 | return MM_FALSE; 592 | } 593 | 594 | void 595 | mm_print_vm_page_details(vm_page_t *vm_page, uint32_t i){ 596 | 597 | printf("\tPage Index : %u , address = %p\n", vm_page->page_index, vm_page); 598 | printf("\t\t next = %p, prev = %p\n", vm_page->next, vm_page->prev); 599 | printf("\t\t page family = %s\n", vm_page->pg_family->struct_name); 600 | 601 | uint32_t j = 0; 602 | block_meta_data_t *curr; 603 | ITERATE_VM_PAGE_ALL_BLOCKS_BEGIN(vm_page, curr){ 604 | 605 | printf(ANSI_COLOR_YELLOW "\t\t\t%-14p Block %-3u %s block_size = %-6u " 606 | "offset = %-6u prev = %-14p next = %p\n" 607 | ANSI_COLOR_RESET, curr, 608 | j++, curr->is_free ? "F R E E D" : "ALLOCATED", 609 | curr->block_size, curr->offset, 610 | curr->prev_block, 611 | curr->next_block); 612 | } ITERATE_VM_PAGE_ALL_BLOCKS_END(vm_page, curr); 613 | } 614 | 615 | void 616 | mm_print_memory_usage(char *struct_name){ 617 | 618 | uint32_t i = 0; 619 | vm_page_t *vm_page = NULL; 620 | vm_page_family_t *vm_page_family_curr; 621 | uint32_t number_of_struct_families = 0; 622 | uint32_t total_memory_in_use_by_application = 0; 623 | uint32_t cumulative_vm_pages_claimed_from_kernel = 0; 624 | 625 | printf("\nPage Size = %zu Bytes\n", GB_SYSTEM_PAGE_SIZE); 626 | 627 | ITERATE_PAGE_FAMILIES_BEGIN(gb_heap_segment_start, vm_page_family_curr){ 628 | 629 | if(struct_name){ 630 | if(strncmp(struct_name, vm_page_family_curr->struct_name, 631 | strlen(vm_page_family_curr->struct_name))){ 632 | continue; 633 | } 634 | } 635 | 636 | number_of_struct_families++; 637 | 638 | printf(ANSI_COLOR_GREEN "vm_page_family : %s, struct size = %u\n" 639 | ANSI_COLOR_RESET, 640 | vm_page_family_curr->struct_name, 641 | vm_page_family_curr->struct_size); 642 | printf(ANSI_COLOR_CYAN "\tApp Used Memory %uB, #Sys Calls %u\n" 643 | ANSI_COLOR_RESET, 644 | vm_page_family_curr->total_memory_in_use_by_app, 645 | vm_page_family_curr->\ 646 | no_of_system_calls_to_alloc_dealloc_vm_pages); 647 | 648 | total_memory_in_use_by_application += 649 | vm_page_family_curr->total_memory_in_use_by_app; 650 | 651 | i = 0; 652 | 653 | ITERATE_VM_PAGE_PER_FAMILY_BEGIN(vm_page_family_curr, vm_page){ 654 | 655 | cumulative_vm_pages_claimed_from_kernel++; 656 | mm_print_vm_page_details(vm_page, i++); 657 | 658 | } ITERATE_VM_PAGE_PER_FAMILY_END(vm_page_family_curr, vm_page); 659 | printf("\n"); 660 | } ITERATE_PAGE_FAMILIES_END(gb_heap_segment_start, vm_page_family_curr); 661 | 662 | printf(ANSI_COLOR_MAGENTA "\nTotal Applcation Memory Usage : %u Bytes\n" 663 | ANSI_COLOR_RESET, total_memory_in_use_by_application); 664 | 665 | printf(ANSI_COLOR_MAGENTA "# Of VM Pages in Use : %u (%lu Bytes).\n" \ 666 | "Heap Segment Start ptr = %p, sbrk(0) = %p , gb_hsba = %p, diff = %lu\n" \ 667 | ANSI_COLOR_RESET, 668 | cumulative_vm_pages_claimed_from_kernel, 669 | GB_SYSTEM_PAGE_SIZE * cumulative_vm_pages_claimed_from_kernel, 670 | gb_heap_segment_start, sbrk(0), gb_hsba, 671 | (unsigned long)sbrk(0) - (unsigned long)gb_hsba); 672 | 673 | float memory_app_use_to_total_memory_ratio = 0.0; 674 | 675 | if(cumulative_vm_pages_claimed_from_kernel){ 676 | memory_app_use_to_total_memory_ratio = 677 | (float)(total_memory_in_use_by_application * 100)/\ 678 | (float)(cumulative_vm_pages_claimed_from_kernel * GB_SYSTEM_PAGE_SIZE); 679 | } 680 | printf(ANSI_COLOR_MAGENTA "Memory In Use by Application = %f%%\n" 681 | ANSI_COLOR_RESET, 682 | memory_app_use_to_total_memory_ratio); 683 | 684 | printf("Total Memory being used by Memory Manager = %lu Bytes\n", 685 | ((cumulative_vm_pages_claimed_from_kernel *\ 686 | GB_SYSTEM_PAGE_SIZE) + 687 | (number_of_struct_families * sizeof(vm_page_family_t)))); 688 | } 689 | 690 | void 691 | mm_print_block_usage(){ 692 | 693 | vm_page_t *vm_page_curr; 694 | vm_page_family_t *vm_page_family_curr; 695 | block_meta_data_t *block_meta_data_curr; 696 | uint32_t total_block_count, free_block_count, 697 | occupied_block_count; 698 | uint32_t application_memory_usage; 699 | 700 | ITERATE_PAGE_FAMILIES_BEGIN(gb_heap_segment_start, vm_page_family_curr){ 701 | 702 | total_block_count = 0; 703 | free_block_count = 0; 704 | application_memory_usage = 0; 705 | occupied_block_count = 0; 706 | ITERATE_VM_PAGE_PER_FAMILY_BEGIN(vm_page_family_curr, vm_page_curr){ 707 | 708 | ITERATE_VM_PAGE_ALL_BLOCKS_BEGIN(vm_page_curr, block_meta_data_curr){ 709 | 710 | total_block_count++; 711 | 712 | /*Sanity Checks*/ 713 | if(block_meta_data_curr->is_free == MM_FALSE){ 714 | assert(IS_GLTHREAD_LIST_EMPTY(&block_meta_data_curr->\ 715 | priority_thread_glue)); 716 | } 717 | if(block_meta_data_curr->is_free == MM_TRUE){ 718 | assert(!IS_GLTHREAD_LIST_EMPTY(&block_meta_data_curr->\ 719 | priority_thread_glue)); 720 | } 721 | 722 | if(block_meta_data_curr->is_free == MM_TRUE){ 723 | free_block_count++; 724 | } 725 | else{ 726 | application_memory_usage += 727 | block_meta_data_curr->block_size + \ 728 | sizeof(block_meta_data_t); 729 | occupied_block_count++; 730 | } 731 | } ITERATE_VM_PAGE_ALL_BLOCKS_END(vm_page_curr, block_meta_data_curr); 732 | } ITERATE_VM_PAGE_PER_FAMILY_END(vm_page_family_curr, vm_page_curr); 733 | 734 | printf("%-20s TBC : %-4u FBC : %-4u OBC : %-4u AppMemUsage : %u\n", 735 | vm_page_family_curr->struct_name, total_block_count, 736 | free_block_count, occupied_block_count, application_memory_usage); 737 | 738 | } ITERATE_PAGE_FAMILIES_END(gb_heap_segment_start, vm_page_family_curr); 739 | } 740 | -------------------------------------------------------------------------------- /Course/LinuxMemoryManager/mm.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ===================================================================================== 3 | * 4 | * Filename: mm.c 5 | * 6 | * Description: This file implements the routine for Memory Manager 7 | * 8 | * Version: 1.0 9 | * Created: 01/30/2020 10:31:41 AM 10 | * Revision: none 11 | * Compiler: gcc 12 | * 13 | * Author: Er. Abhishek Sagar, Juniper Networks (https://www.csepracticals.com), sachinites@gmail.com 14 | * Company: Juniper Networks 15 | * 16 | * This file is part of the Linux Memory Manager distribution (https://github.com/sachinites) 17 | * Copyright (c) 2019 Abhishek Sagar. 18 | * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General 19 | * Public License as published by the Free Software Foundation, version 3. 20 | * 21 | * This program is distributed in the hope that it will be useful, but 22 | * WITHOUT ANY WARRANTY; without even the implied warranty of 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 24 | * General Public License for more details. 25 | * 26 | * visit website : https://www.csepracticals.com for more courses and projects 27 | * 28 | * ===================================================================================== 29 | */ 30 | 31 | #include "mm.h" 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include /*for getpagesize*/ 37 | #include 38 | #include 39 | #include "css.h" 40 | 41 | #define __USE_MMAP__ 42 | #undef __USE_BRK__ 43 | #undef __USE_GLIBC__ 44 | 45 | static vm_page_for_families_t *first_vm_page_for_families = NULL; 46 | static vm_page_family_t misc_vm_page_family; 47 | static size_t SYSTEM_PAGE_SIZE = 0; 48 | void *gb_hsba = NULL; 49 | static vm_page_t *vm_page_mm_instance = NULL; 50 | static mm_instance_t *next_mm_instance = NULL; 51 | 52 | void 53 | mm_init(){ 54 | 55 | if (!SYSTEM_PAGE_SIZE) 56 | SYSTEM_PAGE_SIZE = getpagesize() * 2; 57 | gb_hsba = sbrk(0); 58 | misc_vm_page_family.first_page = NULL; 59 | memset(&misc_vm_page_family, 0, sizeof(vm_page_family_t)); 60 | strncpy(misc_vm_page_family.struct_name, "Misc" , 4); 61 | misc_vm_page_family.struct_size = 0; 62 | init_glthread(&misc_vm_page_family.free_block_priority_list_head); 63 | } 64 | 65 | static inline uint32_t 66 | mm_max_page_allocatable_memory(int units){ 67 | 68 | return (uint32_t) 69 | ((SYSTEM_PAGE_SIZE * units) - offset_of(vm_page_t, page_memory)); 70 | } 71 | 72 | #define MAX_PAGE_ALLOCATABLE_MEMORY(units) \ 73 | (mm_max_page_allocatable_memory(units)) 74 | 75 | static vm_page_t * 76 | mm_get_available_page_index(vm_page_family_t *vm_page_family){ 77 | 78 | vm_page_t *curr, *prev; 79 | int page_index = -1; 80 | 81 | if(!vm_page_family->first_page) 82 | return NULL; 83 | 84 | ITERATE_VM_PAGE_BEGIN(vm_page_family, curr){ 85 | 86 | if((int)(curr->page_index) == page_index + 1){ 87 | page_index++; 88 | prev = curr; 89 | continue; 90 | } 91 | return curr->prev; 92 | } ITERATE_VM_PAGE_END(vm_page_family, curr) 93 | 94 | return prev; 95 | } 96 | 97 | static vm_page_t * 98 | mm_sbrk_get_available_page_from_heap_segment(int units){ 99 | 100 | vm_page_t *vm_page_curr = NULL; 101 | 102 | vm_page_t *first_vm_page = (vm_page_t *)gb_hsba; 103 | 104 | ITERATE_HEAP_SEGMENT_PAGE_WISE_BEGIN(first_vm_page, vm_page_curr){ 105 | if(mm_is_vm_page_empty(vm_page_curr)){ 106 | return vm_page_curr; 107 | } 108 | }ITERATE_HEAP_SEGMENT_PAGE_WISE_END(first_vm_page, vm_page_curr); 109 | /*No free Page could be found, expand heap segment*/ 110 | vm_page_curr = (vm_page_t *)sbrk(SYSTEM_PAGE_SIZE * units); 111 | 112 | if(!vm_page_curr){ 113 | printf("Error : Heap Segment Expansion Failed, error no = %d\n", errno); 114 | } 115 | return vm_page_curr; 116 | } 117 | 118 | static vm_page_t * 119 | mm_get_new_vm_page_from_kernel(int units){ 120 | 121 | vm_page_t *vm_page = NULL; 122 | 123 | #ifdef __USE_GLIBC__ 124 | vm_page = (vm_page_t *)calloc(units, SYSTEM_PAGE_SIZE); 125 | 126 | #elif defined(__USE_MMAP__) 127 | char * region = mmap( 128 | sbrk(0), 129 | units * SYSTEM_PAGE_SIZE, 130 | PROT_READ|PROT_WRITE|PROT_EXEC, 131 | MAP_ANON|MAP_PRIVATE, 132 | 0,0); 133 | 134 | if (region == MAP_FAILED) { 135 | printf("Error : VM Page allocation Failed\n"); 136 | return NULL; 137 | } 138 | vm_page = (vm_page_t *)region; 139 | 140 | #elif defined(__USE_BRK__) 141 | vm_page = mm_sbrk_get_available_page_from_heap_segment(units);; 142 | 143 | #endif 144 | memset(vm_page, 0, units * SYSTEM_PAGE_SIZE); 145 | vm_page->page_size = units * SYSTEM_PAGE_SIZE; 146 | return vm_page; 147 | } 148 | 149 | 150 | mm_instance_t * 151 | mm_init_new_instance() { 152 | 153 | vm_page_t *vm_page = NULL; 154 | 155 | if (!SYSTEM_PAGE_SIZE) 156 | SYSTEM_PAGE_SIZE = getpagesize() * 2; 157 | 158 | if (vm_page_mm_instance == NULL) { 159 | vm_page = mm_get_new_vm_page_from_kernel(1); 160 | } 161 | 162 | if (next_mm_instance == NULL) { 163 | next_mm_instance = (mm_instance_t *)(vm_page); 164 | next_mm_instance->gb_hsba = sbrk(0); 165 | return next_mm_instance; 166 | } 167 | 168 | if ((char *)(next_mm_instance + 1) == 169 | (char *)vm_page + SYSTEM_PAGE_SIZE) { 170 | assert(0); 171 | } 172 | 173 | next_mm_instance++; 174 | next_mm_instance->gb_hsba = sbrk(0); 175 | return next_mm_instance; 176 | } 177 | 178 | static void 179 | mm_sbrk_free_vm_page(vm_page_t *vm_page, int units){ 180 | 181 | /* If this VM page is the top-most page of Heap Memory 182 | * Segment, then lower down the heap memory segment. 183 | * Note that, once you lower down the heap memory segment 184 | * this page shall be out of allotted valid virtual address 185 | * of a process, and any access to it shall result in 186 | * segmentation fault*/ 187 | /* Also note that, if the VM page is the top-most page of Heap Memory 188 | * then it could be possible there are free contiguous pages below 189 | * this VM page. We need to lowered down break pointer freeing all 190 | * contiguous VM pages lying below this VM page*/ 191 | 192 | if((void *)vm_page != 193 | (void *)((char *)sbrk(0) - (SYSTEM_PAGE_SIZE * units))){ 194 | return; 195 | } 196 | 197 | vm_page_t *bottom_most_free_page = NULL; 198 | 199 | for(bottom_most_free_page = 200 | MM_GET_NEXT_CONTIGUOUS_PAGE_IN_HEAP_SEGMENT(vm_page, '-'); 201 | mm_is_vm_page_empty(bottom_most_free_page); 202 | bottom_most_free_page = 203 | MM_GET_NEXT_CONTIGUOUS_PAGE_IN_HEAP_SEGMENT(bottom_most_free_page, '-')){ 204 | 205 | if((void *)bottom_most_free_page == gb_hsba) 206 | break; 207 | } 208 | 209 | if((void *)bottom_most_free_page != gb_hsba){ 210 | bottom_most_free_page = 211 | MM_GET_NEXT_CONTIGUOUS_PAGE_IN_HEAP_SEGMENT(bottom_most_free_page, '+'); 212 | } 213 | /*Now lower down the break pointer*/ 214 | assert(!brk((void *)bottom_most_free_page)); 215 | } 216 | 217 | static void 218 | mm_return_vm_page_to_kernel(void *ptr, int units){ 219 | 220 | int rc = 0; 221 | MARK_VM_PAGE_EMPTY(((vm_page_t *)ptr)); 222 | 223 | #ifdef __USE_GLIBC__ 224 | free(ptr); 225 | #elif defined(__USE_MMAP__) 226 | if(rc = munmap(ptr, units * SYSTEM_PAGE_SIZE)){ 227 | printf("Error : Could not munmap VM page (%u) to kernel, errno = %d\n", ((vm_page_t *)ptr)->page_size, errno); 228 | } 229 | #elif defined(__USE_BRK__) 230 | mm_sbrk_free_vm_page((vm_page_t *)ptr, units); 231 | #endif 232 | } 233 | 234 | /*Return a fresh new virtual page*/ 235 | vm_page_t * 236 | allocate_vm_page(vm_page_family_t *vm_page_family, int units){ 237 | 238 | vm_page_t *prev_page = 239 | mm_get_available_page_index(vm_page_family); 240 | 241 | vm_page_t *vm_page = mm_get_new_vm_page_from_kernel(units); 242 | vm_page->block_meta_data.is_free = MM_TRUE; 243 | vm_page->block_meta_data.block_size = 244 | MAX_PAGE_ALLOCATABLE_MEMORY(units); 245 | vm_page->block_meta_data.offset = 246 | offset_of(vm_page_t, block_meta_data); 247 | init_glthread(&vm_page->block_meta_data.priority_thread_glue); 248 | vm_page->block_meta_data.prev_block = NULL; 249 | vm_page->block_meta_data.next_block = NULL; 250 | vm_page->next = NULL; 251 | vm_page->prev = NULL; 252 | vm_page_family->no_of_system_calls_to_alloc_dealloc_vm_pages++; 253 | vm_page->pg_family = vm_page_family; 254 | 255 | if(!prev_page){ 256 | vm_page->page_index = 0; 257 | vm_page->next = vm_page_family->first_page; 258 | if(vm_page_family->first_page) 259 | vm_page_family->first_page->prev = vm_page; 260 | vm_page_family->first_page = vm_page; 261 | return vm_page; 262 | } 263 | 264 | vm_page->next = prev_page->next; 265 | vm_page->prev = prev_page; 266 | if(vm_page->next) 267 | vm_page->next->prev = vm_page; 268 | prev_page->next = vm_page; 269 | vm_page->page_index = prev_page->page_index + 1; 270 | return vm_page; 271 | } 272 | 273 | 274 | void 275 | mm_instantiate_new_page_family( 276 | mm_instance_t *mm_inst, 277 | char *struct_name, 278 | uint32_t struct_size){ 279 | 280 | vm_page_family_t *vm_page_family_curr = NULL; 281 | vm_page_for_families_t *new_vm_page_for_families = NULL; 282 | vm_page_for_families_t *vm_page_for_families_global; 283 | 284 | vm_page_for_families_global = mm_inst ? 285 | mm_inst->first_vm_page_for_families : first_vm_page_for_families; 286 | 287 | if(!vm_page_for_families_global){ 288 | vm_page_for_families_global = (vm_page_for_families_t *)mm_get_new_vm_page_from_kernel(1); 289 | vm_page_for_families_global->next = NULL; 290 | strncpy(vm_page_for_families_global->vm_page_family[0].struct_name, struct_name, 291 | MM_MAX_STRUCT_NAME); 292 | vm_page_for_families_global->vm_page_family[0].struct_size = struct_size; 293 | vm_page_for_families_global->vm_page_family[0].first_page = NULL; 294 | init_glthread(&vm_page_for_families_global->vm_page_family[0].free_block_priority_list_head); 295 | if (mm_inst) 296 | mm_inst->first_vm_page_for_families = vm_page_for_families_global; 297 | else 298 | first_vm_page_for_families = vm_page_for_families_global; 299 | 300 | return; 301 | } 302 | 303 | vm_page_family_curr = lookup_page_family_by_name(mm_inst, struct_name); 304 | 305 | if(vm_page_family_curr) { 306 | assert(0); 307 | } 308 | 309 | uint32_t count = 0; 310 | 311 | ITERATE_PAGE_FAMILIES_BEGIN(vm_page_for_families_global, vm_page_family_curr){ 312 | 313 | count++; 314 | 315 | } ITERATE_PAGE_FAMILIES_END(vm_page_for_families_global, vm_page_family_curr); 316 | 317 | if(count == MAX_FAMILIES_PER_VM_PAGE){ 318 | /*Request a new vm page from kernel to add a new family*/ 319 | new_vm_page_for_families = (vm_page_for_families_t *)mm_get_new_vm_page_from_kernel(1); 320 | new_vm_page_for_families->next = vm_page_for_families_global; 321 | vm_page_for_families_global = new_vm_page_for_families; 322 | vm_page_family_curr = &vm_page_for_families_global->vm_page_family[0]; 323 | } 324 | 325 | strncpy(vm_page_family_curr->struct_name, struct_name, 326 | MM_MAX_STRUCT_NAME); 327 | vm_page_family_curr->struct_size = struct_size; 328 | vm_page_family_curr->first_page = NULL; 329 | init_glthread(&vm_page_family_curr->free_block_priority_list_head); 330 | } 331 | 332 | vm_page_family_t * 333 | lookup_page_family_by_name(mm_instance_t *mm_inst, char *struct_name){ 334 | 335 | vm_page_family_t *vm_page_family_curr = NULL; 336 | vm_page_for_families_t *vm_page_for_families_curr = NULL; 337 | 338 | for(vm_page_for_families_curr = 339 | mm_inst ? mm_inst->first_vm_page_for_families : first_vm_page_for_families; 340 | vm_page_for_families_curr; 341 | vm_page_for_families_curr = vm_page_for_families_curr->next){ 342 | 343 | ITERATE_PAGE_FAMILIES_BEGIN(vm_page_for_families_curr, vm_page_family_curr){ 344 | 345 | if(strncmp(vm_page_family_curr->struct_name, 346 | struct_name, 347 | MM_MAX_STRUCT_NAME) == 0){ 348 | 349 | return vm_page_family_curr; 350 | } 351 | } ITERATE_PAGE_FAMILIES_END(vm_page_for_families_curr, vm_page_family_curr); 352 | } 353 | return NULL; 354 | } 355 | 356 | static int 357 | free_blocks_comparison_function( 358 | void *_block_meta_data1, 359 | void *_block_meta_data2){ 360 | 361 | block_meta_data_t *block_meta_data1 = 362 | (block_meta_data_t *)_block_meta_data1; 363 | 364 | block_meta_data_t *block_meta_data2 = 365 | (block_meta_data_t *)_block_meta_data2; 366 | 367 | if(block_meta_data1->block_size > block_meta_data2->block_size) 368 | return -1; 369 | else if(block_meta_data1->block_size < block_meta_data2->block_size) 370 | return 1; 371 | return 0; 372 | } 373 | 374 | static void 375 | mm_add_free_block_meta_data_to_free_block_list( 376 | vm_page_family_t *vm_page_family, 377 | block_meta_data_t *free_block){ 378 | 379 | assert(free_block->is_free == MM_TRUE); 380 | glthread_priority_insert(&vm_page_family->free_block_priority_list_head, 381 | &free_block->priority_thread_glue, 382 | free_blocks_comparison_function, 383 | offset_of(block_meta_data_t, priority_thread_glue)); 384 | } 385 | 386 | static vm_page_t * 387 | mm_family_new_page_add(vm_page_family_t *vm_page_family, int units){ 388 | 389 | vm_page_t *vm_page = allocate_vm_page(vm_page_family, units); 390 | 391 | if(!vm_page) 392 | return NULL; 393 | 394 | /* The new page is like one free block, add it to the 395 | * free block list*/ 396 | mm_add_free_block_meta_data_to_free_block_list( 397 | vm_page_family, &vm_page->block_meta_data); 398 | 399 | return vm_page; 400 | } 401 | 402 | /* Fn to mark block_meta_data as being Allocated for 403 | * 'size' bytes of application data. Return TRUE if 404 | * block allocation succeeds*/ 405 | 406 | static vm_bool_t 407 | mm_split_free_data_block_for_allocation( 408 | vm_page_family_t *vm_page_family, 409 | block_meta_data_t *block_meta_data, 410 | uint32_t size){ 411 | 412 | block_meta_data_t *next_block_meta_data = NULL; 413 | 414 | assert(block_meta_data->is_free == MM_TRUE); 415 | 416 | if(block_meta_data->block_size < size){ 417 | return MM_FALSE; 418 | } 419 | 420 | uint32_t remaining_size = 421 | block_meta_data->block_size - size; 422 | 423 | block_meta_data->is_free = MM_FALSE; 424 | block_meta_data->block_size = size; 425 | 426 | /*Unchanged*/ 427 | /*block_meta_data->offset = ??*/ 428 | 429 | /* Since this block of memory is going to be allocated to the application, 430 | * remove it from priority list of free blocks*/ 431 | remove_glthread(&block_meta_data->priority_thread_glue); 432 | 433 | vm_page_family->total_memory_in_use_by_app += 434 | sizeof(block_meta_data_t) + size; 435 | 436 | /*Case 1 : No Split*/ 437 | if(!remaining_size){ 438 | /*No need to repair linkages, they do not change*/ 439 | //mm_bind_blocks_for_allocation(block_meta_data, next_block_meta_data); 440 | return MM_TRUE; 441 | } 442 | 443 | /*Case 3 : Partial Split : Soft Internal Fragmentation*/ 444 | else if(sizeof(block_meta_data_t) < remaining_size && 445 | remaining_size < (sizeof(block_meta_data_t) + vm_page_family->struct_size)){ 446 | /*New Meta block is to be created*/ 447 | next_block_meta_data = NEXT_META_BLOCK_BY_SIZE(block_meta_data); 448 | next_block_meta_data->is_free = MM_TRUE; 449 | next_block_meta_data->block_size = 450 | remaining_size - sizeof(block_meta_data_t); 451 | next_block_meta_data->offset = block_meta_data->offset + 452 | sizeof(block_meta_data_t) + block_meta_data->block_size; 453 | init_glthread(&next_block_meta_data->priority_thread_glue); 454 | mm_add_free_block_meta_data_to_free_block_list( 455 | vm_page_family, next_block_meta_data); 456 | mm_bind_blocks_for_allocation(block_meta_data, next_block_meta_data); 457 | } 458 | 459 | /*Case 3 : Partial Split : Hard Internal Fragmentation*/ 460 | else if(remaining_size < sizeof(block_meta_data_t)){ 461 | //next_block_meta_data = block_meta_data->next_block; 462 | /*No need to repair linkages, they do not change*/ 463 | //mm_bind_blocks_for_allocation(block_meta_data, next_block_meta_data); 464 | } 465 | 466 | /*Case 2 : Full Split : New Meta block is Created*/ 467 | else { 468 | /*New Meta block is to be created*/ 469 | next_block_meta_data = NEXT_META_BLOCK_BY_SIZE(block_meta_data); 470 | next_block_meta_data->is_free = MM_TRUE; 471 | next_block_meta_data->block_size = 472 | remaining_size - sizeof(block_meta_data_t); 473 | next_block_meta_data->offset = block_meta_data->offset + 474 | sizeof(block_meta_data_t) + block_meta_data->block_size; 475 | init_glthread(&next_block_meta_data->priority_thread_glue); 476 | mm_add_free_block_meta_data_to_free_block_list( 477 | vm_page_family, next_block_meta_data); 478 | mm_bind_blocks_for_allocation(block_meta_data, next_block_meta_data); 479 | } 480 | 481 | return MM_TRUE; 482 | } 483 | 484 | static block_meta_data_t * 485 | mm_allocate_free_data_block( 486 | vm_page_family_t *vm_page_family, 487 | uint32_t req_size){ 488 | 489 | vm_bool_t status = MM_FALSE; 490 | vm_page_t *vm_page = NULL; 491 | block_meta_data_t *block_meta_data = NULL; 492 | 493 | int n_pages_required = ( req_size / MAX_PAGE_ALLOCATABLE_MEMORY(1)) + 1; 494 | 495 | block_meta_data_t *biggest_block_meta_data = 496 | mm_get_biggest_free_block_page_family(vm_page_family); 497 | 498 | if(!biggest_block_meta_data || 499 | biggest_block_meta_data->block_size < req_size){ 500 | 501 | /*Time to add a new page to Page family to satisfy the request*/ 502 | vm_page = mm_family_new_page_add(vm_page_family, n_pages_required); 503 | 504 | /*Allocate the free block from this page now*/ 505 | status = mm_split_free_data_block_for_allocation(vm_page_family, 506 | &vm_page->block_meta_data, req_size); 507 | 508 | if(status) 509 | return &vm_page->block_meta_data; 510 | 511 | return NULL; 512 | } 513 | /*Step 3*/ 514 | /*The biggest block meta data can satisfy the request*/ 515 | if(biggest_block_meta_data){ 516 | status = mm_split_free_data_block_for_allocation(vm_page_family, 517 | biggest_block_meta_data, req_size); 518 | } 519 | 520 | if(status) 521 | return biggest_block_meta_data; 522 | 523 | return NULL; 524 | } 525 | 526 | /* The public fn to be invoked by the application for Dynamic 527 | * Memory Allocations.*/ 528 | void * 529 | xcalloc(mm_instance_t *mm_inst, char *struct_name, int units){ 530 | 531 | /*Step 1*/ 532 | vm_page_family_t *pg_family = 533 | lookup_page_family_by_name(mm_inst, struct_name); 534 | 535 | if(!pg_family){ 536 | 537 | printf("Error : Structure %s not registered with Memory Manager\n", 538 | struct_name); 539 | assert(0); 540 | return NULL; 541 | } 542 | 543 | /*Find the page which can satisfy the request*/ 544 | block_meta_data_t *free_block_meta_data = NULL; 545 | 546 | free_block_meta_data = mm_allocate_free_data_block( 547 | pg_family, units * pg_family->struct_size); 548 | 549 | if(free_block_meta_data){ 550 | memset((char *)(free_block_meta_data + 1), 0, free_block_meta_data->block_size); 551 | return (void *)(free_block_meta_data + 1); 552 | } 553 | 554 | return NULL; 555 | } 556 | 557 | static int 558 | mm_get_hard_internal_memory_frag_size( 559 | block_meta_data_t *first, 560 | block_meta_data_t *second){ 561 | 562 | block_meta_data_t *next_block = NEXT_META_BLOCK_BY_SIZE(first); 563 | return (int)((unsigned long)second - (unsigned long)(next_block)); 564 | } 565 | 566 | static void 567 | mm_union_free_blocks(block_meta_data_t *first, 568 | block_meta_data_t *second){ 569 | 570 | assert(first->is_free == MM_TRUE && 571 | second->is_free == MM_TRUE); 572 | remove_glthread(&first->priority_thread_glue); 573 | remove_glthread(&second->priority_thread_glue); 574 | mm_bind_blocks_for_deallocation(first, second); 575 | } 576 | 577 | void 578 | mm_vm_page_delete_and_free( 579 | vm_page_t *vm_page){ 580 | 581 | vm_page_family_t *vm_page_family = 582 | vm_page->pg_family; 583 | 584 | assert(vm_page_family->first_page); 585 | 586 | if(vm_page_family->first_page == vm_page){ 587 | vm_page_family->first_page = vm_page->next; 588 | if(vm_page->next) 589 | vm_page->next->prev = NULL; 590 | vm_page_family->no_of_system_calls_to_alloc_dealloc_vm_pages++; 591 | vm_page->next = NULL; 592 | vm_page->prev = NULL; 593 | mm_return_vm_page_to_kernel((void *)vm_page, 594 | vm_page->page_size / SYSTEM_PAGE_SIZE); 595 | return; 596 | } 597 | 598 | if(vm_page->next) 599 | vm_page->next->prev = vm_page->prev; 600 | vm_page->prev->next = vm_page->next; 601 | vm_page_family->no_of_system_calls_to_alloc_dealloc_vm_pages++; 602 | mm_return_vm_page_to_kernel((void *)vm_page, vm_page->page_size / SYSTEM_PAGE_SIZE); 603 | } 604 | 605 | static block_meta_data_t * 606 | mm_free_blocks(block_meta_data_t *to_be_free_block){ 607 | 608 | block_meta_data_t *return_block = NULL; 609 | 610 | assert(to_be_free_block->is_free == MM_FALSE); 611 | 612 | vm_page_t *hosting_page = 613 | MM_GET_PAGE_FROM_META_BLOCK(to_be_free_block); 614 | 615 | vm_page_family_t *vm_page_family = hosting_page->pg_family; 616 | 617 | return_block = to_be_free_block; 618 | 619 | to_be_free_block->is_free = MM_TRUE; 620 | 621 | block_meta_data_t *next_block = NEXT_META_BLOCK(to_be_free_block); 622 | 623 | /*Handling Hard IF memory*/ 624 | if(next_block){ 625 | /*Scenario 1 : When data block to be freed is not the last 626 | * upper most meta block in a VM data page*/ 627 | to_be_free_block->block_size += 628 | mm_get_hard_internal_memory_frag_size (to_be_free_block, next_block); 629 | } 630 | else { 631 | /* Scenario 2: Page Boundry condition*/ 632 | /* Block being freed is the upper most free data block 633 | * in a VM data page, check of hard internal fragmented 634 | * memory and merge*/ 635 | char *end_address_of_vm_page = (char *)((char *)hosting_page + hosting_page->page_size); 636 | char *end_address_of_free_data_block = 637 | (char *)(to_be_free_block + 1) + to_be_free_block->block_size; 638 | int internal_mem_fragmentation = (int)((unsigned long)end_address_of_vm_page - 639 | (unsigned long)end_address_of_free_data_block); 640 | to_be_free_block->block_size += internal_mem_fragmentation; 641 | } 642 | 643 | /*Now perform Merging*/ 644 | if(next_block && next_block->is_free == MM_TRUE){ 645 | /*Union two free blocks*/ 646 | mm_union_free_blocks(to_be_free_block, next_block); 647 | return_block = to_be_free_block; 648 | } 649 | /*Check the previous block if it was free*/ 650 | block_meta_data_t *prev_block = PREV_META_BLOCK(to_be_free_block); 651 | 652 | if(prev_block && prev_block->is_free){ 653 | mm_union_free_blocks(prev_block, to_be_free_block); 654 | return_block = prev_block; 655 | } 656 | 657 | if(mm_is_vm_page_empty(hosting_page)){ 658 | mm_vm_page_delete_and_free(hosting_page); 659 | return NULL; 660 | } 661 | mm_add_free_block_meta_data_to_free_block_list( 662 | hosting_page->pg_family, return_block); 663 | 664 | return return_block; 665 | } 666 | 667 | void 668 | xfree(void *app_data){ 669 | 670 | block_meta_data_t *block_meta_data = 671 | (block_meta_data_t *)((char *)app_data - sizeof(block_meta_data_t)); 672 | 673 | assert(block_meta_data->is_free == MM_FALSE); 674 | mm_free_blocks(block_meta_data); 675 | } 676 | 677 | vm_bool_t 678 | mm_is_vm_page_empty(vm_page_t *vm_page){ 679 | 680 | if(vm_page->block_meta_data.next_block == NULL && 681 | vm_page->block_meta_data.prev_block == NULL && 682 | vm_page->block_meta_data.is_free == MM_TRUE){ 683 | 684 | return MM_TRUE; 685 | } 686 | return MM_FALSE; 687 | } 688 | 689 | void 690 | mm_print_vm_page_details(vm_page_t *vm_page, uint32_t i){ 691 | 692 | printf("\tPage Index : %u \n", vm_page->page_index); 693 | printf("\t\t next = %p, prev = %p\n", vm_page->next, vm_page->prev); 694 | printf("\t\t page family = %s, page_size = %uB\n", 695 | vm_page->pg_family->struct_name, vm_page->page_size); 696 | 697 | uint32_t j = 0; 698 | block_meta_data_t *curr; 699 | ITERATE_VM_PAGE_ALL_BLOCKS_BEGIN(vm_page, curr){ 700 | 701 | printf(ANSI_COLOR_YELLOW "\t\t\t%-14p Block %-3u %s block_size = %-6u " 702 | "offset = %-6u prev = %-14p next = %p\n" 703 | ANSI_COLOR_RESET, curr, 704 | j++, curr->is_free ? "F R E E D" : "ALLOCATED", 705 | curr->block_size, curr->offset, 706 | curr->prev_block, 707 | curr->next_block); 708 | } ITERATE_VM_PAGE_ALL_BLOCKS_END(vm_page, curr); 709 | } 710 | 711 | void 712 | mm_print_memory_usage(mm_instance_t *mm_inst, char *struct_name){ 713 | 714 | uint32_t i = 0; 715 | vm_page_t *vm_page = NULL; 716 | vm_page_family_t *vm_page_family_curr; 717 | uint32_t number_of_struct_families = 0; 718 | uint32_t total_memory_in_use_by_application = 0; 719 | uint32_t cumulative_vm_pages_claimed_from_kernel = 0; 720 | vm_page_for_families_t *vm_page_for_families_global; 721 | 722 | vm_page_for_families_global = mm_inst ? 723 | mm_inst->first_vm_page_for_families : 724 | first_vm_page_for_families; 725 | 726 | printf("\nPage Size = %zu Bytes\n", SYSTEM_PAGE_SIZE); 727 | 728 | ITERATE_PAGE_FAMILIES_BEGIN(vm_page_for_families_global, vm_page_family_curr){ 729 | 730 | if(struct_name){ 731 | if(strncmp(struct_name, vm_page_family_curr->struct_name, 732 | strlen(vm_page_family_curr->struct_name))){ 733 | continue; 734 | } 735 | } 736 | 737 | number_of_struct_families++; 738 | 739 | printf(ANSI_COLOR_GREEN "vm_page_family : %s, struct size = %u\n" 740 | ANSI_COLOR_RESET, 741 | vm_page_family_curr->struct_name, 742 | vm_page_family_curr->struct_size); 743 | printf(ANSI_COLOR_CYAN "\tApp Used Memory %uB, #Sys Calls %u\n" 744 | ANSI_COLOR_RESET, 745 | vm_page_family_curr->total_memory_in_use_by_app, 746 | vm_page_family_curr->\ 747 | no_of_system_calls_to_alloc_dealloc_vm_pages); 748 | 749 | total_memory_in_use_by_application += 750 | vm_page_family_curr->total_memory_in_use_by_app; 751 | 752 | i = 0; 753 | 754 | ITERATE_VM_PAGE_BEGIN(vm_page_family_curr, vm_page){ 755 | 756 | cumulative_vm_pages_claimed_from_kernel++; 757 | mm_print_vm_page_details(vm_page, i++); 758 | 759 | } ITERATE_VM_PAGE_END(vm_page_family_curr, vm_page); 760 | printf("\n"); 761 | } ITERATE_PAGE_FAMILIES_END(vm_page_for_families_global, vm_page_family_curr); 762 | 763 | printf(ANSI_COLOR_MAGENTA "\nTotal Applcation Memory Usage : %u Bytes\n" 764 | ANSI_COLOR_RESET, total_memory_in_use_by_application); 765 | 766 | printf(ANSI_COLOR_MAGENTA "# Of VM Pages in Use : %u (%lu Bytes)\n" \ 767 | ANSI_COLOR_RESET, 768 | cumulative_vm_pages_claimed_from_kernel, 769 | SYSTEM_PAGE_SIZE * cumulative_vm_pages_claimed_from_kernel); 770 | 771 | float memory_app_use_to_total_memory_ratio = 0.0; 772 | 773 | if(cumulative_vm_pages_claimed_from_kernel){ 774 | memory_app_use_to_total_memory_ratio = 775 | (float)(total_memory_in_use_by_application * 100)/\ 776 | (float)(cumulative_vm_pages_claimed_from_kernel * SYSTEM_PAGE_SIZE); 777 | } 778 | printf(ANSI_COLOR_MAGENTA "Memory In Use by Application = %f%%\n" 779 | ANSI_COLOR_RESET, 780 | memory_app_use_to_total_memory_ratio); 781 | 782 | printf("Total Memory being used by Memory Manager = %lu Bytes\n", 783 | cumulative_vm_pages_claimed_from_kernel * SYSTEM_PAGE_SIZE); 784 | } 785 | 786 | void 787 | mm_print_block_usage(mm_instance_t *mm_inst){ 788 | 789 | vm_page_t *vm_page_curr; 790 | vm_page_family_t *vm_page_family_curr; 791 | block_meta_data_t *block_meta_data_curr; 792 | uint32_t total_block_count, free_block_count, 793 | occupied_block_count; 794 | uint32_t application_memory_usage; 795 | 796 | vm_page_for_families_t *first_vm_page_for_families_global; 797 | 798 | first_vm_page_for_families_global = mm_inst ? 799 | mm_inst->first_vm_page_for_families : first_vm_page_for_families; 800 | 801 | if (!first_vm_page_for_families_global) return; 802 | 803 | ITERATE_PAGE_FAMILIES_BEGIN(first_vm_page_for_families_global, vm_page_family_curr){ 804 | 805 | total_block_count = 0; 806 | free_block_count = 0; 807 | application_memory_usage = 0; 808 | occupied_block_count = 0; 809 | ITERATE_VM_PAGE_BEGIN(vm_page_family_curr, vm_page_curr){ 810 | 811 | ITERATE_VM_PAGE_ALL_BLOCKS_BEGIN(vm_page_curr, block_meta_data_curr){ 812 | 813 | total_block_count++; 814 | 815 | /*Sanity Checks*/ 816 | if(block_meta_data_curr->is_free == MM_FALSE){ 817 | assert(IS_GLTHREAD_LIST_EMPTY(&block_meta_data_curr->\ 818 | priority_thread_glue)); 819 | } 820 | if(block_meta_data_curr->is_free == MM_TRUE){ 821 | assert(!IS_GLTHREAD_LIST_EMPTY(&block_meta_data_curr->\ 822 | priority_thread_glue)); 823 | } 824 | 825 | if(block_meta_data_curr->is_free == MM_TRUE){ 826 | free_block_count++; 827 | } 828 | else{ 829 | application_memory_usage += 830 | block_meta_data_curr->block_size + \ 831 | sizeof(block_meta_data_t); 832 | occupied_block_count++; 833 | } 834 | } ITERATE_VM_PAGE_ALL_BLOCKS_END(vm_page_curr, block_meta_data_curr); 835 | } ITERATE_VM_PAGE_END(vm_page_family_curr, vm_page_curr); 836 | 837 | printf("%-20s TBC : %-4u FBC : %-4u OBC : %-4u AppMemUsage : %u\n", 838 | vm_page_family_curr->struct_name, total_block_count, 839 | free_block_count, occupied_block_count, application_memory_usage); 840 | 841 | } ITERATE_PAGE_FAMILIES_END(first_vm_page_for_families_global, vm_page_family_curr); 842 | } 843 | 844 | void 845 | mm_print_variable_buffers(mm_instance_t *mm_inst) { 846 | 847 | uint32_t total_block_count = 0; 848 | uint32_t free_block_count = 0; 849 | uint32_t application_memory_usage = 0; 850 | uint32_t occupied_block_count = 0; 851 | 852 | vm_page_t *vm_page_curr; 853 | vm_page_family_t *vm_page_family_curr; 854 | block_meta_data_t *block_meta_data_curr; 855 | vm_page_family_t *misc_vm_page_family_global; 856 | 857 | misc_vm_page_family_global = mm_inst ? 858 | &mm_inst->misc_vm_page_family : &misc_vm_page_family; 859 | 860 | ITERATE_VM_PAGE_BEGIN(misc_vm_page_family_global, vm_page_curr){ 861 | 862 | ITERATE_VM_PAGE_ALL_BLOCKS_BEGIN(vm_page_curr, block_meta_data_curr){ 863 | 864 | total_block_count++; 865 | 866 | /*Sanity Checks*/ 867 | if(block_meta_data_curr->is_free == MM_FALSE){ 868 | assert(IS_GLTHREAD_LIST_EMPTY(&block_meta_data_curr->\ 869 | priority_thread_glue)); 870 | } 871 | if(block_meta_data_curr->is_free == MM_TRUE){ 872 | assert(!IS_GLTHREAD_LIST_EMPTY(&block_meta_data_curr->\ 873 | priority_thread_glue)); 874 | } 875 | 876 | if(block_meta_data_curr->is_free == MM_TRUE){ 877 | free_block_count++; 878 | } 879 | else{ 880 | application_memory_usage += 881 | block_meta_data_curr->block_size + \ 882 | sizeof(block_meta_data_t); 883 | occupied_block_count++; 884 | } 885 | } ITERATE_VM_PAGE_ALL_BLOCKS_END(vm_page_curr, block_meta_data_curr); 886 | } ITERATE_VM_PAGE_END(misc_vm_page_family_global, vm_page_curr); 887 | 888 | printf("%-20s TBC : %-4u FBC : %-4u OBC : %-4u AppMemUsage : %u\n", 889 | misc_vm_page_family.struct_name, total_block_count, 890 | free_block_count, occupied_block_count, application_memory_usage); 891 | 892 | } 893 | 894 | void 895 | mm_print_registered_page_families(mm_instance_t *mm_inst){ 896 | 897 | vm_page_family_t *vm_page_family_curr = NULL; 898 | vm_page_for_families_t *vm_page_for_families_curr = NULL; 899 | vm_page_for_families_t *vm_page_for_families_global; 900 | 901 | vm_page_for_families_global = mm_inst ? 902 | mm_inst->first_vm_page_for_families : first_vm_page_for_families; 903 | 904 | for(vm_page_for_families_curr = vm_page_for_families_global; 905 | vm_page_for_families_curr; 906 | vm_page_for_families_curr = vm_page_for_families_curr->next){ 907 | 908 | ITERATE_PAGE_FAMILIES_BEGIN(vm_page_for_families_curr, 909 | vm_page_family_curr){ 910 | 911 | 912 | printf("Page Family : %s, Size = %u\n", 913 | vm_page_family_curr->struct_name, 914 | vm_page_family_curr->struct_size); 915 | 916 | } ITERATE_PAGE_FAMILIES_END(vm_page_for_families_curr, 917 | vm_page_family_curr); 918 | } 919 | } 920 | 921 | void * 922 | xcalloc_buff(mm_instance_t *mm_inst, uint32_t bytes) { 923 | 924 | vm_page_family_t *misc_vm_page_family_global; 925 | 926 | misc_vm_page_family_global = mm_inst ? 927 | &mm_inst->misc_vm_page_family : &misc_vm_page_family; 928 | 929 | block_meta_data_t *free_block_meta_data = NULL; 930 | 931 | free_block_meta_data = mm_allocate_free_data_block( 932 | misc_vm_page_family_global, bytes); 933 | 934 | misc_vm_page_family_global->struct_size = bytes; 935 | 936 | if(free_block_meta_data){ 937 | memset((char *)(free_block_meta_data + 1), 0, free_block_meta_data->block_size); 938 | return (void *)(free_block_meta_data + 1); 939 | } 940 | 941 | return NULL; 942 | } 943 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------