libc

package
v0.0.0-...-01156f5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddAssign

func AddAssign[T number](p *T, v T) T

AddAssign is similar to x += v expression in C, but accepts a pointer to a variable.

func AndAssign

func AndAssign[T constraints.Integer](p *T, v T) T

AndAssign is similar to x &= v expression in C, but accepts a pointer to a variable.

func Assert

func Assert(cond bool)

Assert panics when the condition is false.

func Assign

func Assign[T any](p *T, v T) T

Assign is similar to x = v expression in C, but accepts a pointer to a variable.

func BoolToInt

func BoolToInt(v bool) int

BoolToInt is a helper that return 1 for true and 0 for false boolean values.

func CString

func CString(s string) *byte

CString is similar to C.CString, but uses the allocator provided by this package.

Caller must free the string with Free.

func CString16

func CString16(s string) *uint16

CString16 is similar to CString, but produces a UTF-16 string.

func CStringS

func CStringS(s string) []byte

CStringS is similar to C.CString, but uses the allocator provided by this package. It returns slice.

Caller must free the string with Free.

func CStringS16

func CStringS16(s string) []uint16

CStringS16 is similar to CString, but produces a UTF-16 string. It returns slice.

func Calloc

func Calloc(cnt, sz uintptr) unsafe.Pointer

Calloc allocates memory using the allocator. It is similar calloc in C.

func Clone

func Clone[T any](p *T) *T

Clone the object, using memory provided by the allocator. Caller must free the result with Free.

func CloneS

func CloneS[T any](p []T) []T

CloneS the slice, using memory provided by the allocator. Caller must free the result with FreeS.

func DivAssign

func DivAssign[T number](p *T, v T) T

DivAssign is similar to x /= v expression in C, but accepts a pointer to a variable.

func Free

func Free[T any](p *T)

Free allocated memory.

func FreeP

func FreeP(p unsafe.Pointer)

FreeP is similar to Free, but accepts unsafe.Pointer.

func FreeS

func FreeS[T any](p []T)

FreeS is similar to Free, but accepts a slice.

func GoString deprecated

func GoString(p *byte) string

GoString is similar to C.GoString, but doesn't use C package.

Deprecated: Unsafe, use GoStringS.

func GoString16 deprecated

func GoString16(p *uint16) string

GoString16 is similar to GoString, but operates on UTF-16 string.

Deprecated: Unsafe, use GoStringS16.

func GoStringS

func GoStringS(p []byte) string

GoStringS is similar to C.GoString, but doesn't use C package. It accepts slice.

func GoStringS16

func GoStringS16(p []uint16) string

GoStringS16 is similar to GoString, but operates on UTF-16 string. It accepts slice.

func If

func If[T any](cond bool, then, els T) T

If is similar to a ternary operator in C. It ALWAYS evaluates side effects. For correctly handling side effects, use IfFunc.

func IfFunc

func IfFunc[T any](cond bool, then, els func() T) T

IfFunc is similar to a ternary operator in C. It preserves C semantic in terms of expression evaluation.

func IndexS

func IndexS[T comparable](s []T, v T) int

IndexS returns first index of value v in slice s. It returns -1 if v is not in s.

func LastIndexS

func LastIndexS[T comparable](s []T, v T) int

LastIndexS returns last index of value v in slice s. It returns -1 if v is not in s.

func LshAssign

func LshAssign[T constraints.Integer](p *T, v T) T

LshAssign is similar to x <<= v expression in C, but accepts a pointer to a variable.

func Make

func Make[T any](sz int) []T

Make is similar to builtin make, but uses the allocator. Caller must free the result with FreeS.

func Malloc

func Malloc(sz uintptr) unsafe.Pointer

Malloc allocates memory using the allocator. It is similar malloc in C, but always zero-initializes memory.

func MemChar

func MemChar[T comparable](p *T, v T, sz int) *T

MemChar is a generic implementation of C memchr.

