OSDN Git Service

Avoid always splitting the stack when calling append and copy.
[pf3gnuchains/gcc-fork.git] / libgo / runtime / mheapmap64.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, 64-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__ == 8
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, i3;
24
25         i3 = k & MHeapMap_Level3Mask;
26         k >>= MHeapMap_Level3Bits;
27         i2 = k & MHeapMap_Level2Mask;
28         k >>= MHeapMap_Level2Bits;
29         i1 = k & MHeapMap_Level1Mask;
30         k >>= MHeapMap_Level1Bits;
31         if(k != 0)
32                 runtime_throw("MHeapMap_Get");
33
34         return m->p[i1]->p[i2]->s[i3];
35 }
36
37 MSpan*
38 runtime_MHeapMap_GetMaybe(MHeapMap *m, PageID k)
39 {
40         int32 i1, i2, i3;
41         MHeapMapNode2 *p2;
42         MHeapMapNode3 *p3;
43
44         i3 = k & MHeapMap_Level3Mask;
45         k >>= MHeapMap_Level3Bits;
46         i2 = k & MHeapMap_Level2Mask;
47         k >>= MHeapMap_Level2Bits;
48         i1 = k & MHeapMap_Level1Mask;
49         k >>= MHeapMap_Level1Bits;
50         if(k != 0)
51                 runtime_throw("MHeapMap_Get");
52
53         p2 = m->p[i1];
54         if(p2 == nil)
55                 return nil;
56         p3 = p2->p[i2];
57         if(p3 == nil)
58                 return nil;
59         return p3->s[i3];
60 }
61
62 void
63 runtime_MHeapMap_Set(MHeapMap *m, PageID k, MSpan *s)
64 {
65         int32 i1, i2, i3;
66
67         i3 = k & MHeapMap_Level3Mask;
68         k >>= MHeapMap_Level3Bits;
69         i2 = k & MHeapMap_Level2Mask;
70         k >>= MHeapMap_Level2Bits;
71         i1 = k & MHeapMap_Level1Mask;
72         k >>= MHeapMap_Level1Bits;
73         if(k != 0)
74                 runtime_throw("MHeapMap_Set");
75
76         m->p[i1]->p[i2]->s[i3] = s;
77 }
78
79 // Allocate the storage required for entries [k, k+1, ..., k+len-1]
80 // so that Get and Set calls need not check for nil pointers.
81 bool
82 runtime_MHeapMap_Preallocate(MHeapMap *m, PageID k, uintptr len)
83 {
84         uintptr end;
85         int32 i1, i2;
86         MHeapMapNode2 *p2;
87         MHeapMapNode3 *p3;
88
89         end = k+len;
90         while(k < end) {
91                 if((k >> MHeapMap_TotalBits) != 0)
92                         return false;
93                 i2 = (k >> MHeapMap_Level3Bits) & MHeapMap_Level2Mask;
94                 i1 = (k >> (MHeapMap_Level3Bits + MHeapMap_Level2Bits)) & MHeapMap_Level1Mask;
95
96                 // first-level pointer
97                 if((p2 = m->p[i1]) == nil) {
98                         p2 = m->allocator(sizeof *p2);
99                         if(p2 == nil)
100                                 return false;
101                         mstats.heapmap_sys += sizeof *p2;
102                         m->p[i1] = p2;
103                 }
104
105                 // second-level pointer
106                 if(p2->p[i2] == nil) {
107                         p3 = m->allocator(sizeof *p3);
108                         if(p3 == nil)
109                                 return false;
110                         mstats.heapmap_sys += sizeof *p3;
111                         p2->p[i2] = p3;
112                 }
113
114                 // advance key past this leaf node
115                 k = ((k >> MHeapMap_Level3Bits) + 1) << MHeapMap_Level3Bits;
116         }
117         return true;
118 }
119
120 #endif /* __SIZEOF_POINTER__ == 8 */