storage_xsb.c

00001 /* File:      storage_xsb.c  -- support for the storage.P module
00002 ** Author(s): Michael Kifer
00003 ** Contact:   xsb-contact@cs.sunysb.edu
00004 ** 
00005 ** Copyright (C) The Research Foundation of SUNY, 2001,2002
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: storage_xsb.c,v 1.9 2005/09/16 00:56:41 tswift Exp $
00022 ** 
00023 */
00024 
00025 #include "xsb_config.h"
00026 #include "xsb_debug.h"
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 
00031 #include "auxlry.h"
00032 #include "cell_xsb.h"
00033 #include "psc_xsb.h"
00034 #include "cinterf.h"
00035 #include "trie_internals.h"
00036 #include "macro_xsb.h"
00037 #include "error_xsb.h"
00038 #include "tr_utils.h"
00039 #include "storage_xsb.h"
00040 
00041 #include "debug_xsb.h"
00042 #include "flags_xsb.h"
00043 #include "context.h"
00044 
00045 /* this func would insert handle into hashtable, if it isn't there */
00046 #define find_or_insert_storage_handle(name)  \
00047             (STORAGE_HANDLE *)search_bucket(name,&bt_storage_hash_table,hashtable_insert)
00048 #define destroy_storage_handle(name) \
00049             search_bucket(name,&bt_storage_hash_table,hashtable_delete)
00050 #define show_table_state()    show_table_state(&bt_storage_hash_table)
00051 
00052 static STORAGE_HANDLE        *increment_storage_snapshot(CTXTdeclc Cell name);
00053 static STORAGE_HANDLE        *mark_storage_changed(CTXTdeclc Cell name);
00054 
00055 #ifndef MULTI_THREAD
00056 xsbHashTable bt_storage_hash_table =
00057   {STORAGE_TBL_SIZE,sizeof(STORAGE_HANDLE),FALSE,NULL};
00058 #endif
00059 
00060 static inline STORAGE_HANDLE *get_storage_handle(CTXTdeclc Cell name)
00061 {
00062   STORAGE_HANDLE *handle_cell;
00063 
00064   handle_cell = find_or_insert_storage_handle(name);
00065   /* new buckets are filled out with 0's by the calloc in hashtable_xsb.c */
00066   if (handle_cell->handle==(Cell)0) {
00067     /* initialize new handle */
00068     xsb_dbgmsg((LOG_STORAGE,
00069                "GET_STORAGE_HANDLE: New trie created for %s\n", 
00070                string_val(name)));
00071     handle_cell->handle= newtrie(CTXT);
00072     /* Note: not necessary to initialize snapshot_number&changed: handle_cell
00073        was calloc()'ed 
00074        handle_cell->snapshot_number=0;
00075        handle_cell->changed=FALSE;
00076     */
00077   }
00078   else
00079     xsb_dbgmsg((LOG_STORAGE,
00080                "GET_STORAGE_HANDLE: Using existing trie for %s\n",
00081                string_val(name)));
00082   return handle_cell;
00083 }
00084 
00085 STORAGE_HANDLE *storage_builtin(CTXTdeclc int builtin_number, Cell name)
00086 {
00087   switch (builtin_number) {
00088   case GET_STORAGE_HANDLE:
00089     return get_storage_handle(CTXTc name);
00090   case INCREMENT_STORAGE_SNAPSHOT:
00091     return increment_storage_snapshot(CTXTc name);
00092   case MARK_STORAGE_CHANGED:
00093     return mark_storage_changed(CTXTc name);
00094   case DESTROY_STORAGE_HANDLE: {
00095     xsb_dbgmsg((LOG_STORAGE,
00096                "STORAGE_BUILTIN: Destroying storage handle for %s\n",
00097                string_val(name)));
00098     destroy_storage_handle(name);
00099     return NULL;
00100   }
00101   case SHOW_TABLE_STATE: {
00102     show_table_state();
00103     return NULL;
00104   }
00105   default:
00106     xsb_abort("Unknown storage builtin");
00107     return NULL;
00108   }
00109 }
00110 
00111 
00112 static STORAGE_HANDLE *increment_storage_snapshot(CTXTdeclc Cell name)
00113 {
00114   STORAGE_HANDLE *ptr = find_or_insert_storage_handle(name);
00115   ptr->snapshot_number++;
00116   ptr->changed = FALSE;
00117   return ptr;
00118 }
00119 
00120 static STORAGE_HANDLE *mark_storage_changed(CTXTdeclc Cell name)
00121 {
00122   STORAGE_HANDLE *ptr = find_or_insert_storage_handle(name);
00123   ptr->changed = TRUE;
00124   return ptr;
00125 }

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