forked from ldmud/ldmud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.h
415 lines (354 loc) · 14.5 KB
/
object.h
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#ifndef OBJECT_H__
#define OBJECT_H__ 1
#include "driver.h"
#include "typedefs.h"
#include "main.h"
#include "mstrings.h"
#include "sent.h" /* O_GET_* */
#ifdef DEBUG
#include <stdio.h> /* printf() for refcount tracing */
#endif
/* --- Types --- */
/* --- struct object: the base structure of every object
*/
struct object_s
{
unsigned short flags; /* Bits or'ed together, see below */
p_int ref; /* Reference count. */
mp_int time_reset; /* Time of next reset, or 0 if none */
mp_int time_of_ref; /* Time when last referenced. Used by swap */
mp_int time_cleanup; /* Time when the next variable cleanup is due. */
mp_int load_time; /* Time when the object was created. */
p_int load_id; /* Load-ID within the time the object was created */
#ifdef DEBUG
p_int extra_ref; /* Used to check ref count. */
#endif
program_t *prog; /* Program code for this object */
string_t *name;
/* name of the object (untabled), always w/o leading '/' */
string_t *load_name;
/* name of the object's blueprint (tabled), in compat
* mode without leading '/'
*/
object_t *next_all; /* Next object in global list */
object_t *prev_all; /* Previous object in global list */
object_t *next_hash; /* Next object in chain in the otable */
object_t *next_inv; /* Next object in the current environment */
object_t *contains; /* First contained object */
object_t *super; /* Current environment */
sentence_t *sent; /* Sentences, shadows, interactive data */
wiz_list_t *user; /* What wizard defined this object */
wiz_list_t *eff_user; /* Effective user */
#ifdef DEBUG
int extra_num_variables;
/* amylaar : used to determine where to check ref counts at all... */
#endif
#ifdef USE_SQLITE
Bool open_sqlite_db; /* does this object have an open sqlite db? */
#endif
#ifdef USE_PYTHON
void* python_dict;
/* This is a PyObject*, but we don't wand to include the
* Python headers here.
*/
#endif
svalue_t *variables;
/* All variables to this object: an array of svalues, allocated
* in a separate block.
*/
unsigned long ticks, gigaticks;
/* Evalcost used by this object. The total cost
* is computed with gigaticks*1E9+ticks.
*/
};
/* Values of object_t.flags: */
#define O_HEART_BEAT 0x01 /* Does it have an heart beat? */
#define O_ENABLE_COMMANDS 0x04 /* Can it execute commands? */
#define O_CLONE 0x08 /* Is it cloned from a master copy? */
#define O_DESTRUCTED 0x10 /* Is it destructed ? */
#define O_SWAPPED 0x20 /* Is it swapped to file */
#define O_ONCE_INTERACTIVE 0x40 /* Has it ever been interactive? */
#define O_UNUSED_80 0x80
#define O_RESET_STATE 0x100 /* Object in a 'reset':ed state ? */
#define O_WILL_CLEAN_UP 0x200 /* clean_up will be called next time */
#define O_LAMBDA_REFERENCED 0x400 /* be careful with replace_program() */
#define O_SHADOW 0x800 /* Is the object shadowed/shadowing? */
#define O_REPLACED 0x1000 /* Was the program replaced? */
/* If an object's program or variables are swapped out, the values
* of .prog resp. .variables are replaced with the associated (even)
* swap number assigned by the swapper, and the lowest bit of the number
* is set. The swap number '-1' means 'not swapped'.
* TODO: This assumes that pointers are always even.
*/
#define P_PROG_SWAPPED(p) ((p_int)(p) & 1)
/* Is the program <p> swapped out?
*/
#define O_PROG_SWAPPED(ob) ((p_int)(ob)->prog & 1)
/* Is the program of <ob> swapped out?
*/
#define O_VAR_SWAPPED(ob) ((p_int)(ob)->variables & 1)
/* Are the variables of <ob> swapped out?
*/
#define O_SWAP_NUM(ob) \
(O_PROG_SWAPPED(ob) ? (p_int)(ob)->prog & ~1 : (ob)->prog->swap_num)
/* The swap number for the program of <ob>.
*/
#define O_IS_INTERACTIVE(o) \
(((o)->flags & O_SHADOW) && (NULL != O_GET_INTERACTIVE(o)) )
/* Bool O_IS_INTERACTIVE(object_t *o)
* Return TRUE if ob is an interactive object.
*/
#define O_SET_INTERACTIVE(ip,o) \
( ( ((o)->flags & O_SHADOW) && (NULL != (ip = O_GET_INTERACTIVE(o))) ) \
|| ( (ip = NULL), MY_FALSE ) )
/* Bool O_SET_INTERACTIVE(interactive_t *ip, object_t *o)
* Return TRUE if ob is an interactive object and set ip to the interactive
* structure. Return FALSE is not and clear ip.
*/
/* --- struct replace_ob_s: one scheduled program replacement
*
* A list of this structure (obj_list_replace) keeps track of all
* requested replace_program()s during one backend round.
*
* It is possible, though not very useful, to replace an object's
* program by the very same program.
*/
struct replace_ob_s
{
object_t *ob; /* Object requesting the new program */
program_t *new_prog; /* Requested new program */
int var_offset; /* Variable offset of .new_prog */
int fun_offset; /* Function offset of .new_prog */
replace_ob_t *next; /* Link pointer for list */
struct lambda_replace_program_protector *lambda_rpp;
/* Additional information about lambdas bound to the program
* after the replacement was scheduled. The exact information
* is private to closure.c.
*/
#ifdef USE_PYTHON
struct python_replace_program_protector *python_rpp;
/* Similar to lambdas above, this one is for Python. */
#endif
};
/* --- Variables --- */
extern replace_ob_t *obj_list_replace;
extern long tot_alloc_object;
extern long tot_alloc_object_size;
extern object_t NULL_object;
extern Bool dest_last_ref_gone;
/* --- Prototypes --- */
extern int32 renumber_programs(void);
extern void tell_object(object_t *, string_t *);
extern void tell_object_str(object_t *, const char *);
extern void tell_npc(object_t *, string_t *);
extern void tell_npc_str(object_t *, const char *);
extern void reference_prog(program_t *, char *);
#ifdef DEALLOCATE_MEMORY_AT_SHUTDOWN
extern void remove_all_objects(void);
#endif
extern void do_free_sub_strings(int num_strings, string_t ** strings
, int num_variables, variable_t *variables
, int num_includes, include_t *includes
,int num_structs, struct_def_t *struct_defs
);
#ifndef CHECK_OBJECT_REF
extern void free_prog(program_t *progp, Bool free_all);
#else
extern void _free_prog(program_t *progp, Bool free_all, const char * file, int line);
#endif
extern void reset_object(object_t *ob, int arg, int num_arg);
extern void logon_object (object_t *ob, p_int flag);
extern void replace_programs(void);
extern Bool shadow_catch_message(object_t *ob, const char *str);
#ifndef CHECK_OBJECT_REF
extern void dealloc_object(object_t *);
#else
extern void dealloc_object(object_t *, const char * file, int line);
#endif
extern object_t *get_empty_object(int num_var);
extern void init_object_variables (object_t *ob, object_t *templ);
extern svalue_t *v_function_exists(svalue_t *sp, int num_arg);
extern svalue_t *f_functionlist(svalue_t *sp);
extern svalue_t *v_variable_exists (svalue_t *sp, int num_arg);
extern svalue_t *f_variable_list (svalue_t *sp);
extern svalue_t *v_include_list (svalue_t *sp, int num_arg);
extern svalue_t *v_inherit_list(svalue_t *sp, int num_arg);
extern svalue_t *f_load_name(svalue_t *sp);
extern svalue_t *f_object_name(svalue_t *sp);
extern svalue_t *f_object_time(svalue_t *sp);
extern svalue_t *f_program_name(svalue_t *sp);
extern svalue_t *f_program_time(svalue_t *sp);
extern svalue_t *f_rename_object(svalue_t *sp);
extern svalue_t *v_replace_program(svalue_t *sp, int num_arg);
extern svalue_t *f_tell_object(svalue_t *sp);
extern svalue_t *f_set_next_reset(svalue_t *sp);
extern svalue_t *f_geteuid(svalue_t *sp);
extern svalue_t *f_getuid(svalue_t *sp);
extern svalue_t *v_all_environment(svalue_t *sp, int num_arg);
extern svalue_t *f_all_inventory(svalue_t *sp);
#if defined(USE_PARSE_COMMAND)
extern vector_t *deep_inventory(object_t *ob, Bool take_top, p_int depth);
/* needed by parse.c */
#endif
extern svalue_t *v_deep_inventory(svalue_t *sp, int num_arg);
extern svalue_t *v_environment(svalue_t *sp, int num_arg);
extern svalue_t *f_first_inventory(svalue_t *sp);
extern svalue_t *f_next_inventory(svalue_t *sp);
extern svalue_t *f_move_object (svalue_t *sp);
extern svalue_t *v_present(svalue_t *sp, int num_arg);
extern svalue_t *v_say(svalue_t *sp, int num_arg);
extern svalue_t *v_tell_room(svalue_t *sp, int num_arg);
extern svalue_t *f_set_environment(svalue_t *sp);
#ifdef USE_DEPRECATED
extern svalue_t *f_transfer(svalue_t *svp);
#endif
extern svalue_t *v_save_object(svalue_t *sp, int numarg);
extern svalue_t *v_save_value(svalue_t *sp, int numarg);
extern svalue_t *f_restore_object(svalue_t *sp);
extern svalue_t *f_restore_value(svalue_t *sp);
extern void free_save_object_buffers(void);
/* --- Inline functions and macros --- */
#ifndef DEBUG
static INLINE object_t *ref_object(object_t *const o, const char* const from) __attribute__((nonnull(1,2)));
static INLINE object_t *ref_object(object_t *const o, const char* const from)
/* Add another ref to object <o> from function <from>
* and return the object <o>.
*/
{
o->ref++;
return o;
}
#else
static INLINE object_t *_ref_object(object_t *const o, const char* const from, const char* const file, int const line) __attribute__((nonnull(1,2,3)));
static INLINE object_t *_ref_object(object_t *const o, const char* const from, const char* const file, int const line)
/* Add another ref to object <o> from function <from>
* and return the object <o>.
*/
{
o->ref++;
if (d_flag > 1)
printf("Add ref to object %s: %"PRIdPINT" (%s) %s %d\n"
, get_txt((o)->name), (o)->ref, from, file, line);
return o;
}
# define ref_object(o,from) _ref_object((o),(from), __FILE__, __LINE__)
#endif /* DEBUG */
static INLINE object_t *ref_valid_object(object_t *const o, const char* const from) __attribute__((nonnull(2)));
static INLINE object_t *ref_valid_object(object_t *const o, const char* const from)
/* Add another ref to object <o> from function <from> if <o>
* is not a destructed object or the NULL pointer.
* Return <o>, or NULL if <o> is destructed.
*/
{
if (o && !(o->flags & O_DESTRUCTED))
return ref_object(o,from);
return NULL;
}
#if !defined(DEBUG) && !defined(CHECK_OBJECT_REF)
static INLINE void free_object(object_t *const o, const char* const from) __attribute__((nonnull(1,2)));
static INLINE void free_object(object_t *const o, const char* const from)
/* Subtract one ref from object <o> from function <o>, and free the
* object fully if the refcount reaches zero.
*/
{
if (o->ref == 2)
dest_last_ref_gone = MY_TRUE;
if (--(o->ref) <= 0)
dealloc_object(o);
}
#else /* DEBUG || CHECK_OBJECT_REF */
static INLINE void _free_object(object_t *const o, const char* const from, const char* const file, int const line) __attribute__((nonnull(1,2,3)));
static INLINE void _free_object(object_t *const o, const char* const from, const char* const file, int const line)
/* Subtract one ref from object <o> from function <o>, and free the
* object fully if the refcount reaches zero.
*/
{
if (o->ref == 2)
dest_last_ref_gone = MY_TRUE;
o->ref--;
#ifdef DEBUG
if (d_flag > 1)
printf("Sub ref from object %s: %"PRIdPINT" (%s) %s %d\n"
, get_txt(o->name), o->ref, from, file, line);
#endif
if (o->ref <= 0)
#ifndef CHECK_OBJECT_REF
dealloc_object(o);
#else
dealloc_object(o, file, line);
#endif
}
# define free_object(o,from) _free_object((o),(from), __FILE__, __LINE__)
#endif
#ifndef DEBUG
static INLINE void deref_object(object_t *const o, const char* const from) __attribute__((nonnull(1,2)));
static INLINE void deref_object(object_t *const o, const char* const from)
/* Subtract one ref from object <o> from function <from>, but don't
* check if it needs to be freed.
*/
{
o->ref--;
}
#else
static INLINE void _deref_object(object_t *const o, const char* const from, const char* const file, int const line) __attribute__((nonnull(1,2,3)));
static INLINE void _deref_object(object_t *const o, const char* const from, const char* const file, int const line)
/* Subtract one ref from object <o> from function <from>, but don't
* check if it needs to be freed.
*/
{
o->ref--;
if (d_flag > 1)
printf("Sub ref from object %s: %"PRIdPINT" (%s)\n"
, get_txt((o)->name), (o)->ref, from);
}
# define deref_object(o,from) _deref_object(o, from, __FILE__, __LINE__)
#endif
static INLINE object_t* check_object(object_t *const o) __attribute__((pure));
static INLINE object_t* check_object(object_t *const o)
/* Return NULL, if object <o> is NULL or destructed,
* return <o> else.
*/
{
if (o && (o->flags&O_DESTRUCTED))
return NULL;
else
return o;
}
#ifdef CHECK_OBJECT_REF
#define free_prog(p,f) _free_prog(p,f, __FILE__, __LINE__)
#endif
static INLINE void put_ref_object(svalue_t * const dest, object_t * const obj, const char * const from)
__attribute__((nonnull(1,2,3)));
static INLINE void put_ref_object(svalue_t * const dest, object_t * const obj, const char * const from)
/* Put the object <obj> into <dest>, which is considered empty,
* and increment the refcount of <obj>.
*/
{
*dest = svalue_object(ref_object(obj, from));
}
static INLINE void put_valid_object(svalue_t * const dest, object_t * const obj)
__attribute__((nonnull(1)));
static INLINE void put_valid_object(svalue_t * const dest, object_t * const obj)
/* Put the object <obj> into <dest>, which is considered empty.
* If <obj> is a destructed object or NULL, put the number 0 there instead.
*/
{
if (obj && !(obj->flags & O_DESTRUCTED))
put_object(dest, obj);
else
put_number(dest, 0);
}
static INLINE void put_ref_valid_object(svalue_t * const dest, object_t * const obj, const char * const from)
__attribute__((nonnull(1,3)));
static INLINE void put_ref_valid_object(svalue_t * const dest, object_t * const obj, const char * const from)
/* Put the object <obj> into <dest>, which is considered empty,
* and increment the refcount of <obj>.
* If <obj> is a destructed object or NULL, put the number 0 there instead.
*/
{
if (obj && !(obj->flags & O_DESTRUCTED))
put_ref_object(dest, obj, from);
else
put_number(dest, 0);
}
#endif /* OBJECT_H__ */