initial
[prop.git] / lib-src / gc / gcbitmap.cc
blobbe74a906f478999640b62281f1e449f47f52ca7c
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are welcomed to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome(read crave for)
19 // any suggestions and help from the users.
21 // Allen Leung (leunga@cs.nyu.edu)
22 // 1994-1995
23 //////////////////////////////////////////////////////////////////////////////
25 #include <iostream.h>
26 #include <string.h>
27 #include <assert.h>
28 #include <AD/gc/gcbitmap.h>
29 #include <AD/gc/gcheaps.h>
30 #include <AD/gc/gcmacros.h>
32 // #define DEBUG_GC
34 //////////////////////////////////////////////////////////////////////////////
35 // Constructor
36 //////////////////////////////////////////////////////////////////////////////
37 GCBitMap::GCBitMap()
38 : lowest_addr(0), highest_addr(0),
39 bitmap(0), bitmap_bot(0), bitmap_top(0), bitmap_core(0),
40 bitmap_size(0) {}
42 //////////////////////////////////////////////////////////////////////////////
43 // Destructor
44 //////////////////////////////////////////////////////////////////////////////
45 GCBitMap::~GCBitMap() { release(); }
47 //////////////////////////////////////////////////////////////////////////////
48 // Method to clear the entire bitmap
49 //////////////////////////////////////////////////////////////////////////////
50 void GCBitMap::clear()
51 { memset(bitmap_bot, 0, bitmap_size);
54 //////////////////////////////////////////////////////////////////////////////
55 // Method to release all the memory of a bitmap
56 //////////////////////////////////////////////////////////////////////////////
57 void GCBitMap::release()
59 if (bitmap_core) {
60 HM::deallocate_pages_on_boundaries(bitmap_core, bitmap_size);
61 lowest_addr = 0;
62 highest_addr = 0;
63 bitmap = 0;
64 bitmap_bot = 0;
65 bitmap_top = 0;
66 bitmap_core = 0;
67 bitmap_size = 0;
71 //////////////////////////////////////////////////////////////////////////////
72 // Method to grow the bitmap.
73 //////////////////////////////////////////////////////////////////////////////
74 void GCBitMap::grow (void * low, void * high)
76 // roundup to page size
77 void * real_low = GC_TRUNC_PAGE_ADDR(low);
78 void * real_high = GC_ROUNDUP_PAGE_ADDR((Byte*)high + 1);
80 // compute new address boundaries
81 void * new_low = (real_low < lowest_addr || lowest_addr == 0)
82 ? real_low : lowest_addr;
83 void * new_high = real_high > highest_addr ? real_high : highest_addr;
85 // Check whether the boundaries have changed.
86 // If so, expand the bitmap
87 if (new_low != lowest_addr || new_high != highest_addr) {
88 size_t new_size = ((Byte*)new_high - (Byte*)new_low) /
89 (8 * GC_ALIGNMENT);
90 new_size = GC_ROUNDUP_PAGE_SIZE(new_size);
91 void * new_bitmap_core;
92 Glob * new_bitmap_bot =
93 (Glob*)HM::allocate_pages_on_boundaries(new_size, new_bitmap_core);
94 size_t shift =
95 lowest_addr == 0 ? 0 :
96 (((Byte*)lowest_addr - (Byte*)new_low) /
97 (8 * GC_ALIGNMENT * sizeof(Glob)));
99 // copy old bitmap to new
100 memcpy(new_bitmap_bot + shift, bitmap_bot, bitmap_size);
102 // clean up old bitmap and delete it
103 HM::deallocate_pages_on_boundaries(bitmap_core, bitmap_size);
105 // readjust bitmap pointers and offsets
106 lowest_addr = new_low;
107 highest_addr = new_high;
108 bitmap_size = new_size;
109 bitmap_core = new_bitmap_core;
110 bitmap_bot = new_bitmap_bot;
111 bitmap = new_bitmap_bot -
112 ((size_t)new_low / (sizeof(Glob) * 8 * GC_ALIGNMENT));
113 bitmap_top = bitmap_bot + new_size / sizeof(Glob);
115 #ifdef DEBUG_GC
116 cerr << "[ New bitmap at " << (void*)bitmap_bot
117 << " ... " << (void*)bitmap_top << " ]\n";
118 #endif