]> gitweb.factorcode.org Git - factor.git/blob - vm/write_barrier.hpp
Merge branch 'hashcash' of git://github.com/martind/factor
[factor.git] / vm / write_barrier.hpp
1 /* card marking write barrier. a card is a byte storing a mark flag,
2 and the offset (in cells) of the first object in the card.
3
4 the mark flag is set by the write barrier when an object in the
5 card has a slot written to.
6
7 the offset of the first object is set by the allocator. */
8
9 VM_C_API factor::cell cards_offset;
10 VM_C_API factor::cell decks_offset;
11
12 namespace factor
13 {
14
15 /* if CARD_POINTS_TO_NURSERY is set, CARD_POINTS_TO_AGING must also be set. */
16 #define CARD_POINTS_TO_NURSERY 0x80
17 #define CARD_POINTS_TO_AGING 0x40
18 #define CARD_MARK_MASK (CARD_POINTS_TO_NURSERY | CARD_POINTS_TO_AGING)
19 typedef u8 card;
20
21 #define CARD_BITS 8
22 #define CARD_SIZE (1<<CARD_BITS)
23 #define ADDR_CARD_MASK (CARD_SIZE-1)
24
25 inline static card *addr_to_card(cell a)
26 {
27         return (card*)(((cell)(a) >> CARD_BITS) + cards_offset);
28 }
29
30 inline static cell card_to_addr(card *c)
31 {
32         return ((cell)c - cards_offset) << CARD_BITS;
33 }
34
35 inline static cell card_offset(card *c)
36 {
37         return *(c - (cell)data->cards + (cell)data->allot_markers);
38 }
39
40 typedef u8 card_deck;
41
42 #define DECK_BITS (CARD_BITS + 10)
43 #define DECK_SIZE (1<<DECK_BITS)
44 #define ADDR_DECK_MASK (DECK_SIZE-1)
45
46 inline static card_deck *addr_to_deck(cell a)
47 {
48         return (card_deck *)(((cell)a >> DECK_BITS) + decks_offset);
49 }
50
51 inline static cell deck_to_addr(card_deck *c)
52 {
53         return ((cell)c - decks_offset) << DECK_BITS;
54 }
55
56 inline static card *deck_to_card(card_deck *d)
57 {
58         return (card *)((((cell)d - decks_offset) << (DECK_BITS - CARD_BITS)) + cards_offset);
59 }
60
61 #define INVALID_ALLOT_MARKER 0xff
62
63 extern cell allot_markers_offset;
64
65 inline static card *addr_to_allot_marker(object *a)
66 {
67         return (card *)(((cell)a >> CARD_BITS) + allot_markers_offset);
68 }
69
70 /* the write barrier must be called any time we are potentially storing a
71 pointer from an older generation to a younger one */
72 inline static void write_barrier(object *obj)
73 {
74         *addr_to_card((cell)obj) = CARD_MARK_MASK;
75         *addr_to_deck((cell)obj) = CARD_MARK_MASK;
76 }
77
78 /* we need to remember the first object allocated in the card */
79 inline static void allot_barrier(object *address)
80 {
81         card *ptr = addr_to_allot_marker(address);
82         if(*ptr == INVALID_ALLOT_MARKER)
83                 *ptr = ((cell)address & ADDR_CARD_MASK);
84 }
85
86 }