Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Stefane Fermigier
neo
Commits
5e12ff76
Commit
5e12ff76
authored
Apr 20, 2017
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
f1278d15
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
38 deletions
+31
-38
t/neo/xcommon/xbytes/alloc.go
t/neo/xcommon/xbytes/alloc.go
+23
-32
t/neo/xcommon/xbytes/alloc_test.go
t/neo/xcommon/xbytes/alloc_test.go
+8
-6
No files found.
t/neo/xcommon/xbytes/alloc.go
View file @
5e12ff76
...
...
@@ -23,8 +23,8 @@ import (
"../xmath"
)
// Grow increase
length of
slice by n elements.
// If there is not enough capacity the slice is reallocated.
// Grow increase
s length of byte
slice by n elements.
// If there is not enough capacity the slice is reallocated
and copied
.
// The memory for grown elements is not initialized.
func
Grow
(
b
[]
byte
,
n
int
)
[]
byte
{
ln
:=
len
(
b
)
+
n
...
...
@@ -37,8 +37,22 @@ func Grow(b []byte, n int) []byte {
return
bb
}
// Resize resized the slice to be of length n.
// If slice length is increased and there is not enough capacity the slice is reallocated.
// MakeRoom makes sure cap(b) - len(b) >= n
// If there is not enough capacity the slice is reallocated and copied.
// Length of the slice remains unchanged.
func
MakeRoom
(
b
[]
byte
,
n
int
)
[]
byte
{
ln
:=
len
(
b
)
+
n
if
ln
<=
cap
(
b
)
{
return
b
}
bb
:=
make
([]
byte
,
len
(
b
),
xmath
.
CeilPow2
(
uint64
(
ln
)))
copy
(
bb
,
b
)
return
bb
}
// Resize resized byte slice to be of length n.
// If slice length is increased and there is not enough capacity the slice is reallocated and copied.
// The memory for grown elements, if any, is not initialized.
func
Resize
(
b
[]
byte
,
n
int
)
[]
byte
{
if
cap
(
b
)
>=
n
{
...
...
@@ -51,10 +65,12 @@ func Resize(b []byte, n int) []byte {
}
// Realloc resizes
th
e slice to be of length n not preserving content.
// If slice length is increased and there is not enough capacity the slice is reallocated.
// Realloc resizes
byt
e slice to be of length n not preserving content.
// If slice length is increased and there is not enough capacity the slice is reallocated
but not copied
.
// The memory for all elements becomes uninitialized.
// XXX semantic clash with C realloc(3) ? or it does not matter?
//
// NOTE semantic is different from C realloc(3) where content is preserved
// NOTE use Resize when you need to preserve slice content
func
Realloc
(
b
[]
byte
,
n
int
)
[]
byte
{
return
Realloc64
(
b
,
int64
(
n
))
}
...
...
@@ -67,28 +83,3 @@ func Realloc64(b []byte, n int64) []byte {
return
make
([]
byte
,
n
,
xmath
.
CeilPow2
(
uint64
(
n
)))
}
// TODO MakeRoom
// TODO Prealloc (make sure cap is enough but length stays unchanged) (was GrowSlice)
// TODO Resize without copy ?
// // GrowSlice makes sure cap(b) >= n.
// // If not it reallocates/copies the slice appropriately.
// // len of returned slice remains original len(b).
// func GrowSlice(b []byte, n int) []byte {
// if cap(b) >= n {
// return b
// }
//
// bb := make([]byte, len(b), CeilPow2(uint64(n)))
// copy(bb, b)
// return bb
// }
//
// // makeRoom makes sure len([len(b):cap(b)]) >= n.
// // If it is not it reallocates the slice appropriately.
// // len of returned slice remains original len(b).
// func MakeRoom(b []byte, n int) []byte {
// return GrowSlice(b, len(b) + n)
// }
t/neo/xcommon/xbytes/alloc_test.go
View file @
5e12ff76
...
...
@@ -40,10 +40,12 @@ func TestSlice(t *testing.T) {
// here "Hello" is assigned
{
Grow
,
6
,
11
,
16
,
false
,
[]
byte
(
"Hello
\x00\x00\x00\x00\x00\x00
"
)},
{
Resize
,
8
,
8
,
16
,
true
,
[]
byte
(
"Hello
\x00\x00\x00
"
)},
{
Resize
,
17
,
17
,
32
,
false
,
[]
byte
(
"Hello
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
"
)},
{
Realloc
,
16
,
16
,
32
,
true
,
[]
byte
(
"Hello
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
"
)},
{
Realloc
,
33
,
33
,
64
,
false
,
make
([]
byte
,
33
)},
{
MakeRoom
,
4
,
11
,
16
,
true
,
[]
byte
(
"Hello
\x00\x00\x00\x00\x00\x00
"
)},
{
MakeRoom
,
6
,
11
,
32
,
false
,
[]
byte
(
"Hello
\x00\x00\x00\x00\x00\x00
"
)},
{
Resize
,
8
,
8
,
32
,
true
,
[]
byte
(
"Hello
\x00\x00\x00
"
)},
{
Resize
,
33
,
33
,
64
,
false
,
append
([]
byte
(
"Hello"
),
bytes
.
Repeat
([]
byte
{
0
},
33
-
5
)
...
)},
{
Realloc
,
16
,
16
,
64
,
true
,
[]
byte
(
"Hello
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
"
)},
{
Realloc
,
65
,
65
,
128
,
false
,
make
([]
byte
,
65
)},
}
for
i
,
tt
:=
range
testv
{
...
...
@@ -51,7 +53,7 @@ func TestSlice(t *testing.T) {
s
=
tt
.
op
(
s
,
tt
.
n
)
if
!
(
len
(
s
)
==
tt
.
Len
&&
cap
(
s
)
==
tt
.
Cap
&&
bytes
.
Equal
(
s
,
tt
.
content
))
{
t
.
Fatalf
(
"step %d: %v: unexpected slice state: %v
"
,
i
,
tt
,
s
)
t
.
Fatalf
(
"step %d: %v: unexpected slice state: %v
(cap: %v)"
,
i
,
tt
,
s
,
cap
(
s
)
)
}
if
!
(
aliases
(
s
,
sprev
)
==
tt
.
aliased
)
{
...
...
@@ -59,7 +61,7 @@ func TestSlice(t *testing.T) {
}
// assign data after fi
sr
t iteration
// assign data after fi
rs
t iteration
if
i
==
0
{
copy
(
s
,
"Hello"
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment