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 an overview.
9 // The MCentral doesn't actually contain the list of free objects; the MSpan does.
10 // Each MCentral is two lists of MSpans: those with free objects (c->nonempty)
11 // and those that are completely allocated (c->empty).
13 // TODO(rsc): tcmalloc uses a "transfer cache" to split the list
14 // into sections of class_to_transfercount[sizeclass] objects
15 // so that it is faster to move those lists between MCaches and MCentrals.
21 static bool MCentral_Grow(MCentral
*c
);
22 static void MCentral_Free(MCentral
*c
, void *v
);
24 // Initialize a single central free list.
26 runtime_MCentral_Init(MCentral
*c
, int32 sizeclass
)
28 c
->sizeclass
= sizeclass
;
29 runtime_MSpanList_Init(&c
->nonempty
);
30 runtime_MSpanList_Init(&c
->empty
);
33 // Allocate a list of objects from the central free list.
34 // Return the number of objects allocated.
35 // The objects are linked together by their first words.
36 // On return, *pfirst points at the first object.
38 runtime_MCentral_AllocList(MCentral
*c
, MLink
**pfirst
)
44 // Replenish central list if empty.
45 if(runtime_MSpanList_IsEmpty(&c
->nonempty
)) {
46 if(!MCentral_Grow(c
)) {
53 cap
= (s
->npages
<< PageShift
) / s
->elemsize
;
55 *pfirst
= s
->freelist
;
59 runtime_MSpanList_Remove(s
);
60 runtime_MSpanList_Insert(&c
->empty
, s
);
65 // Free the list of objects back into the central free list.
67 runtime_MCentral_FreeList(MCentral
*c
, MLink
*start
)
72 for(; start
!= nil
; start
= next
) {
74 MCentral_Free(c
, start
);
79 // Helper: free one object back into the central free list.
81 MCentral_Free(MCentral
*c
, void *v
)
88 s
= runtime_MHeap_Lookup(&runtime_mheap
, v
);
89 if(s
== nil
|| s
->ref
== 0)
90 runtime_throw("invalid free");
92 // Move to nonempty if necessary.
93 if(s
->freelist
== nil
) {
94 runtime_MSpanList_Remove(s
);
95 runtime_MSpanList_Insert(&c
->nonempty
, s
);
98 // Add v back to s's free list.
100 p
->next
= s
->freelist
;
104 // If s is completely freed, return it to the heap.
106 size
= runtime_class_to_size
[c
->sizeclass
];
107 runtime_MSpanList_Remove(s
);
108 runtime_unmarkspan((byte
*)(s
->start
<<PageShift
), s
->npages
<<PageShift
);
109 *(uintptr
*)(s
->start
<<PageShift
) = 1; // needs zeroing
111 c
->nfree
-= (s
->npages
<< PageShift
) / size
;
113 runtime_MHeap_Free(&runtime_mheap
, s
, 0);
118 // Free n objects from a span s back into the central free list c.
121 runtime_MCentral_FreeSpan(MCentral
*c
, MSpan
*s
, int32 n
, MLink
*start
, MLink
*end
)
127 // Move to nonempty if necessary.
128 if(s
->freelist
== nil
) {
129 runtime_MSpanList_Remove(s
);
130 runtime_MSpanList_Insert(&c
->nonempty
, s
);
133 // Add the objects back to s's free list.
134 end
->next
= s
->freelist
;
139 // If s is completely freed, return it to the heap.
141 size
= runtime_class_to_size
[c
->sizeclass
];
142 runtime_MSpanList_Remove(s
);
143 *(uintptr
*)(s
->start
<<PageShift
) = 1; // needs zeroing
145 c
->nfree
-= (s
->npages
<< PageShift
) / size
;
147 runtime_unmarkspan((byte
*)(s
->start
<<PageShift
), s
->npages
<<PageShift
);
148 runtime_MHeap_Free(&runtime_mheap
, s
, 0);
155 runtime_MGetSizeClassInfo(int32 sizeclass
, uintptr
*sizep
, int32
*npagesp
, int32
*nobj
)
160 npages
= runtime_class_to_allocnpages
[sizeclass
];
161 size
= runtime_class_to_size
[sizeclass
];
164 *nobj
= (npages
<< PageShift
) / size
;
167 // Fetch a new span from the heap and
168 // carve into objects for the free list.
170 MCentral_Grow(MCentral
*c
)
179 runtime_MGetSizeClassInfo(c
->sizeclass
, &size
, &npages
, &n
);
180 s
= runtime_MHeap_Alloc(&runtime_mheap
, npages
, c
->sizeclass
, 0, 1);
182 // TODO(rsc): Log out of memory
187 // Carve span into sequence of blocks.
188 tailp
= &s
->freelist
;
189 p
= (byte
*)(s
->start
<< PageShift
);
190 s
->limit
= p
+ size
*n
;
198 runtime_markspan((byte
*)(s
->start
<<PageShift
), size
, n
, size
*n
< (s
->npages
<<PageShift
));
202 runtime_MSpanList_Insert(&c
->nonempty
, s
);