func MemCharP

func MemCharP(p unsafe.Pointer, v byte, sz int) unsafe.Pointer

MemCharP is the same as MemChar, but accepts and returns unsafe.Pointer.

func MemCmp

func MemCmp[T comparable](p1, p2 *T, sz int) int

MemCmp is a generic version of C memcmp.

func MemCmpP

func MemCmpP(p1, p2 unsafe.Pointer, sz int) int

MemCmpP is similar to MemCmp, but accepts unsafe.Pointer.

func MemCmpS

func MemCmpS[T comparable](p1, p2 []T) int

MemCmpS is similar to MemCmp, but accepts slices.

func MemCopy

func MemCopy[T any](dst, src *T, sz int)

MemCopy is a generic implementation of C memcpy.

func MemCopyP

func MemCopyP(dst, src unsafe.Pointer, sz int)

MemCopyP is similar to MemCopy, but accepts unsafe.Pointer.

func MemIndex

func MemIndex[T comparable](p *T, v T, sz int) int

MemIndex is similar to IndexS, but accepts a pointer and a size.

func MemLastIndex

func MemLastIndex[T comparable](p *T, v T, sz int) int

MemLastIndex is similar to LastIndexS, but accepts a pointer and a size.

func MemRChar

func MemRChar[T comparable](p *T, v T, sz int) *T

MemRChar is a generic implementation of C memchr, but searches the value from the end.

func MemRCharP

func MemRCharP(p unsafe.Pointer, v byte, sz int) unsafe.Pointer

MemRCharP is the same as MemChar, but accepts and returns unsafe.Pointer.

func MemSet

func MemSet[T any](p *T, v T, sz int)

MemSet is a generic implementation of C memset.

func MemSetS

func MemSetS[T any](dst []T, v T)

MemSetS is a generic implementation of C memset that accepts a slice.

func ModAssign

func ModAssign[T constraints.Integer](p *T, v T) T

ModAssign is similar to x %= v expression in C, but accepts a pointer to a variable.

func MulAssign

func MulAssign[T number](p *T, v T) T

MulAssign is similar to x *= v expression in C, but accepts a pointer to a variable.

func New

func New[T any]() *T

New is similar to builtin new, but uses the allocator. Caller must free the result with Free.

func OrAssign

func OrAssign[T constraints.Integer](p *T, v T) T

OrAssign is similar to x |= v expression in C, but accepts a pointer to a variable.

func PostDec

func PostDec[T number](p *T) T

PostDec is similar to x-- expression in C, but accepts a pointer to a variable.

func PostInc

func PostInc[T number](p *T) T

PostInc is similar to x++ expression in C, but accepts a pointer to a variable.

func PreDec

func PreDec[T number](p *T) T

PreDec is similar to --x expression in C, but accepts a pointer to a variable.

func PreInc

func PreInc[T number](p *T) T

PreInc is similar to ++x expression in C, but accepts a pointer to a variable.

func PtrAdd

func PtrAdd[T any](p *T, off int) *T

PtrAdd is a generic version of unsafe.Add. It can be used in places where C pointer arithmetic was used.

func Realloc

func Realloc[T any](p *T, sz int) *T

Realloc is a generic implementation of C realloc.

func ReallocP

func ReallocP(p unsafe.Pointer, cnt, sz uintptr) unsafe.Pointer

ReallocP is similar to C realloc.

func Remake

func Remake[T any](p []T, sz int) []T

Remake is similar to builtin make and C realloc. Caller must free the result with FreeS.

func RshAssign

func RshAssign[T constraints.Integer](p *T, v T) T

RshAssign is similar to x >>= v expression in C, but accepts a pointer to a variable.

func StrCaseCmp deprecated

func StrCaseCmp(p1, p2 *byte) int

StrCaseCmp is similar to C strcasecmp.

Deprecated: Unsafe, use StrCaseCmpS or StrCaseCmpN.

