-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
skip.go
63 lines (50 loc) · 1.21 KB
/
skip.go
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
// Copyright 2019 Kazuhisa TAKEI<xtakei@rytr.jp>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package loncha/list_head is like a kernel's LIST_HEAD
// list_head is used by loncha/gen/containers_list
package list_head
import (
"sync/atomic"
"unsafe"
)
type SkipHead struct {
len int64
up *SkipHead
start *ListHead
last *ListHead
ListHead
}
type SuperSkipHead struct {
up *SuperSkipHead
start *SkipHead
last *SkipHead
}
func skipHeadFromListHead(head *ListHead) *SkipHead {
return (*SkipHead)(ElementOf(&SkipHead{}, head))
}
func (e *SkipHead) Offset() uintptr {
return unsafe.Offsetof(e.ListHead)
}
func (e *SkipHead) PtrListHead() *ListHead {
return &e.ListHead
}
func (e *SkipHead) FromListHead(head *ListHead) List {
return entryRmapFromListHead(head)
}
func (e *SkipHead) reduce(ocnt int) {
if e.ListHead.Next().Empty() {
return
}
n := skipHeadFromListHead(e.ListHead.Next())
cnt := ocnt
if cnt >= int(e.len) {
cnt = int(e.len) - 1
}
for i := 0; i < cnt; i++ {
e.last = e.last.Prev(WaitNoM())
}
atomic.AddInt64(&e.len, -int64(cnt))
n.start = e.last.Next(WaitNoM())
atomic.AddInt64(&n.len, int64(cnt))
}