Skip to content

Commit

Permalink
Fix memory leak when clean or remove from vector (#118)
Browse files Browse the repository at this point in the history
* Fix memory leak when clean or remove from vector, close #117
  • Loading branch information
chen3feng authored Aug 17, 2023
1 parent a5a5c91 commit 6793044
Show file tree
Hide file tree
Showing 7 changed files with 622 additions and 325 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ indicating that a custom comparison function can be passed.
- `Copy` return a copies of specified slice
- `CopyTo` copies all elements in slice a to slice to, return the copied slice.
- `Fill` repeatedly fills a slice with the specified value
- `FillZero` fills each element in slice a with zero value.
- `FillPattern` repeatedly fills a slice with the specified pattern
- `Replace` replaces every element that equals to old with new
- `ReplaceIf` replaces every element that make preq returns true with new
Expand Down
1 change: 1 addition & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func TestSkipList_ForEachMutable(t *testing.T) {
- `Copy` 返回切片的拷贝
- `CopyTo` 拷贝切片的内容到另一个切片
- `Fill` 用指定的值重复填充一个切片
- `FillZero` 用类型的零值重复填充一个切片
- `FillPattern` 用指定的模式重复填充一个切片
- `Replace` 替换所有等于指定值的元素为新值
- `ReplaceIf` 替换所有让函数返回 `true` 的元素为新值
Expand Down
911 changes: 586 additions & 325 deletions generated_doc.md

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func Fill[T any](a []T, v T) {
}
}

// FillZero fills each element in slice a with zero value.
//
// Complexity: O(len(a)).
func FillZero[T any](a []T) {
var zero T
Fill(a, zero)
}

// FillPattern fills elements in slice a with specified pattern.
//
// Complexity: O(len(a)).
Expand Down
6 changes: 6 additions & 0 deletions transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ func Test_Fill(t *testing.T) {
expectTrue(t, Equal(a, []int{1, 1, 1, 1}))
}

func Test_FillZero(t *testing.T) {
a := []int{1, 2, 3, 4}
FillZero(a)
expectTrue(t, Equal(a, []int{0, 0, 0, 0}))
}

func naiveFill[T any](a []T, v T) {
for i := range a {
a[i] = v
Expand Down
5 changes: 5 additions & 0 deletions vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (v *Vector[T]) Cap() int {
// Clear erases all elements from the vector. After this call, Len() returns zero.
// Leaves the Cap() of the vector unchanged.
func (v *Vector[T]) Clear() {
FillZero(*v)
*v = (*v)[0:0]
}

Expand Down Expand Up @@ -92,7 +93,9 @@ func (v *Vector[T]) PushBack(x T) {
// It must be called when IsEmpty() returned false,
// otherwise it will panic.
func (v *Vector[T]) PopBack() T {
var zero T
e := (*v)[v.Len()-1]
(*v)[len(*v)-1] = zero
*v = (*v)[0 : v.Len()-1]
return e
}
Expand Down Expand Up @@ -149,7 +152,9 @@ func (v *Vector[T]) Remove(i int) {

// RemoveRange removes the elements in the range[i, j) from the vector.
func (v *Vector[T]) RemoveRange(i, j int) {
oldV := *v
*v = append((*v)[:i], (*v)[j:]...)
FillZero(oldV[v.Len():])
}

// RemoveLength removes the elements in the range[i, i+len) from the vector.
Expand Down
15 changes: 15 additions & 0 deletions vector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ func Test_Vector_PopBack(t *testing.T) {
expectPanic(t, func() { v.PopBack() })
}

func Test_Vector_PopBack_Clear(t *testing.T) {
v := VectorOf(1, 2)
oldV := v
v.PopBack()
expectEq(t, oldV[1], 0)
}

func Test_Vector_Back(t *testing.T) {
v := VectorOf(1)
expectEq(t, v.Back(), 1)
Expand Down Expand Up @@ -134,29 +141,37 @@ func Test_Vector_Insert_Cap(t *testing.T) {

func Test_Vector_Remove(t *testing.T) {
v := VectorOf(1, 2, 3)
oldV := v
v.Remove(1)
expectEq(t, v.Len(), 2)
expectEq(t, v.Cap(), 3)
expectEq(t, v[0], 1)
expectEq(t, v[1], 3)
expectEq(t, oldV[2], 0)
}

func Test_Vector_RemoveRange(t *testing.T) {
v := VectorOf(1, 2, 3, 4)
oldV := v
v.RemoveRange(1, 3)
expectEq(t, v.Len(), 2)
expectEq(t, v.Cap(), 4)
expectEq(t, v[0], 1)
expectEq(t, v[1], 4)
expectEq(t, oldV[2], 0)
expectEq(t, oldV[3], 0)
}

func Test_Vector_RemoveLength(t *testing.T) {
v := VectorOf(1, 2, 3, 4)
oldV := v
v.RemoveLength(1, 2)
expectEq(t, v.Len(), 2)
expectEq(t, v.Cap(), 4)
expectEq(t, v[0], 1)
expectEq(t, v[1], 4)
expectEq(t, oldV[2], 0)
expectEq(t, oldV[3], 0)
}

func Test_Vector_ForEach(t *testing.T) {
Expand Down

0 comments on commit 6793044

Please sign in to comment.