00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
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
00066 if (handle_cell->handle==(Cell)0) {
00067
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
00073
00074
00075
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 }