void lf_dynarray_destroy(LF_DYNARRAY *array) { int i; for (i= 0; i < LF_DYNARRAY_LEVELS; i++) recursive_free(array->level[i], i); my_atomic_rwlock_destroy(&array->lock); }
// Free the children of p->root, except for the child exception. // Connect the parent of bl with the exception child. If except == TP_NULL, remove all children. // Then we starts a few threads to free the tree (also free bl). void tree_simple_free_except(TreePool *p, TreeBlock *except) { TreeBlock *r = p->root->children[0].child; if (r == TP_NULL) { memset(&p->root->data.stats[0], 0, sizeof(Stat)); return; } // Free the nodes. for (int i = 0; i < r->n; ++i) { if (r->children[i].child != except) { recursive_free(p, r->children[i].child); } } // Reconnect. Note this is run in single thread, so order does not matter. p->root->children[0].child = except; if (except != TP_NULL) { float black_win = 0.0; int total = 0; // In this case, we need to recompute the stats. for (int i = 0; i < except->n; ++i) { black_win += except->data.stats[i].black_win; total += except->data.stats[i].total; } p->root->data.stats[0].black_win = black_win; p->root->data.stats[0].total = total; except->parent = p->root; except->parent_offset = 0; } else { // Empty the child and reset the statistics. RESET_BIT(p->root->expansion, 0); memset(&p->root->data.stats[0], 0, sizeof(Stat)); } }
static void recursive_free(void **alloc, int level) { if (!alloc) return; if (level) { int i; for (i= 0; i < LF_DYNARRAY_LEVEL_LENGTH; i++) recursive_free(alloc[i], level-1); my_free(alloc); } else my_free(alloc[-1]); }
static void recursive_free(TreePool *p, TreeBlock *r) { // Open multithread to recursively free the tree. // For now just single thread if (r == TP_NULL) return; for (int i = 0; i < r->n; ++i) { if (r->children[i].child != TP_NULL) { recursive_free(p, r->children[i].child); } } if (r->parent != TP_NULL) { RESET_BIT(r->parent->expansion, r->parent_offset); r->parent->children[r->parent_offset].child = TP_NULL; event_count_destroy(&r->parent->children[r->parent_offset].event_count); } for (int j = 0; j < BIT_CNN_NUM_BITS; ++j) { event_count_destroy(&r->cnn_data.event_counts[j]); } if (r->extra) free(r->extra); free(r); p->allocated --; return; }
// Free the tree_pool void tree_simple_pool_free(TreePool* p) { recursive_free(p, p->root); }