sub_delete.c

00001 /* File:      sub_delete.c
00002 ** Author(s): Ernie Johnson
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 1986, 1993-1998
00006 ** 
00007 ** XSB is free software; you can redistribute it and/or modify it under the
00008 ** terms of the GNU Library General Public License as published by the Free
00009 ** Software Foundation; either version 2 of the License, or (at your option)
00010 ** any later version.
00011 ** 
00012 ** XSB is distributed in the hope that it will be useful, but WITHOUT ANY
00013 ** WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 ** FOR A PARTICULAR PURPOSE.  See the GNU Library General Public License for
00015 ** more details.
00016 ** 
00017 ** You should have received a copy of the GNU Library General Public License
00018 ** along with XSB; if not, write to the Free Software Foundation,
00019 ** Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020 **
00021 ** $Id: sub_delete.c,v 1.15 2006/03/24 16:40:28 tswift Exp $
00022 ** 
00023 */
00024 
00025 
00026 #include "xsb_config.h"
00027 #include "xsb_debug.h"
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 
00032 #include "auxlry.h"
00033 #include "cell_xsb.h"
00034 #include "psc_xsb.h"
00035 #include "trie_internals.h"
00036 #include "macro_xsb.h"
00037 #include "error_xsb.h"
00038 #include "thread_xsb.h"
00039 #include "memory_xsb.h"
00040 
00041 extern BTHTptr hhadded;
00042 
00043 /* TLS: this file is used to delete subsumptive tables.  In the
00044    current MT engine, all structure managers used for the mt engine
00045    are private, and this code relies on that fact. I've made these
00046    dependencies explicit in the various function names. */
00047 
00048 /* Freeing Individual Structures
00049    ----------------------------- */
00050 
00051 static void free_private_tstn(CTXTdeclc TSTNptr tstn) {
00052   SM_DeallocateStruct(smTSTN,tstn);
00053 }
00054 
00055 static void free_private_tstht(CTXTdeclc TSTHTptr tstht) {
00056   TrieHT_RemoveFromAllocList(smTSTHT,tstht);
00057   SM_DeallocateStruct(smTSTHT,tstht);
00058 }
00059 
00060 static void free_private_tsi(CTXTdeclc TSTHTptr tstht) {
00061   if ( IsNonNULL(TSTHT_IndexHead(tstht)) )
00062     SM_DeallocateStructList(smTSIN,TSTHT_IndexTail(tstht),
00063                             TSTHT_IndexHead(tstht));
00064 }
00065 
00066 
00067 /*
00068  * Answer List of a Consumer may already be completely deallocated,
00069  * even the dummy node.  free_answer_list will handle either shared or
00070  * private SMs.
00071  */
00072 static void free_private_al(CTXTdeclc VariantSF sf) {
00073   if ( IsNonNULL(subg_ans_list_ptr(sf)) )
00074     free_answer_list(sf);
00075 }
00076 
00077 
00078 /* Deleting Structures with Substructures
00079    -------------------------------------- */
00080 
00081 static void delete_private_btht(CTXTdeclc BTHTptr btht) {
00082   mem_dealloc(BTHT_BucketArray(btht),BTHT_NumBuckets(btht)*sizeof(void *),TABLE_SPACE);
00083   TrieHT_RemoveFromAllocList(subsumptive_smBTHT,btht);
00084   SM_DeallocateStruct(subsumptive_smBTHT,btht);
00085 }
00086 
00087 static void delete_private_tstht(CTXTdeclc TSTHTptr tstht) {
00088   mem_dealloc(BTHT_BucketArray(tstht),BTHT_NumBuckets(tstht)*sizeof(void *),TABLE_SPACE);
00089   free_private_tsi(CTXTc tstht);
00090   free_private_tstht(CTXTc tstht);
00091 }
00092 
00093 static void delete_private_sf(CTXTdeclc VariantSF sf) {
00094   free_private_al(CTXTc sf);
00095   if ( IsProducingSubgoal(sf) )
00096     FreeProducerSF(sf)
00097   else
00098     SM_DeallocateStruct(smConsSF,sf);
00099 }
00100 
00101 /*-----------------------------------------------------------------------*/
00102 
00103 /*
00104  *  Delete the given TST Answer Set node and recursively all of
00105  *  its children.
00106  */
00107 
00108 static void delete_tst_answer_set(CTXTdeclc TSTNptr root) {
00109 
00110   TSTNptr current, sibling;
00111   TSTHTptr hash_hdr;
00112   unsigned int i;
00113 
00114 
00115   if ( IsNULL(root) )
00116     return;
00117 
00118   /* I inserted the check for TSTN_Child(root) below to avoid
00119      a segmentation fault. It seems to be working fine with it, 
00120      and there doesn't seem to be any memory leak (wrt abolishing
00121      subsumptive tables), but as I don't know the code, I'd feel
00122      better if somebody who did looked at it.       -- lfcastro */
00123 
00124   if ( TSTN_Child(root) && IsHashHeader(TSTN_Child(root)) ) {
00125     hash_hdr = TSTN_GetHashHdr(root);
00126     for ( i = 0;  i < TSTHT_NumBuckets(hash_hdr);  i++ )
00127       for ( current = TSTHT_BucketArray(hash_hdr)[i];
00128             IsNonNULL(current);  current = sibling ) {
00129         sibling = TSTN_Sibling(current);
00130         delete_tst_answer_set(CTXTc current);
00131       }
00132     delete_private_tstht(CTXTc hash_hdr);
00133   }
00134   else if ( ! IsLeafNode(root) )
00135     for ( current = TSTN_Child(root);  IsNonNULL(current);
00136           current = sibling ) {
00137       sibling = TSTN_Sibling(current);
00138       delete_tst_answer_set(CTXTc current);
00139     }
00140   free_private_tstn(CTXTc root);
00141 }
00142 
00143 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
00144 
00145 /*
00146  *  Delete the given Call Table node and recursively all of its
00147  * children.  
00148  * 
00149  * TLS: subsumptive tables apparently are using BTNs, so make sure
00150  * that you delete from the correct SM.
00151  */
00152 
00153 void delete_call_index(CTXTdeclc BTNptr root) {
00154 
00155   BTNptr current, sibling;
00156   BTHTptr hash_hdr;
00157   unsigned int i;
00158 
00159   if ( IsNULL(root) )
00160     return;
00161 
00162   if ( ! IsLeafNode(root) ) {
00163     if ( IsHashHeader(BTN_Child(root)) ) {
00164 
00165       hash_hdr = BTN_GetHashHdr(root);
00166       for ( i = 0;  i < BTHT_NumBuckets(hash_hdr);  i++ )
00167         for ( current = BTHT_BucketArray(hash_hdr)[i];
00168               IsNonNULL(current);  current = sibling ) {
00169           sibling = BTN_Sibling(current);
00170           delete_call_index(CTXTc current);
00171         }
00172       delete_private_btht(CTXTc hash_hdr);
00173     }
00174     else 
00175       for ( current = BTN_Child(root);  IsNonNULL(current);
00176             current = sibling ) {
00177         sibling = BTN_Sibling(current);
00178         delete_call_index(CTXTc current);
00179       }
00180   }
00181   SM_DeallocateStruct(subsumptive_smBTN,root);
00182 }
00183 
00184 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
00185 
00186 void delete_subsumptive_table(CTXTdeclc TIFptr tif) {
00187 
00188   SubProdSF cur_prod, next_prod;
00189   SubConsSF cur_cons, next_cons;
00190 
00191   for ( cur_prod = (SubProdSF)TIF_Subgoals(tif);
00192         IsNonNULL(cur_prod);  cur_prod = next_prod ) {
00193     for ( cur_cons = subg_consumers(cur_prod);
00194           IsNonNULL(cur_cons);  cur_cons = next_cons ) {
00195       next_cons = conssf_consumers(cur_cons);
00196       delete_private_sf(CTXTc (VariantSF)cur_cons);
00197     }
00198     next_prod = (SubProdSF)subg_next_subgoal(cur_prod);
00199     delete_tst_answer_set(CTXTc (TSTNptr)subg_ans_root_ptr(cur_prod));
00200     delete_private_sf(CTXTc (VariantSF)cur_prod);
00201   }
00202   delete_call_index(CTXTc TIF_CallTrie(tif));
00203 }
00204 
00205 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
00206 
00207 /* TLS: use this when table has been deleted (via an abolish), but
00208    space for it has not been reclaimed.  In this case, we get the
00209    access points from the DelTF rather than the TIF. For now, called
00210    only when 1 active thread.
00211 
00212    Input types reflect those of the TIFs */
00213 
00214 void reclaim_deleted_subsumptive_table(CTXTdeclc DelTFptr deltf_ptr) {
00215 
00216   SubProdSF cur_prod, next_prod;
00217   SubConsSF cur_cons, next_cons;
00218 
00219   for ( cur_prod = (SubProdSF) DTF_Subgoals(deltf_ptr);
00220         IsNonNULL(cur_prod);  cur_prod = next_prod ) {
00221     for ( cur_cons = subg_consumers(cur_prod);
00222           IsNonNULL(cur_cons);  cur_cons = next_cons ) {
00223       next_cons = conssf_consumers(cur_cons);
00224       delete_private_sf(CTXTc (VariantSF)cur_cons);
00225     }
00226     next_prod = (SubProdSF)subg_next_subgoal(cur_prod);
00227     delete_tst_answer_set(CTXTc (TSTNptr)subg_ans_root_ptr(cur_prod));
00228     delete_private_sf(CTXTc (VariantSF)cur_prod);
00229   }
00230   delete_call_index(CTXTc DTF_CallTrie(deltf_ptr));
00231 }
00232 

Generated on Wed Jul 26 13:30:42 2006 for XSB by  doxygen 1.4.5