From b8d8146555a7a3e1c684f5828fb086b417edd507 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sat, 24 Oct 2026 06:33:48 +0000 Subject: use c++ --- malloclab-handout/mm.c | 110 ------------------------------------------------ malloclab-handout/mm.cc | 110 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 110 deletions(-) delete mode 100644 malloclab-handout/mm.c create mode 100644 malloclab-handout/mm.cc (limited to 'malloclab-handout') diff --git a/malloclab-handout/mm.c b/malloclab-handout/mm.c deleted file mode 100644 index a482a43..0000000 --- a/malloclab-handout/mm.c +++ /dev/null @@ -1,110 +0,0 @@ -/* - * mm-naive.c - The fastest, least memory-efficient malloc package. - * - * In this naive approach, a block is allocated by simply incrementing - * the brk pointer. A block is pure payload. There are no headers or - * footers. Blocks are never coalesced or reused. Realloc is - * implemented directly using mm_malloc and mm_free. - * - * NOTE TO STUDENTS: Replace this header comment with your own header - * comment that gives a high level description of your solution. - */ -#include -#include -#include -#include -#include - -#include "mm.h" -#include "memlib.h" - -/********************************************************* - * NOTE TO STUDENTS: Before you do anything else, please - * provide your team information in the following struct. - ********************************************************/ -team_t team = { - /* Team name */ - "ateam", - /* First member's full name */ - "Harry Bovik", - /* First member's email address */ - "bovik@cs.cmu.edu", - /* Second member's full name (leave blank if none) */ - "", - /* Second member's email address (leave blank if none) */ - "" -}; - -/* single word (4) or double word (8) alignment */ -#define ALIGNMENT 8 - -/* rounds up to the nearest multiple of ALIGNMENT */ -#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7) - - -#define SIZE_T_SIZE (ALIGN(sizeof(size_t))) - -/* - * mm_init - initialize the malloc package. - */ -int mm_init(void) -{ - return 0; -} - -/* - * mm_malloc - Allocate a block by incrementing the brk pointer. - * Always allocate a block whose size is a multiple of the alignment. - */ -void *mm_malloc(size_t size) -{ - int newsize = ALIGN(size + SIZE_T_SIZE); - void *p = mem_sbrk(newsize); - if (p == (void *)-1) - return NULL; - else { - *(size_t *)p = size; - return (void *)((char *)p + SIZE_T_SIZE); - } -} - -/* - * mm_free - Freeing a block does nothing. - */ -void mm_free(void *ptr) -{ -} - -/* - * mm_realloc - Implemented simply in terms of mm_malloc and mm_free - */ -void *mm_realloc(void *ptr, size_t size) -{ - void *oldptr = ptr; - void *newptr; - size_t copySize; - - newptr = mm_malloc(size); - if (newptr == NULL) - return NULL; - copySize = *(size_t *)((char *)oldptr - SIZE_T_SIZE); - if (size < copySize) - copySize = size; - memcpy(newptr, oldptr, copySize); - mm_free(oldptr); - return newptr; -} - - - - - - - - - - - - - - diff --git a/malloclab-handout/mm.cc b/malloclab-handout/mm.cc new file mode 100644 index 0000000..a482a43 --- /dev/null +++ b/malloclab-handout/mm.cc @@ -0,0 +1,110 @@ +/* + * mm-naive.c - The fastest, least memory-efficient malloc package. + * + * In this naive approach, a block is allocated by simply incrementing + * the brk pointer. A block is pure payload. There are no headers or + * footers. Blocks are never coalesced or reused. Realloc is + * implemented directly using mm_malloc and mm_free. + * + * NOTE TO STUDENTS: Replace this header comment with your own header + * comment that gives a high level description of your solution. + */ +#include +#include +#include +#include +#include + +#include "mm.h" +#include "memlib.h" + +/********************************************************* + * NOTE TO STUDENTS: Before you do anything else, please + * provide your team information in the following struct. + ********************************************************/ +team_t team = { + /* Team name */ + "ateam", + /* First member's full name */ + "Harry Bovik", + /* First member's email address */ + "bovik@cs.cmu.edu", + /* Second member's full name (leave blank if none) */ + "", + /* Second member's email address (leave blank if none) */ + "" +}; + +/* single word (4) or double word (8) alignment */ +#define ALIGNMENT 8 + +/* rounds up to the nearest multiple of ALIGNMENT */ +#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7) + + +#define SIZE_T_SIZE (ALIGN(sizeof(size_t))) + +/* + * mm_init - initialize the malloc package. + */ +int mm_init(void) +{ + return 0; +} + +/* + * mm_malloc - Allocate a block by incrementing the brk pointer. + * Always allocate a block whose size is a multiple of the alignment. + */ +void *mm_malloc(size_t size) +{ + int newsize = ALIGN(size + SIZE_T_SIZE); + void *p = mem_sbrk(newsize); + if (p == (void *)-1) + return NULL; + else { + *(size_t *)p = size; + return (void *)((char *)p + SIZE_T_SIZE); + } +} + +/* + * mm_free - Freeing a block does nothing. + */ +void mm_free(void *ptr) +{ +} + +/* + * mm_realloc - Implemented simply in terms of mm_malloc and mm_free + */ +void *mm_realloc(void *ptr, size_t size) +{ + void *oldptr = ptr; + void *newptr; + size_t copySize; + + newptr = mm_malloc(size); + if (newptr == NULL) + return NULL; + copySize = *(size_t *)((char *)oldptr - SIZE_T_SIZE); + if (size < copySize) + copySize = size; + memcpy(newptr, oldptr, copySize); + mm_free(oldptr); + return newptr; +} + + + + + + + + + + + + + + -- cgit v1.3.1-4-g156e