-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsds.go
More file actions
156 lines (134 loc) · 3.08 KB
/
sds.go
File metadata and controls
156 lines (134 loc) · 3.08 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package adt
import (
"unsafe"
)
const (
SdsType5 = iota
SdsType8
SdsType16
SdsType32
SdsType64
)
type SdsHdr interface {
SdsNewLen(len int)
}
type Sdshdr5 struct {
flag uint8 // 低 3 位存类型,高 5 位存长度
buf []byte
}
type Sdshdr8 struct {
len uint8 // 已使用长度
alloc uint8 //总长度
flag uint8 //低 3 位存类型,高 5 位预留
buf []byte
}
// 本来需要根据 buf 长度来返回不同的类型,但是现在 go 不支持 1字节对齐,所以只返回一种
func SdsNewLen(s []byte) unsafe.Pointer {
strLen := len(s)
types := SdsReqType(strLen)
if types == SdsType5 || strLen < 1<<32 {
types = SdsType32
}
// 1 2 4 8 字节, 小于 1 字节暂不计算
// 计算不同头部所需的空间
//hdrlen := SdsHdrSize(types)
switch types {
case SdsType5:
case SdsType8:
sds := Sdshdr8{
len: uint8(strLen),
alloc: uint8(strLen),
flag: uint8(1),
buf: s,
}
sdsPointer := unsafe.Pointer(&sds)
return unsafe.Pointer(uintptr(sdsPointer) + unsafe.Offsetof(sds.buf))
case SdsType16:
sds := Sdshdr16{
len: uint16(strLen),
alloc: uint16(strLen),
flag: uint8(2),
buf: s,
}
sdsPointer := unsafe.Pointer(&sds)
return unsafe.Pointer(uintptr(sdsPointer) + unsafe.Offsetof(sds.buf))
case SdsType32:
sds := Sdshdr32{
len: uint32(strLen),
alloc: uint32(strLen),
flag: uint8(3),
buf: s,
}
sdsPointer := unsafe.Pointer(&sds)
return unsafe.Pointer(uintptr(sdsPointer) + unsafe.Offsetof(sds.buf))
}
sds := Sdshdr64{
len: uint64(strLen),
alloc: uint64(strLen),
flag: uint8(4),
buf: s,
}
sdsPointer := unsafe.Pointer(&sds)
return unsafe.Pointer(uintptr(sdsPointer) + unsafe.Offsetof(sds.buf))
}
func SdsFree() {
}
func SdsSetLen() {
}
func SdsHdrSize(types int) int {
switch types {
case SdsType5:
return int(unsafe.Sizeof(Sdshdr5{}))
case SdsType8:
return int(unsafe.Sizeof(Sdshdr8{}))
case SdsType16:
return int(unsafe.Sizeof(Sdshdr16{}))
case SdsType32:
return int(unsafe.Sizeof(Sdshdr32{}))
case SdsType64:
return int(unsafe.Sizeof(Sdshdr64{}))
}
return 0
}
func SdsReqType(stringSize int) int {
if stringSize < 1<<5 {
return SdsType5
}
if stringSize < 1<<8 {
return SdsType8
}
if stringSize < 1<<16 {
return SdsType16
}
if stringSize < 1<<32 {
return SdsType32
}
// 不考虑 32 位机器
return SdsType64
}
// 根据 buf 长度计算 flag 太复杂,这里不在计算
func PointOffset(types int) int {
return 0
}
// 因为确定的 sdstype32 所以 -8 找到 flags
func GetFlagsPointByBufPoint(buf unsafe.Pointer) unsafe.Pointer {
return unsafe.Pointer(uintptr(buf) - uintptr(8))
}
type Sdshdr16 struct {
len uint16 // 已使用长度
alloc uint16 //总长度
flag uint8 //低 3 位存类型,高 5 位预留
buf []byte
}
type Sdshdr32 struct {
len uint32 // 已使用长度
alloc uint32 //总长度
flag uint8 //低 3 位存类型,高 5 位预留
buf []byte
}
type Sdshdr64 struct {
len uint64 // 已使用长度
alloc uint64 //总长度
flag uint8 //低 3 位存类型,高 5 位预留
buf []byte
}