func StrCaseCmp16 deprecated

func StrCaseCmp16(p1, p2 *uint16) int

StrCaseCmp16 is similar to C strcasecmp for UTF-16.

Deprecated: Unsafe, use StrCaseCmpS16 or StrCaseCmpN16.

func StrCaseCmpN

func StrCaseCmpN(p1, p2 *byte, max int) int

StrCaseCmpN is safe version of StrCaseCmp which requires length.

func StrCaseCmpN16

func StrCaseCmpN16(p1, p2 *uint16, max int) int

StrCaseCmpN16 is safe version of StrCaseCmp16 which requires length.

func StrCaseCmpS

func StrCaseCmpS(p1, p2 []byte) int

StrCaseCmpS is safe version of StrCaseCmp.

func StrCaseCmpS16

func StrCaseCmpS16(p1, p2 []uint16) int

StrCaseCmpS16 is safe version of StrCaseCmp16.

func StrCat

func StrCat[T comparable](dst *T, src *T) *T

func StrCatS

func StrCatS[T comparable](dst []T, src []T) []T

func StrCatStr

func StrCatStr(dst *byte, src string) *byte

func StrCatStrS

func StrCatStrS(dst []byte, src string) []byte

func StrChar deprecated

func StrChar[T comparable](p *T, v T) *T

StrChar returns a pointer to the first occurrence of value in the C array/string p.

The terminating null-value is considered part of the C array. Therefore, it can also be located in order to retrieve a pointer to the end of array/string.

Deprecated: Unsafe, use StrCharS.

func StrCharS

func StrCharS[T comparable](p []T, v T) []T

StrCharS is a safe version of StrChar.

func StrCmp deprecated

func StrCmp[T constraints.Ordered](p1, p2 *T) int

StrCmp is a generic version of C strcmp.

Deprecated: Unsafe, use StrCmpS.

func StrCmpN

func StrCmpN[T constraints.Ordered](p1, p2 *T, max int) int

StrCmpN is the same as StrCmp, but accepts length for underlying arrays. It still looks for a zero value as a terminator.

func StrCmpS

func StrCmpS[T constraints.Ordered](p1, p2 []T) int

StrCmpS is the same as StrCmp, but accepts slices. It still looks for a zero value as a terminator.

func StrCopy deprecated

func StrCopy[T comparable](p *T, s *T)

StrCopy copies zero-terminated string s into a buffer pointed by p, including zero terminator.

Size of buffer p must be large enough for the string and zero terminator.

Deprecated: Unsafe, use StrCopyS.

func StrCopyS

func StrCopyS[T comparable](p []T, s []T)

StrCopyS copies zero-terminated string s into a buffer p, including zero terminator.

If size of the buffer is too small, the string will be trimmed.

There is subtle difference between StrCopyS and StrNCopyS. Both accept slices and will stop writing if buffer is too small. However, StrNCopyS is allowed to omit zero terminator in this case, while StrCopyS will always add it. Thus, effective capacity of buffer for StrCopyS is len(p)-1, while for StrNCopyS it's len(p).

func StrCopySlice deprecated

func StrCopySlice[T comparable](p *T, s []T)

StrCopySlice copies zero-terminated string s into a buffer pointed by p, including zero terminator.

Size of buffer p must be large enough for the string and zero terminator.

Deprecated: Unsafe, use StrCopyS.

func StrCopyStr deprecated

func StrCopyStr(p *byte, s string)

StrCopyStr copies string s into a buffer pointed by p, including zero terminator.

Size of buffer p must be large enough for the string and zero terminator.

Deprecated: Unsafe, use StrCopyStrS.

func StrCopyStr16 deprecated

func StrCopyStr16(p *uint16, s string)

StrCopyStr16 copies string s into a UTF-16 buffer pointed by p, including zero terminator.

Size of buffer p must be large enough for the string and zero terminator.

Deprecated: Unsafe, use StrCopyStr16S.

