forked from SWI-Prolog/swipl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpl-dbref.c
309 lines (226 loc) · 6.27 KB
/
pl-dbref.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2010-2019, VU University Amsterdam
CWI, Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "pl-incl.h"
#include "pl-dbref.h"
#include "pl-proc.h"
#include "pl-rec.h"
#include "pl-fli.h"
typedef struct recref
{ RecordRef record;
} recref;
static int
write_clause_ref(IOSTREAM *s, atom_t aref, int flags)
{ ClauseRef ref = PL_blob_data(aref, NULL, NULL);
(void)flags;
Sfprintf(s, "<clause>(%p)", ref->value.clause);
return TRUE;
}
static void
acquire_clause_blob(atom_t aref)
{ ClauseRef ref = PL_blob_data(aref, NULL, NULL);
set(ref->value.clause, DBREF_CLAUSE);
}
static int
release_clause_blob(atom_t aref)
{ ClauseRef ref = PL_blob_data(aref, NULL, NULL);
clear(ref->value.clause, DBREF_CLAUSE);
if ( true(ref->value.clause, CL_ERASED) )
ATOMIC_DEC(&GD->clauses.db_erased_refs);
if ( true(ref->value.clause, DBREF_ERASED_CLAUSE) )
unallocClause(ref->value.clause);
return TRUE;
}
static int
save_clause_ref(atom_t aref, IOSTREAM *fd)
{ ClauseRef ref = PL_blob_data(aref, NULL, NULL);
(void)fd;
return PL_warning("Cannot save reference to <clause>(%p)",
ref->value.clause);
}
static atom_t
load_clause_ref(IOSTREAM *fd)
{ (void)fd;
return PL_new_atom("<saved-clause-ref>");
}
static PL_blob_t clause_blob =
{ PL_BLOB_MAGIC,
PL_BLOB_UNIQUE,
"clause",
release_clause_blob,
NULL,
write_clause_ref,
acquire_clause_blob,
save_clause_ref,
load_clause_ref
};
static int
write_record_ref(IOSTREAM *s, atom_t aref, int flags)
{ recref *ref = PL_blob_data(aref, NULL, NULL);
Sfprintf(s, "<record>(%p)", ref->record);
return TRUE;
}
static void
acquire_record(atom_t aref)
{ recref *ref = PL_blob_data(aref, NULL, NULL);
set(ref->record->record, R_DBREF);
}
static int
release_record(atom_t aref)
{ recref *ref = PL_blob_data(aref, NULL, NULL);
if ( ref->record->record )
clear(ref->record->record, R_DBREF);
else
unallocRecordRef(ref->record);
return TRUE;
}
static PL_blob_t record_blob =
{ PL_BLOB_MAGIC,
PL_BLOB_UNIQUE,
"record",
release_record,
NULL,
write_record_ref,
acquire_record
};
atom_t
lookup_clref(Clause clause)
{ clause_ref ref;
int new;
DEBUG(0,
{ GET_LD
assert(!onStackArea(local, clause));
});
ref.next = NULL;
ref.d.key = 0;
ref.value.clause = clause;
return lookupBlob((const char*)&ref, SIZEOF_CREF_CLAUSE, &clause_blob, &new);
}
/* Note that the clause may be erased */
ClauseRef
clause_clref(atom_t aref)
{ PL_blob_t *type;
ClauseRef ref = PL_blob_data(aref, NULL, &type);
if ( type == &clause_blob )
return ref;
return NULL;
}
int
PL_unify_clref(term_t t, Clause clause)
{ GET_LD
atom_t a = lookup_clref(clause);
int rc = _PL_unify_atomic(t, a);
PL_unregister_atom(a);
return rc;
}
int
PL_put_clref(term_t t, Clause clause)
{ atom_t a = lookup_clref(clause);
_PL_put_atomic(t, a);
PL_unregister_atom(a);
return TRUE;
}
int
PL_unify_recref(term_t t, RecordRef r)
{ struct recref ref;
ref.record = r;
return PL_unify_blob(t, &ref, sizeof(ref), &record_blob);
}
int
PL_is_dbref(term_t t)
{ PL_blob_t *type;
if ( PL_is_blob(t, &type) &&
( type == &clause_blob ||
type == &record_blob ) )
return TRUE;
return FALSE;
}
void *
PL_get_dbref(term_t t, db_ref_type *type_ptr)
{ void *data;
PL_blob_t *type;
if ( !PL_get_blob(t, &data, NULL, &type) )
{ error:
PL_error(NULL, 0, NULL, ERR_TYPE, ATOM_db_reference, t);
return NULL;
}
if ( type == &clause_blob )
{ ClauseRef ref = data;
if ( false(ref->value.clause, CL_ERASED) )
{ *type_ptr = DB_REF_CLAUSE;
return ref;
}
} else if ( type == &record_blob )
{ recref *ref = data;
if ( ref->record->record &&
false(ref->record->record, R_ERASED) )
{ *type_ptr = DB_REF_RECORD;
return ref->record;
}
} else
{ goto error;
}
return NULL;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Returns FALSE: error
TRUE: existing clause
-1: erased clause
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
int
PL_get_clref(term_t t, Clause *cl)
{ ClauseRef ref;
PL_blob_t *type;
if ( !PL_get_blob(t, (void**)&ref, NULL, &type) ||
type != &clause_blob )
return PL_error(NULL, 0, NULL, ERR_TYPE, ATOM_db_reference, t);
*cl = ref->value.clause;
if ( true(ref->value.clause, CL_ERASED) )
return -1;
return TRUE;
}
int
PL_get_recref(term_t t, RecordRef *rec)
{ struct recref *ref;
PL_blob_t *type;
if ( !PL_get_blob(t, (void**)&ref, NULL, &type) ||
type != &record_blob )
return PL_error(NULL, 0, NULL, ERR_TYPE, ATOM_db_reference, t);
if ( ref->record->record &&
false(ref->record->record, R_ERASED) )
{ *rec = ref->record;
return TRUE;
}
return FALSE;
}
void
initDBRef(void)
{ PL_register_blob_type(&record_blob);
PL_register_blob_type(&clause_blob);
}