2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
15 #define AO_SCHEME_CONST_BITS
17 #include "ao_scheme.h"
21 #ifdef AO_SCHEME_MAKE_CONST
24 * When building the constant table, it is the
25 * pool for allocations.
29 uint8_t ao_scheme_const[AO_SCHEME_POOL_CONST] __attribute__((aligned(4)));
30 #define ao_scheme_pool ao_scheme_const
32 #define AO_SCHEME_POOL AO_SCHEME_POOL_CONST
36 uint8_t ao_scheme_pool[AO_SCHEME_POOL + AO_SCHEME_POOL_EXTRA] __attribute__((aligned(4)));
41 #define DBG_MEM_STATS DBG_MEM
44 #define DBG_MEM_STACK 0
46 char *mem_collect_stack;
47 int64_t mem_collect_max_depth;
50 ao_scheme_check_stack(void)
55 depth = mem_collect_stack - &x;
56 if (depth > mem_collect_max_depth)
57 mem_collect_max_depth = depth;
61 _ao_scheme_reset_stack(char *x)
63 mem_collect_stack = x;
64 // mem_collect_max_depth = 0;
66 #define ao_scheme_declare_stack char x;
67 #define ao_scheme_reset_stack() _ao_scheme_reset_stack(&x)
69 #define ao_scheme_check_stack()
70 #define ao_scheme_declare_stack
71 #define ao_scheme_reset_stack()
75 #define DBG_MEM_RECORD 1
80 int dbg_mem = DBG_MEM_START;
85 struct ao_scheme_record {
86 struct ao_scheme_record *next;
87 const struct ao_scheme_type *type;
92 static struct ao_scheme_record *record_head, **record_tail;
95 ao_scheme_record_free(struct ao_scheme_record *record)
98 struct ao_scheme_record *next = record->next;
105 ao_scheme_record_reset(void)
107 ao_scheme_record_free(record_head);
109 record_tail = &record_head;
113 ao_scheme_record(const struct ao_scheme_type *type,
117 struct ao_scheme_record *r = malloc(sizeof (struct ao_scheme_record));
124 record_tail = &r->next;
127 static struct ao_scheme_record *
128 ao_scheme_record_save(void)
130 struct ao_scheme_record *r = record_head;
133 record_tail = &record_head;
138 ao_scheme_record_compare(const char *where,
139 struct ao_scheme_record *a,
140 struct ao_scheme_record *b)
143 if (a->type != b->type || a->size != b->size) {
144 printf("%s record difers %d %s %d -> %d %s %d\n",
146 MDBG_OFFSET(a->addr),
149 MDBG_OFFSET(b->addr),
158 printf("%s record differs %d %s %d -> NULL\n",
160 MDBG_OFFSET(a->addr),
166 printf("%s record differs NULL -> %d %s %d\n",
168 MDBG_OFFSET(b->addr),
176 #define ao_scheme_record_reset()
177 #define ao_scheme_record(t,a,s)
180 uint8_t ao_scheme_exception;
182 struct ao_scheme_root {
183 const struct ao_scheme_type *type;
187 #define AO_SCHEME_NUM_STASH 6
188 static ao_poly stash_poly[AO_SCHEME_NUM_STASH];
189 static int stash_poly_ptr;
191 static const struct ao_scheme_root ao_scheme_root[] = {
194 .addr = (void **) (void *) &stash_poly[0]
198 .addr = (void **) (void *) &stash_poly[1]
202 .addr = (void **) (void *) &stash_poly[2]
206 .addr = (void **) (void *) &stash_poly[3]
210 .addr = (void **) (void *) &stash_poly[4]
214 .addr = (void **) (void *) &stash_poly[5]
217 .type = &ao_scheme_frame_type,
218 .addr = (void **) &ao_scheme_frame_global,
221 .type = &ao_scheme_frame_type,
222 .addr = (void **) &ao_scheme_frame_current,
225 .type = &ao_scheme_stack_type,
226 .addr = (void **) &ao_scheme_stack,
230 .addr = (void **) (void *) &ao_scheme_v,
233 .type = &ao_scheme_cons_type,
234 .addr = (void **) &ao_scheme_read_cons,
237 .type = &ao_scheme_cons_type,
238 .addr = (void **) &ao_scheme_read_cons_tail,
241 .type = &ao_scheme_cons_type,
242 .addr = (void **) &ao_scheme_read_stack,
244 #ifdef AO_SCHEME_FEATURE_PORT
247 .addr = (void **) (void *) &ao_scheme_stdin,
251 .addr = (void **) (void *) &ao_scheme_stdout,
255 .addr = (void **) (void *) &ao_scheme_stderr,
258 #ifdef AO_SCHEME_MAKE_CONST
260 .type = &ao_scheme_bool_type,
261 .addr = (void **) &ao_scheme_false,
264 .type = &ao_scheme_bool_type,
265 .addr = (void **) &ao_scheme_true,
270 #define AO_SCHEME_ROOT (sizeof (ao_scheme_root) / sizeof (ao_scheme_root[0]))
272 static const void ** const ao_scheme_cache[] = {
273 (const void **) &ao_scheme_cons_free_list,
274 (const void **) &ao_scheme_stack_free_list,
275 (const void **) &ao_scheme_frame_free_list[0],
276 (const void **) &ao_scheme_frame_free_list[1],
277 (const void **) &ao_scheme_frame_free_list[2],
278 (const void **) &ao_scheme_frame_free_list[3],
279 (const void **) &ao_scheme_frame_free_list[4],
280 (const void **) &ao_scheme_frame_free_list[5],
283 #if AO_SCHEME_FRAME_FREE != 6
284 #error Unexpected AO_SCHEME_FRAME_FREE value
287 #define AO_SCHEME_CACHE (sizeof (ao_scheme_cache) / sizeof (ao_scheme_cache[0]))
289 #define AO_SCHEME_BUSY_SIZE ((AO_SCHEME_POOL + 31) / 32)
291 static int ao_scheme_printing, ao_scheme_print_cleared;
293 static int ao_scheme_collecting;
295 static uint8_t ao_scheme_busy[AO_SCHEME_BUSY_SIZE];
296 static uint8_t ao_scheme_cons_note[AO_SCHEME_BUSY_SIZE];
297 static uint8_t ao_scheme_cons_last[AO_SCHEME_BUSY_SIZE];
298 static uint8_t ao_scheme_cons_noted;
300 uint16_t ao_scheme_top;
302 struct ao_scheme_chunk {
310 #define AO_SCHEME_NCHUNK (AO_SCHEME_POOL / 64)
312 static struct ao_scheme_chunk ao_scheme_chunk[AO_SCHEME_NCHUNK];
314 /* Offset of an address within the pool. */
315 static inline uint16_t pool_offset(void *addr) {
317 if (!ao_scheme_is_pool_addr(addr))
320 return ((uint8_t *) addr) - ao_scheme_pool;
323 static inline void mark(uint8_t *tag, int offset) {
324 int byte = offset >> 5;
325 int bit = (offset >> 2) & 7;
326 ao_scheme_check_stack();
327 tag[byte] |= (1 << bit);
330 static inline void clear(uint8_t *tag, int offset) {
331 int byte = offset >> 5;
332 int bit = (offset >> 2) & 7;
333 tag[byte] &= ~(1 << bit);
336 static inline int busy(uint8_t *tag, int offset) {
337 int byte = offset >> 5;
338 int bit = (offset >> 2) & 7;
339 return (tag[byte] >> bit) & 1;
342 static inline int min(int a, int b) { return a < b ? a : b; }
343 static inline int max(int a, int b) { return a > b ? a : b; }
345 static inline int limit(int offset) {
346 return min(AO_SCHEME_POOL, max(offset, 0));
350 note_cons(uint16_t offset)
352 MDBG_MOVE("note cons %d\n", offset);
353 ao_scheme_cons_noted = 1;
354 mark(ao_scheme_cons_note, offset);
357 static uint16_t chunk_low, chunk_high;
358 static uint16_t chunk_first, chunk_last;
361 find_chunk(uint16_t offset)
364 /* Binary search for the location */
368 int m = (l + r) >> 1;
369 if (ao_scheme_chunk[m].old_offset < offset)
378 note_chunk(uint16_t offset, uint16_t size)
383 if (offset < chunk_low || chunk_high <= offset)
386 l = find_chunk(offset);
389 * The correct location is always in 'l', with r = l-1 being
390 * the entry before the right one
394 /* Off the right side */
395 if (l >= AO_SCHEME_NCHUNK)
398 /* Off the left side */
399 if (l == 0 && chunk_last && offset > ao_scheme_chunk[0].old_offset)
402 if (l < chunk_last && ao_scheme_chunk[l].old_offset == offset)
406 /* Shuffle existing entries right */
407 end = min(AO_SCHEME_NCHUNK, chunk_last + 1);
409 memmove(&ao_scheme_chunk[l+1],
411 (end - (l+1)) * sizeof (struct ao_scheme_chunk));
414 ao_scheme_chunk[l].old_offset = offset;
415 ao_scheme_chunk[l].size = size;
417 /* Increment the number of elements up to the size of the array */
418 if (chunk_last < AO_SCHEME_NCHUNK)
421 /* Set the top address if the array is full */
422 if (chunk_last == AO_SCHEME_NCHUNK)
423 chunk_high = ao_scheme_chunk[AO_SCHEME_NCHUNK-1].old_offset +
424 ao_scheme_chunk[AO_SCHEME_NCHUNK-1].size;
430 chunk_high = ao_scheme_top;
436 * Walk all referenced objects calling functions on each one
440 walk(int (*visit_addr)(const struct ao_scheme_type *type, void **addr),
441 int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
445 ao_scheme_record_reset();
446 memset(ao_scheme_busy, '\0', sizeof (ao_scheme_busy));
447 memset(ao_scheme_cons_note, '\0', sizeof (ao_scheme_cons_note));
448 ao_scheme_cons_noted = 0;
449 for (i = 0; i < (int) AO_SCHEME_ROOT; i++) {
450 if (ao_scheme_root[i].type) {
451 void **a = ao_scheme_root[i].addr, *v;
453 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
454 visit_addr(ao_scheme_root[i].type, a);
457 ao_poly *a = (ao_poly *) ao_scheme_root[i].addr, p;
459 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_scheme_ref(p)));
464 while (ao_scheme_cons_noted) {
465 memcpy(ao_scheme_cons_last, ao_scheme_cons_note, sizeof (ao_scheme_cons_note));
466 memset(ao_scheme_cons_note, '\0', sizeof (ao_scheme_cons_note));
467 ao_scheme_cons_noted = 0;
468 for (i = 0; i < AO_SCHEME_POOL; i += 4) {
469 if (busy(ao_scheme_cons_last, i)) {
470 void *v = ao_scheme_pool + i;
471 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
472 visit_addr(&ao_scheme_cons_type, &v);
484 for (i = 0; i < ao_scheme_top; i += 4) {
485 if ((i & 0xff) == 0) {
488 else if ((i & 0x1f) == 0)
490 if (busy(ao_scheme_busy, i))
497 #define DUMP_BUSY() dump_busy()
504 dump_atoms(int show_marked)
506 struct ao_scheme_atom *atom;
509 for (atom = ao_scheme_atoms; atom; atom = ao_scheme_poly_atom(atom->next)) {
510 printf("\t%d: %s", MDBG_OFFSET(atom), atom->name);
512 printf(" %s", ao_scheme_marked(atom) ? "referenced" : "unreferenced");
518 #define DUMP_ATOMS(a) dump_atoms(a)
520 #define DUMP_ATOMS(a)
523 static const struct ao_scheme_type * const ao_scheme_types[AO_SCHEME_NUM_TYPE] = {
524 [AO_SCHEME_CONS] = &ao_scheme_cons_type,
525 [AO_SCHEME_INT] = NULL,
526 #ifdef AO_SCHEME_FEATURE_BIGINT
527 [AO_SCHEME_BIGINT] = &ao_scheme_bigint_type,
529 [AO_SCHEME_OTHER] = (void *) 0x1,
530 [AO_SCHEME_ATOM] = &ao_scheme_atom_type,
531 [AO_SCHEME_BUILTIN] = &ao_scheme_builtin_type,
532 [AO_SCHEME_FRAME] = &ao_scheme_frame_type,
533 [AO_SCHEME_FRAME_VALS] = &ao_scheme_frame_vals_type,
534 [AO_SCHEME_LAMBDA] = &ao_scheme_lambda_type,
535 [AO_SCHEME_STACK] = &ao_scheme_stack_type,
536 [AO_SCHEME_BOOL] = &ao_scheme_bool_type,
537 [AO_SCHEME_STRING] = &ao_scheme_string_type,
538 #ifdef AO_SCHEME_FEATURE_FLOAT
539 [AO_SCHEME_FLOAT] = &ao_scheme_float_type,
541 #ifdef AO_SCHEME_FEATURE_VECTOR
542 [AO_SCHEME_VECTOR] = &ao_scheme_vector_type,
544 #ifdef AO_SCHEME_FEATURE_PORT
545 [AO_SCHEME_PORT] = &ao_scheme_port_type,
550 ao_scheme_mark(const struct ao_scheme_type *type, void *addr);
553 ao_scheme_move(const struct ao_scheme_type *type, void **ref);
556 ao_scheme_mark_ref(const struct ao_scheme_type *type, void **ref)
558 return ao_scheme_mark(type, *ref);
562 ao_scheme_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
564 return ao_scheme_poly_mark(*p, do_note_cons);
568 uint64_t ao_scheme_collects[2];
569 uint64_t ao_scheme_freed[2];
570 uint64_t ao_scheme_loops[2];
573 int ao_scheme_last_top;
574 int ao_scheme_collect_counts;
577 ao_scheme_collect(uint8_t style)
579 ao_scheme_declare_stack
586 struct ao_scheme_record *mark_record = NULL, *move_record = NULL;
588 MDBG_MOVE("collect %lu\n", ao_scheme_collects[style]);
590 MDBG_DO(ao_scheme_frame_write(stdout, ao_scheme_frame_poly(ao_scheme_frame_global), true));
591 MDBG_DO(++ao_scheme_collecting);
593 ao_scheme_reset_stack();
595 /* The first time through, we're doing a full collect */
596 if (ao_scheme_last_top == 0)
597 style = AO_SCHEME_COLLECT_FULL;
599 /* One in a while, just do a full collect */
601 if (ao_scheme_collect_counts >= 128)
602 style = AO_SCHEME_COLLECT_FULL;
604 if (style == AO_SCHEME_COLLECT_FULL)
605 ao_scheme_collect_counts = 0;
607 /* Clear references to all caches */
608 for (i = 0; i < (int) AO_SCHEME_CACHE; i++)
609 *ao_scheme_cache[i] = NULL;
610 if (style == AO_SCHEME_COLLECT_FULL) {
613 chunk_low = top = ao_scheme_last_top;
616 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
617 /* Find the sizes of the first chunk of objects to move */
619 walk(ao_scheme_mark_ref, ao_scheme_poly_mark_ref);
621 #ifdef AO_SCHEME_FEATURE_PORT
622 ao_scheme_port_check_references();
624 ao_scheme_atom_check_references();
627 ao_scheme_record_free(mark_record);
628 mark_record = ao_scheme_record_save();
629 if (mark_record && move_record)
630 ao_scheme_record_compare("mark", move_record, mark_record);
636 /* Find the first moving object */
637 for (i = 0; i < chunk_last; i++) {
638 uint16_t size = ao_scheme_chunk[i].size;
644 if (ao_scheme_chunk[i].old_offset > top)
647 MDBG_MOVE("chunk %d %d not moving\n",
648 ao_scheme_chunk[i].old_offset,
649 ao_scheme_chunk[i].size);
651 if (ao_scheme_chunk[i].old_offset != top)
657 /* Short-circuit the rest of the loop when all of the
658 * found objects aren't moving. This isn't strictly
659 * necessary as the rest of the loop is structured to
660 * work in this case, but GCC 7.2.0 with optimization
661 * greater than 2 generates incorrect code for this...
663 if (i == AO_SCHEME_NCHUNK) {
664 chunk_low = chunk_high;
672 * Limit amount of chunk array used in mapping moves
673 * to the active region
676 chunk_low = ao_scheme_chunk[i].old_offset;
678 /* Copy all of the objects */
679 for (; i < chunk_last; i++) {
680 uint16_t size = ao_scheme_chunk[i].size;
687 MDBG_MOVE("chunk %d %d -> %d\n",
688 ao_scheme_chunk[i].old_offset,
691 ao_scheme_chunk[i].new_offset = top;
693 memmove(&ao_scheme_pool[top],
694 &ao_scheme_pool[ao_scheme_chunk[i].old_offset],
700 if (chunk_first < chunk_last) {
701 /* Relocate all references to the objects */
702 walk(ao_scheme_move, ao_scheme_poly_move);
703 ao_scheme_atom_move();
704 #ifdef AO_SCHEME_FEATURE_PORT
705 /* the set of open ports gets relocated but not marked, so
706 * just deal with it separately
708 ao_scheme_poly_move(&ao_scheme_open_ports, 0);
712 ao_scheme_record_free(move_record);
713 move_record = ao_scheme_record_save();
714 if (mark_record && move_record)
715 ao_scheme_record_compare("move", mark_record, move_record);
723 /* If we ran into the end of the heap, then
724 * there's no need to keep walking
726 if (chunk_last != AO_SCHEME_NCHUNK)
729 /* Next loop starts right above this loop */
730 chunk_low = chunk_high;
735 ++ao_scheme_collects[style];
736 ao_scheme_freed[style] += ao_scheme_top - top;
737 ao_scheme_loops[style] += loops;
741 if (style == AO_SCHEME_COLLECT_FULL)
742 ao_scheme_last_top = top;
744 MDBG_DO(memset(ao_scheme_chunk, '\0', sizeof (ao_scheme_chunk));
745 walk(ao_scheme_mark_ref, ao_scheme_poly_mark_ref));
748 fprintf(stderr, "max collect stack depth %lu\n", mem_collect_max_depth);
750 MDBG_DO(--ao_scheme_collecting);
751 return AO_SCHEME_POOL - ao_scheme_top;
756 ao_scheme_cons_check(struct ao_scheme_cons *cons)
763 walk(ao_scheme_mark_ref, ao_scheme_poly_mark_ref);
765 if (!ao_scheme_is_pool_addr(cons))
767 offset = pool_offset(cons);
768 if (busy(ao_scheme_busy, offset)) {
769 ao_scheme_printf("cons at %p offset %d poly %d is busy\n\t%v\n", cons, offset, ao_scheme_cons_poly(cons), ao_scheme_cons_poly(cons));
773 if (!ao_scheme_is_pair(cdr))
775 cons = ao_scheme_poly_cons(cdr);
781 * Mark interfaces for objects
786 * Note a reference to memory and collect information about a few
787 * object sizes at a time
791 ao_scheme_mark_memory(const struct ao_scheme_type *type, void *addr)
794 if (!ao_scheme_is_pool_addr(addr))
797 offset = pool_offset(addr);
798 MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
799 if (busy(ao_scheme_busy, offset)) {
800 MDBG_MOVE("already marked\n");
803 mark(ao_scheme_busy, offset);
804 note_chunk(offset, ao_scheme_size(type, addr));
809 * Mark an object and all that it refereces
812 ao_scheme_mark(const struct ao_scheme_type *type, void *addr)
815 MDBG_MOVE("mark offset %d\n", MDBG_OFFSET(addr));
817 ret = ao_scheme_mark_memory(type, addr);
819 MDBG_MOVE("mark recurse\n");
827 * Mark an object, unless it is a cons cell and
828 * do_note_cons is set. In that case, just
829 * set a bit in the cons note array; those
830 * will be marked in a separate pass to avoid
831 * deep recursion in the collector
834 ao_scheme_poly_mark(ao_poly p, uint8_t do_note_cons)
840 type = ao_scheme_poly_base_type(p);
842 if (type == AO_SCHEME_INT)
845 addr = ao_scheme_ref(p);
846 if (!ao_scheme_is_pool_addr(addr))
849 if (type == AO_SCHEME_CONS && do_note_cons) {
850 note_cons(pool_offset(addr));
853 const struct ao_scheme_type *lisp_type;
855 if (type == AO_SCHEME_OTHER)
856 type = ao_scheme_other_type(addr);
858 lisp_type = ao_scheme_types[type];
864 MDBG_MOVE("poly_mark offset %d\n", MDBG_OFFSET(addr));
866 ret = ao_scheme_mark_memory(lisp_type, addr);
868 MDBG_MOVE("mark recurse\n");
869 lisp_type->mark(addr);
877 * Find the current location of an object
878 * based on the original location. For unmoved
879 * objects, this is simple. For moved objects,
884 move_map(uint16_t offset)
888 if (offset < chunk_low || chunk_high <= offset)
891 l = find_chunk(offset);
894 if (ao_scheme_chunk[l].old_offset != offset)
897 return ao_scheme_chunk[l].new_offset;
901 ao_scheme_move_memory(const struct ao_scheme_type *type, void **ref)
904 uint16_t offset, orig_offset;
906 if (!ao_scheme_is_pool_addr(addr))
911 MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
912 orig_offset = pool_offset(addr);
913 offset = move_map(orig_offset);
914 if (offset != orig_offset) {
915 MDBG_MOVE("update ref %d %d -> %d\n",
916 ao_scheme_is_pool_addr(ref) ? MDBG_OFFSET(ref) : -1,
917 orig_offset, offset);
918 *ref = ao_scheme_pool + offset;
920 if (busy(ao_scheme_busy, offset)) {
921 MDBG_MOVE("already moved\n");
924 mark(ao_scheme_busy, offset);
925 ao_scheme_record(type, addr, ao_scheme_size(type, addr));
930 ao_scheme_move(const struct ao_scheme_type *type, void **ref)
933 MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
935 ret = ao_scheme_move_memory(type, ref);
937 MDBG_MOVE("move recurse\n");
945 ao_scheme_poly_move(ao_poly *ref, uint8_t do_note_cons)
950 uint16_t offset, orig_offset;
952 if (ao_scheme_poly_base_type(p) == AO_SCHEME_INT)
955 addr = ao_scheme_ref(p);
956 if (!ao_scheme_is_pool_addr(addr))
959 orig_offset = pool_offset(addr);
960 offset = move_map(orig_offset);
962 if (ao_scheme_poly_base_type(p) == AO_SCHEME_CONS && do_note_cons) {
963 note_cons(orig_offset);
966 uint8_t type = ao_scheme_poly_base_type(p);
967 const struct ao_scheme_type *lisp_type;
969 if (type == AO_SCHEME_OTHER)
970 type = ao_scheme_other_type(ao_scheme_pool + offset);
972 lisp_type = ao_scheme_types[type];
977 /* inline ao_scheme_move to save stack space */
978 MDBG_MOVE("move object %d\n", MDBG_OFFSET(addr));
980 ret = ao_scheme_move_memory(lisp_type, &addr);
982 MDBG_MOVE("move recurse\n");
983 lisp_type->move(addr);
988 /* Re-write the poly value */
989 if (offset != orig_offset) {
990 ao_poly np = ao_scheme_poly(ao_scheme_pool + offset, ao_scheme_poly_base_type(p));
991 MDBG_MOVE("poly %d moved %d -> %d\n",
992 ao_scheme_poly_type(np), orig_offset, offset);
999 ao_scheme_marked(void *addr)
1001 if (!ao_scheme_is_pool_addr(addr))
1003 return busy(ao_scheme_busy, pool_offset(addr));
1008 ao_scheme_validate(void)
1011 memset(ao_scheme_chunk, '\0', sizeof (ao_scheme_chunk));
1012 walk(ao_scheme_mark_ref, ao_scheme_poly_mark_ref);
1020 ao_scheme_alloc(int size)
1024 MDBG_DO(++dbg_allocs);
1025 MDBG_DO(if (dbg_validate) ao_scheme_validate());
1026 size = ao_scheme_size_round(size);
1027 if (AO_SCHEME_POOL - ao_scheme_top < size &&
1028 ao_scheme_collect(AO_SCHEME_COLLECT_INCREMENTAL) < size &&
1029 ao_scheme_collect(AO_SCHEME_COLLECT_FULL) < size)
1031 ao_scheme_error(AO_SCHEME_OOM, "out of memory");
1034 addr = ao_scheme_pool + ao_scheme_top;
1035 ao_scheme_top += size;
1036 MDBG_MOVE("alloc %d size %d\n", MDBG_OFFSET(addr), size);
1041 ao_scheme_poly_stash(ao_poly p)
1043 assert(stash_poly_ptr < AO_SCHEME_NUM_STASH);
1044 stash_poly[stash_poly_ptr++] = p;
1048 ao_scheme_poly_fetch(void)
1052 assert (stash_poly_ptr > 0);
1053 p = stash_poly[--stash_poly_ptr];
1054 stash_poly[stash_poly_ptr] = AO_SCHEME_NIL;
1059 ao_scheme_print_mark_addr(void *addr)
1064 if (ao_scheme_collecting)
1068 if (!ao_scheme_is_pool_addr(addr))
1071 if (!ao_scheme_print_cleared) {
1072 ao_scheme_print_cleared = 1;
1073 memset(ao_scheme_busy, '\0', sizeof (ao_scheme_busy));
1075 offset = pool_offset(addr);
1076 if (busy(ao_scheme_busy, offset))
1078 mark(ao_scheme_busy, offset);
1083 ao_scheme_print_clear_addr(void *addr)
1088 if (ao_scheme_collecting)
1092 if (!ao_scheme_is_pool_addr(addr))
1095 if (!ao_scheme_print_cleared)
1097 offset = pool_offset(addr);
1098 clear(ao_scheme_busy, offset);
1101 /* Notes that printing has started */
1103 ao_scheme_print_start(void)
1105 ao_scheme_printing++;
1108 /* Notes that printing has ended. Returns 1 if printing is still going on */
1110 ao_scheme_print_stop(void)
1112 ao_scheme_printing--;
1113 if (ao_scheme_printing != 0)
1115 ao_scheme_print_cleared = 0;