Skip to content

Commit

Permalink
doc
Browse files Browse the repository at this point in the history
  • Loading branch information
qiulaidongfeng committed Mar 14, 2022
1 parent c895b36 commit 796cdb7
Show file tree
Hide file tree
Showing 22 changed files with 76 additions and 23 deletions.
4 changes: 4 additions & 0 deletions cextend/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//本包提供C语言功能的go语言API。
//
//本包使用了cgo,编译需要cgo,在使用时请考虑这对交叉编译的影响!
package cextend
15 changes: 15 additions & 0 deletions cextend/doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cextend

func ExampleMalloc() {
//获得内存
ptr := Malloc(4)
//获取成败判断
if ptr != nil {
//失败处理
//......
}
//不用时释放获取的内存,避免内存泄露
defer Free(ptr)
//使用内存
//.......
}
6 changes: 5 additions & 1 deletion cextend/memory.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// memory
package cextend

/*
Expand All @@ -12,26 +11,31 @@ import (
"unsafe"
)

//C语言<stdlib.h>中提供的realloc函数的go语言API
func Realloc(ptr unsafe.Pointer, size uint) (nptr unsafe.Pointer) {
nptr = C.realloc(ptr, C.size_t(size))
return
}

//C语言<stdlib.h>中提供的malloc函数的go语言API
func Malloc(size uint) (ptr unsafe.Pointer) {
ptr = C.malloc(C.size_t(size))
return
}

//C语言<stdlib.h>中提供的calloc函数的go语言API
func Calloc(nitems uint, size uint) (ptr unsafe.Pointer) {
ptr = C.calloc(C.size_t(nitems), C.size_t(size))
return
}

//C语言<stdlib.h>中提供的Free函数的go语言API
func Free(ptr unsafe.Pointer) {
C.free(ptr)
return
}

//C语言<string.h>中提供的memcpy函数的go语言API
func Memcpy(dest, src unsafe.Pointer, n uint) {
gcdest := cgo.NewHandle(dest)
cdest := gcdest.Value().(unsafe.Pointer)
Expand Down
1 change: 0 additions & 1 deletion cextend/memory_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// memory_test
package cextend

import (
Expand Down
10 changes: 8 additions & 2 deletions errors/Error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Error
//本包实现了一个增强型的errors
//
//本包是实验性的,可能出现不兼容的修改
package errors

const (
Expand All @@ -16,17 +18,20 @@ const (
UnknownError TypeError = "Unknown , Error"
)

//错误原因类型
type TypeError string

//增强的error
type Error interface {
//返回错误类型
Type() TypeError
//错误信息
error
//返回被包装的错误,nil表示没有被保装的错误
//返回被包装的错误,nil表示没有被包装的错误
Unwrap() Error
}

//Error接口的实现
type GoError struct {
errorType TypeError
err string
Expand All @@ -44,6 +49,7 @@ func (err *GoError) Type() TypeError {
return err.errorType
}

//创建错误,返回的是GoError结构体
func NewError(Type TypeError, err string) Error {
return &GoError{errorType: Type, err: err}
}
1 change: 0 additions & 1 deletion errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// error
package errors

import (
Expand Down
5 changes: 2 additions & 3 deletions errors/stackerror.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// stckerror
package errors

import (
Expand All @@ -19,13 +18,13 @@ type WrapStackError struct {
stack []byte
}

//创建带有栈踪迹信息的错误
//创建带有栈踪迹信息的错误,返回的是WrapStackError结构体
func NewWrapStackError(err string, all bool) PlusWrapError {
errorerr := Errorstring(err)
return &WrapStackError{err: &errorerr, stack: Stack(all)}
}

//包装当前栈信息(
//包装当前栈信息
func WrapFuncStackError(err *WrapStackError) {
err.stack = append(callStack(), err.stack...)
}
Expand Down
3 changes: 1 addition & 2 deletions errors/wrap.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// wrap
package errors

//被包装的错误类型
Expand All @@ -16,7 +15,7 @@ func (err *WrapError) Unwrap() error {
return err.err
}

//包装错误
//包装错误,返回的是WrapError结构体
func NewWrapError(err error, message string) WrapErrorType {
return &WrapError{err: err, message: message}
}
16 changes: 13 additions & 3 deletions gsync/LMrwmutex.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// lmrwmutex
package gsync

import (
Expand All @@ -7,8 +6,11 @@ import (
"sync/atomic"
)

//读写锁错误
var (
Noreadlock error = errors.New("No read lock")
//读锁没有被持有
Noreadlock error = errors.New("No read lock")
//写锁没有被持有
Nowritelock error = errors.New("No write lock")
)

Expand All @@ -19,11 +21,16 @@ const (

type nocopy struct{}

type LMrwmutex struct {
//使用内存少的读写锁,不能复制
type LowMemory_rwMutex struct {
_ nocopy
nm int64
}

//使用内存少的读写锁,不能复制
type LMrwmutex = LowMemory_rwMutex

//获取写锁
func (m *LMrwmutex) Lock() {
for {
ok := atomic.CompareAndSwapInt64(&m.nm, nolock, writelock)
Expand All @@ -34,6 +41,7 @@ func (m *LMrwmutex) Lock() {
}
}

//释放写锁
func (m *LMrwmutex) Unlock() {
nm := atomic.LoadInt64(&m.nm)
if nm >= 0 {
Expand All @@ -42,6 +50,7 @@ func (m *LMrwmutex) Unlock() {
atomic.StoreInt64(&m.nm, nolock)
}

//获取读锁
func (m *LMrwmutex) RLock() {
for {
ok := atomic.CompareAndSwapInt64(&m.nm, writelock, writelock)
Expand All @@ -54,6 +63,7 @@ func (m *LMrwmutex) RLock() {
}
}

//释放读锁
func (m *LMrwmutex) RUnlock() {
nm := atomic.LoadInt64(&m.nm)
if nm == -1 {
Expand Down
2 changes: 2 additions & 0 deletions gsync/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//本包提供实现并发安全的工具
package gsync
2 changes: 1 addition & 1 deletion gsync/gsync.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// gsync
package gsync

import (
"sync"
)

//读写锁接口
type Rwlock interface {
sync.Locker
RLock()
Expand Down
4 changes: 4 additions & 0 deletions gunsafe/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//本包提供unsafe包的拓展功能
//
//注意,本包提供的一切因为绕开了go语言安全机制,导入不安全的软件包可能是不可移植的,并且不受Go 1兼容性指南的保护。
package gunsafe
9 changes: 9 additions & 0 deletions gunsafe/doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gunsafe

func ExampleBtos() {
b := []byte("hello")
s := Btos(b)
fmt.Println(s)
//Output:
//hello
}
3 changes: 2 additions & 1 deletion gunsafe/gunsafe.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// unsafe
package gunsafe

import "unsafe"

//将string零拷贝转换为[]byte
func Stob(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}

//将[]byte零拷贝转换为string
func Btos(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
2 changes: 2 additions & 0 deletions list/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//本包提供链表实现
package list
5 changes: 4 additions & 1 deletion list/slist.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// slist
package list

import (
Expand All @@ -8,6 +7,7 @@ import (
"sync"
)

//链表中某一元素
type Element struct {
value interface{}
NextOne *Element
Expand Down Expand Up @@ -61,13 +61,16 @@ func (e *Element) String() string {
return fmt.Sprintf("%s", e.value)
}

//单链表的实现
type SingleLinkedList struct {
node *Element
len uint64
}

//单链表的实现
type Slist = SingleLinkedList

//创建单链表
func NewSlist() Slist {
n := Slist{
len: 0,
Expand Down
3 changes: 0 additions & 3 deletions list/slist_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// slist_test.go
package list

import (
"testing"

. "gtl/list"
)

func Benchmark_Newslist(b *testing.B) {
Expand Down
3 changes: 2 additions & 1 deletion stack/GLMstack.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// gsstack
package stack

import (
Expand All @@ -12,12 +11,14 @@ const (
poppp int64 = 1
)

//操作时间
const (
Poptime = int64(70)
Pushtime = int64(140)
Waittime = int64(200)
)

//类型大小
const (
Int8size uint64 = uint64((unsafe.Sizeof(int8(1))))
Int16size uint64 = uint64((unsafe.Sizeof(int16(1))))
Expand Down
2 changes: 2 additions & 0 deletions sys/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//本包提供调用系统功能的封装
package sys
1 change: 0 additions & 1 deletion sys/error.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// error
package sys

import (
Expand Down
1 change: 0 additions & 1 deletion sys/mmap_linux.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// mmap_linux
package sys

import (
Expand Down
1 change: 0 additions & 1 deletion sys/mmap_windows.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// mmap_windows
package sys

import (
Expand Down

0 comments on commit 796cdb7

Please sign in to comment.