Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
lesismal committed May 16, 2024
1 parent a66232b commit 89018f2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
44 changes: 44 additions & 0 deletions mempool/mempool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,47 @@ func TestAlignedMemPool(t *testing.T) {
}
}
}

func TestTraceDebugerPool(t *testing.T) {
pool := NewTraceDebuger(New(1, 1))
// for i := 0; i < 1024*1024; i++ {
// buf := pool.Malloc(i)
// if len(buf) != i {
// t.Fatalf("invalid len: %v != %v", len(buf), i)
// }
// pool.Free(buf)
// }
// for i := 1024 * 1024; i < 1024*1024*1024; i += 1024 * 1024 {
// buf := pool.Malloc(i)
// if len(buf) != i {
// t.Fatalf("invalid len: %v != %v", len(buf), i)
// }
// pool.Free(buf)
// }

buf := pool.Malloc(1)
for i := 1; i < 1024; i++ {
buf = pool.Append(buf[:1], make([]byte, i)...)
if len(buf) != i+1 {
t.Fatalf("invalid len: %v != %v", len(buf), i)
}
}
pool.Free(buf)
// pool.Free(buf)
}

func TestStackFuncs(t *testing.T) {
stack1, stackPtr := getStackAndPtr()
stack2 := ptr2StackString(stackPtr)
if stack1 != stack2 {
t.Fatalf("stack not equal:\n\n%v\n\n%v", stack1, stack2)
}

buf := []byte{1}
p1 := bytesPointer(buf)
buf[0] = 2
p2 := bytesPointer(buf)
if p1 != p2 {
t.Fatalf("buf pointer not equal: %v, %v", p1, p2)
}
}
4 changes: 2 additions & 2 deletions mempool/trace_debugger.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (td *TraceDebugger) Malloc(size int) []byte {
buf := td.allocator.Malloc(size)
ptr := bytesPointer(buf)
if stackPtr, ok := td.pAlloced[ptr]; ok {
printStack("malloc got a buf which has been malloced by otherwhere", stackPtr)
printStack(fmt.Sprintf("malloc got a buf which has been malloced by otherwhere: %v", ptr), stackPtr)
}
td.setBufferPointer(ptr)
return buf
Expand Down Expand Up @@ -78,7 +78,7 @@ func (td *TraceDebugger) Append(buf []byte, more ...byte) []byte {
pnew := bytesPointer(newBuf)
if pnew != pold {
if preStack, ok := td.pAlloced[pnew]; ok {
printStack("Append got another new buf which has been malloced by otherwhere", preStack)
printStack(fmt.Sprintf("Append got another new buf which has been malloced by otherwhere: %v", pnew), preStack)
}
td.deleteBufferPointer(pold)
td.setBufferPointer(pnew)
Expand Down

0 comments on commit 89018f2

Please sign in to comment.