func StrCopyStr16S

func StrCopyStr16S(p []uint16, s string)

StrCopyStr16S copies string s into a UTF-16 buffer p, including zero terminator.

If size of the buffer is too small, the string will be trimmed.

For difference between StrCopyStr16S and StrNCopyStr16S see docs for StrCopyS or StrNCopyS.

func StrCopyStrS

func StrCopyStrS(p []byte, s string)

StrCopyStrS copies string s into a buffer p, including zero terminator.

If size of the buffer is too small, the string will be trimmed.

For difference between StrCopyStrS and StrNCopyStrS see docs for StrCopyS or StrNCopyS.

func StrDup deprecated

func StrDup[T comparable](s *T) *T

StrDup makes a copy of the array or string.

Deprecated: Unsafe, use StrDupS.

func StrDupS

func StrDupS[T comparable](s []T) []T

StrDupS makes a copy of the array or string. It accepts and returns slices.

func StrIndex deprecated

func StrIndex[T comparable](p *T, v T) int

StrIndex is similar to C strchr, but returns an index.

Deprecated: Unsafe, use StrIndexS.

func StrIndexS

func StrIndexS[T comparable](p []T, v T) int

StrIndexS is a safe version of StrIndex.

func StrLastIndex deprecated

func StrLastIndex[T comparable](p *T, v T) int

StrLastIndex is similar to C strrchr, but returns an index.

Deprecated: Unsafe, use StrLastIndexS.

func StrLastIndexS

func StrLastIndexS[T comparable](p []T, v T) int

StrLastIndexS is a safe version of StrLastIndex.

func StrLen deprecated

func StrLen[T comparable](p *T) int

StrLen is a generic version of C strlen.

It accepts a pointer to the first array element and advances it until it reaches a zero value. Position of this zero terminator is returned, which equals to a length of the array/string.

Additionally, function checks for a nil pointer and returns 0 length in this case.

The behavior is undefined if p is not a pointer to a null-terminated array.

Deprecated: Unsafe, use StrNLen or StrLenS.

func StrLenS

func StrLenS[T comparable](p []T) int

StrLenS is similar to StrLen, but accepts a slice. It still looks for a zero value as a terminator in the slice.

func StrNCopy deprecated

func StrNCopy[T comparable](p *T, s *T, sz int)

StrNCopy copies zero-terminated string s into a buffer pointed by p, including zero terminator.

Size of buffer p must be large enough for the string. It may omit zero terminator.

Deprecated: Unsafe, use StrNCopyS.

func StrNCopyS

func StrNCopyS[T comparable](p []T, s []T)

StrNCopyS copies zero-terminated string s into a buffer p, including zero terminator.

If size of the buffer is too small, the string will be trimmed. It may omit zero terminator.

There is subtle difference between StrCopyS and StrNCopyS. Both accept slices and will stop writing if buffer is too small. However, StrNCopyS is allowed to omit zero terminator in this case, while StrCopyS will always add it. Thus, effective capacity of buffer for StrCopyS is len(p)-1, while for StrNCopyS it's len(p).

func StrNCopySlice deprecated

func StrNCopySlice[T comparable](p *T, s []T, sz int)

StrNCopySlice copies zero-terminated string s into a buffer pointed by p, including zero terminator.

Size of buffer p must be large enough for the string. It may omit zero terminator.

Deprecated: Unsafe, use StrNCopyS.

func StrNCopyStr deprecated

func StrNCopyStr(p *byte, s string, sz int)

StrNCopyStr copies string s into a buffer pointed by p, including zero terminator.

Size of buffer p must be large enough for the string. It may omit zero terminator.

Deprecated: Unsafe, use StrNCopyStrS.

func StrNCopyStr16 deprecated

func StrNCopyStr16(p *uint16, s string, sz int)

StrNCopyStr16 copies string s into a UTF-16 buffer pointed by p, including zero terminator.

Size of buffer p must be large enough for the string. It may omit zero terminator.

