├── libmy_malloc.so ├── libmy_malloc_x86_64.so ├── .gitattributes ├── main.c ├── src ├── error.c ├── calloc.c ├── show_alloc_mem.c ├── realloc.c ├── malloc.c ├── free.c └── algorithm.c ├── .gitignore ├── Makefile └── inc └── malloc.h /libmy_malloc.so: -------------------------------------------------------------------------------- 1 | IntxLNKlibmy_malloc_x86_64.so -------------------------------------------------------------------------------- /libmy_malloc_x86_64.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kefranabg/MyMalloc/master/libmy_malloc_x86_64.so -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** main.c for main.c in /home/abgral_f/rendu/PSU_2013_malloc 3 | ** 4 | ** Made by abgral_f 5 | ** Login 6 | ** 7 | ** Started on Thu Feb 13 19:16:48 2014 abgral_f 8 | ** Last update Thu Feb 13 19:20:53 2014 abgral_f 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | int main() 15 | { 16 | char *lol; 17 | //lol = NULL; 18 | 19 | //lol = malloc( 8); 20 | //free(); 21 | free(lol); 22 | } 23 | -------------------------------------------------------------------------------- /src/error.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** error.c for error.c in /home/loeb_t/rendu/PSU_2013_malloc 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Wed Feb 12 22:37:53 2014 LOEB Thomas 8 | ** Last update Wed Feb 12 22:39:31 2014 LOEB Thomas 9 | */ 10 | 11 | #include "malloc.h" 12 | 13 | void *error_return_ptr(char *message) 14 | { 15 | printf("%s\n", message); 16 | return ((void*)-1); 17 | } 18 | 19 | void error_return_void(char *message) 20 | { 21 | printf("%s\n", message); 22 | return ; 23 | } 24 | 25 | void error_exit(char *message) 26 | { 27 | printf("%s\n", message); 28 | exit(0); 29 | } 30 | -------------------------------------------------------------------------------- /src/calloc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** calloc.c for calloc.c in /home/loeb_t/rendu/PSU_2013_malloc/src 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Thu Feb 13 17:18:25 2014 LOEB Thomas 8 | ** Last update Sun Feb 16 17:09:56 2014 abgral_f 9 | */ 10 | 11 | #include "malloc.h" 12 | 13 | void *calloc(size_t nmemb, size_t size) 14 | { 15 | void *ret; 16 | size_t total; 17 | 18 | if (nmemb == 0 || size == 0) 19 | return (NULL); 20 | total = nmemb * size; 21 | while (total % sizeof(int) != 0) 22 | total++; 23 | ret = malloc(total); 24 | if (ret != NULL) 25 | memset(ret, 0, total); 26 | return (ret); 27 | } 28 | -------------------------------------------------------------------------------- /src/show_alloc_mem.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** show_alloc_mem.c for show_alloc_mem.c in /home/loeb_t/rendu/PSU_2013_malloc 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Mon Feb 10 20:03:55 2014 LOEB Thomas 8 | ** Last update Thu Feb 13 17:18:16 2014 LOEB Thomas 9 | */ 10 | 11 | #include "malloc.h" 12 | 13 | void show_alloc_mem() 14 | { 15 | t_malloc *end; 16 | 17 | if (first == NULL) 18 | printf("break : %p\n", sbrk(0)); 19 | else 20 | printf("break : %p\n", (void*)break_point); 21 | end = first; 22 | while (end != NULL) 23 | { 24 | if (end->isFree == 0) 25 | printf("%p - %p : %u octets\n", 26 | (void*)end + sizeof(t_malloc), 27 | (void*)end + sizeof(t_malloc) + end->size, end->size); 28 | end = end->next; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ## 2 | ## Makefile for Makefile in /home/loeb_t/rendu/PSU_2013_malloc 3 | ## 4 | ## Made by LOEB Thomas 5 | ## Login 6 | ## 7 | ## Started on Tue Feb 4 15:32:18 2014 LOEB Thomas 8 | ## Last update Thu Feb 13 18:10:03 2014 LOEB Thomas 9 | ## 10 | 11 | NAME = libmy_malloc_$(HOSTTYPE).so 12 | 13 | LINK = libmy_malloc.so 14 | 15 | INC_F = inc/ 16 | 17 | SRC_F = src/ 18 | 19 | SRC = $(SRC_F)malloc.c \ 20 | $(SRC_F)free.c \ 21 | $(SRC_F)realloc.c \ 22 | $(SRC_F)show_alloc_mem.c \ 23 | $(SRC_F)algorithm.c \ 24 | $(SRC_F)calloc.c \ 25 | $(SRC_F)error.c 26 | 27 | OBJ = $(SRC:.c=.o) 28 | 29 | CFLAGS = -W -Wall -Werror -fPIC -I $(INC_F) -lpthread 30 | 31 | all: $(NAME) 32 | 33 | $(NAME): $(OBJ) 34 | gcc -shared -o $(NAME) $(OBJ) 35 | ln -s $(NAME) $(LINK) 36 | 37 | clean: 38 | rm -f $(OBJ) 39 | 40 | fclean: clean 41 | rm -f $(NAME) 42 | rm -f $(LINK) 43 | 44 | re: fclean all 45 | -------------------------------------------------------------------------------- /src/realloc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** realloc.c for realloc.c in /home/loeb_t/rendu/PSU_2013_malloc 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Mon Feb 10 20:06:32 2014 LOEB Thomas 8 | ** Last update Thu Feb 13 17:18:03 2014 LOEB Thomas 9 | */ 10 | 11 | #include "malloc.h" 12 | 13 | void *realloc(void *ptr, size_t size) 14 | { 15 | t_malloc *search; 16 | void *ret; 17 | 18 | if (ptr == NULL || first == NULL) 19 | return (malloc(size)); 20 | search = first; 21 | while (search != NULL && (void*)search + sizeof(t_malloc) != ptr) 22 | search = search->next; 23 | if (search == NULL) 24 | return (malloc(size)); 25 | search->isFree = 1; 26 | free_regroup(search, 0); 27 | if (ptr != (void*)search + sizeof(t_malloc)) 28 | memcpy((void*)search + sizeof(t_malloc), ptr, 29 | (size_t)(ptr - sizeof(t_malloc) + sizeof(unsigned int))); 30 | ret = malloc(size); 31 | if (ret != NULL && ret != (void*)search + sizeof(t_malloc)) 32 | { 33 | memcpy(ret, (void*)search + sizeof(t_malloc), size); 34 | free((void*)search + sizeof(t_malloc)); 35 | } 36 | return (ret); 37 | } 38 | -------------------------------------------------------------------------------- /inc/malloc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** malloc.h for malloc.h in /home/loeb_t/rendu/PSU_2013_malloc 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Wed Feb 5 16:48:59 2014 LOEB Thomas 8 | ** Last update Sun Feb 16 15:49:45 2014 abgral_f 9 | */ 10 | 11 | #ifndef MALLOC_H_ 12 | # define MALLOC_H_ 13 | 14 | # include 15 | # include 16 | # include 17 | # include 18 | # include 19 | 20 | # define PS getpagesize() 21 | # define ONE_MORE(x) (((x) != 0) ? 1 : 0) 22 | # define ALLOC(x, y) (((x) / (y)) * (y)) + (ONE_MORE((x) % (y)) * (y)) 23 | 24 | pthread_mutex_t my_mutex; 25 | 26 | typedef struct s_malloc { 27 | unsigned int isFree; 28 | unsigned int size; 29 | struct s_malloc *next; 30 | struct s_malloc *prev; 31 | } t_malloc; 32 | 33 | t_malloc *first; 34 | size_t break_point; 35 | 36 | void *malloc(size_t size); 37 | void free(void *ptr); 38 | void *realloc(void *ptr, size_t size); 39 | void show_alloc_mem(); 40 | void free_regroup(t_malloc *search, char erase); 41 | t_malloc *algorithm_malloc(size_t size, t_malloc *ptr); 42 | 43 | void *calloc(size_t nmemb, size_t size); 44 | 45 | void *error_return_ptr(char *message); 46 | void error_return_void(char *message); 47 | void error_exit(char *message); 48 | 49 | # define ERROR_SBRK "Error: system call 'sbrk' failed" 50 | # define ERROR_BRK "Error: system call 'brk' failed" 51 | # define ERROR_FREE_PTR "Error: free: invalid pointer" 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /src/malloc.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** malloc.c for malloc.c in /home/loeb_t/rendu/PSU_2013_malloc 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Tue Feb 4 15:23:46 2014 LOEB Thomas 8 | ** Last update Sun Feb 16 15:47:47 2014 abgral_f 9 | */ 10 | 11 | #include "malloc.h" 12 | 13 | static void *malloc_add_end(size_t size, t_malloc *ptr) 14 | { 15 | if (size > break_point - (size_t)ptr) 16 | { 17 | if (brk((void*)break_point) == -1) 18 | return (error_return_ptr(ERROR_BRK)); 19 | if (sbrk(ALLOC(size - (break_point - (size_t)ptr), PS)) == (void*)-1) 20 | return (error_return_ptr(ERROR_SBRK)); 21 | break_point += ALLOC(size - (break_point - (size_t)ptr), PS); 22 | } 23 | return (NULL); 24 | } 25 | 26 | static void *malloc_add_new(size_t size, t_malloc *ptr) 27 | { 28 | t_malloc new; 29 | 30 | new.isFree = 0; 31 | new.size = size; 32 | new.next = NULL; 33 | size += sizeof(t_malloc); 34 | if (ptr == NULL) 35 | { 36 | if ((first = sbrk(ALLOC(size, PS))) == (void*)-1) 37 | return (error_return_ptr(ERROR_SBRK)); 38 | break_point = (size_t)first + ALLOC(size, PS); 39 | new.prev = NULL; 40 | ptr = first; 41 | } 42 | else 43 | { 44 | while (ptr->next != NULL) 45 | ptr = ptr->next; 46 | new.prev = ptr; 47 | ptr->next = (void*)ptr + sizeof(t_malloc) + ptr->size; 48 | ptr = ptr->next; 49 | if (malloc_add_end(size, ptr) == (void*)-1) 50 | return ((void*)-1); 51 | } 52 | return (memcpy(ptr, &new, sizeof(t_malloc))); 53 | } 54 | 55 | void *malloc(size_t size) 56 | { 57 | t_malloc *end; 58 | 59 | if (size <= 0) 60 | return (NULL); 61 | pthread_mutex_init (&my_mutex, NULL); 62 | pthread_mutex_lock (&my_mutex); 63 | while (size % sizeof(int) != 0) 64 | size++; 65 | if ((end = algorithm_malloc(size, first)) == NULL) 66 | end = malloc_add_new(size, first); 67 | pthread_mutex_unlock (&my_mutex); 68 | pthread_mutex_destroy(&my_mutex); 69 | if (end == (void*)-1) 70 | return (NULL); 71 | return ((void*)end + sizeof(t_malloc)); 72 | } 73 | -------------------------------------------------------------------------------- /src/free.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** free.c for free.c in /home/loeb_t/rendu/PSU_2013_malloc 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Mon Feb 10 20:00:01 2014 LOEB Thomas 8 | ** Last update Sun Feb 16 15:48:03 2014 abgral_f 9 | */ 10 | 11 | #include "malloc.h" 12 | 13 | static void free_erase(t_malloc *search) 14 | { 15 | size_t total; 16 | 17 | memset((void*)search + sizeof(t_malloc), 0, search->size); 18 | search->isFree = 1; 19 | if (search->next == NULL) 20 | { 21 | if (search->prev == NULL) 22 | { 23 | total = (break_point - (size_t)first) / PS; 24 | first = NULL; 25 | } 26 | else 27 | { 28 | total = (break_point - (size_t)search) / PS; 29 | search->prev->next = NULL; 30 | } 31 | if (total > 0) 32 | { 33 | if (brk((void*)break_point) == -1) 34 | return (error_return_void(ERROR_BRK)); 35 | if (sbrk(- (total * PS)) == (void*)-1) 36 | return (error_return_void(ERROR_SBRK)); 37 | break_point -= (total * PS); 38 | } 39 | } 40 | } 41 | 42 | void free_regroup(t_malloc *search, char erase) 43 | { 44 | if (search->prev != NULL && search->prev->isFree == 1) 45 | { 46 | search->prev->size += (sizeof(t_malloc) + search->size); 47 | search->prev->next = search->next; 48 | if (search->next != NULL) 49 | search->next->prev = search->prev; 50 | search = search->prev; 51 | } 52 | if (search->next != NULL && search->next->isFree == 1) 53 | { 54 | search->size += (sizeof(t_malloc) + search->next->size); 55 | search->next = search->next->next; 56 | if (search->next != NULL) 57 | search->next->prev = search; 58 | } 59 | if (erase == 1) 60 | free_erase(search); 61 | } 62 | 63 | void free(void *ptr) 64 | { 65 | t_malloc *search; 66 | 67 | if (ptr == NULL || first == NULL) 68 | return ; 69 | pthread_mutex_init (&my_mutex, NULL); 70 | pthread_mutex_lock (&my_mutex); 71 | search = first; 72 | while (search != NULL && (void*)search + sizeof(t_malloc) != ptr) 73 | search = search->next; 74 | if (search != NULL) 75 | free_regroup(search, 1); 76 | pthread_mutex_unlock (&my_mutex); 77 | pthread_mutex_destroy(&my_mutex); 78 | } 79 | -------------------------------------------------------------------------------- /src/algorithm.c: -------------------------------------------------------------------------------- 1 | /* 2 | ** algorithm.c for algorithm.c in /home/loeb_t/rendu/PSU_2013_malloc 3 | ** 4 | ** Made by LOEB Thomas 5 | ** Login 6 | ** 7 | ** Started on Mon Feb 10 19:34:11 2014 LOEB Thomas 8 | ** Last update Thu Feb 13 18:06:55 2014 LOEB Thomas 9 | */ 10 | 11 | #include "malloc.h" 12 | 13 | static t_malloc *algorithm_split(size_t size, t_malloc *ptr) 14 | { 15 | t_malloc new; 16 | 17 | if (ptr != NULL) 18 | { 19 | ptr->isFree = 0; 20 | if ((int)(ptr->size - (size + sizeof(t_malloc))) > 0) 21 | { 22 | new.isFree = 1; 23 | new.size = (int)(ptr->size - (size + sizeof(t_malloc))); 24 | new.next = ptr->next; 25 | new.prev = ptr; 26 | ptr->size = size; 27 | ptr->next = (void*)ptr + sizeof(t_malloc) + ptr->size; 28 | memcpy(ptr->next, &new, sizeof(t_malloc)); 29 | if (ptr->next->next != NULL) 30 | ptr->next->next->prev = ptr->next; 31 | } 32 | } 33 | return (ptr); 34 | } 35 | 36 | static void *algorithm_realloc(size_t size, t_malloc *ptr) 37 | { 38 | size_t alloc; 39 | 40 | alloc = (size_t)((void*)ptr + sizeof(t_malloc) + ptr->size); 41 | if (size - ptr->size > break_point - alloc) 42 | { 43 | if (brk((void*)break_point) == -1) 44 | return (error_return_ptr(ERROR_BRK)); 45 | if (sbrk(ALLOC((size - ptr->size) - 46 | (break_point - alloc), PS)) == (void*)-1) 47 | return (error_return_ptr(ERROR_SBRK)); 48 | break_point += ALLOC((size - ptr->size) - (break_point - alloc), PS); 49 | } 50 | ptr->size = size; 51 | return (NULL); 52 | } 53 | 54 | t_malloc *algorithm_malloc(size_t size, t_malloc *ptr) 55 | { 56 | t_malloc *ret; 57 | 58 | if (ptr == NULL) 59 | return (NULL); 60 | ret = NULL; 61 | while (ptr != NULL) 62 | { 63 | if (ptr->isFree == 1) 64 | { 65 | if (ptr->next == NULL && ptr->size < size) 66 | if (algorithm_realloc(size, ptr) == (void*)-1) 67 | return ((void*)-1); 68 | if (ptr->size == size || 69 | (ptr->size > size && 70 | (ret == NULL || ptr->size >= ret->size))) 71 | ret = ptr; 72 | } 73 | ptr = ptr->next; 74 | } 75 | return (algorithm_split(size, ret)); 76 | } 77 | --------------------------------------------------------------------------------