OSDN Git Service

Update to current version of Go library (revision 94d654be2064).
[pf3gnuchains/gcc-fork.git] / libgo / runtime / mheap.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 // Page heap.
6 //
7 // See malloc.h for overview.
8 //
9 // When a MSpan is in the heap free list, state == MSpanFree
10 // and heapmap(s->start) == span, heapmap(s->start+s->npages-1) == span.
11 //
12 // When a MSpan is allocated, state == MSpanInUse
13 // and heapmap(i) == span for all s->start <= i < s->start+s->npages.
14
15 #include "runtime.h"
16 #include "malloc.h"
17
18 static MSpan *MHeap_AllocLocked(MHeap*, uintptr, int32);
19 static bool MHeap_Grow(MHeap*, uintptr);
20 static void MHeap_FreeLocked(MHeap*, MSpan*);
21 static MSpan *MHeap_AllocLarge(MHeap*, uintptr);
22 static MSpan *BestFit(MSpan*, uintptr, MSpan*);
23
24 static void
25 RecordSpan(void *vh, byte *p)
26 {
27         MHeap *h;
28         MSpan *s;
29
30         h = vh;
31         s = (MSpan*)p;
32         s->allnext = h->allspans;
33         h->allspans = s;
34 }
35
36 // Initialize the heap; fetch memory using alloc.
37 void
38 runtime_MHeap_Init(MHeap *h, void *(*alloc)(uintptr))
39 {
40         uint32 i;
41
42         runtime_initlock(h);
43         runtime_FixAlloc_Init(&h->spanalloc, sizeof(MSpan), alloc, RecordSpan, h);
44         runtime_FixAlloc_Init(&h->cachealloc, sizeof(MCache), alloc, nil, nil);
45         // h->mapcache needs no init
46         for(i=0; i<nelem(h->free); i++)
47                 runtime_MSpanList_Init(&h->free[i]);
48         runtime_MSpanList_Init(&h->large);
49         for(i=0; i<nelem(h->central); i++)
50                 runtime_MCentral_Init(&h->central[i], i);
51 }
52
53 // Allocate a new span of npage pages from the heap
54 // and record its size class in the HeapMap and HeapMapCache.
55 MSpan*
56 runtime_MHeap_Alloc(MHeap *h, uintptr npage, int32 sizeclass, int32 acct)
57 {
58         MSpan *s;
59
60         runtime_lock(h);
61         mstats.heap_alloc += m->mcache->local_alloc;
62         m->mcache->local_alloc = 0;
63         mstats.heap_objects += m->mcache->local_objects;
64         m->mcache->local_objects = 0;
65         s = MHeap_AllocLocked(h, npage, sizeclass);
66         if(s != nil) {
67                 mstats.heap_inuse += npage<<PageShift;
68                 if(acct) {
69                         mstats.heap_objects++;
70                         mstats.heap_alloc += npage<<PageShift;
71                 }
72         }
73         runtime_unlock(h);
74         return s;
75 }
76
77 static MSpan*
78 MHeap_AllocLocked(MHeap *h, uintptr npage, int32 sizeclass)
79 {
80         uintptr n;
81         MSpan *s, *t;
82         PageID p;
83
84         // Try in fixed-size lists up to max.
85         for(n=npage; n < nelem(h->free); n++) {
86                 if(!runtime_MSpanList_IsEmpty(&h->free[n])) {
87                         s = h->free[n].next;
88                         goto HaveSpan;
89                 }
90         }
91
92         // Best fit in list of large spans.
93         if((s = MHeap_AllocLarge(h, npage)) == nil) {
94                 if(!MHeap_Grow(h, npage))
95                         return nil;
96                 if((s = MHeap_AllocLarge(h, npage)) == nil)
97                         return nil;
98         }
99
100 HaveSpan:
101         // Mark span in use.
102         if(s->state != MSpanFree)
103                 runtime_throw("MHeap_AllocLocked - MSpan not free");
104         if(s->npages < npage)
105                 runtime_throw("MHeap_AllocLocked - bad npages");
106         runtime_MSpanList_Remove(s);
107         s->state = MSpanInUse;
108
109         if(s->npages > npage) {
110                 // Trim extra and put it back in the heap.
111                 t = runtime_FixAlloc_Alloc(&h->spanalloc);
112                 mstats.mspan_inuse = h->spanalloc.inuse;
113                 mstats.mspan_sys = h->spanalloc.sys;
114                 runtime_MSpan_Init(t, s->start + npage, s->npages - npage);
115                 s->npages = npage;
116                 p = t->start;
117                 if(sizeof(void*) == 8)
118                         p -= ((uintptr)h->arena_start>>PageShift);
119                 if(p > 0)
120                         h->map[p-1] = s;
121                 h->map[p] = t;
122                 h->map[p+t->npages-1] = t;
123                 *(uintptr*)(t->start<<PageShift) = *(uintptr*)(s->start<<PageShift);  // copy "needs zeroing" mark
124                 t->state = MSpanInUse;
125                 MHeap_FreeLocked(h, t);
126         }
127
128         if(*(uintptr*)(s->start<<PageShift) != 0)
129                 runtime_memclr((byte*)(s->start<<PageShift), s->npages<<PageShift);
130
131         // Record span info, because gc needs to be
132         // able to map interior pointer to containing span.
133         s->sizeclass = sizeclass;
134         p = s->start;
135         if(sizeof(void*) == 8)
136                 p -= ((uintptr)h->arena_start>>PageShift);
137         for(n=0; n<npage; n++)
138                 h->map[p+n] = s;
139         return s;
140 }
141
142 // Allocate a span of exactly npage pages from the list of large spans.
143 static MSpan*
144 MHeap_AllocLarge(MHeap *h, uintptr npage)
145 {
146         return BestFit(&h->large, npage, nil);
147 }
148
149 // Search list for smallest span with >= npage pages.
150 // If there are multiple smallest spans, take the one
151 // with the earliest starting address.
152 static MSpan*
153 BestFit(MSpan *list, uintptr npage, MSpan *best)
154 {
155         MSpan *s;
156
157         for(s=list->next; s != list; s=s->next) {
158                 if(s->npages < npage)
159                         continue;
160                 if(best == nil
161                 || s->npages < best->npages
162                 || (s->npages == best->npages && s->start < best->start))
163                         best = s;
164         }
165         return best;
166 }
167
168 // Try to add at least npage pages of memory to the heap,
169 // returning whether it worked.
170 static bool
171 MHeap_Grow(MHeap *h, uintptr npage)
172 {
173         uintptr ask;
174         void *v;
175         MSpan *s;
176         PageID p;
177
178         // Ask for a big chunk, to reduce the number of mappings
179         // the operating system needs to track; also amortizes
180         // the overhead of an operating system mapping.
181         // Allocate a multiple of 64kB (16 pages).
182         npage = (npage+15)&~15;
183         ask = npage<<PageShift;
184         if(ask > (uintptr)(h->arena_end - h->arena_used))
185                 return false;
186         if(ask < HeapAllocChunk && HeapAllocChunk <= h->arena_end - h->arena_used)
187                 ask = HeapAllocChunk;
188
189         v = runtime_MHeap_SysAlloc(h, ask);
190         if(v == nil) {
191                 if(ask > (npage<<PageShift)) {
192                         ask = npage<<PageShift;
193                         v = runtime_MHeap_SysAlloc(h, ask);
194                 }
195                 if(v == nil)
196                         return false;
197         }
198         mstats.heap_sys += ask;
199
200         // Create a fake "in use" span and free it, so that the
201         // right coalescing happens.
202         s = runtime_FixAlloc_Alloc(&h->spanalloc);
203         mstats.mspan_inuse = h->spanalloc.inuse;
204         mstats.mspan_sys = h->spanalloc.sys;
205         runtime_MSpan_Init(s, (uintptr)v>>PageShift, ask>>PageShift);
206         p = s->start;
207         if(sizeof(void*) == 8)
208                 p -= ((uintptr)h->arena_start>>PageShift);
209         h->map[p] = s;
210         h->map[p + s->npages - 1] = s;
211         s->state = MSpanInUse;
212         MHeap_FreeLocked(h, s);
213         return true;
214 }
215
216 // Look up the span at the given address.
217 // Address is guaranteed to be in map
218 // and is guaranteed to be start or end of span.
219 MSpan*
220 runtime_MHeap_Lookup(MHeap *h, void *v)
221 {
222         uintptr p;
223         
224         p = (uintptr)v;
225         if(sizeof(void*) == 8)
226                 p -= (uintptr)h->arena_start;
227         return h->map[p >> PageShift];
228 }
229
230 // Look up the span at the given address.
231 // Address is *not* guaranteed to be in map
232 // and may be anywhere in the span.
233 // Map entries for the middle of a span are only
234 // valid for allocated spans.  Free spans may have
235 // other garbage in their middles, so we have to
236 // check for that.
237 MSpan*
238 runtime_MHeap_LookupMaybe(MHeap *h, void *v)
239 {
240         MSpan *s;
241         PageID p, q;
242
243         if((byte*)v < h->arena_start || (byte*)v >= h->arena_used)
244                 return nil;
245         p = (uintptr)v>>PageShift;
246         q = p;
247         if(sizeof(void*) == 8)
248                 q -= (uintptr)h->arena_start >> PageShift;
249         s = h->map[q];
250         if(s == nil || p < s->start || p - s->start >= s->npages)
251                 return nil;
252         if(s->state != MSpanInUse)
253                 return nil;
254         return s;
255 }
256
257 // Free the span back into the heap.
258 void
259 runtime_MHeap_Free(MHeap *h, MSpan *s, int32 acct)
260 {
261         runtime_lock(h);
262         mstats.heap_alloc += m->mcache->local_alloc;
263         m->mcache->local_alloc = 0;
264         mstats.heap_objects += m->mcache->local_objects;
265         m->mcache->local_objects = 0;
266         mstats.heap_inuse -= s->npages<<PageShift;
267         if(acct) {
268                 mstats.heap_alloc -= s->npages<<PageShift;
269                 mstats.heap_objects--;
270         }
271         MHeap_FreeLocked(h, s);
272         runtime_unlock(h);
273 }
274
275 static void
276 MHeap_FreeLocked(MHeap *h, MSpan *s)
277 {
278         uintptr *sp, *tp;
279         MSpan *t;
280         PageID p;
281
282         if(s->state != MSpanInUse || s->ref != 0) {
283                 // runtime_printf("MHeap_FreeLocked - span %p ptr %p state %d ref %d\n", s, s->start<<PageShift, s->state, s->ref);
284                 runtime_throw("MHeap_FreeLocked - invalid free");
285         }
286         s->state = MSpanFree;
287         runtime_MSpanList_Remove(s);
288         sp = (uintptr*)(s->start<<PageShift);
289
290         // Coalesce with earlier, later spans.
291         p = s->start;
292         if(sizeof(void*) == 8)
293                 p -= (uintptr)h->arena_start >> PageShift;
294         if(p > 0 && (t = h->map[p-1]) != nil && t->state != MSpanInUse) {
295                 tp = (uintptr*)(t->start<<PageShift);
296                 *tp |= *sp;     // propagate "needs zeroing" mark
297                 s->start = t->start;
298                 s->npages += t->npages;
299                 p -= t->npages;
300                 h->map[p] = s;
301                 runtime_MSpanList_Remove(t);
302                 t->state = MSpanDead;
303                 runtime_FixAlloc_Free(&h->spanalloc, t);
304                 mstats.mspan_inuse = h->spanalloc.inuse;
305                 mstats.mspan_sys = h->spanalloc.sys;
306         }
307         if(p+s->npages < nelem(h->map) && (t = h->map[p+s->npages]) != nil && t->state != MSpanInUse) {
308                 tp = (uintptr*)(t->start<<PageShift);
309                 *sp |= *tp;     // propagate "needs zeroing" mark
310                 s->npages += t->npages;
311                 h->map[p + s->npages - 1] = s;
312                 runtime_MSpanList_Remove(t);
313                 t->state = MSpanDead;
314                 runtime_FixAlloc_Free(&h->spanalloc, t);
315                 mstats.mspan_inuse = h->spanalloc.inuse;
316                 mstats.mspan_sys = h->spanalloc.sys;
317         }
318
319         // Insert s into appropriate list.
320         if(s->npages < nelem(h->free))
321                 runtime_MSpanList_Insert(&h->free[s->npages], s);
322         else
323                 runtime_MSpanList_Insert(&h->large, s);
324
325         // TODO(rsc): IncrementalScavenge() to return memory to OS.
326 }
327
328 // Initialize a new span with the given start and npages.
329 void
330 runtime_MSpan_Init(MSpan *span, PageID start, uintptr npages)
331 {
332         span->next = nil;
333         span->prev = nil;
334         span->start = start;
335         span->npages = npages;
336         span->freelist = nil;
337         span->ref = 0;
338         span->sizeclass = 0;
339         span->state = 0;
340 }
341
342 // Initialize an empty doubly-linked list.
343 void
344 runtime_MSpanList_Init(MSpan *list)
345 {
346         list->state = MSpanListHead;
347         list->next = list;
348         list->prev = list;
349 }
350
351 void
352 runtime_MSpanList_Remove(MSpan *span)
353 {
354         if(span->prev == nil && span->next == nil)
355                 return;
356         span->prev->next = span->next;
357         span->next->prev = span->prev;
358         span->prev = nil;
359         span->next = nil;
360 }
361
362 bool
363 runtime_MSpanList_IsEmpty(MSpan *list)
364 {
365         return list->next == list;
366 }
367
368 void
369 runtime_MSpanList_Insert(MSpan *list, MSpan *span)
370 {
371         if(span->next != nil || span->prev != nil) {
372                 // runtime_printf("failed MSpanList_Insert %p %p %p\n", span, span->next, span->prev);
373                 runtime_throw("MSpanList_Insert");
374         }
375         span->next = list->next;
376         span->prev = list;
377         span->next->prev = span;
378         span->prev->next = span;
379 }
380
381