package wazevoapi

import (
	
	
	
	
)

var PerfMap *Perfmap

func init() {
	if PerfMapEnabled {
		 := os.Getpid()
		 := "/tmp/perf-" + strconv.Itoa() + ".map"

		,  := os.OpenFile(, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0o644)
		if  != nil {
			panic()
		}

		PerfMap = &Perfmap{fh: }
	}
}

// Perfmap holds perfmap entries to be flushed into a perfmap file.
type Perfmap struct {
	entries []entry
	mux     sync.Mutex
	fh      *os.File
}

type entry struct {
	index  int
	offset int64
	size   uint64
	name   string
}

func ( *Perfmap) () {
	.mux.Lock()
}

func ( *Perfmap) () {
	.mux.Unlock()
}

// AddModuleEntry adds a perfmap entry into the perfmap file.
// index is the index of the function in the module, offset is the offset of the function in the module,
// size is the size of the function, and name is the name of the function.
//
// Note that the entries are not flushed into the perfmap file until Flush is called,
// and the entries are module-scoped; Perfmap must be locked until Flush is called.
func ( *Perfmap) ( int,  int64,  uint64,  string) {
	 := entry{index: , offset: , size: , name: }
	if .entries == nil {
		.entries = []entry{}
		return
	}
	.entries = append(.entries, )
}

// Flush writes the perfmap entries into the perfmap file where the entries are adjusted by the given `addr` and `functionOffsets`.
func ( *Perfmap) ( uintptr,  []int) {
	defer func() {
		_ = .fh.Sync()
	}()

	for ,  := range .entries {
		if ,  := .fh.WriteString(fmt.Sprintf("%x %s %s\n",
			uintptr(.offset)++uintptr([.index]),
			strconv.FormatUint(.size, 16),
			.name,
		));  != nil {
			panic()
		}
	}
	.entries = .entries[:0]
}

// Clear clears the perfmap entries not yet flushed.
func ( *Perfmap) () {
	.entries = .entries[:0]
}

// AddEntry writes a perfmap entry directly into the perfmap file, not using the entries.
func ( *Perfmap) ( uintptr,  uint64,  string) {
	,  := .fh.WriteString(fmt.Sprintf("%x %s %s\n",
		,
		strconv.FormatUint(, 16),
		,
	))
	if  != nil {
		panic()
	}
}