From 08f25e576c34f447cb60870d037d9146b2113d66 Mon Sep 17 00:00:00 2001 From: fujuntang <fujuntang@smartai.com> Date: 星期一, 25 十月 2021 11:53:47 +0800 Subject: [PATCH] Optimize the lock operate logic. --- src/shm/mm.cpp | 30 ++++++++++++++++-------------- 1 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/shm/mm.cpp b/src/shm/mm.cpp index 7f1fda3..13ec443 100644 --- a/src/shm/mm.cpp +++ b/src/shm/mm.cpp @@ -113,7 +113,6 @@ newsize = ALIGN(size + (SIZE_T_SIZE << 1) + (PTR_SIZE << 1) ); - //fprintf(stderr, "mm_malloc : size=%u\n", size); /* Search the free list for a fit */ SemUtil::dec(mutex); if ((ptr = find_fit(newsize)) != NULL) @@ -123,8 +122,11 @@ return aptr; } else { SemUtil::inc(mutex); - abort(); - err_exit(0, "mm_malloc : out of memery\n"); + err_msg(0, "mm_malloc : out of memery\n"); + LoggerFactory::getLogger()->fatal("mm_malloc : out of memery\n"); + // abort(); + exit(1); + return NULL; } @@ -145,7 +147,7 @@ *} */ - SemUtil::dec(mutex); + SemUtil::dec_uni(mutex); size_t size = GET_SIZE(HDRP(ptr)); PUT(HDRP(ptr), PACK(size, 0)); PUT(FTRP(ptr), PACK(size, 0)); @@ -229,12 +231,13 @@ /* * mm_init - Initialize the memory manager, M unit + * @return 鏄惁绗竴娆″垱寤� */ bool mm_init(size_t heap_size) { //鍚屼竴杩涚▼鍐呭凡缁忓垵濮嬪寲杩囦簡 - SemUtil::dec(mutex); + SemUtil::dec_uni(mutex); if (shmid != -1){ hashtable = (hashtable_t *)shmp; SemUtil::inc(mutex); @@ -253,6 +256,7 @@ first = false; shmid = shmget(SHM_KEY, 0, 0); } + if (shmid == -1) err_exit(errno, "mm_init shmget"); shmp = shmat(shmid, key_addr, 0); @@ -301,16 +305,17 @@ return first; } - +/** + * @return 鏄惁鐪熸閿�姣佹垚鍔� + */ bool mm_destroy(void) { struct shmid_ds shmid_ds; - - SemUtil::dec(mutex); + SemUtil::dec_uni(mutex); if(shmctl(shmid, IPC_STAT, &shmid_ds) == 0) { //LoggerFactory::getLogger()->debug("shm_nattch=%d\n", shmid_ds.shm_nattch); - // 鍙湁褰撳墠涓�涓繘绋媋ttach鍒板叡浜唴瀛樹笂 + // 澶氫釜杩涚▼attach鍦ㄥ叡浜唴瀛樹笂 if (shmid_ds.shm_nattch > 1) { //detache if (shmdt(shmp) == -1) { @@ -319,7 +324,7 @@ SemUtil::inc(mutex); return false; } else { - + // 鍙湁褰撳墠涓�涓繘绋媋ttach鍒板叡浜唴瀛樹笂 hashtable_destroy(hashtable); //detache if (shmdt(shmp) == -1) { @@ -331,10 +336,6 @@ //remove shared memery if (shmctl(shmid, IPC_RMID, 0) == -1) err_exit(errno, "mm_destroy shmctl IPC_RMID"); - else - LoggerFactory::getLogger()->debug("shared memory destroy\n"); - - LoggerFactory::getLogger()->debug( "mm_destroy: real destroy."); SemUtil::inc(mutex); SemUtil::remove(mutex); @@ -359,6 +360,7 @@ void mm_free_by_key(int key) { void *ptr; + ptr = hashtable_get(hashtable, key); if(ptr != NULL) { mm_free(ptr); -- Gitblit v1.8.0