tb_void_t tb_heap_exit(tb_heap_ref_t heap) { // check tb_heap_impl_t* impl = (tb_heap_impl_t*)heap; tb_assert_and_check_return(impl); // clear data tb_heap_clear(heap); // free data if (impl->data) tb_free(impl->data); impl->data = tb_null; // free it tb_free(impl); }
tb_void_t tb_timer_clear(tb_timer_ref_t timer) { tb_timer_impl_t* impl = (tb_timer_impl_t*)timer; if (impl) { // enter tb_spinlock_enter(&impl->lock); // clear heap if (impl->heap) tb_heap_clear(impl->heap); // clear pool if (impl->pool) tb_fixed_pool_clear(impl->pool); // leave tb_spinlock_leave(&impl->lock); } }
tb_bool_t tb_heap_load(tb_heap_ref_t heap, tb_stream_ref_t stream) { // check tb_heap_impl_t* impl = (tb_heap_impl_t*)heap; tb_assert_and_check_return_val(impl && stream, tb_false); tb_assert_and_check_return_val(impl->func.hash && impl->func.load && impl->func.free, tb_false); // clear the heap first tb_heap_clear(heap); // the offset tb_hize_t offset = tb_stream_offset(stream); // done tb_bool_t ok = tb_false; tb_uint32_t crc32 = 0; tb_pointer_t buff = tb_null; do { // calc type crc32 = tb_crc_encode_cstr(TB_CRC_MODE_32_IEEE_LE, crc32, "heap"); // calc item type crc32 = tb_crc_encode_value(TB_CRC_MODE_32_IEEE_LE, crc32, impl->func.type); // calc item size crc32 = tb_crc_encode_value(TB_CRC_MODE_32_IEEE_LE, crc32, impl->func.size); // load the head crc32 tb_uint32_t crc32_head = tb_stream_bread_u32_be(stream); tb_assert_and_check_break(crc32_head == crc32); // make item buffer buff = impl->func.size? tb_malloc(impl->func.size) : tb_null; // load size tb_uint32_t size = tb_stream_bread_u32_be(stream); // load heap tb_uint32_t load = 0; for (load = 0; load < size; load++) { // load item if (!impl->func.load(&impl->func, buff, stream)) break; // the item data tb_cpointer_t data = impl->func.data(&impl->func, buff); // hash item tb_size_t hash = impl->func.hash(&impl->func, data, -1, 0); // calc item crc32 = tb_crc_encode_value(TB_CRC_MODE_32_IEEE_LE, crc32, hash); // save item tb_heap_put(heap, data); // free name impl->func.free(&impl->func, buff); } // check tb_assert_and_check_break(load == size); // load the body crc32 tb_uint32_t crc32_body = tb_stream_bread_u32_be(stream); tb_assert_and_check_break(crc32_body == crc32); // ok ok = tb_true; } while (0); // failed? if (!ok) { // restore it tb_stream_seek(stream, offset); // clear it tb_heap_clear(heap); } // exit buffer if (buff) tb_free(buff); buff = tb_null; // ok? return ok; }
tb_void_t tb_priority_queue_clear(tb_priority_queue_ref_t self) { tb_heap_clear((tb_heap_ref_t)self); }
tb_void_t tb_priority_queue_clear(tb_priority_queue_ref_t queue) { tb_heap_clear((tb_heap_ref_t)queue); }