From 30b2b767d6d902787b90476fd00eee4c9b3a3f15 Mon Sep 17 00:00:00 2001 From: Mark Freeman Date: Mon, 28 Apr 2025 13:32:14 -0400 Subject: [PATCH] pkgbits: alias RelocKind with a SectionKind type I think that SectionKind better conveys the original intent here, and goes nicely with codifying section relative indices. Change-Id: I96a245e67295a5f9f8e462756a14f60eccec6862 Reviewed-on: https://go-review.googlesource.com/c/go/+/668538 Reviewed-by: Mark Freeman LUCI-TryBot-Result: Go LUCI Auto-Submit: Mark Freeman Reviewed-by: Robert Griesemer --- src/internal/pkgbits/decoder.go | 24 ++++++++++++------------ src/internal/pkgbits/encoder.go | 14 +++++++------- src/internal/pkgbits/reloc.go | 5 +++-- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/internal/pkgbits/decoder.go b/src/internal/pkgbits/decoder.go index 6268ed13fc..9a8a3da240 100644 --- a/src/internal/pkgbits/decoder.go +++ b/src/internal/pkgbits/decoder.go @@ -109,7 +109,7 @@ func NewPkgDecoder(pkgPath, input string) PkgDecoder { } // NumElems returns the number of elements in section k. -func (pr *PkgDecoder) NumElems(k RelocKind) int { +func (pr *PkgDecoder) NumElems(k SectionKind) int { count := int(pr.elemEndsEnds[k]) if k > 0 { count -= int(pr.elemEndsEnds[k-1]) @@ -131,7 +131,7 @@ func (pr *PkgDecoder) Fingerprint() [8]byte { // AbsIdx returns the absolute index for the given (section, index) // pair. -func (pr *PkgDecoder) AbsIdx(k RelocKind, idx RelIndex) int { +func (pr *PkgDecoder) AbsIdx(k SectionKind, idx RelIndex) int { absIdx := int(idx) if k > 0 { absIdx += int(pr.elemEndsEnds[k-1]) @@ -144,7 +144,7 @@ func (pr *PkgDecoder) AbsIdx(k RelocKind, idx RelIndex) int { // DataIdx returns the raw element bitstream for the given (section, // index) pair. -func (pr *PkgDecoder) DataIdx(k RelocKind, idx RelIndex) string { +func (pr *PkgDecoder) DataIdx(k SectionKind, idx RelIndex) string { absIdx := pr.AbsIdx(k, idx) var start uint32 @@ -163,7 +163,7 @@ func (pr *PkgDecoder) StringIdx(idx RelIndex) string { // NewDecoder returns a Decoder for the given (section, index) pair, // and decodes the given SyncMarker from the element bitstream. -func (pr *PkgDecoder) NewDecoder(k RelocKind, idx RelIndex, marker SyncMarker) Decoder { +func (pr *PkgDecoder) NewDecoder(k SectionKind, idx RelIndex, marker SyncMarker) Decoder { r := pr.NewDecoderRaw(k, idx) r.Sync(marker) return r @@ -173,7 +173,7 @@ func (pr *PkgDecoder) NewDecoder(k RelocKind, idx RelIndex, marker SyncMarker) D // and decodes the given SyncMarker from the element bitstream. // If possible the Decoder should be RetireDecoder'd when it is no longer // needed, this will avoid heap allocations. -func (pr *PkgDecoder) TempDecoder(k RelocKind, idx RelIndex, marker SyncMarker) Decoder { +func (pr *PkgDecoder) TempDecoder(k SectionKind, idx RelIndex, marker SyncMarker) Decoder { r := pr.TempDecoderRaw(k, idx) r.Sync(marker) return r @@ -187,7 +187,7 @@ func (pr *PkgDecoder) RetireDecoder(d *Decoder) { // NewDecoderRaw returns a Decoder for the given (section, index) pair. // // Most callers should use NewDecoder instead. -func (pr *PkgDecoder) NewDecoderRaw(k RelocKind, idx RelIndex) Decoder { +func (pr *PkgDecoder) NewDecoderRaw(k SectionKind, idx RelIndex) Decoder { r := Decoder{ common: pr, k: k, @@ -199,13 +199,13 @@ func (pr *PkgDecoder) NewDecoderRaw(k RelocKind, idx RelIndex) Decoder { r.Relocs = make([]RelocEnt, r.Len()) for i := range r.Relocs { r.Sync(SyncReloc) - r.Relocs[i] = RelocEnt{RelocKind(r.Len()), RelIndex(r.Len())} + r.Relocs[i] = RelocEnt{SectionKind(r.Len()), RelIndex(r.Len())} } return r } -func (pr *PkgDecoder) TempDecoderRaw(k RelocKind, idx RelIndex) Decoder { +func (pr *PkgDecoder) TempDecoderRaw(k SectionKind, idx RelIndex) Decoder { r := Decoder{ common: pr, k: k, @@ -223,7 +223,7 @@ func (pr *PkgDecoder) TempDecoderRaw(k RelocKind, idx RelIndex) Decoder { } for i := range r.Relocs { r.Sync(SyncReloc) - r.Relocs[i] = RelocEnt{RelocKind(r.Len()), RelIndex(r.Len())} + r.Relocs[i] = RelocEnt{SectionKind(r.Len()), RelIndex(r.Len())} } return r @@ -237,7 +237,7 @@ type Decoder struct { Relocs []RelocEnt Data strings.Reader - k RelocKind + k SectionKind Idx RelIndex } @@ -292,7 +292,7 @@ func (r *Decoder) rawVarint() int64 { return x } -func (r *Decoder) rawReloc(k RelocKind, idx int) RelIndex { +func (r *Decoder) rawReloc(k SectionKind, idx int) RelIndex { e := r.Relocs[idx] assert(e.Kind == k) return e.Idx @@ -401,7 +401,7 @@ func (r *Decoder) Code(mark SyncMarker) int { // Reloc decodes a relocation of expected section k from the element // bitstream and returns an index to the referenced element. -func (r *Decoder) Reloc(k RelocKind) RelIndex { +func (r *Decoder) Reloc(k SectionKind) RelIndex { r.Sync(SyncUseReloc) return r.rawReloc(k, r.Len()) } diff --git a/src/internal/pkgbits/encoder.go b/src/internal/pkgbits/encoder.go index 2d5b8f0ab7..c6d5eb46ec 100644 --- a/src/internal/pkgbits/encoder.go +++ b/src/internal/pkgbits/encoder.go @@ -121,7 +121,7 @@ func (pw *PkgEncoder) StringIdx(s string) RelIndex { // NewEncoder returns an Encoder for a new element within the given // section, and encodes the given SyncMarker as the start of the // element bitstream. -func (pw *PkgEncoder) NewEncoder(k RelocKind, marker SyncMarker) Encoder { +func (pw *PkgEncoder) NewEncoder(k SectionKind, marker SyncMarker) Encoder { e := pw.NewEncoderRaw(k) e.Sync(marker) return e @@ -131,7 +131,7 @@ func (pw *PkgEncoder) NewEncoder(k RelocKind, marker SyncMarker) Encoder { // section. // // Most callers should use NewEncoder instead. -func (pw *PkgEncoder) NewEncoderRaw(k RelocKind) Encoder { +func (pw *PkgEncoder) NewEncoderRaw(k SectionKind) Encoder { idx := RelIndex(len(pw.elems[k])) pw.elems[k] = append(pw.elems[k], "") // placeholder @@ -153,7 +153,7 @@ type Encoder struct { encodingRelocHeader bool - k RelocKind + k SectionKind Idx RelIndex // index within relocation section } @@ -210,8 +210,8 @@ func (w *Encoder) rawVarint(x int64) { w.rawUvarint(ux) } -func (w *Encoder) rawReloc(r RelocKind, idx RelIndex) int { - e := RelocEnt{r, idx} +func (w *Encoder) rawReloc(k SectionKind, idx RelIndex) int { + e := RelocEnt{k, idx} if w.RelocMap != nil { if i, ok := w.RelocMap[e]; ok { return int(i) @@ -302,9 +302,9 @@ func (w *Encoder) Uint(x uint) { w.Uint64(uint64(x)) } // Note: Only the index is formally written into the element // bitstream, so bitstream decoders must know from context which // section an encoded relocation refers to. -func (w *Encoder) Reloc(r RelocKind, idx RelIndex) { +func (w *Encoder) Reloc(k SectionKind, idx RelIndex) { w.Sync(SyncUseReloc) - w.Len(w.rawReloc(r, idx)) + w.Len(w.rawReloc(k, idx)) } // Code encodes and writes a Code value into the element bitstream. diff --git a/src/internal/pkgbits/reloc.go b/src/internal/pkgbits/reloc.go index 63be6c0afc..e4b494d749 100644 --- a/src/internal/pkgbits/reloc.go +++ b/src/internal/pkgbits/reloc.go @@ -8,9 +8,10 @@ package pkgbits // unified export data. Any object given a dedicated section can be referred to // via a section / index pair (and thus dereferenced) in other sections. type RelocKind int32 // TODO(markfreeman): Replace with uint8. +type SectionKind = RelocKind const ( - RelocString RelocKind = iota + RelocString SectionKind = iota RelocMeta RelocPosBase RelocPkg @@ -36,7 +37,7 @@ type RelIndex = Index // table. All elements are preceded by a reference table which provides // locations for all dereferences that the element may use. type RelocEnt struct { - Kind RelocKind + Kind SectionKind Idx RelIndex } -- 2.51.0