Deprecated: Unsafe, use StrNCopyStr16S.

func StrNCopyStr16S

func StrNCopyStr16S(p []uint16, s string)

StrNCopyStr16S copies string s into a UTF-16 buffer p, including zero terminator.

If size of the buffer is too small, the string will be trimmed. It may omit zero terminator.

For difference between StrCopyStr16S and StrNCopyStr16S see docs for StrCopyS or StrNCopyS.

func StrNCopyStrS

func StrNCopyStrS(p []byte, s string)

StrNCopyStrS copies string s into a buffer p, including zero terminator.

If size of the buffer is too small, the string will be trimmed. It may omit zero terminator.

For difference between StrCopyStrS and StrNCopyStrS see docs for StrCopyS or StrNCopyS.

func StrNLen

func StrNLen[T comparable](p *T, max int) int

StrNLen is a generic version of C strnlen. It's the same as StrLen, but accepts a length of the buffer.

It is safer than using StrLenS and unsafe.Slice directly, because it checks for a zero pointer before converting to a slice.

func StrNSlice

func StrNSlice[T comparable](p *T, max int) []T

StrNSlice uses StrLen to determine the length of a zero-terminated string, and returns it as a slice.

It uses max as a capacity of the returned slice, making it suitable for writing to a string buffer.

func StrRChar deprecated

func StrRChar[T comparable](p *T, v T) *T

StrRChar returns a pointer to the last occurrence of value in the C array/string p.

The terminating null-value is considered part of the C array. Therefore, it can also be located to retrieve a pointer to the end of array/string.

Deprecated: Unsafe, use StrRCharS.

func StrRCharS

func StrRCharS[T comparable](p []T, v T) []T

StrRCharS is a safe version of StrRChar.

func StrSlice deprecated

func StrSlice[T comparable](p *T) []T

StrSlice uses StrLen to determine the length of a zero-terminated string, and returns it as a slice.

This method is only suitable for reading from a string. For writing to a string buffer, see StrSliceS or StrNSlice.

Deprecated: Unsafe, use StrSliceS or StrNSlice.

func StrSliceS

func StrSliceS[T comparable](p []T) []T

StrSliceS uses StrLenS to determine the length of a zero-terminated string in a slice, and returns a new slice.

func StrSliceStr

func StrSliceStr(s string) string

StrSliceStr is similar to StrSlice, but accept a string.

func SubAssign

func SubAssign[T number](p *T, v T) T

SubAssign is similar to x -= v expression in C, but accepts a pointer to a variable.

func XorAssign

func XorAssign[T constraints.Integer](p *T, v T) T

XorAssign is similar to x ^= v expression in C, but accepts a pointer to a variable.

Types

type Allocator

type Allocator interface {
	// Realloc allocates a zero-initialized array of cnt element, each of size sz.
	//
	// If the old pointer is given, all data is copied to the new array (possibly trimming the content),
	// and the old array is automatically freed. Zero-initialization is best-effort in this case.
	Realloc(ptr unsafe.Pointer, cnt, sz uintptr) unsafe.Pointer
	// Free previously allocated memory.
	Free(ptr unsafe.Pointer)
}

Allocator is an interface that provides C-like allocation mechanisms.

func SetAllocator

func SetAllocator(a Allocator) Allocator

SetAllocator sets the global allocator. By default, GoAllocator is used. It returns the old allocator.

type GoAllocator

type GoAllocator struct {
	// contains filtered or unexported fields
}

GoAllocator is a simple allocator that uses mechanism similar to make to allocate memory. It keeps pointers of all allocations, so that GC will never reclaim it until Free. Zero value is safe for use.

func (*GoAllocator) Free

func (a *GoAllocator) Free(p unsafe.Pointer)

func (*GoAllocator) Realloc

func (a *GoAllocator) Realloc(p unsafe.Pointer, cnt, sz uintptr) unsafe.Pointer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL