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.
7 // See malloc.h for overview.
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.
12 // When a MSpan is allocated, state == MSpanInUse
13 // and heapmap(i) == span for all s->start <= i < s->start+s->npages.
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*);
25 RecordSpan(void *vh, byte *p)
32 s->allnext = h->allspans;
36 // Initialize the heap; fetch memory using alloc.
38 runtime_MHeap_Init(MHeap *h, void *(*alloc)(uintptr))
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);
53 // Allocate a new span of npage pages from the heap
54 // and record its size class in the HeapMap and HeapMapCache.
56 runtime_MHeap_Alloc(MHeap *h, uintptr npage, int32 sizeclass, int32 acct)
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);
67 mstats.heap_inuse += npage<<PageShift;
69 mstats.heap_objects++;
70 mstats.heap_alloc += npage<<PageShift;
78 MHeap_AllocLocked(MHeap *h, uintptr npage, int32 sizeclass)
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])) {
92 // Best fit in list of large spans.
93 if((s = MHeap_AllocLarge(h, npage)) == nil) {
94 if(!MHeap_Grow(h, npage))
96 if((s = MHeap_AllocLarge(h, npage)) == nil)
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;
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);
117 if(sizeof(void*) == 8)
118 p -= ((uintptr)h->arena_start>>PageShift);
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);
128 if(*(uintptr*)(s->start<<PageShift) != 0)
129 runtime_memclr((byte*)(s->start<<PageShift), s->npages<<PageShift);
131 // Record span info, because gc needs to be
132 // able to map interior pointer to containing span.
133 s->sizeclass = sizeclass;
135 if(sizeof(void*) == 8)
136 p -= ((uintptr)h->arena_start>>PageShift);
137 for(n=0; n<npage; n++)
142 // Allocate a span of exactly npage pages from the list of large spans.
144 MHeap_AllocLarge(MHeap *h, uintptr npage)
146 return BestFit(&h->large, npage, nil);
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.
153 BestFit(MSpan *list, uintptr npage, MSpan *best)
157 for(s=list->next; s != list; s=s->next) {
158 if(s->npages < npage)
161 || s->npages < best->npages
162 || (s->npages == best->npages && s->start < best->start))
168 // Try to add at least npage pages of memory to the heap,
169 // returning whether it worked.
171 MHeap_Grow(MHeap *h, uintptr npage)
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))
186 if(ask < HeapAllocChunk && HeapAllocChunk <= h->arena_end - h->arena_used)
187 ask = HeapAllocChunk;
189 v = runtime_MHeap_SysAlloc(h, ask);
191 if(ask > (npage<<PageShift)) {
192 ask = npage<<PageShift;
193 v = runtime_MHeap_SysAlloc(h, ask);
198 mstats.heap_sys += ask;
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);
207 if(sizeof(void*) == 8)
208 p -= ((uintptr)h->arena_start>>PageShift);
210 h->map[p + s->npages - 1] = s;
211 s->state = MSpanInUse;
212 MHeap_FreeLocked(h, s);
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.
220 runtime_MHeap_Lookup(MHeap *h, void *v)
225 if(sizeof(void*) == 8)
226 p -= (uintptr)h->arena_start;
227 return h->map[p >> PageShift];
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
238 runtime_MHeap_LookupMaybe(MHeap *h, void *v)
243 if((byte*)v < h->arena_start || (byte*)v >= h->arena_used)
245 p = (uintptr)v>>PageShift;
247 if(sizeof(void*) == 8)
248 q -= (uintptr)h->arena_start >> PageShift;
250 if(s == nil || p < s->start || p - s->start >= s->npages)
252 if(s->state != MSpanInUse)
257 // Free the span back into the heap.
259 runtime_MHeap_Free(MHeap *h, MSpan *s, int32 acct)
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;
268 mstats.heap_alloc -= s->npages<<PageShift;
269 mstats.heap_objects--;
271 MHeap_FreeLocked(h, s);
276 MHeap_FreeLocked(MHeap *h, MSpan *s)
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");
286 s->state = MSpanFree;
287 runtime_MSpanList_Remove(s);
288 sp = (uintptr*)(s->start<<PageShift);
290 // Coalesce with earlier, later spans.
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
298 s->npages += t->npages;
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;
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;
319 // Insert s into appropriate list.
320 if(s->npages < nelem(h->free))
321 runtime_MSpanList_Insert(&h->free[s->npages], s);
323 runtime_MSpanList_Insert(&h->large, s);
325 // TODO(rsc): IncrementalScavenge() to return memory to OS.
328 // Initialize a new span with the given start and npages.
330 runtime_MSpan_Init(MSpan *span, PageID start, uintptr npages)
335 span->npages = npages;
336 span->freelist = nil;
342 // Initialize an empty doubly-linked list.
344 runtime_MSpanList_Init(MSpan *list)
346 list->state = MSpanListHead;
352 runtime_MSpanList_Remove(MSpan *span)
354 if(span->prev == nil && span->next == nil)
356 span->prev->next = span->next;
357 span->next->prev = span->prev;
363 runtime_MSpanList_IsEmpty(MSpan *list)
365 return list->next == list;
369 runtime_MSpanList_Insert(MSpan *list, MSpan *span)
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");
375 span->next = list->next;
377 span->next->prev = span;
378 span->prev->next = span;