File: layout.go

package info (click to toggle)
golang-github-cilium-ebpf 0.17.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,684 kB
  • sloc: ansic: 1,259; makefile: 127; python: 113; awk: 29; sh: 24
file content (41 lines) | stat: -rw-r--r-- 1,017 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found at https://go.dev/LICENSE.

package sysenc

import (
	"reflect"
	"sync"
)

var hasUnexportedFieldsCache sync.Map // map[reflect.Type]bool

func hasUnexportedFields(typ reflect.Type) bool {
	switch typ.Kind() {
	case reflect.Slice, reflect.Array, reflect.Pointer:
		return hasUnexportedFields(typ.Elem())

	case reflect.Struct:
		if unexported, ok := hasUnexportedFieldsCache.Load(typ); ok {
			return unexported.(bool)
		}

		unexported := false
		for i, n := 0, typ.NumField(); i < n; i++ {
			field := typ.Field(i)
			// Package binary allows _ fields but always writes zeroes into them.
			if (!field.IsExported() && field.Name != "_") || hasUnexportedFields(field.Type) {
				unexported = true
				break
			}
		}

		hasUnexportedFieldsCache.Store(typ, unexported)
		return unexported

	default:
		// NB: It's not clear what this means for Chan and so on.
		return false
	}
}