From 00dba6082e245d917cb7d6eed3c627211ff41cd7 Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期五, 25 九月 2020 15:53:21 +0800
Subject: [PATCH] update

---
 src/queue/mm.c |  130 ++++++++++++++++++++++++------------------
 1 files changed, 74 insertions(+), 56 deletions(-)

diff --git a/src/queue/mm.c b/src/queue/mm.c
index 592fda0..39aca9e 100644
--- a/src/queue/mm.c
+++ b/src/queue/mm.c
@@ -81,7 +81,7 @@
 static void *place(void *bp, size_t size);
 static void *find_fit(size_t size);
 static void *coalesce(void *bp);
-static void rm_fblock(void *bp);
+static inline void rm_fblock(void *bp);
 static void insert_fblock (void *bp);
 
 static void *mem_sbrk(int incr);
@@ -122,6 +122,7 @@
     SemUtil::inc(mutex);
     return aptr;
   } else {
+    SemUtil::inc(mutex);
     err_msg(0, "mm_malloc : out of memery\n");
     return NULL;
   }
@@ -143,9 +144,8 @@
    *}
    */
 
-  
-  size_t size = GET_SIZE(HDRP(ptr));
   SemUtil::dec(mutex);
+  size_t size = GET_SIZE(HDRP(ptr));
   PUT(HDRP(ptr), PACK(size, 0));
   PUT(FTRP(ptr), PACK(size, 0));
   coalesce(ptr);
@@ -304,73 +304,90 @@
   return hashtable;
 }
 
+void * mm_get_by_key(int key) {
+  return hashtable_get(hashtable, key);
+}
+
+void mm_free_by_key(int key) {
+  void *ptr;
+  ptr = hashtable_get(hashtable, key);
+  if(ptr != NULL) {
+    mm_free(ptr);
+    hashtable_remove(hashtable, key);
+  }
+}
+
+
+int mm_alloc_key() {
+  return hashtable_alloc_key(hashtable);
+}
 /*
  * mm_init - Initialize the memory manager
  */
