wangzhengquan
2020-08-01 f0732bbcbdca67ef8f9444f2aafaf44da81c9e5a
update
2个文件已修改
108 ■■■■ 已修改文件
src/queue/include/shm_queue.h 4 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/queue/mm.c 104 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/queue/include/shm_queue.h
@@ -80,7 +80,10 @@
template <typename ELEM_T>
void SHMQueue<ELEM_T>::remove_queues_include(int keys[], size_t length) {
  hashtable_t *hashtable = mm_get_hashtable();
  LockFreeQueue<ELEM_T, SHM_Allocator> *mqueue;
  for(int i = 0; i< length; i++) {
    mqueue = (LockFreeQueue<ELEM_T, SHM_Allocator> *)mm_get_by_key(keys[i]);
    delete mqueue;
    hashtable_remove(hashtable, keys[i]);
  }
}
@@ -90,7 +93,6 @@
  hashtable_t *hashtable = mm_get_hashtable();
  queue = (LockFreeQueue<ELEM_T, SHM_Allocator> *)hashtable_get(hashtable, key);
  // LockFreeQueue<int, 10000> q;
  if (queue == NULL || (void *)queue == (void *)1) {
    queue = new LockFreeQueue<ELEM_T, SHM_Allocator>(qsize);
    hashtable_put(hashtable, key, (void *)queue);
src/queue/mm.c
@@ -324,70 +324,70 @@
/*
 * 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;
// }