wangzhengquan
2020-07-07 379f42982b8c57ee6511cb8e498019f454323977
squeue/mm.c
@@ -2,7 +2,7 @@
 * 管理共享内存的分配,与释放
 */
#include "mm.h"
#include "pcsem.h"
#include "sem_util.h"
/* $begin mallocmacros */
@@ -28,7 +28,7 @@
#define MIN_BLOCK_SIZE (ALIGN( (SIZE_T_SIZE << 1) + SIZE_T_SIZE + (PTR_SIZE << 1) ))
#define MAX(x, y) ((x) > (y)? (x) : (y))
//#define MAX(x, y) ((x) > (y)? (x) : (y))
/* Pack a size and allocated bit into a word */
#define PACK(size, alloc)  ((size) | (alloc)) //line:vm:mm:pack
@@ -77,7 +77,8 @@
static  int shmid = -1;
static  void *shmp;
static int mutex = pcsem::init(8899, 1);
//static int mutex = SemUtil::get(8899, 1);
static int mutex = SemUtil::get(IPC_PRIVATE, 1);
static void *mem_start_brk;  /* points to first byte of heap */
static void *mem_brk;        /* points to last byte of heap */
@@ -99,11 +100,11 @@
    //fprintf(stderr, "mm_malloc : size=%u\n", size);
  /* Search the free list for a fit */
  pcsem::dec(mutex);
  SemUtil::dec(mutex);
  if ((ptr = find_fit(newsize)) != NULL)
  {
    aptr = place(ptr, newsize);
    pcsem::inc(mutex);
    SemUtil::inc(mutex);
    return aptr;
  } else {
    fprintf(stderr, "mm_malloc : out of memery\n");
@@ -129,11 +130,11 @@
  
  size_t size = GET_SIZE(HDRP(ptr));
  pcsem::dec(mutex);
  SemUtil::dec(mutex);
  PUT(HDRP(ptr), PACK(size, 0));
  PUT(FTRP(ptr), PACK(size, 0));
  coalesce(ptr);
  pcsem::inc(mutex);
  SemUtil::inc(mutex);
}
@@ -214,10 +215,10 @@
{
  
  //已经初始化过了
  pcsem::dec(mutex);
  SemUtil::dec(mutex);
  if (shmid != -1){
    *addr = shmp;
    pcsem::inc(mutex);
    SemUtil::inc(mutex);
    return false;
  }
@@ -246,7 +247,7 @@
  if(!first) {
    *addr = shmp;
    pcsem::inc(mutex);
    SemUtil::inc(mutex);
    return first;
  }
@@ -271,19 +272,19 @@
    err_exit(errno, "mm_init extend_heap");
  *addr = shmp;
  pcsem::inc(mutex);
  SemUtil::inc(mutex);
  return first;
}
 
void mm_deinit(void) {
void mm_destroy(void) {
  if (shmdt(shmp) == -1)
    err_exit(errno, "mm_init shmdt");
  if (shmctl(shmid, IPC_RMID, 0) == -1)
    err_exit(errno, "mm_init shmctl IPC_RMID");
  pcsem::remove(mutex);
  SemUtil::remove(mutex);
}
/*
 * extend_heap - Extend heap with free block and return its block pointer