@@ -77,6 +77,21 @@ os_create_stack_context(korp_tid handle)
7777 return BH_MALLOC (sizeof (sigjmp_buf ));
7878}
7979
80+ static void
81+ free_stack_context_list (void * stack_context_list )
82+ {
83+ bh_list * context_list = (bh_list * )stack_context_list ;
84+
85+ sigjmp_buf * context = bh_list_first_elem (context_list );
86+ while (context ) {
87+ sigjmp_buf * next = bh_list_elem_next (context );
88+ BH_FREE (context );
89+ context = next ;
90+ };
91+
92+ BH_FREE (context_list );
93+ }
94+
8095bool
8196os_stack_contexts_init ()
8297{
@@ -96,7 +111,7 @@ os_stack_contexts_init()
96111
97112 stack_context_handler .contexts = bh_hash_map_create (
98113 32 , false, (HashFunc )thread_handle_hash ,
99- (KeyEqualFunc )thread_handle_equal , NULL , wasm_runtime_free );
114+ (KeyEqualFunc )thread_handle_equal , NULL , free_stack_context_list );
100115 if (stack_context_handler .contexts == NULL ) {
101116 LOG_ERROR ("failed to init stack contexts" );
102117 return false;
@@ -106,11 +121,16 @@ os_stack_contexts_init()
106121}
107122
108123void
109- os_stack_contexts_deinit ()
124+ os_stack_contexts_destroy ()
110125{
111- os_mutex_destroy (& stack_context_handler .lock );
126+ os_mutex_lock (& stack_context_handler .lock );
127+ if (!bh_hash_map_destroy (stack_context_handler .contexts ))
128+ LOG_ERROR ("failed to destroy stack context map" );
129+ stack_context_handler .contexts = NULL ;
130+ os_mutex_unlock (& stack_context_handler .lock );
112131
113- // TODO(eloparco): Release memory
132+ if (os_mutex_destroy (& stack_context_handler .lock ) != BHT_OK )
133+ LOG_ERROR ("failed to destroy stack contexts" );
114134}
115135
116136void
@@ -125,6 +145,7 @@ os_remove_stack_context(korp_tid handle, sigjmp_buf *context)
125145 assert (context_list );
126146 int ret = bh_list_remove (context_list , context );
127147 assert (ret == BH_LIST_SUCCESS );
148+ BH_FREE (context );
128149
129150 os_mutex_unlock (& stack_context_handler .lock );
130151}
@@ -143,30 +164,36 @@ os_is_returning_from_signal(korp_tid handle, sigjmp_buf *context)
143164 if (!context_list ) {
144165 context_list = BH_MALLOC (sizeof (bh_list ));
145166 if (!context_list ) {
146- os_mutex_unlock ( & stack_context_handler . lock );
147- return NULL ;
167+ LOG_ERROR ( "failed to allocate stack context list" );
168+ goto unlock ;
148169 }
170+
149171 int ret_list = bh_list_init (context_list );
150172 if (ret_list != BH_LIST_SUCCESS ) {
151- BH_FREE (context_list );
152- os_mutex_unlock (& stack_context_handler .lock );
153- return NULL ;
173+ LOG_ERROR ("failed to initialize stack context list" );
174+ goto free_list_and_unlock ;
154175 }
155176
156177 bool ret_map =
157178 bh_hash_map_insert (stack_context_handler .contexts ,
158179 (void * )(uintptr_t )handle , context_list );
159180 if (!ret_map ) {
160- BH_FREE (context_list );
161- os_mutex_unlock (& stack_context_handler .lock );
162- return NULL ;
181+ LOG_ERROR ("failed to insert stack context list into map" );
182+ goto free_list_and_unlock ;
163183 }
164184 }
165185
166186 /* Add stack context to stack context list */
167187 int bh_res = bh_list_insert (context_list , context );
168- assert (bh_res == BH_LIST_SUCCESS );
188+ if (bh_res != BH_LIST_SUCCESS ) {
189+ LOG_ERROR ("failed to insert stack context into list" );
190+ goto free_list_and_unlock ;
191+ }
192+ goto unlock ;
169193
194+ free_list_and_unlock :
195+ BH_FREE (context_list );
196+ unlock :
170197 os_mutex_unlock (& stack_context_handler .lock );
171198 return false;
172199}
0 commit comments