nih.mx raw

   1  // Copyright 2014 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  
   5  package sys
   6  
   7  // NOTE: keep in sync with cmd/compile/internal/types.CalcSize
   8  // to make the compiler recognize this as an intrinsic type.
   9  type nih struct{}
  10  
  11  // NotInHeap is a type must never be allocated from the GC'd heap or on the stack,
  12  // and is called not-in-heap.
  13  //
  14  // Other types can embed NotInHeap to make it not-in-heap. Specifically, pointers
  15  // to these types must always fail the `runtime.inheap` check. The type may be used
  16  // for global variables, or for objects in unmanaged memory (e.g., allocated with
  17  // `sysAlloc`, `persistentalloc`, `fixalloc`, or from a manually-managed span).
  18  //
  19  // Specifically:
  20  //
  21  // 1. `new(T)`, `make([]T)`, `append([]T, ...)` and implicit heap
  22  // allocation of T are disallowed. (Though implicit allocations are
  23  // disallowed in the runtime anyway.)
  24  //
  25  // 2. A pointer to a regular type (other than `unsafe.Pointer`) cannot be
  26  // converted to a pointer to a not-in-heap type, even if they have the
  27  // same underlying type.
  28  //
  29  // 3. Any type that containing a not-in-heap type is itself considered as not-in-heap.
  30  //
  31  // - Structs and arrays are not-in-heap if their elements are not-in-heap.
  32  // - Maps and channels contains no-in-heap types are disallowed.
  33  //
  34  // 4. Write barriers on pointers to not-in-heap types can be omitted.
  35  //
  36  // The last point is the real benefit of NotInHeap. The runtime uses
  37  // it for low-level internal structures to avoid memory barriers in the
  38  // scheduler and the memory allocator where they are illegal or simply
  39  // inefficient. This mechanism is reasonably safe and does not compromise
  40  // the readability of the runtime.
  41  type NotInHeap struct{ _ nih }
  42