msize.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // Malloc small size classes.
  5. //
  6. // See malloc.h for overview.
  7. //
  8. // The size classes are chosen so that rounding an allocation
  9. // request up to the next size class wastes at most 12.5% (1.125x).
  10. //
  11. // Each size class has its own page count that gets allocated
  12. // and chopped up when new objects of the size class are needed.
  13. // That page count is chosen so that chopping up the run of
  14. // pages into objects of the given size wastes at most 12.5% (1.125x)
  15. // of the memory. It is not necessary that the cutoff here be
  16. // the same as above.
  17. //
  18. // The two sources of waste multiply, so the worst possible case
  19. // for the above constraints would be that allocations of some
  20. // size might have a 26.6% (1.266x) overhead.
  21. // In practice, only one of the wastes comes into play for a
  22. // given size (sizes < 512 waste mainly on the round-up,
  23. // sizes > 512 waste mainly on the page chopping).
  24. //
  25. // TODO(rsc): Compute max waste for any given size.
  26. #include "runtime.h"
  27. #include "arch.h"
  28. #include "malloc.h"
  29. int32 runtime_class_to_size[NumSizeClasses];
  30. int32 runtime_class_to_allocnpages[NumSizeClasses];
  31. // The SizeToClass lookup is implemented using two arrays,
  32. // one mapping sizes <= 1024 to their class and one mapping
  33. // sizes >= 1024 and <= MaxSmallSize to their class.
  34. // All objects are 8-aligned, so the first array is indexed by
  35. // the size divided by 8 (rounded up). Objects >= 1024 bytes
  36. // are 128-aligned, so the second array is indexed by the
  37. // size divided by 128 (rounded up). The arrays are filled in
  38. // by InitSizes.
  39. int8 runtime_size_to_class8[1024/8 + 1];
  40. int8 runtime_size_to_class128[(MaxSmallSize-1024)/128 + 1];
  41. int32
  42. runtime_SizeToClass(int32 size)
  43. {
  44. if(size > MaxSmallSize)
  45. runtime_throw("SizeToClass - invalid size");
  46. if(size > 1024-8)
  47. return runtime_size_to_class128[(size-1024+127) >> 7];
  48. return runtime_size_to_class8[(size+7)>>3];
  49. }
  50. void
  51. runtime_InitSizes(void)
  52. {
  53. int32 align, sizeclass, size, nextsize, n;
  54. uint32 i;
  55. uintptr allocsize, npages;
  56. // Initialize the runtime_class_to_size table (and choose class sizes in the process).
  57. runtime_class_to_size[0] = 0;
  58. sizeclass = 1; // 0 means no class
  59. align = 8;
  60. for(size = align; size <= MaxSmallSize; size += align) {
  61. if((size&(size-1)) == 0) { // bump alignment once in a while
  62. if(size >= 2048)
  63. align = 256;
  64. else if(size >= 128)
  65. align = size / 8;
  66. else if(size >= 16)
  67. align = 16; // required for x86 SSE instructions, if we want to use them
  68. }
  69. if((align&(align-1)) != 0)
  70. runtime_throw("InitSizes - bug");
  71. // Make the allocnpages big enough that
  72. // the leftover is less than 1/8 of the total,
  73. // so wasted space is at most 12.5%.
  74. allocsize = PageSize;
  75. while(allocsize%size > allocsize/8)
  76. allocsize += PageSize;
  77. npages = allocsize >> PageShift;
  78. // If the previous sizeclass chose the same
  79. // allocation size and fit the same number of
  80. // objects into the page, we might as well
  81. // use just this size instead of having two
  82. // different sizes.
  83. if(sizeclass > 1 &&
  84. (int32)npages == runtime_class_to_allocnpages[sizeclass-1] &&
  85. allocsize/size == allocsize/runtime_class_to_size[sizeclass-1]) {
  86. runtime_class_to_size[sizeclass-1] = size;
  87. continue;
  88. }
  89. runtime_class_to_allocnpages[sizeclass] = npages;
  90. runtime_class_to_size[sizeclass] = size;
  91. sizeclass++;
  92. }
  93. if(sizeclass != NumSizeClasses) {
  94. runtime_printf("sizeclass=%d NumSizeClasses=%d\n", sizeclass, NumSizeClasses);
  95. runtime_throw("InitSizes - bad NumSizeClasses");
  96. }
  97. // Initialize the size_to_class tables.
  98. nextsize = 0;
  99. for (sizeclass = 1; sizeclass < NumSizeClasses; sizeclass++) {
  100. for(; nextsize < 1024 && nextsize <= runtime_class_to_size[sizeclass]; nextsize+=8)
  101. runtime_size_to_class8[nextsize/8] = sizeclass;
  102. if(nextsize >= 1024)
  103. for(; nextsize <= runtime_class_to_size[sizeclass]; nextsize += 128)
  104. runtime_size_to_class128[(nextsize-1024)/128] = sizeclass;
  105. }
  106. // Double-check SizeToClass.
  107. if(0) {
  108. for(n=0; n < MaxSmallSize; n++) {
  109. sizeclass = runtime_SizeToClass(n);
  110. if(sizeclass < 1 || sizeclass >= NumSizeClasses || runtime_class_to_size[sizeclass] < n) {
  111. runtime_printf("size=%d sizeclass=%d runtime_class_to_size=%d\n", n, sizeclass, runtime_class_to_size[sizeclass]);
  112. runtime_printf("incorrect SizeToClass");
  113. goto dump;
  114. }
  115. if(sizeclass > 1 && runtime_class_to_size[sizeclass-1] >= n) {
  116. runtime_printf("size=%d sizeclass=%d runtime_class_to_size=%d\n", n, sizeclass, runtime_class_to_size[sizeclass]);
  117. runtime_printf("SizeToClass too big");
  118. goto dump;
  119. }
  120. }
  121. }
  122. // Copy out for statistics table.
  123. for(i=0; i<nelem(runtime_class_to_size); i++)
  124. mstats.by_size[i].size = runtime_class_to_size[i];
  125. return;
  126. dump:
  127. if(1){
  128. runtime_printf("NumSizeClasses=%d\n", NumSizeClasses);
  129. runtime_printf("runtime_class_to_size:");
  130. for(sizeclass=0; sizeclass<NumSizeClasses; sizeclass++)
  131. runtime_printf(" %d", runtime_class_to_size[sizeclass]);
  132. runtime_printf("\n\n");
  133. runtime_printf("size_to_class8:");
  134. for(i=0; i<nelem(runtime_size_to_class8); i++)
  135. runtime_printf(" %d=>%d(%d)\n", i*8, runtime_size_to_class8[i],
  136. runtime_class_to_size[runtime_size_to_class8[i]]);
  137. runtime_printf("\n");
  138. runtime_printf("size_to_class128:");
  139. for(i=0; i<nelem(runtime_size_to_class128); i++)
  140. runtime_printf(" %d=>%d(%d)\n", i*128, runtime_size_to_class128[i],
  141. runtime_class_to_size[runtime_size_to_class128[i]]);
  142. runtime_printf("\n");
  143. }
  144. runtime_throw("InitSizes failed");
  145. }
  146. // Returns size of the memory block that mallocgc will allocate if you ask for the size.
  147. uintptr
  148. runtime_roundupsize(uintptr size)
  149. {
  150. if(size < MaxSmallSize) {
  151. if(size <= 1024-8)
  152. return runtime_class_to_size[runtime_size_to_class8[(size+7)>>3]];
  153. else
  154. return runtime_class_to_size[runtime_size_to_class128[(size-1024+127) >> 7]];
  155. }
  156. if(size + PageSize < size)
  157. return size;
  158. return ROUND(size, PageSize);
  159. }