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