Debian Codegraph @

src/c/alloc_2.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * alloc_2.c - memory allocation based on the Boehm GC
6 *
7 * Copyright (c) 2001 Juan Jose Garcia Ripoll
8 *
9 * See file 'LICENSE' for the copyright details.
10 *
11 */
12
13 #include <stdio.h>
14 #include <ecl/ecl.h>
15 #ifdef ECL_THREADS
16 # ifdef ECL_WINDOWS_THREADS
17 # include <windows.h>
18 # else
19 # include <pthread.h>
20 # endif
21 #endif
22 #include <ecl/ecl-inl.h>
23 #include <ecl/internal.h>
24 #include <ecl/page.h>
25 #ifdef ECL_WSOCK
26 #include <winsock.h>
27 #endif
28
29 #ifdef GBC_BOEHM
30 #include <gc/gc_mark.h>
31
32 static void (*GC_old_start_callback)(void) = NULL;
33 static void gather_statistics(void);
34 static void update_bytes_consed(void);
35 static void ecl_mark_env(struct cl_env_struct *env);
36
37 #ifdef GBC_BOEHM_PRECISE
38 # if GBC_BOEHM
39 # undef GBC_BOEHM_PRECISE
40 # else
41 # include <gc/gc_typed.h>
42 # define GBC_BOEHM_OWN_MARKER
43 static int cl_object_kind, cl_object_mark_proc_index;
44 static void **cl_object_free_list;
45 # endif
46 #endif
47
48 /**********************************************************
49 * OBJECT ALLOCATION *
50 **********************************************************/
51
52 void
53 _ecl_set_max_heap_size(size_t new_size)
54 {
55 const cl_env_ptr the_env = ecl_process_env();
56 ecl_disable_interrupts_env(the_env);
57 GC_set_max_heap_size(cl_core.max_heap_size = new_size);
58 if (new_size == 0) {
59 cl_index size = ecl_option_values[ECL_OPT_HEAP_SAFETY_AREA];
60 cl_core.safety_region = ecl_alloc_atomic_unprotected(size);
61 } else if (cl_core.safety_region) {
62 GC_FREE(cl_core.safety_region);
63 cl_core.safety_region = 0;
64 }
65 ecl_enable_interrupts_env(the_env);
66 }
67
68 static int failure;
69 static void *
70 out_of_memory_check(size_t requested_bytes)
71 {
72 failure = 1;
73 return 0;
74 }
75
76 static void
77 no_warnings(char *msg, GC_word arg)
78 {
79 }
80
81 static void *
82 out_of_memory(size_t requested_bytes)
83 {
84 const cl_env_ptr the_env = ecl_process_env();
85 int interrupts = the_env->disable_interrupts;
86 int method = 0;
87 void *output;
88 /* Disable interrupts only with the ext::*interrupts-enabled*
89 * mechanism to allow for writes in the thread local environment */
90 if (interrupts)
91 ecl_enable_interrupts_env(the_env);
92 ecl_bds_bind(the_env, @'ext::*interrupts-enabled*', ECL_NIL);
93 /* Free the input / output buffers */
94 the_env->string_pool = ECL_NIL;
95
96 /* The out of memory condition may happen in more than one thread */
97 /* But then we have to ensure the error has not been solved */
98 #ifdef ECL_THREADS
99 ecl_mutex_lock(&cl_core.error_lock);
100 ECL_UNWIND_PROTECT_BEGIN(the_env)
101 #endif
102 {
103 failure = 0;
104 GC_gcollect();
105 GC_set_oom_fn(out_of_memory_check);
106 {
107 output = GC_MALLOC(requested_bytes);
108 GC_set_oom_fn(out_of_memory);
109 if (output != 0 && failure == 0) {
110 method = 2;
111 goto OUTPUT;
112 }
113 }
114 if (cl_core.max_heap_size == 0) {
115 /* We did not set any limit in the amount of memory,
116 * yet we failed, or we had some limits but we have
117 * not reached them. */
118 if (cl_core.safety_region) {
119 /* We can free some memory and try handling the error */
120 GC_FREE(cl_core.safety_region);
121 the_env->string_pool = ECL_NIL;
122 cl_core.safety_region = 0;
123 method = 0;
124 } else {
125 /* No possibility of continuing */
126 method = 2;
127 }
128 } else {
129 cl_core.max_heap_size += ecl_option_values[ECL_OPT_HEAP_SAFETY_AREA];
130 GC_set_max_heap_size(cl_core.max_heap_size);
131 method = 1;
132 }
133 OUTPUT:
134 (void)0;
135 }
136 #ifdef ECL_THREADS
137 ECL_UNWIND_PROTECT_EXIT {
138 ecl_mutex_unlock(&cl_core.error_lock);
139 } ECL_UNWIND_PROTECT_END;
140 #endif
141 ecl_bds_unwind1(the_env);
142 ecl_check_pending_interrupts(the_env);
143 switch (method) {
144 case 0: cl_error(1, @'ext::storage-exhausted');
145 break;
146 case 1: cl_cerror(2, @"Extend heap size",
147 @'ext::storage-exhausted');
148 break;
149 case 2:
150 return output;
151 default:
152 ecl_internal_error("Memory exhausted, quitting program.");
153 break;
154 }
155 if (!interrupts)
156 ecl_disable_interrupts_env(the_env);
157 GC_set_max_heap_size(cl_core.max_heap_size +=
158 cl_core.max_heap_size / 2);
159 /* Default allocation. Note that we do not allocate atomic. */
160 return GC_MALLOC(requested_bytes);
161 }
162
163 static struct ecl_type_information {
164 size_t size;
165 #ifdef GBC_BOEHM_PRECISE
166 GC_word descriptor;
167 #endif
168 cl_object (*allocator)(struct ecl_type_information *);
169 size_t t;
170 } type_info[t_end];
171
172 #ifdef GBC_BOEHM_PRECISE
173 static void
174 error_wrong_tag(cl_type t)
175 {
176 ecl_internal_error("Collector called with invalid tag number.");
177 }
178 #endif
179
180 cl_index
181 ecl_object_byte_size(cl_type t)
182 {
183 if (t == t_fixnum || t == t_character)
184 FEerror("ecl_object_byte_size invoked with an immediate type ~D",
185 1, ecl_make_fixnum(1));
186 if (t >= t_end)
187 FEerror("ecl_object_byte_size invoked with an unknown type ~D",
188 1, ecl_make_fixnum(1));
189 return type_info[t].size;
190 }
191
192 static cl_object
193 allocate_object_atomic(struct ecl_type_information *type_info)
194 {
195 const cl_env_ptr the_env = ecl_process_env();
196 cl_object op;
197 ecl_disable_interrupts_env(the_env);
198 op = GC_MALLOC_ATOMIC(type_info->size);
199 op->d.t = type_info->t;
200 ecl_enable_interrupts_env(the_env);
201 return op;
202 }
203
204 static cl_object
205 allocate_object_full(struct ecl_type_information *type_info)
206 {
207 const cl_env_ptr the_env = ecl_process_env();
208 cl_object op;
209 ecl_disable_interrupts_env(the_env);
210 op = GC_MALLOC(type_info->size);
211 op->d.t = type_info->t;
212 ecl_enable_interrupts_env(the_env);
213 return op;
214 }
215
216 #ifdef GBC_BOEHM_PRECISE
217 static cl_object
218 allocate_object_typed(struct ecl_type_information *type_info)
219 {
220 const cl_env_ptr the_env = ecl_process_env();
221 cl_object op;
222 ecl_disable_interrupts_env(the_env);
223 op = GC_malloc_explicitly_typed(type_info->size, type_info->descriptor);
224 op->d.t = type_info->t;
225 ecl_enable_interrupts_env(the_env);
226 return op;
227 }
228 #endif
229
230 #ifdef GBC_BOEHM_OWN_MARKER
231
232 static struct GC_ms_entry *
233 cl_object_mark_proc(void *addr, struct GC_ms_entry *msp, struct GC_ms_entry *msl,
234 GC_word env)
235 {
236 cl_type t = ((cl_object)addr)->d.t;
237 if (ecl_likely(t > t_start && t < t_end)) {
238 struct ecl_type_information *info = type_info + t;
239 GC_word d = info->descriptor;
240 GC_word *p;
241 for (p = addr; d; p++, d<<=1) {
242 if ((GC_signed_word)d < 0) {
243 GC_word aux = *p;
244 if ((aux & 2) ||
245 aux <= (GC_word)GC_least_plausible_heap_addr ||
246 aux >= (GC_word)GC_greatest_plausible_heap_addr)
247 continue;
248 msp = GC_mark_and_push((void*)aux, (void*)msp,
249 (void*)msl, (void*)p);
250 }
251 }
252 }
253 return msp;
254 }
255
256 static cl_object
257 allocate_object_marked(struct ecl_type_information *type_info)
258 {
259 const cl_env_ptr the_env = ecl_process_env();
260 cl_object op;
261 ecl_disable_interrupts_env(the_env);
262 op = GC_generic_malloc(type_info->size, cl_object_kind);
263 op->d.t = type_info->t;
264 ecl_enable_interrupts_env(the_env);
265 return op;
266 }
267 #endif
268
269 cl_object
270 ecl_alloc_object(cl_type t)
271 {
272 #ifdef GBC_BOEHM_PRECISE
273 struct ecl_type_information *ti;
274 if (ecl_likely(t > t_start && t < t_end)) {
275 ti = type_info + t;
276 return ti->allocator(ti);
277 }
278 error_wrong_tag(t);
279 return OBJNULL;
280 #else
281 const cl_env_ptr the_env = ecl_process_env();
282
283 /* GC_MALLOC already resets objects */
284 switch (t) {
285 case t_fixnum:
286 return ecl_make_fixnum(0); /* Immediate fixnum */
287 case t_character:
288 return ECL_CODE_CHAR(' '); /* Immediate character */
289 #ifdef ECL_SSE2
290 case t_sse_pack:
291 #endif
292 case t_longfloat:
293 #ifdef ECL_COMPLEX_FLOAT
294 case t_csfloat:
295 case t_cdfloat:
296 case t_clfloat:
297 #endif
298 case t_singlefloat:
299 case t_doublefloat: {
300 cl_object obj;
301 ecl_disable_interrupts_env(the_env);
302 obj = (cl_object)GC_MALLOC_ATOMIC(type_info[t].size);
303 ecl_enable_interrupts_env(the_env);
304 obj->d.t = t;
305 return obj;
306 }
307 case t_bignum:
308 case t_ratio:
309 case t_complex:
310 case t_symbol:
311 case t_package:
312 case t_hashtable:
313 case t_array:
314 case t_vector:
315 case t_base_string:
316 #ifdef ECL_UNICODE
317 case t_string:
318 #endif
319 case t_bitvector:
320 case t_stream:
321 case t_random:
322 case t_readtable:
323 case t_pathname:
324 case t_bytecodes:
325 case t_bclosure:
326 case t_cfun:
327 case t_cfunfixed:
328 case t_cclosure:
329 case t_instance:
330 #ifdef ECL_THREADS
331 case t_process:
332 case t_lock:
333 case t_rwlock:
334 case t_condition_variable:
335 case t_semaphore:
336 case t_barrier:
337 case t_mailbox:
338 #endif
339 case t_foreign:
340 case t_codeblock: {
341 cl_object obj;
342 ecl_disable_interrupts_env(the_env);
343 obj = (cl_object)GC_MALLOC(type_info[t].size);
344 ecl_enable_interrupts_env(the_env);
345 obj->d.t = t;
346 return obj;
347 }
348 default:
349 printf("\ttype = %d\n", t);
350 ecl_internal_error("alloc botch.");
351 }
352 #endif
353 }
354
355 cl_object
356 ecl_alloc_compact_object(cl_type t, cl_index extra_space)
357 {
358 const cl_env_ptr the_env = ecl_process_env();
359 cl_index size = type_info[t].size;
360 cl_object x;
361 ecl_disable_interrupts_env(the_env);
362 x = (cl_object)GC_MALLOC_ATOMIC(size + extra_space);
363 ecl_enable_interrupts_env(the_env);
364 x->array.t = t;
365 x->array.displaced = (void*)(((char*)x) + size);
366 return x;
367 }
368
369 cl_object
370 ecl_cons(cl_object a, cl_object d)
371 {
372 const cl_env_ptr the_env = ecl_process_env();
373 struct ecl_cons *obj;
374 ecl_disable_interrupts_env(the_env);
375 obj = GC_MALLOC(sizeof(struct ecl_cons));
376 ecl_enable_interrupts_env(the_env);
377 #ifdef ECL_SMALL_CONS
378 obj->car = a;
379 obj->cdr = d;
380 return ECL_PTR_CONS(obj);
381 #else
382 obj->t = t_list;
383 obj->car = a;
384 obj->cdr = d;
385 return (cl_object)obj;
386 #endif
387 }
388
389 cl_object
390 ecl_alloc_instance(cl_index slots)
391 {
392 cl_object i;
393 i = ecl_alloc_object(t_instance);
394 i->instance.slots = (cl_object *)ecl_alloc(sizeof(cl_object) * slots);
395 i->instance.length = slots;
396 i->instance.entry = FEnot_funcallable_vararg;
397 i->instance.slotds = ECL_UNBOUND;
398 return i;
399 }
400
401 static cl_index stamp = 0;
402 cl_index ecl_next_stamp() {
403 #if ECL_THREADS
404 return AO_fetch_and_add((AO_t*)&stamp, 1) + 1;
405 #else
406 return ++stamp;
407 #endif
408 }
409
410 void *
411 ecl_alloc_uncollectable(size_t size)
412 {
413 const cl_env_ptr the_env = ecl_process_env();
414 void *output;
415 ecl_disable_interrupts_env(the_env);
416 output = GC_MALLOC_UNCOLLECTABLE(size);
417 ecl_enable_interrupts_env(the_env);
418 return output;
419 }
420
421 void
422 ecl_free_uncollectable(void *pointer)
423 {
424 const cl_env_ptr the_env = ecl_process_env();
425 ecl_disable_interrupts_env(the_env);
426 GC_FREE(pointer);
427 ecl_enable_interrupts_env(the_env);
428 }
429
430 void *
431 ecl_alloc_unprotected(cl_index n)
432 {
433 return GC_MALLOC_IGNORE_OFF_PAGE(n);
434 }
435
436 void *
437 ecl_alloc_atomic_unprotected(cl_index n)
438 {
439 return GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(n);
440 }
441
442 void *
443 ecl_alloc(cl_index n)
444 {
445 const cl_env_ptr the_env = ecl_process_env();
446 void *output;
447 ecl_disable_interrupts_env(the_env);
448 output = ecl_alloc_unprotected(n);
449 ecl_enable_interrupts_env(the_env);
450 return output;
451 }
452
453 void *
454 ecl_alloc_atomic(cl_index n)
455 {
456 const cl_env_ptr the_env = ecl_process_env();
457 void *output;
458 ecl_disable_interrupts_env(the_env);
459 output = ecl_alloc_atomic_unprotected(n);
460 ecl_enable_interrupts_env(the_env);
461 return output;
462 }
463
464 void
465 ecl_dealloc(void *ptr)
466 {
467 const cl_env_ptr the_env = ecl_process_env();
468 ecl_disable_interrupts_env(the_env);
469 GC_FREE(ptr);
470 ecl_enable_interrupts_env(the_env);
471 }
472
473 #ifdef GBC_BOEHM_PRECISE
474 static cl_index
475 to_bitmap(void *x, void *y)
476 {
477 cl_index n = (char*)y - (char*)x;
478 if (n % sizeof(void*))
479 ecl_internal_error("Misaligned pointer in ECL structure.");
480 n /= sizeof(void*);
481 return 1 << n;
482 }
483 #endif
484
485 void init_type_info (void)
486 {
487 #ifdef GBC_BOEHM_PRECISE
488 union cl_lispunion o;
489 struct ecl_cons c;
490 #endif
491 int i;
492 #define init_tm(/* cl_type */ type, \
493 /* char* */ name, \
494 /* cl_index */ object_size, \
495 /* cl_index */ maxpage) { \
496 type_info[type].size = (object_size); \
497 if ((maxpage) == 0) { \
498 type_info[type].allocator = allocate_object_atomic; \
499 } \
500 }
501 for (i = 0; i < t_end; i++) {
502 type_info[i].t = i;
503 type_info[i].size = 0;
504 type_info[i].allocator = allocate_object_full;
505 }
506 init_tm(t_list, "CONS", sizeof(struct ecl_cons), 2);
507 init_tm(t_bignum, "BIGNUM", sizeof(struct ecl_bignum), 2);
508 init_tm(t_ratio, "RATIO", sizeof(struct ecl_ratio), 2);
509 init_tm(t_singlefloat, "SINGLE-FLOAT", sizeof(struct ecl_singlefloat), 0);
510 init_tm(t_doublefloat, "DOUBLE-FLOAT", sizeof(struct ecl_doublefloat), 0);
511 init_tm(t_longfloat, "LONG-FLOAT", sizeof(struct ecl_long_float), 0);
512 init_tm(t_complex, "COMPLEX", sizeof(struct ecl_complex), 2);
513 #ifdef ECL_COMPLEX_FLOAT
514 init_tm(t_csfloat, "COMPLEX-SINGLE-FLOAT", sizeof(struct ecl_csfloat), 0);
515 init_tm(t_cdfloat, "COMPLEX-DOUBLE-FLOAT", sizeof(struct ecl_cdfloat), 0);
516 init_tm(t_clfloat, "COMPLEX-LONG-FLOAT", sizeof(struct ecl_clfloat), 0);
517 #endif
518 init_tm(t_symbol, "SYMBOL", sizeof(struct ecl_symbol), 5);
519 init_tm(t_package, "PACKAGE", sizeof(struct ecl_package), -1); /* 36 */
520 #ifdef ECL_THREADS
521 init_tm(t_hashtable, "HASH-TABLE", sizeof(struct ecl_hashtable), 3);
522 #else
523 init_tm(t_hashtable, "HASH-TABLE", sizeof(struct ecl_hashtable), 4);
524 #endif
525 init_tm(t_array, "ARRAY", sizeof(struct ecl_array), 3);
526 init_tm(t_vector, "VECTOR", sizeof(struct ecl_vector), 2);
527 #ifdef ECL_UNICODE
528 init_tm(t_string, "STRING", sizeof(struct ecl_string), 2);
529 #endif
530 init_tm(t_base_string, "BASE-STRING", sizeof(struct ecl_base_string), 2);
531 init_tm(t_bitvector, "BIT-VECTOR", sizeof(struct ecl_vector), 2);
532 init_tm(t_stream, "STREAM", sizeof(struct ecl_stream), 6);
533 init_tm(t_random, "RANDOM-STATE", sizeof(struct ecl_random), -1);
534 init_tm(t_readtable, "READTABLE", sizeof(struct ecl_readtable), 2);
535 init_tm(t_pathname, "PATHNAME", sizeof(struct ecl_pathname), -1);
536 init_tm(t_bytecodes, "BYTECODES", sizeof(struct ecl_bytecodes), -1);
537 init_tm(t_bclosure, "BCLOSURE", sizeof(struct ecl_bclosure), 3);
538 init_tm(t_cfun, "CFUN", sizeof(struct ecl_cfun), -1);
539 init_tm(t_cfunfixed, "CFUNFIXED", sizeof(struct ecl_cfunfixed), -1);
540 init_tm(t_cclosure, "CCLOSURE", sizeof(struct ecl_cclosure), -1);
541 init_tm(t_instance, "INSTANCE", sizeof(struct ecl_instance), 4);
542 #ifdef ECL_THREADS
543 init_tm(t_process, "PROCESS", sizeof(struct ecl_process), 8);
544 init_tm(t_lock, "LOCK", sizeof(struct ecl_lock), 2);
545 init_tm(t_rwlock, "RWLOCK", sizeof(struct ecl_rwlock), 0);
546 init_tm(t_condition_variable, "CONDITION-VARIABLE",
547 sizeof(struct ecl_condition_variable), 0);
548 init_tm(t_semaphore, "SEMAPHORES", sizeof(struct ecl_semaphore), 0);
549 init_tm(t_barrier, "BARRIER", sizeof(struct ecl_barrier), 0);
550 init_tm(t_mailbox, "MAILBOX", sizeof(struct ecl_mailbox), 0);
551 #endif
552 init_tm(t_codeblock, "CODEBLOCK", sizeof(struct ecl_codeblock), -1);
553 init_tm(t_foreign, "FOREIGN", sizeof(struct ecl_foreign), 2);
554 init_tm(t_frame, "STACK-FRAME", sizeof(struct ecl_stack_frame), 0);
555 init_tm(t_weak_pointer, "WEAK-POINTER", sizeof(struct ecl_weak_pointer), 0);
556 #ifdef ECL_SSE2
557 init_tm(t_sse_pack, "SSE-PACK", sizeof(struct ecl_sse_pack), 0);
558 #endif
559 #ifdef GBC_BOEHM_PRECISE
560 type_info[t_list].descriptor =
561 to_bitmap(&c, &(c.car)) |
562 to_bitmap(&c, &(c.cdr));
563 type_info[t_bignum].descriptor =
564 to_bitmap(&o, &(ECL_BIGNUM_LIMBS(&o)));
565 type_info[t_ratio].descriptor =
566 to_bitmap(&o, &(o.ratio.num)) |
567 to_bitmap(&o, &(o.ratio.den));
568 type_info[t_singlefloat].descriptor = 0;
569 type_info[t_doublefloat].descriptor = 0;
570 type_info[t_longfloat].descriptor = 0;
571 type_info[t_complex].descriptor =
572 to_bitmap(&o, &(o.gencomplex.real)) |
573 to_bitmap(&o, &(o.gencomplex.imag));
574 #ifdef ECL_COMPLEX_FLOAT
575 type_info[t_csfloat].descriptor = 0;
576 type_info[t_cdfloat].descriptor = 0;
577 type_info[t_clfloat].descriptor = 0;
578 #endif
579 type_info[t_symbol].descriptor =
580 to_bitmap(&o, &(o.symbol.value)) |
581 to_bitmap(&o, &(o.symbol.gfdef)) |
582 to_bitmap(&o, &(o.symbol.macfun)) |
583 to_bitmap(&o, &(o.symbol.sfdef)) |
584 to_bitmap(&o, &(o.symbol.plist)) |
585 to_bitmap(&o, &(o.symbol.name)) |
586 to_bitmap(&o, &(o.symbol.hpack));
587 type_info[t_package].descriptor =
588 to_bitmap(&o, &(o.pack.name)) |
589 to_bitmap(&o, &(o.pack.nicknames)) |
590 to_bitmap(&o, &(o.pack.local_nicknames)) |
591 to_bitmap(&o, &(o.pack.nicknamedby)) |
592 to_bitmap(&o, &(o.pack.shadowings)) |
593 to_bitmap(&o, &(o.pack.uses)) |
594 to_bitmap(&o, &(o.pack.usedby)) |
595 to_bitmap(&o, &(o.pack.internal)) |
596 to_bitmap(&o, &(o.pack.external));
597 type_info[t_hashtable].descriptor =
598 to_bitmap(&o, &(o.hash.data)) |
599 to_bitmap(&o, &(o.hash.sync_lock)) |
600 to_bitmap(&o, &(o.hash.generic_test)) |
601 to_bitmap(&o, &(o.hash.generic_hash)) |
602 to_bitmap(&o, &(o.hash.rehash_size)) |
603 to_bitmap(&o, &(o.hash.threshold));
604 type_info[t_array].descriptor =
605 to_bitmap(&o, &(o.array.dims)) |
606 to_bitmap(&o, &(o.array.self.t)) |
607 to_bitmap(&o, &(o.array.displaced));
608 type_info[t_vector].descriptor =
609 to_bitmap(&o, &(o.vector.self.t)) |
610 to_bitmap(&o, &(o.vector.displaced));
611 # ifdef ECL_UNICODE
612 type_info[t_string].descriptor =
613 to_bitmap(&o, &(o.string.self)) |
614 to_bitmap(&o, &(o.string.displaced));
615 # endif
616 type_info[t_base_string].descriptor =
617 to_bitmap(&o, &(o.base_string.self)) |
618 to_bitmap(&o, &(o.base_string.displaced));
619 type_info[t_bitvector].descriptor =
620 to_bitmap(&o, &(o.vector.self.t)) |
621 to_bitmap(&o, &(o.vector.displaced));
622 type_info[t_stream].descriptor =
623 to_bitmap(&o, &(o.stream.ops)) |
624 to_bitmap(&o, &(o.stream.object0)) |
625 to_bitmap(&o, &(o.stream.object1)) |
626 to_bitmap(&o, &(o.stream.last_byte)) |
627 to_bitmap(&o, &(o.stream.byte_stack)) |
628 to_bitmap(&o, &(o.stream.buffer)) |
629 to_bitmap(&o, &(o.stream.format)) |
630 to_bitmap(&o, &(o.stream.format_table));
631 type_info[t_random].descriptor =
632 to_bitmap(&o, &(o.random.value));
633 type_info[t_readtable].descriptor =
634 # ifdef ECL_UNICODE
635 to_bitmap(&o, &(o.readtable.hash)) |
636 # endif
637 to_bitmap(&o, &(o.readtable.table));
638 type_info[t_pathname].descriptor =
639 to_bitmap(&o, &(o.pathname.version)) |
640 to_bitmap(&o, &(o.pathname.type)) |
641 to_bitmap(&o, &(o.pathname.name)) |
642 to_bitmap(&o, &(o.pathname.directory)) |
643 to_bitmap(&o, &(o.pathname.device)) |
644 to_bitmap(&o, &(o.pathname.host));
645 type_info[t_bytecodes].descriptor =
646 to_bitmap(&o, &(o.bytecodes.name)) |
647 to_bitmap(&o, &(o.bytecodes.definition)) |
648 to_bitmap(&o, &(o.bytecodes.code)) |
649 to_bitmap(&o, &(o.bytecodes.data)) |
650 to_bitmap(&o, &(o.bytecodes.flex)) |
651 to_bitmap(&o, &(o.bytecodes.file)) |
652 to_bitmap(&o, &(o.bytecodes.file_position));
653 type_info[t_bclosure].descriptor =
654 to_bitmap(&o, &(o.bclosure.code)) |
655 to_bitmap(&o, &(o.bclosure.lex));
656 type_info[t_cfun].descriptor =
657 to_bitmap(&o, &(o.cfun.name)) |
658 to_bitmap(&o, &(o.cfun.block)) |
659 to_bitmap(&o, &(o.cfun.file)) |
660 to_bitmap(&o, &(o.cfun.file_position));
661 type_info[t_cfunfixed].descriptor =
662 to_bitmap(&o, &(o.cfunfixed.name)) |
663 to_bitmap(&o, &(o.cfunfixed.block)) |
664 to_bitmap(&o, &(o.cfunfixed.file)) |
665 to_bitmap(&o, &(o.cfunfixed.file_position));
666 type_info[t_cclosure].descriptor =
667 to_bitmap(&o, &(o.cclosure.env)) |
668 to_bitmap(&o, &(o.cclosure.block)) |
669 to_bitmap(&o, &(o.cclosure.file)) |
670 to_bitmap(&o, &(o.cclosure.file_position));
671 type_info[t_instance].descriptor =
672 to_bitmap(&o, &(o.instance.clas)) |
673 to_bitmap(&o, &(o.instance.slotds)) |
674 to_bitmap(&o, &(o.instance.slots));
675 # ifdef ECL_THREADS
676 type_info[t_process].descriptor =
677 to_bitmap(&o, &(o.process.name)) |
678 to_bitmap(&o, &(o.process.function)) |
679 to_bitmap(&o, &(o.process.args)) |
680 to_bitmap(&o, &(o.process.interrupt)) |
681 to_bitmap(&o, &(o.process.inherit_bindings_p)) |
682 to_bitmap(&o, &(o.process.parent)) |
683 to_bitmap(&o, &(o.process.exit_values)) |
684 to_bitmap(&o, &(o.process.woken_up)) |
685 to_bitmap(&o, &(o.process.env));
686 type_info[t_lock].descriptor =
687 to_bitmap(&o, &(o.lock.name)) |
688 to_bitmap(&o, &(o.lock.owner));
689 type_info[t_rwlock].descriptor =
690 to_bitmap(&o, &(o.rwlock.name));
691 type_info[t_condition_variable].descriptor = 0;
692 type_info[t_semaphore].descriptor =
693 to_bitmap(&o, &(o.semaphore.name));
694 type_info[t_barrier].descriptor =
695 to_bitmap(&o, &(o.barrier.name));
696 type_info[t_mailbox].descriptor =
697 to_bitmap(&o, &(o.mailbox.name)) |
698 to_bitmap(&o, &(o.mailbox.data));
699 # endif
700 type_info[t_codeblock].descriptor =
701 to_bitmap(&o, &(o.cblock.data)) |
702 to_bitmap(&o, &(o.cblock.temp_data)) |
703 to_bitmap(&o, &(o.cblock.next)) |
704 to_bitmap(&o, &(o.cblock.name)) |
705 to_bitmap(&o, &(o.cblock.links)) |
706 to_bitmap(&o, &(o.cblock.source)) |
707 to_bitmap(&o, &(o.cblock.refs)) |
708 to_bitmap(&o, &(o.cblock.error));
709 type_info[t_foreign].descriptor =
710 to_bitmap(&o, &(o.foreign.data)) |
711 to_bitmap(&o, &(o.foreign.tag));
712 type_info[t_frame].descriptor =
713 to_bitmap(&o, &(o.frame.env));
714 type_info[t_weak_pointer].descriptor = 0;
715 #ifdef ECL_SSE2
716 type_info[t_sse_pack].descriptor = 0;
717 #endif
718 for (i = 0; i < t_end; i++) {
719 GC_word descriptor = type_info[i].descriptor;
720 int bits = type_info[i].size / sizeof(GC_word);
721 if (descriptor) {
722 #ifdef GBC_BOEHM_OWN_MARKER
723 type_info[i].allocator = allocate_object_marked;
724 descriptor = GC_make_descriptor(&descriptor, bits);
725 descriptor &= ~GC_DS_TAGS;
726 #else
727 GC_word mask = (1 << (bits-1)) - 1;
728 mask ^= (descriptor >> 1);
729 if (mask == 0)
730 type_info[i].allocator = allocate_object_full;
731 else
732 type_info[i].allocator = allocate_object_typed;
733 descriptor = GC_make_descriptor(&descriptor, bits);
734 #endif
735 } else {
736 type_info[i].allocator = allocate_object_atomic;
737 descriptor = 0;
738 }
739 type_info[i].descriptor = descriptor;
740 }
741 #endif /* GBC_BOEHM_PRECISE */
742 }
743
744 extern void (*GC_push_other_roots)();
745 static void (*old_GC_push_other_roots)();
746 static void stacks_scanner();
747
748 void
749 init_alloc(int pass)
750 {
751 if (pass == 1) {
752 GC_enable();
753 return;
754 }
755 /*
756 * Garbage collector restrictions: we set up the garbage collector
757 * library to work as follows
758 *
759 * 1) The garbage collector shall not scan shared libraries
760 * explicitely.
761 * 2) We only detect objects that are referenced by a pointer to
762 * the begining or to the first byte.
763 * 3) Out of the incremental garbage collector, we only use the
764 * generational component.
765 * 4) GC should handle fork() which is used to run subprocess on
766 * some platforms.
767 */
768 GC_set_no_dls(1);
769 GC_set_all_interior_pointers(0);
770 GC_set_time_limit(GC_TIME_UNLIMITED);
771 #ifndef ECL_MS_WINDOWS_HOST
772 GC_set_handle_fork(1);
773 #endif
774 GC_init();
775 #ifdef ECL_THREADS
776 # if GC_VERSION_MAJOR > 7 || GC_VERSION_MINOR > 1
777 GC_allow_register_threads();
778 # endif
779 #endif
780 if (ecl_option_values[ECL_OPT_INCREMENTAL_GC]) {
781 GC_enable_incremental();
782 }
783 GC_register_displacement(1);
784 GC_clear_roots();
785 GC_disable();
786
787 #ifdef GBC_BOEHM_PRECISE
788 # ifdef GBC_BOEHM_OWN_MARKER
789 cl_object_free_list = (void **)GC_new_free_list_inner();
790 cl_object_mark_proc_index = GC_new_proc((GC_mark_proc)cl_object_mark_proc);
791 cl_object_kind = GC_new_kind_inner(cl_object_free_list,
792 GC_MAKE_PROC(cl_object_mark_proc_index, 0),
793 FALSE, TRUE);
794 # endif
795 #endif /* !GBC_BOEHM_PRECISE */
796
797 GC_set_max_heap_size(cl_core.max_heap_size = ecl_option_values[ECL_OPT_HEAP_SIZE]);
798 /* Save some memory for the case we get tight. */
799 if (cl_core.max_heap_size == 0) {
800 cl_index size = ecl_option_values[ECL_OPT_HEAP_SAFETY_AREA];
801 cl_core.safety_region = ecl_alloc_atomic_unprotected(size);
802 } else if (cl_core.safety_region) {
803 cl_core.safety_region = 0;
804 }
805
806 init_type_info();
807
808 old_GC_push_other_roots = GC_push_other_roots;
809 GC_push_other_roots = stacks_scanner;
810 GC_old_start_callback = GC_get_start_callback();
811 GC_set_start_callback(gather_statistics);
812 GC_set_java_finalization(1);
813 GC_set_oom_fn(out_of_memory);
814 GC_set_warn_proc(no_warnings);
815 }
816
817 /**********************************************************
818 * FINALIZATION *
819 **********************************************************/
820
821 static void
822 standard_finalizer(cl_object o)
823 {
824 switch (o->d.t) {
825 #ifdef ENABLE_DLOPEN
826 case t_codeblock:
827 ecl_library_close(o);
828 break;
829 #endif
830 case t_stream:
831 cl_close(1, o);
832 break;
833 case t_weak_pointer:
834 GC_unregister_disappearing_link((void**)&(o->weak.value));
835 break;
836 #ifdef ECL_THREADS
837 case t_lock: {
838 const cl_env_ptr the_env = ecl_process_env();
839 ecl_disable_interrupts_env(the_env);
840 ecl_mutex_destroy(&o->lock.mutex);
841 ecl_enable_interrupts_env(the_env);
842 break;
843 }
844 case t_condition_variable: {
845 const cl_env_ptr the_env = ecl_process_env();
846 ecl_disable_interrupts_env(the_env);
847 ecl_cond_var_destroy(&o->condition_variable.cv);
848 ecl_enable_interrupts_env(the_env);
849 break;
850 }
851 case t_barrier: {
852 const cl_env_ptr the_env = ecl_process_env();
853 ecl_disable_interrupts_env(the_env);
854 ecl_mutex_destroy(&o->barrier.mutex);
855 ecl_cond_var_destroy(&o->barrier.cv);
856 ecl_enable_interrupts_env(the_env);
857 break;
858 }
859 case t_semaphore: {
860 const cl_env_ptr the_env = ecl_process_env();
861 ecl_disable_interrupts_env(the_env);
862 ecl_mutex_destroy(&o->semaphore.mutex);
863 ecl_cond_var_destroy(&o->semaphore.cv);
864 ecl_enable_interrupts_env(the_env);
865 break;
866 }
867 case t_mailbox: {
868 const cl_env_ptr the_env = ecl_process_env();
869 ecl_disable_interrupts_env(the_env);
870 ecl_mutex_destroy(&o->mailbox.mutex);
871 ecl_cond_var_destroy(&o->mailbox.reader_cv);
872 ecl_cond_var_destroy(&o->mailbox.writer_cv);
873 ecl_enable_interrupts_env(the_env);
874 break;
875 }
876 case t_rwlock: {
877 const cl_env_ptr the_env = ecl_process_env();
878 ecl_disable_interrupts_env(the_env);
879 ecl_rwlock_destroy(&o->rwlock.mutex);
880 ecl_enable_interrupts_env(the_env);
881 break;
882 }
883 case t_process: {
884 const cl_env_ptr the_env = ecl_process_env();
885 ecl_disable_interrupts_env(the_env);
886 ecl_mutex_destroy(&o->process.start_stop_lock);
887 ecl_cond_var_destroy(&o->process.exit_barrier);
888 ecl_enable_interrupts_env(the_env);
889 break;
890 }
891 case t_symbol: {
892 if (o->symbol.binding != ECL_MISSING_SPECIAL_BINDING) {
893 ecl_atomic_push(&cl_core.reused_indices, ecl_make_fixnum(o->symbol.binding));
894 o->symbol.binding = ECL_MISSING_SPECIAL_BINDING;
895 }
896 }
897 #endif /* ECL_THREADS */
898 default:;
899 }
900 }
901
902 static void
903 wrapped_finalizer(cl_object o, cl_object finalizer);
904
905 static void
906 register_finalizer(cl_object o, void *finalized_object,
907 GC_finalization_proc fn, void *cd,
908 GC_finalization_proc *ofn, void **ocd)
909 {
910 /* Finalizers for some builtin objects are only run when the object is not
911 * reachable by any means, including through other finalizers which might
912 * make the object reachable again. The objects must not contain any cyclic
913 * references for which finalizers are registered.
914 *
915 * We don't use this type of finalizer for user-defined finalizers, because
916 * those might contain cyclic references which would prevent the objects
917 * from being garbage collected. It is instead the duty of the user to write
918 * the finalizers in a consistent way.
919 *
920 * case t_symbol: is not finalized with the "unreachable" finalizer because
921 * it might contain cyclic references; Also running the finalizer too early
922 * doesn't lead to any problems, we will simply choose a new binding index
923 * the next time a binding is established. */
924 switch (o->d.t) {
925 #ifdef ENABLE_DLOPEN
926 case t_codeblock:
927 #endif
928 case t_stream:
929 #if defined(ECL_THREADS)
930 case t_lock:
931 case t_condition_variable:
932 case t_barrier:
933 case t_semaphore:
934 case t_mailbox:
935 case t_rwlock:
936 case t_process:
937 #endif /* ECL_THREADS */
938 /* Don't delete the standard finalizer. */
939 if (fn == NULL) {
940 fn = (GC_finalization_proc)wrapped_finalizer;
941 cd = ECL_T;
942 }
943 GC_REGISTER_FINALIZER_UNREACHABLE(finalized_object, fn, cd, ofn, ocd);
944 break;
945 case t_weak_pointer:
946 #if defined(ECL_THREADS)
947 case t_symbol:
948 #endif
949 /* Don't delete the standard finalizer. */
950 if (fn == NULL) {
951 fn = (GC_finalization_proc)wrapped_finalizer;
952 cd = ECL_T;
953 }
954 /* fallthrough */
955 default:
956 GC_REGISTER_FINALIZER_NO_ORDER(finalized_object, fn, cd, ofn, ocd);
957 break;
958 }
959 }
960
961 static void
962 deferred_finalizer(cl_object* x)
963 {
964 wrapped_finalizer(x[0], x[1]);
965 }
966
967 static void
968 wrapped_finalizer(cl_object o, cl_object finalizer)
969 {
970 if (finalizer != ECL_NIL && finalizer != NULL) {
971 #ifdef ECL_THREADS
972 const cl_env_ptr the_env = ecl_process_env_unsafe();
973 if (!the_env
974 || !the_env->own_process
975 || the_env->own_process->process.phase < ECL_PROCESS_ACTIVE)
976 {
977 /*
978 * The finalizer is invoked while we are registering or setup a
979 * new lisp process. As example that may happen when we are
980 * doing ecl_import_current_thread. That mean the finalizer
981 * can not be executed right now, so in some way we need to
982 * queue the finalization. When we return from this function
983 * the original finalizer is no more registered to o, and if o
984 * is not anymore reachable it will be colleted. To prevent
985 * this we need to make this object reachable again after that
986 * roundtrip and postpone the finalization to the next garbage
987 * collection. Given that this is a rare condition one way to
988 * do that is:
989 */
990 GC_finalization_proc ofn;
991 void *odata;
992 cl_object* wrapper = GC_MALLOC(2*sizeof(cl_object));
993 wrapper[0] = o;
994 wrapper[1] = finalizer;
995
996 register_finalizer(o, wrapper,
997 (GC_finalization_proc)deferred_finalizer, 0,
998 &ofn, &odata);
999 return;
1000 }
1001 #endif /* ECL_THREADS */
1002 CL_NEWENV_BEGIN {
1003 if (finalizer != ECL_T) {
1004 funcall(2, finalizer, o);
1005 }
1006 standard_finalizer(o);
1007 } CL_NEWENV_END;
1008 }
1009 }
1010
1011 cl_object
1012 si_get_finalizer(cl_object o)
1013 {
1014 const cl_env_ptr the_env = ecl_process_env();
1015 cl_object output;
1016 GC_finalization_proc ofn;
1017 void *odata;
1018 ecl_disable_interrupts_env(the_env);
1019 register_finalizer(o, o, (GC_finalization_proc)0, 0, &ofn, &odata);
1020 if (ofn == 0) {
1021 output = ECL_NIL;
1022 } else if (ofn == (GC_finalization_proc)wrapped_finalizer) {
1023 output = (cl_object)odata;
1024 } else {
1025 output = ECL_NIL;
1026 }
1027 register_finalizer(o, o, ofn, odata, &ofn, &odata);
1028 ecl_enable_interrupts_env(the_env);
1029 @(return output);
1030 }
1031
1032 void
1033 ecl_set_finalizer_unprotected(cl_object o, cl_object finalizer)
1034 {
1035 GC_finalization_proc ofn;
1036 void *odata;
1037 if (finalizer == ECL_NIL) {
1038 register_finalizer(o, o, (GC_finalization_proc)0, 0, &ofn, &odata);
1039 } else {
1040 GC_finalization_proc newfn;
1041 newfn = (GC_finalization_proc)wrapped_finalizer;
1042 register_finalizer(o, o, newfn, finalizer, &ofn, &odata);
1043 }
1044 }
1045
1046 cl_object
1047 si_set_finalizer(cl_object o, cl_object finalizer)
1048 {
1049 const cl_env_ptr the_env = ecl_process_env();
1050 ecl_disable_interrupts_env(the_env);
1051 ecl_set_finalizer_unprotected(o, finalizer);
1052 ecl_enable_interrupts_env(the_env);
1053 @(return);
1054 }
1055
1056 /* If we do not build our own version of the library, we do not have
1057 * control over the existence of this variable. */
1058 #if GBC_BOEHM == 0
1059 extern int GC_print_stats;
1060 #else
1061 static int GC_print_stats;
1062 #endif
1063
1064 cl_object
1065 si_gc_stats(cl_object enable)
1066 {
1067 cl_object old_status;
1068 cl_object size1;
1069 cl_object size2;
1070 if (cl_core.gc_stats == 0) {
1071 old_status = ECL_NIL;
1072 } else if (GC_print_stats) {
1073 old_status = @':full';
1074 } else {
1075 old_status = ECL_T;
1076 }
1077 if (cl_core.bytes_consed == ECL_NIL) {
1078 cl_core.bytes_consed = ecl_alloc_object(t_bignum);
1079 mpz_init2(ecl_bignum(cl_core.bytes_consed), 128);
1080 cl_core.gc_counter = ecl_alloc_object(t_bignum);
1081 mpz_init2(ecl_bignum(cl_core.gc_counter), 128);
1082 }
1083
1084 update_bytes_consed();
1085 /* We need fresh copies of the bignums */
1086 size1 = _ecl_big_register_copy(cl_core.bytes_consed);
1087 size2 = _ecl_big_register_copy(cl_core.gc_counter);
1088
1089 if (enable == ECL_NIL) {
1090 GC_print_stats = 0;
1091 cl_core.gc_stats = 0;
1092 } else if (enable == ecl_make_fixnum(0)) {
1093 mpz_set_ui(ecl_bignum(cl_core.bytes_consed), 0);
1094 mpz_set_ui(ecl_bignum(cl_core.gc_counter), 0);
1095 } else {
1096 cl_core.gc_stats = 1;
1097 GC_print_stats = (enable == @':full');
1098 }
1099 @(return size1 size2 old_status);
1100 }
1101
1102 /* This procedure is invoked after garbage collection. Note that we
1103 * cannot cons because this procedure is invoked with the garbage
1104 * collection lock on. */
1105 static void
1106 gather_statistics()
1107 {
1108 /* GC stats rely on bignums */
1109 if (cl_core.gc_stats) {
1110 update_bytes_consed();
1111 mpz_add_ui(ecl_bignum(cl_core.gc_counter),
1112 ecl_bignum(cl_core.gc_counter),
1113 1);
1114 }
1115 if (GC_old_start_callback)
1116 GC_old_start_callback();
1117 }
1118
1119 static void
1120 update_bytes_consed () {
1121 #if GBC_BOEHM == 0
1122 mpz_add_ui(ecl_bignum(cl_core.bytes_consed),
1123 ecl_bignum(cl_core.bytes_consed),
1124 GC_get_bytes_since_gc());
1125 #else
1126 /* This is not accurate and may wrap around. We try to detect this
1127 assuming that an overflow in an unsigned integer will produce
1128 a smaller integer.*/
1129 static cl_index bytes = 0;
1130 cl_index new_bytes = GC_get_total_bytes();
1131 if (bytes > new_bytes) {
1132 cl_index wrapped;
1133 wrapped = ~((cl_index)0) - bytes;
1134 mpz_add_ui(ecl_bignum(cl_core.bytes_consed),
1135 ecl_bignum(cl_core.bytes_consed),
1136 wrapped);
1137 mpz_add_ui(ecl_bignum(cl_core.bytes_consed),
1138 ecl_bignum(cl_core.bytes_consed),
1139 new_bytes);
1140 } else {
1141 mpz_add_ui(ecl_bignum(cl_core.bytes_consed),
1142 ecl_bignum(cl_core.bytes_consed),
1143 new_bytes - bytes);
1144 }
1145 bytes = new_bytes;
1146 #endif
1147 }
1148
1149 /**********************************************************
1150 * GARBAGE COLLECTOR *
1151 **********************************************************/
1152
1153 static void
1154 ecl_mark_env(struct cl_env_struct *env)
1155 {
1156 /* Environments and stacks are allocated without GC */
1157 if (env->run_stack.org)
1158 GC_push_all((void *)env->run_stack.org, (void *)env->run_stack.top);
1159 if (env->frs_stack.org)
1160 GC_push_all((void *)env->frs_stack.org, (void *)(env->frs_stack.top+1));
1161 if (env->bds_stack.org)
1162 GC_push_all((void *)env->bds_stack.org, (void *)(env->bds_stack.top+1));
1163 #ifdef ECL_THREADS
1164 if (env->bds_stack.tl_bindings)
1165 GC_push_all((void *)env->bds_stack.tl_bindings,
1166 (void *)(env->bds_stack.tl_bindings + env->bds_stack.tl_bindings_size));
1167 #endif
1168 GC_push_all((void *)env, (void *)(env + 1));
1169 }
1170
1171 static void
1172 stacks_scanner()
1173 {
1174 cl_object l = cl_core.libraries;
1175 loop_for_on_unsafe(l) {
1176 cl_object dll = ECL_CONS_CAR(l);
1177 if (dll->cblock.locked) {
1178 GC_push_conditional((void *)dll, (void *)(&dll->cblock + 1), 1);
1179 GC_set_mark_bit((void *)dll);
1180 }
1181 } end_loop_for_on_unsafe(l);
1182 GC_push_all((void *)(&cl_core), (void *)(&cl_core + 1));
1183 GC_push_all((void *)cl_symbols, (void *)(cl_symbols + cl_num_symbols_in_core));
1184 ecl_mark_env(cl_core.first_env);
1185 #ifdef ECL_THREADS
1186 l = cl_core.processes;
1187 if (l != OBJNULL) {
1188 cl_index i, size;
1189 for (i = 0, size = l->vector.dim; i < size; i++) {
1190 cl_object process = l->vector.self.t[i];
1191 if (!Null(process)) {
1192 cl_env_ptr env = process->process.env;
1193 if (env && (env != cl_core.first_env)) ecl_mark_env(env);
1194 }
1195 }
1196 }
1197 #endif
1198 if (old_GC_push_other_roots)
1199 (*old_GC_push_other_roots)();
1200 }
1201
1202 /**********************************************************
1203 * GARBAGE COLLECTION *
1204 **********************************************************/
1205
1206 void
1207 ecl_register_root(cl_object *p)
1208 {
1209 const cl_env_ptr the_env = ecl_process_env();
1210 ecl_disable_interrupts_env(the_env);
1211 GC_add_roots((char*)p, (char*)(p+1));
1212 ecl_enable_interrupts_env(the_env);
1213 }
1214
1215 cl_object
1216 si_gc(cl_narg narg, ...)
1217 {
1218 const cl_env_ptr the_env = ecl_process_env();
1219 ecl_disable_interrupts_env(the_env);
1220 GC_gcollect();
1221 ecl_enable_interrupts_env(the_env);
1222 @(return);
1223 }
1224
1225 cl_object
1226 si_gc_dump()
1227 {
1228 const cl_env_ptr the_env = ecl_process_env();
1229 ecl_disable_interrupts_env(the_env);
1230 GC_dump();
1231 ecl_enable_interrupts_env(the_env);
1232 @(return);
1233 }
1234
1235 /**********************************************************************
1236 * WEAK POINTERS
1237 */
1238
1239 cl_object
1240 ecl_alloc_weak_pointer(cl_object o)
1241 {
1242 const cl_env_ptr the_env = ecl_process_env();
1243 struct ecl_weak_pointer *obj;
1244 ecl_disable_interrupts_env(the_env);
1245 obj = GC_MALLOC_ATOMIC(sizeof(struct ecl_weak_pointer));
1246 ecl_enable_interrupts_env(the_env);
1247 obj->t = t_weak_pointer;
1248 obj->value = o;
1249 if (!ECL_IMMEDIATE(o)) {
1250 GC_GENERAL_REGISTER_DISAPPEARING_LINK((void**)&(obj->value), (void*)o);
1251 si_set_finalizer((cl_object)obj, ECL_T);
1252 }
1253 return (cl_object)obj;
1254 }
1255
1256 static cl_object
1257 ecl_weak_pointer_value(cl_object o)
1258 {
1259 return ecl_weak_pointer(o);
1260 }
1261
1262 cl_object
1263 si_make_weak_pointer(cl_object o)
1264 {
1265 cl_object pointer = ecl_alloc_weak_pointer(o);
1266 @(return pointer);
1267 }
1268
1269 cl_object
1270 si_weak_pointer_value(cl_object o)
1271 {
1272 const cl_env_ptr the_env = ecl_process_env();
1273 cl_object value;
1274 if (ecl_unlikely(ecl_t_of(o) != t_weak_pointer))
1275 FEwrong_type_only_arg(@[ext::weak-pointer-value], o,
1276 @[ext::weak-pointer]);
1277 value = (cl_object)GC_call_with_alloc_lock((GC_fn_type)ecl_weak_pointer_value, o);
1278 if (value) {
1279 ecl_return2(the_env, value, ECL_T);
1280 } else {
1281 ecl_return2(the_env, ECL_NIL, ECL_NIL);
1282 }
1283 }
1284
1285 #endif /* GBC_BOEHM */