github.com/cloudwego/frugal@v0.1.7/internal/loader/funcdata.go (about)

     1  /*
     2   * Copyright 2022 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package loader
    18  
    19  import (
    20      `sync`
    21      _ `unsafe`
    22  )
    23  
    24  const (
    25      _PCDATA_UnsafePoint       = 0
    26      _PCDATA_StackMapIndex     = 1
    27      _PCDATA_UnsafePointUnsafe = -2
    28  )
    29  
    30  //go:linkname lastmoduledatap runtime.lastmoduledatap
    31  //goland:noinspection GoUnusedGlobalVariable
    32  var lastmoduledatap *_ModuleData
    33  
    34  //go:linkname moduledataverify1 runtime.moduledataverify1
    35  func moduledataverify1(_ *_ModuleData)
    36  
    37  var (
    38      modLock sync.Mutex
    39      modList []*_ModuleData
    40  )
    41  
    42  func toZigzag(v int) int {
    43      return (v << 1) ^ (v >> 31)
    44  }
    45  
    46  func encodeFirst(v int) []byte {
    47      return encodeValue(v + 1)
    48  }
    49  
    50  func encodeValue(v int) []byte {
    51      return encodeVariant(toZigzag(v))
    52  }
    53  
    54  func encodeVariant(v int) []byte {
    55      var u int
    56      var r []byte
    57  
    58      /* split every 7 bits */
    59      for v > 127 {
    60          u = v & 0x7f
    61          v = v >> 7
    62          r = append(r, byte(u) | 0x80)
    63      }
    64  
    65      /* check for last one */
    66      if v == 0 {
    67          return r
    68      }
    69  
    70      /* add the last one */
    71      r = append(r, byte(v))
    72      return r
    73  }
    74  
    75  func registerModule(mod *_ModuleData) {
    76      modLock.Lock()
    77      modList = append(modList, mod)
    78      lastmoduledatap.next = mod
    79      lastmoduledatap = mod
    80      modLock.Unlock()
    81  }