-bool mm_init2(size_t offset, void **addr)
-{
+// bool mm_init2(size_t offset, void **addr)
+// {
   
-  //宸茬粡鍒濆鍖栬繃浜�
-  SemUtil::dec(mutex);
-  if (shmid != -1){
-    *addr = shmp;
-    SemUtil::inc(mutex);
-    return false;
-  }
+//   //宸茬粡鍒濆鍖栬繃浜�
+//   SemUtil::dec(mutex);
+//   if (shmid != -1){
+//     *addr = shmp;
+//     SemUtil::inc(mutex);
+//     return false;
+//   }
 
-  bool first = true;
+//   bool first = true;
   
   
-  shmid = shmget(SHM_KEY, MAX_HEAP, IPC_CREAT | IPC_EXCL | OBJ_PERMS);
-  if (shmid == -1 && errno == EEXIST) {
-    first = false;
-    shmid  = shmget(SHM_KEY, 0, 0);
-  }
-  if (shmid == -1)
-    err_exit(errno, "mm_init shmget");
-  shmp = shmat(shmid, key_addr, 0);
-  if ((long)shmp == -1)
-    err_exit(errno, "mm_init shmat");
+//   shmid = shmget(SHM_KEY, MAX_HEAP, IPC_CREAT | IPC_EXCL | OBJ_PERMS);
+//   if (shmid == -1 && errno == EEXIST) {
+//     first = false;
+//     shmid  = shmget(SHM_KEY, 0, 0);
+//   }
+//   if (shmid == -1)
+//     err_exit(errno, "mm_init shmget");
+//   shmp = shmat(shmid, key_addr, 0);
+//   if ((long)shmp == -1)
+//     err_exit(errno, "mm_init shmat");
 
-  mem_start_brk = (void*)ALIGN((size_t)((char *)shmp + offset + ALIGNMENT));
-  mem_max_addr = (void *)((char *)shmp+ MAX_HEAP);
-  mem_max_size = (char *)mem_max_addr - (char*)mem_start_brk;
-  mem_brk = mem_start_brk;
-  void *free_listp;
-  /* Create the initial empty heap */
-  int initsize = ALIGN(3 * SIZE_T_SIZE + 2 * PTR_SIZE);
-  heap_listp = (char *)mem_start_brk + initsize - 2 * SIZE_T_SIZE - 2 * PTR_SIZE;
+//   mem_start_brk = (void*)ALIGN((size_t)((char *)shmp + offset + ALIGNMENT));
+//   mem_max_addr = (void *)((char *)shmp+ MAX_HEAP);
+//   mem_max_size = (char *)mem_max_addr - (char*)mem_start_brk;
+//   mem_brk = mem_start_brk;
+//   void *free_listp;
+//   /* Create the initial empty heap */
+//   int initsize = ALIGN(3 * SIZE_T_SIZE + 2 * PTR_SIZE);
+//   heap_listp = (char *)mem_start_brk + initsize - 2 * SIZE_T_SIZE - 2 * PTR_SIZE;
 
-  if(!first) {
-    *addr = shmp;
-    SemUtil::inc(mutex);
-    return first;
-  }
+//   if(!first) {
+//     *addr = shmp;
+//     SemUtil::inc(mutex);
+//     return first;
+//   }
 
  
-  if ((mem_sbrk(initsize)) == (void *) - 1)
-    err_exit(errno, "mm_init mem_sbrk");
+//   if ((mem_sbrk(initsize)) == (void *) - 1)
+//     err_exit(errno, "mm_init mem_sbrk");
   
-  PUT((char *)mem_start_brk + initsize - SIZE_T_SIZE, PACK(0, 1));   /* Epilogue header */
-  /*PUT(HDRP(heap_listp), PACK(initsize - SIZE_T_SIZE, 1));
-  PUT(FTRP(heap_listp), PACK(initsize - SIZE_T_SIZE, 1));*/
+//   PUT((char *)mem_start_brk + initsize - SIZE_T_SIZE, PACK(0, 1));   /* Epilogue header */
+//   /*PUT(HDRP(heap_listp), PACK(initsize - SIZE_T_SIZE, 1));
+//   PUT(FTRP(heap_listp), PACK(initsize - SIZE_T_SIZE, 1));*/
 
-  PUT(HDRP(heap_listp), PACK(initsize - SIZE_T_SIZE, 1));
-  PUT(FTRP(heap_listp), PACK(initsize - SIZE_T_SIZE, 1));
+//   PUT(HDRP(heap_listp), PACK(initsize - SIZE_T_SIZE, 1));
+//   PUT(FTRP(heap_listp), PACK(initsize - SIZE_T_SIZE, 1));
   
-  /**
-   * here the heap_listp can be look as a ancher which concat the header and tail of free-list to form a ring, and the heap_list itself will never be used as a free block
-  */
-  PUT_PTR(SUCCRP(heap_listp), heap_listp);
-  PUT_PTR(PREDRP(heap_listp), heap_listp);
-  /* Extend the empty heap with a free block of CHUNKSIZE bytes */
-  if ((free_listp = extend_heap(mem_max_size - initsize - ALIGNMENT)) == NULL)
-    err_exit(errno, "mm_init extend_heap");
+//   /**
+//    * here the heap_listp can be look as a ancher which concat the header and tail of free-list to form a ring, and the heap_list itself will never be used as a free block
+//   */
+//   PUT_PTR(SUCCRP(heap_listp), heap_listp);
+//   PUT_PTR(PREDRP(heap_listp), heap_listp);
+//   /* Extend the empty heap with a free block of CHUNKSIZE bytes */
+//   if ((free_listp = extend_heap(mem_max_size - initsize - ALIGNMENT)) == NULL)
+//     err_exit(errno, "mm_init extend_heap");
 
-  *addr = shmp;
-  SemUtil::inc(mutex);
-  return first;
-}
+//   *addr = shmp;
+//   SemUtil::inc(mutex);
+//   return first;
+// }
 
  
 
@@ -444,9 +461,10 @@
 /**
  * remove a block form free list
  */
-static void rm_fblock(void *rbp)
+static inline void rm_fblock(void *rbp)
 {
   // the successor of the previous block of rbp point to next block of rbp
+
   PUT_PTR(SUCCRP(PREV_FBLKP(rbp)), NEXT_FBLKP(rbp));
   // the predecessor of then next block of rbp point to previous block of rbp
   PUT_PTR(PREDRP(NEXT_FBLKP(rbp)), PREV_FBLKP(rbp));

--
Gitblit v1.8.0