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