mgc0.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2012 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. // Garbage collector (GC)
  5. // GC instruction opcodes.
  6. //
  7. // The opcode of an instruction is followed by zero or more
  8. // arguments to the instruction.
  9. //
  10. // Meaning of arguments:
  11. // off Offset (in bytes) from the start of the current object
  12. // objgc Pointer to GC info of an object
  13. // objgcrel Offset to GC info of an object
  14. // len Length of an array
  15. // elemsize Size (in bytes) of an element
  16. // size Size (in bytes)
  17. //
  18. // NOTE: There is a copy of these in ../reflect/type.go.
  19. // They must be kept in sync.
  20. enum {
  21. GC_END, // End of object, loop or subroutine. Args: none
  22. GC_PTR, // A typed pointer. Args: (off, objgc)
  23. GC_APTR, // Pointer to an arbitrary object. Args: (off)
  24. GC_ARRAY_START, // Start an array with a fixed length. Args: (off, len, elemsize)
  25. GC_ARRAY_NEXT, // The next element of an array. Args: none
  26. GC_CALL, // Call a subroutine. Args: (off, objgcrel)
  27. GC_CHAN_PTR, // Go channel. Args: (off, ChanType*)
  28. GC_STRING, // Go string. Args: (off)
  29. GC_EFACE, // interface{}. Args: (off)
  30. GC_IFACE, // interface{...}. Args: (off)
  31. GC_SLICE, // Go slice. Args: (off, objgc)
  32. GC_REGION, // A region/part of the current object. Args: (off, size, objgc)
  33. GC_NUM_INSTR, // Number of instruction opcodes
  34. };
  35. enum {
  36. // Size of GC's fixed stack.
  37. //
  38. // The current GC implementation permits:
  39. // - at most 1 stack allocation because of GC_CALL
  40. // - at most GC_STACK_CAPACITY allocations because of GC_ARRAY_START
  41. GC_STACK_CAPACITY = 8,
  42. };
  43. enum {
  44. ScanStackByFrames = 1,
  45. IgnorePreciseGC = 0,
  46. // Four bits per word (see #defines below).
  47. wordsPerBitmapWord = sizeof(void*)*8/4,
  48. bitShift = sizeof(void*)*8/4,
  49. };
  50. // Bits in per-word bitmap.
  51. // #defines because enum might not be able to hold the values.
  52. //
  53. // Each word in the bitmap describes wordsPerBitmapWord words
  54. // of heap memory. There are 4 bitmap bits dedicated to each heap word,
  55. // so on a 64-bit system there is one bitmap word per 16 heap words.
  56. // The bits in the word are packed together by type first, then by
  57. // heap location, so each 64-bit bitmap word consists of, from top to bottom,
  58. // the 16 bitMarked bits for the corresponding heap words,
  59. // then the 16 bitScan/bitBlockBoundary bits, then the 16 bitAllocated bits.
  60. // This layout makes it easier to iterate over the bits of a given type.
  61. //
  62. // The bitmap starts at mheap.arena_start and extends *backward* from
  63. // there. On a 64-bit system the off'th word in the arena is tracked by
  64. // the off/16+1'th word before mheap.arena_start. (On a 32-bit system,
  65. // the only difference is that the divisor is 8.)
  66. //
  67. // To pull out the bits corresponding to a given pointer p, we use:
  68. //
  69. // off = p - (uintptr*)mheap.arena_start; // word offset
  70. // b = (uintptr*)mheap.arena_start - off/wordsPerBitmapWord - 1;
  71. // shift = off % wordsPerBitmapWord
  72. // bits = *b >> shift;
  73. // /* then test bits & bitAllocated, bits & bitMarked, etc. */
  74. //
  75. #define bitAllocated ((uintptr)1<<(bitShift*0)) /* block start; eligible for garbage collection */
  76. #define bitScan ((uintptr)1<<(bitShift*1)) /* when bitAllocated is set */
  77. #define bitMarked ((uintptr)1<<(bitShift*2)) /* when bitAllocated is set */
  78. #define bitBlockBoundary ((uintptr)1<<(bitShift*1)) /* when bitAllocated is NOT set - mark for FlagNoGC objects */
  79. #define bitMask (bitAllocated | bitScan | bitMarked)