9.20

using malloc lib code from csapp site

  • implicit idle list
  • allocated block with header & footer
  • idle block with header & footer
  • no GC
  • first fit strategy

another modification to malloc lib file

mm.h

--- ../vm/mm.h	2021-02-25 07:26:33.315926002 +0000
+++ mm.h	2021-02-25 07:26:33.315926002 +0000
@@ -5,6 +5,9 @@
 extern void mm_free (void *ptr);
 /* $end mallocinterface */
 
+#define malloc(size) mm_malloc(size)
+#define free(ptr) mm_free(ptr)
+
 extern void *mm_realloc(void *ptr, size_t size);
 extern void *mm_calloc (size_t nmemb, size_t size);
 extern void mm_checkheap(int verbose);

memlib.c

--- ../vm/memlib.c	2021-02-25 07:26:33.315926002 +0000
+++ memlib.c	2021-02-25 07:26:33.315926002 +0000
@@ -15,23 +15,18 @@
 #include "csapp.h"
 #include "memlib.h"
 
-#define MAX_HEAP (20*(1<<20))  /* 20 MB */
-
-
 /* $begin memlib */
 /* Private global variables */
 static char *mem_heap;     /* Points to first byte of heap */
 static char *mem_brk;      /* Points to last byte of heap plus 1 */
-static char *mem_max_addr; /* Max legal heap addr plus 1*/
 
 /*
  * mem_init - Initialize the memory system model
  */
 void mem_init(void)
 {
-  mem_heap = (char *)Malloc(MAX_HEAP);
+  mem_heap = (char *)sbrk(0);
   mem_brk = (char *)mem_heap;
-  mem_max_addr = (char *)(mem_heap + MAX_HEAP);
 }
 
 /*
@@ -43,7 +38,7 @@
 {
   char *old_brk = mem_brk;
 
-  if ( (incr < 0) || ((mem_brk + incr) > mem_max_addr)) {
+  if ( (incr < 0) || ((mem_brk = sbrk(incr)) == (void *)-1)) {
     errno = ENOMEM;
     fprintf(stderr, "ERROR: mem_sbrk failed. Ran out of memory...\n");
     return (void *)-1;

main.c file measure malloc performance

/*
 * main.c
 */
#include <stdio.h>
#include "csapp.h"

#ifdef CUS_MALLOC
#include "mm.h"
#include "memlib.h"
#else
#include <stdlib.h>
#endif

#define LOOP 10000

int main(int argc, char* argv[]) {
  void* m_start = sbrk(0);
  size_t malloc_size = 0;

  int i;
  for (i = 0; i < LOOP; i+=2) {
    void* ptr_f = malloc(i);
    void* ptr = malloc(i+1);
    free(ptr_f);

    malloc_size += i+1;
  }

  void* m_end = sbrk(0);
  size_t heap_size = (size_t)(m_end - m_start);

  printf("malloc size: %ld, heap_size: %ld\n", malloc_size, heap_size);

  return 0;
}




run make to generate both origin main executable file and custom version(using -DCUS_MALLOC)

CC = gcc
CFLAGS = -m64 -pthread -DCUS_MALLOC
SRCS = mm.c memlib.c csapp.c

all: origin custom diff

measure:
	time ./origin.main
	time ./custom.main

origin:
	$(CC) -m64 main.c -o origin.main

custom:
	$(CC) $(CFLAGS) $(SRCS) main.c -o custom.main

diff:
	(diff -u ../vm/mm.h mm.h > mm.h.diff; cd .)
	(diff -u ../vm/memlib.c memlib.c > memlib.c.diff; cd .)

test:

.PHONY: clean
clean:
	find . -type f -executable -print0 | xargs -0 rm -f --


measurement

(cd ./site/content/chapter9/code/malloc; make measure)

time ./origin.main
malloc size: 25000000, heap_size: 28311552
0.00user 0.01system 0:00.01elapsed 100%CPU (0avgtext+0avgdata 19256maxresident)k
0inputs+0outputs (0major+4547minor)pagefaults 0swaps

time ./custom.main
malloc size: 25000000, heap_size: 31327104
0.58user 0.00system 0:00.58elapsed 99%CPU (0avgtext+0avgdata 30592maxresident)k
0inputs+0outputs (0major+7339minor)pagefaults 0swaps
comments powered by Disqus