docs: add commets for gc mutex

docs: add commets for tinygo gc
This commit is contained in:
Haolan
2025-11-13 20:23:15 +08:00
parent eec2c271bd
commit bb29e8c768
3 changed files with 25 additions and 2 deletions

View File

@@ -4,6 +4,9 @@ package tinygogc
import "unsafe"
// LLGoPackage instructs the LLGo linker to wrap C standard library memory allocation
// functions (malloc, realloc, calloc) so they use the tinygogc allocator instead.
// This ensures all memory allocations go through the GC, including C library calls.
const LLGoPackage = "link: --wrap=malloc --wrap=realloc --wrap=calloc"
//export __wrap_malloc
@@ -12,8 +15,13 @@ func __wrap_malloc(size uintptr) unsafe.Pointer {
}
//export __wrap_calloc
func __wrap_calloc(size uintptr) unsafe.Pointer {
return Alloc(size)
func __wrap_calloc(nmemb, size uintptr) unsafe.Pointer {
totalSize := nmemb * size
// Check for multiplication overflow
if nmemb != 0 && totalSize/nmemb != size {
return nil // Overflow
}
return Alloc(totalSize)
}
//export __wrap_realloc

View File

@@ -16,6 +16,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package tinygogc implements a conservative mark-and-sweep garbage collector
// for baremetal environments where the standard Go runtime and bdwgc are unavailable.
//
// This implementation is based on TinyGo's GC and is designed for resource-constrained
// embedded systems. It uses a block-based allocator with conservative pointer scanning.
//
// Build tags:
// - baremetal: Enables this GC for baremetal targets
// - testGC: Enables testing mode with mock implementations
//
// Memory Layout:
// The heap is divided into fixed-size blocks (32 bytes on 64-bit). Metadata is stored
// at the end of the heap, using 2 bits per block to track state (free/head/tail/mark).
package tinygogc
import (

View File

@@ -1,5 +1,6 @@
package tinygogc
// TODO(MeteorsLiu): mutex lock for baremetal GC
type mutex struct{}
func lock(m *mutex) {}