From 2b43ef443f14889e086bdf0ccb28e66d17efd2ae Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期二, 22 十二月 2020 12:01:13 +0800
Subject: [PATCH] update
---
src/queue/mm.c | 146 ++++++++++++++----------------------------------
test_net_socket/test_net_mod_socket.c | 5 +
src/queue/hashtable.c | 6 ++
src/queue/hashtable.h | 1
4 files changed, 53 insertions(+), 105 deletions(-)
diff --git a/src/queue/hashtable.c b/src/queue/hashtable.c
index 03b64b6..243067b 100755
--- a/src/queue/hashtable.c
+++ b/src/queue/hashtable.c
@@ -36,6 +36,12 @@
}
+void hashtable_destroy(hashtable_t *hashtable) {
+ SemUtil::remove( hashtable->mutex);
+ SemUtil::remove( hashtable->wlock);
+ SemUtil::remove( hashtable->cond);
+}
+
static inline void *_hashtable_get(hashtable_t *hashtable, int key)
{
diff --git a/src/queue/hashtable.h b/src/queue/hashtable.h
index 30affe2..ea4dc64 100755
--- a/src/queue/hashtable.h
+++ b/src/queue/hashtable.h
@@ -19,6 +19,7 @@
typedef void (*hashtable_foreach_cb)(int key, void *value);
void hashtable_init(hashtable_t *hashtable);
+void hashtable_destroy(hashtable_t *hashtable);
void *hashtable_get(hashtable_t *hashtable, int key);
void hashtable_put(hashtable_t *hashtable, int key, void *value);
void *hashtable_remove(hashtable_t *hashtable, int key);
diff --git a/src/queue/mm.c b/src/queue/mm.c
index f09fbab..979cced 100644
--- a/src/queue/mm.c
+++ b/src/queue/mm.c
@@ -300,6 +300,48 @@
return first;
}
+
+bool mm_destroy(void) {
+ struct shmid_ds shmid_ds;
+
+
+ SemUtil::dec(mutex);
+
+ if(shmctl(shmid, IPC_STAT, &shmid_ds) == 0) {
+ //LoggerFactory::getLogger()->debug("shm_nattch=%d\n", shmid_ds.shm_nattch);
+ // 鍙湁褰撳墠涓�涓繘绋媋ttach鍒板叡浜唴瀛樹笂
+ if(shmid_ds.shm_nattch == 1) {
+
+ hashtable_destroy(hashtable);
+ //detache
+ if (shmdt(shmp) == -1) {
+ LoggerFactory::getLogger()->error(errno, "mm_destroy shmdt");
+ SemUtil::inc(mutex);
+ return false;
+ }
+
+ //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");
+
+ SemUtil::inc(mutex);
+ SemUtil::remove(mutex);
+ return true;
+
+ } else {
+ SemUtil::inc(mutex);
+ return false;
+ }
+
+ } else {
+ err_msg(errno, "mm_destroy shmctl IPC_STAT");
+ SemUtil::inc(mutex);
+ return false;
+ }
+}
+
hashtable_t * mm_get_hashtable() {
return hashtable;
}
@@ -321,111 +363,7 @@
int mm_alloc_key() {
return hashtable_alloc_key(hashtable);
}
-/*
- * mm_init - Initialize the memory manager
- */
-// bool mm_init2(size_t offset, void **addr)
-// {
-
-// //宸茬粡鍒濆鍖栬繃浜�
-// SemUtil::dec(mutex);
-// if (shmid != -1){
-// *addr = shmp;
-// SemUtil::inc(mutex);
-// return false;
-// }
-// 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");
-
-// 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 ((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(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");
-
-// *addr = shmp;
-// SemUtil::inc(mutex);
-// return first;
-// }
-
-
-
-bool mm_destroy(void) {
- struct shmid_ds shmid_ds;
- //detache
- SemUtil::dec(mutex);
- if (shmdt(shmp) == -1) {
- err_msg(errno, "mm_destroy shmdt");
- SemUtil::inc(mutex);
- }
-
-
- if(shmctl(shmid, IPC_STAT, &shmid_ds) == 0) {
- //LoggerFactory::getLogger()->debug("shm_nattch=%d\n", shmid_ds.shm_nattch);
- if(shmid_ds.shm_nattch == 0) {
- //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");
-
- SemUtil::inc(mutex);
- SemUtil::remove(mutex);
- return true;
-
- } else {
- SemUtil::inc(mutex);
- }
- return false;
- } else {
- err_msg(errno, "mm_destroy shmctl IPC_STAT");
- SemUtil::inc(mutex);
- return false;
- }
-
-
-}
/*
* extend_heap - Extend heap with free block and return its block pointer
*/
diff --git a/test_net_socket/test_net_mod_socket.c b/test_net_socket/test_net_mod_socket.c
index 1b6cf33..fecf754 100644
--- a/test_net_socket/test_net_mod_socket.c
+++ b/test_net_socket/test_net_mod_socket.c
@@ -83,7 +83,7 @@
void * server_socket = bus_server_socket_wrapper_open();
pthread_t tid;
// 鍒涘缓涓�涓嚎绋�,鍙互鍏抽棴bus
- pthread_create(&tid, NULL, bus_handler, server_socket);
+ // pthread_create(&tid, NULL, bus_handler, server_socket);
if(bus_server_socket_wrapper_start_bus(server_socket) != 0) {
printf("start bus failed\n");
exit(1);
@@ -432,6 +432,9 @@
}
+ printf("==========end========\n");
+ shm_mm_wrapper_destroy();
+
}
--
Gitblit v1.8.0