This source file includes following definitions.
- runtime·SizeToClass
- runtime·InitSizes
- runtime·roundupsize
#include "runtime.h"
#include "arch_GOARCH.h"
#include "malloc.h"
#include "../../cmd/ld/textflag.h"
#pragma dataflag NOPTR
int32 runtime·class_to_size[NumSizeClasses];
#pragma dataflag NOPTR
int32 runtime·class_to_allocnpages[NumSizeClasses];
#pragma dataflag NOPTR
int8 runtime·size_to_class8[1024/8 + 1];
#pragma dataflag NOPTR
int8 runtime·size_to_class128[(MaxSmallSize-1024)/128 + 1];
void runtime·testdefersizes(void);
int32
runtime·SizeToClass(int32 size)
{
if(size > MaxSmallSize)
runtime·throw("SizeToClass - invalid size");
if(size > 1024-8)
return runtime·size_to_class128[(size-1024+127) >> 7];
return runtime·size_to_class8[(size+7)>>3];
}
void
runtime·InitSizes(void)
{
int32 align, sizeclass, size, nextsize, n;
uint32 i;
uintptr allocsize, npages;
runtime·class_to_size[0] = 0;
sizeclass = 1;
align = 8;
for(size = align; size <= MaxSmallSize; size += align) {
if((size&(size-1)) == 0) {
if(size >= 2048)
align = 256;
else if(size >= 128)
align = size / 8;
else if(size >= 16)
align = 16;
}
if((align&(align-1)) != 0)
runtime·throw("InitSizes - bug");
allocsize = PageSize;
while(allocsize%size > allocsize/8)
allocsize += PageSize;
npages = allocsize >> PageShift;
if(sizeclass > 1 &&
npages == runtime·class_to_allocnpages[sizeclass-1] &&
allocsize/size == allocsize/runtime·class_to_size[sizeclass-1]) {
runtime·class_to_size[sizeclass-1] = size;
continue;
}
runtime·class_to_allocnpages[sizeclass] = npages;
runtime·class_to_size[sizeclass] = size;
sizeclass++;
}
if(sizeclass != NumSizeClasses) {
runtime·printf("sizeclass=%d NumSizeClasses=%d\n", sizeclass, NumSizeClasses);
runtime·throw("InitSizes - bad NumSizeClasses");
}
nextsize = 0;
for (sizeclass = 1; sizeclass < NumSizeClasses; sizeclass++) {
for(; nextsize < 1024 && nextsize <= runtime·class_to_size[sizeclass]; nextsize+=8)
runtime·size_to_class8[nextsize/8] = sizeclass;
if(nextsize >= 1024)
for(; nextsize <= runtime·class_to_size[sizeclass]; nextsize += 128)
runtime·size_to_class128[(nextsize-1024)/128] = sizeclass;
}
if(0) {
for(n=0; n < MaxSmallSize; n++) {
sizeclass = runtime·SizeToClass(n);
if(sizeclass < 1 || sizeclass >= NumSizeClasses || runtime·class_to_size[sizeclass] < n) {
runtime·printf("size=%d sizeclass=%d runtime·class_to_size=%d\n", n, sizeclass, runtime·class_to_size[sizeclass]);
runtime·printf("incorrect SizeToClass");
goto dump;
}
if(sizeclass > 1 && runtime·class_to_size[sizeclass-1] >= n) {
runtime·printf("size=%d sizeclass=%d runtime·class_to_size=%d\n", n, sizeclass, runtime·class_to_size[sizeclass]);
runtime·printf("SizeToClass too big");
goto dump;
}
}
}
runtime·testdefersizes();
for(i=0; i<nelem(runtime·class_to_size); i++)
mstats.by_size[i].size = runtime·class_to_size[i];
return;
dump:
if(1){
runtime·printf("NumSizeClasses=%d\n", NumSizeClasses);
runtime·printf("runtime·class_to_size:");
for(sizeclass=0; sizeclass<NumSizeClasses; sizeclass++)
runtime·printf(" %d", runtime·class_to_size[sizeclass]);
runtime·printf("\n\n");
runtime·printf("size_to_class8:");
for(i=0; i<nelem(runtime·size_to_class8); i++)
runtime·printf(" %d=>%d(%d)\n", i*8, runtime·size_to_class8[i],
runtime·class_to_size[runtime·size_to_class8[i]]);
runtime·printf("\n");
runtime·printf("size_to_class128:");
for(i=0; i<nelem(runtime·size_to_class128); i++)
runtime·printf(" %d=>%d(%d)\n", i*128, runtime·size_to_class128[i],
runtime·class_to_size[runtime·size_to_class128[i]]);
runtime·printf("\n");
}
runtime·throw("InitSizes failed");
}
uintptr
runtime·roundupsize(uintptr size)
{
if(size < MaxSmallSize) {
if(size <= 1024-8)
return runtime·class_to_size[runtime·size_to_class8[(size+7)>>3]];
else
return runtime·class_to_size[runtime·size_to_class128[(size-1024+127) >> 7]];
}
if(size + PageSize < size)
return size;
return ROUND(size, PageSize);
}