diff --git a/mempool/mempool_test.go b/mempool/mempool_test.go index b9f641cf..6ad6072b 100644 --- a/mempool/mempool_test.go +++ b/mempool/mempool_test.go @@ -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) + } +} diff --git a/mempool/trace_debugger.go b/mempool/trace_debugger.go index cc9b5644..64afb658 100644 --- a/mempool/trace_debugger.go +++ b/mempool/trace_debugger.go @@ -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 @@ -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)