diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-24 06:34:07 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-24 06:34:07 +0000 |
| commit | 72632b769bfc7dd6cd57e972f648d9d4a1125d84 (patch) | |
| tree | 276a3d717f6090ddf28eb8625869a702c811be13 /malloclab-handout | |
| parent | b8d8146555a7a3e1c684f5828fb086b417edd507 (diff) | |
use c++
Diffstat (limited to 'malloclab-handout')
| -rw-r--r-- | malloclab-handout/Makefile | 26 | ||||
| -rw-r--r-- | malloclab-handout/memlib.h | 8 | ||||
| -rw-r--r-- | malloclab-handout/mm-naive-free.cc | 110 | ||||
| -rw-r--r-- | malloclab-handout/mm.h | 8 |
4 files changed, 146 insertions, 6 deletions
diff --git a/malloclab-handout/Makefile b/malloclab-handout/Makefile index 3d5dc9e..36c382d 100644 --- a/malloclab-handout/Makefile +++ b/malloclab-handout/Makefile @@ -6,16 +6,30 @@ VERSION = 1 HANDINDIR = /afs/cs.cmu.edu/academic/class/15213-f01/malloclab/handin CC = gcc -CFLAGS = -Wall -O2 -m32 +CXX = g++ +CFLAGS = -Wall -Og -m32 +CXXFLAGS = -Wall -Og -m32 -OBJS = mdriver.o mm.o memlib.o fsecs.o fcyc.o clock.o ftimer.o +OBJS = mdriver.o memlib.o fsecs.o fcyc.o clock.o ftimer.o +MM_TARGETS = $(basename $(wildcard mm*.cc)) -mdriver: $(OBJS) - $(CC) $(CFLAGS) -o mdriver $(OBJS) +MAKEFLAGS += --no-builtin-rules +.SUFFIXES: + +.PHONY: $(MM_TARGETS) +$(MM_TARGETS): %: %.out + +%.out: %.o $(OBJS) + $(CXX) $(CXXFLAGS) -o $@ $^ + +%.o: %.cc mm.h memlib.h + $(CXX) $(CXXFLAGS) -c -o $@ $< + +%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< mdriver.o: mdriver.c fsecs.h fcyc.h clock.h memlib.h config.h mm.h memlib.o: memlib.c memlib.h -mm.o: mm.c mm.h memlib.h fsecs.o: fsecs.c fsecs.h config.h fcyc.o: fcyc.c fcyc.h ftimer.o: ftimer.c ftimer.h config.h @@ -25,6 +39,6 @@ handin: cp mm.c $(HANDINDIR)/$(TEAM)-$(VERSION)-mm.c clean: - rm -f *~ *.o mdriver + rm -f *~ *.o *.out diff --git a/malloclab-handout/memlib.h b/malloclab-handout/memlib.h index 247c732..3d5e6ee 100644 --- a/malloclab-handout/memlib.h +++ b/malloclab-handout/memlib.h @@ -1,5 +1,9 @@ #include <unistd.h> +#ifdef __cplusplus +extern "C" { +#endif + void mem_init(void); void mem_deinit(void); void *mem_sbrk(int incr); @@ -9,3 +13,7 @@ void *mem_heap_hi(void); size_t mem_heapsize(void); size_t mem_pagesize(void); +#ifdef __cplusplus +} +#endif + diff --git a/malloclab-handout/mm-naive-free.cc b/malloclab-handout/mm-naive-free.cc new file mode 100644 index 0000000..a482a43 --- /dev/null +++ b/malloclab-handout/mm-naive-free.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 <stdio.h> +#include <stdlib.h> +#include <assert.h> +#include <unistd.h> +#include <string.h> + +#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.h b/malloclab-handout/mm.h index d41eef7..7cd95f7 100644 --- a/malloclab-handout/mm.h +++ b/malloclab-handout/mm.h @@ -1,5 +1,9 @@ #include <stdio.h> +#ifdef __cplusplus +extern "C" { +#endif + extern int mm_init (void); extern void *mm_malloc (size_t size); extern void mm_free (void *ptr); @@ -21,3 +25,7 @@ typedef struct { extern team_t team; +#ifdef __cplusplus +} +#endif + |
