OSDN Git Service

* Make-lang.in (CXX_PARSER_H): New.
[pf3gnuchains/gcc-fork.git] / libgo / runtime / mheapmap32.c
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Heap map, 32-bit version
6 // See malloc.h and mheap.c for overview.
7
8 #include "runtime.h"
9 #include "malloc.h"
10
11 #if __SIZEOF_POINTER__ == 4
12
13 // 3-level radix tree mapping page ids to Span*.
14 void
15 runtime_MHeapMap_Init(MHeapMap *m, void *(*allocator)(uintptr))
16 {
17         m->allocator = allocator;
18 }
19
20 MSpan*
21 runtime_MHeapMap_Get(MHeapMap *m, PageID k)
22 {
23         int32 i1, i2;
24
25         i2 = k & MHeapMap_Level2Mask;
26         k >>= MHeapMap_Level2Bits;
27         i1 = k & MHeapMap_Level1Mask;
28         k >>= MHeapMap_Level1Bits;
29         if(k != 0)
30                 runtime_throw("MHeapMap_Get");
31
32         return m->p[i1]->s[i2];
33 }
34
35 MSpan*
36 runtime_MHeapMap_GetMaybe(MHeapMap *m, PageID k)
37 {
38         int32 i1, i2;
39         MHeapMapNode2 *p2;
40
41         i2 = k & MHeapMap_Level2Mask;
42         k >>= MHeapMap_Level2Bits;
43         i1 = k & MHeapMap_Level1Mask;
44         k >>= MHeapMap_Level1Bits;
45         if(k != 0)
46                 runtime_throw("MHeapMap_Get");
47
48         p2 = m->p[i1];
49         if(p2 == nil)
50                 return nil;
51         return p2->s[i2];
52 }
53
54 void
55 runtime_MHeapMap_Set(MHeapMap *m, PageID k, MSpan *s)
56 {
57         int32 i1, i2;
58
59         i2 = k & MHeapMap_Level2Mask;
60         k >>= MHeapMap_Level2Bits;
61         i1 = k & MHeapMap_Level1Mask;
62         k >>= MHeapMap_Level1Bits;
63         if(k != 0)
64                 runtime_throw("MHeapMap_Set");
65
66         m->p[i1]->s[i2] = s;
67 }
68
69 // Allocate the storage required for entries [k, k+1, ..., k+len-1]
70 // so that Get and Set calls need not check for nil pointers.
71 bool
72 runtime_MHeapMap_Preallocate(MHeapMap *m, PageID k, uintptr len)
73 {
74         uintptr end;
75         int32 i1;
76         MHeapMapNode2 *p2;
77
78         end = k+len;
79         while(k < end) {
80                 if((k >> MHeapMap_TotalBits) != 0)
81                         return false;
82                 i1 = (k >> MHeapMap_Level2Bits) & MHeapMap_Level1Mask;
83
84                 // first-level pointer
85                 if(m->p[i1] == nil) {
86                         p2 = m->allocator(sizeof *p2);
87                         if(p2 == nil)
88                                 return false;
89                         mstats.heapmap_sys += sizeof *p2;
90                         m->p[i1] = p2;
91                 }
92
93                 // advance key past this leaf node
94                 k = ((k >> MHeapMap_Level2Bits) + 1) << MHeapMap_Level2Bits;
95         }
96         return true;
97 }
98
99 #endif /* __SIZEOF_POINTER__ == 4 */