LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // Malloc small size classes. | 5 // Malloc small size classes. |
6 // | 6 // |
7 // See malloc.h for overview. | 7 // See malloc.h for overview. |
8 // | 8 // |
9 // The size classes are chosen so that rounding an allocation | 9 // The size classes are chosen so that rounding an allocation |
10 // request up to the next size class wastes at most 12.5% (1.125x). | 10 // request up to the next size class wastes at most 12.5% (1.125x). |
(...skipping 26 matching lines...) Expand all Loading... |
37 // sizes >= 1024 and <= MaxSmallSize to their class. | 37 // sizes >= 1024 and <= MaxSmallSize to their class. |
38 // All objects are 8-aligned, so the first array is indexed by | 38 // All objects are 8-aligned, so the first array is indexed by |
39 // the size divided by 8 (rounded up). Objects >= 1024 bytes | 39 // the size divided by 8 (rounded up). Objects >= 1024 bytes |
40 // are 128-aligned, so the second array is indexed by the | 40 // are 128-aligned, so the second array is indexed by the |
41 // size divided by 128 (rounded up). The arrays are filled in | 41 // size divided by 128 (rounded up). The arrays are filled in |
42 // by InitSizes. | 42 // by InitSizes. |
43 | 43 |
44 int8 runtime·size_to_class8[1024/8 + 1]; | 44 int8 runtime·size_to_class8[1024/8 + 1]; |
45 int8 runtime·size_to_class128[(MaxSmallSize-1024)/128 + 1]; | 45 int8 runtime·size_to_class128[(MaxSmallSize-1024)/128 + 1]; |
46 | 46 |
47 static int32 | 47 int32 |
48 SizeToClass(int32 size) | 48 runtime·SizeToClass(int32 size) |
49 { | 49 { |
50 if(size > MaxSmallSize) | 50 if(size > MaxSmallSize) |
51 runtime·throw("SizeToClass - invalid size"); | 51 runtime·throw("SizeToClass - invalid size"); |
52 if(size > 1024-8) | 52 if(size > 1024-8) |
53 return runtime·size_to_class128[(size-1024+127) >> 7]; | 53 return runtime·size_to_class128[(size-1024+127) >> 7]; |
54 return runtime·size_to_class8[(size+7)>>3]; | 54 return runtime·size_to_class8[(size+7)>>3]; |
55 } | 55 } |
56 | 56 |
57 void | 57 void |
58 runtime·InitSizes(void) | 58 runtime·InitSizes(void) |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 for(; nextsize < 1024 && nextsize <= runtime·class_to_size[sizec
lass]; nextsize+=8) | 112 for(; nextsize < 1024 && nextsize <= runtime·class_to_size[sizec
lass]; nextsize+=8) |
113 runtime·size_to_class8[nextsize/8] = sizeclass; | 113 runtime·size_to_class8[nextsize/8] = sizeclass; |
114 if(nextsize >= 1024) | 114 if(nextsize >= 1024) |
115 for(; nextsize <= runtime·class_to_size[sizeclass]; next
size += 128) | 115 for(; nextsize <= runtime·class_to_size[sizeclass]; next
size += 128) |
116 runtime·size_to_class128[(nextsize-1024)/128] =
sizeclass; | 116 runtime·size_to_class128[(nextsize-1024)/128] =
sizeclass; |
117 } | 117 } |
118 | 118 |
119 // Double-check SizeToClass. | 119 // Double-check SizeToClass. |
120 if(0) { | 120 if(0) { |
121 for(n=0; n < MaxSmallSize; n++) { | 121 for(n=0; n < MaxSmallSize; n++) { |
122 » » » sizeclass = SizeToClass(n); | 122 » » » sizeclass = runtime·SizeToClass(n); |
123 if(sizeclass < 1 || sizeclass >= NumSizeClasses || runti
me·class_to_size[sizeclass] < n) { | 123 if(sizeclass < 1 || sizeclass >= NumSizeClasses || runti
me·class_to_size[sizeclass] < n) { |
124 runtime·printf("size=%d sizeclass=%d runtime·cla
ss_to_size=%d\n", n, sizeclass, runtime·class_to_size[sizeclass]); | 124 runtime·printf("size=%d sizeclass=%d runtime·cla
ss_to_size=%d\n", n, sizeclass, runtime·class_to_size[sizeclass]); |
125 runtime·printf("incorrect SizeToClass"); | 125 runtime·printf("incorrect SizeToClass"); |
126 goto dump; | 126 goto dump; |
127 } | 127 } |
128 if(sizeclass > 1 && runtime·class_to_size[sizeclass-1] >
= n) { | 128 if(sizeclass > 1 && runtime·class_to_size[sizeclass-1] >
= n) { |
129 runtime·printf("size=%d sizeclass=%d runtime·cla
ss_to_size=%d\n", n, sizeclass, runtime·class_to_size[sizeclass]); | 129 runtime·printf("size=%d sizeclass=%d runtime·cla
ss_to_size=%d\n", n, sizeclass, runtime·class_to_size[sizeclass]); |
130 runtime·printf("SizeToClass too big"); | 130 runtime·printf("SizeToClass too big"); |
131 goto dump; | 131 goto dump; |
132 } | 132 } |
(...skipping 18 matching lines...) Expand all Loading... |
151 runtime·class_to_size[runtime·size_to_class8[i]]
); | 151 runtime·class_to_size[runtime·size_to_class8[i]]
); |
152 runtime·printf("\n"); | 152 runtime·printf("\n"); |
153 runtime·printf("size_to_class128:"); | 153 runtime·printf("size_to_class128:"); |
154 for(i=0; i<nelem(runtime·size_to_class128); i++) | 154 for(i=0; i<nelem(runtime·size_to_class128); i++) |
155 runtime·printf(" %d=>%d(%d)\n", i*128, runtime·size_to_c
lass128[i], | 155 runtime·printf(" %d=>%d(%d)\n", i*128, runtime·size_to_c
lass128[i], |
156 runtime·class_to_size[runtime·size_to_class128[i
]]); | 156 runtime·class_to_size[runtime·size_to_class128[i
]]); |
157 runtime·printf("\n"); | 157 runtime·printf("\n"); |
158 } | 158 } |
159 runtime·throw("InitSizes failed"); | 159 runtime·throw("InitSizes failed"); |
160 } | 160 } |
LEFT | RIGHT |