]> gitweb.factorcode.org Git - factor.git/blob - vm/write_barrier.hpp
Merge branch 'master' of /cygdrive/z/Documents/Code/others/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 static const cell card_points_to_nursery = 0x80;
17 static const cell card_points_to_aging = 0x40;
18 static const cell card_mark_mask = (card_points_to_nursery | card_points_to_aging);
19 typedef u8 card;
20
21 static const cell card_bits = 8;
22 static const cell card_size = (1<<card_bits);
23 static const cell 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 static const cell deck_bits = (card_bits + 10);
43 static const cell deck_size = (1<<deck_bits);
44 static const cell 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 static const cell 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 }