github.com/goplus/llgo@v0.8.3/internal/runtime/z_c.go (about)

     1  /*
     2   * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
     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 runtime
    18  
    19  import (
    20  	"unsafe"
    21  
    22  	"github.com/goplus/llgo/internal/abi"
    23  	"github.com/goplus/llgo/internal/runtime/c"
    24  )
    25  
    26  // AllocU allocates uninitialized memory.
    27  func AllocU(size uintptr) unsafe.Pointer {
    28  	return c.Malloc(size)
    29  }
    30  
    31  // AllocZ allocates zero-initialized memory.
    32  func AllocZ(size uintptr) unsafe.Pointer {
    33  	ret := c.Malloc(size)
    34  	return c.Memset(ret, 0, size)
    35  }
    36  
    37  // Zeroinit initializes memory to zero.
    38  func Zeroinit(p c.Pointer, size uintptr) c.Pointer {
    39  	return c.Memset(p, 0, size)
    40  }
    41  
    42  // TracePanic prints panic message.
    43  func TracePanic(v Interface) {
    44  	kind := abi.Kind(v.tab._type.Kind_)
    45  	switch {
    46  	case kind == abi.String:
    47  		stringTracef(c.Stderr, c.Str("panic: %s\n"), *(*String)(v.data))
    48  	}
    49  	// TODO(xsw): other message type
    50  }
    51  
    52  func stringTracef(fp c.FilePtr, format *c.Char, s String) {
    53  	cs := c.Alloca(uintptr(s.len) + 1)
    54  	c.Fprintf(fp, format, CStrCopy(cs, s))
    55  }