Commit 1adb5bd3 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Fix int64 handlemap.

parent 92ccb1ff
......@@ -124,7 +124,7 @@ func (m *portableHandleMap) Forget(h uint64, count int) (forgotten bool, obj *Ha
m.used--
forgotten = true
obj.handle = 0
}
}
m.Unlock()
return forgotten, obj
}
......@@ -166,7 +166,7 @@ func (m *int32HandleMap) Handle(obj *Handled) uint64 {
if obj.count == 0 {
return 0
}
h := uint32(uintptr(unsafe.Pointer(obj)))
return uint64(h)
}
......@@ -245,7 +245,7 @@ func NewHandleMap(portable bool) (hm HandleMap) {
case 8:
return newInt64HandleMap()
case 4:
return newInt32HandleMap()
return newInt32HandleMap()
default:
log.Fatalf("Unknown size.")
}
......@@ -285,6 +285,7 @@ func (m *int64HandleMap) Register(obj *Handled) (handle uint64) {
panic(_ALREADY_MSG)
}
obj.check = check
obj.handle = handle
m.handles[handle] = obj
} else {
handle = m.Handle(obj)
......@@ -299,7 +300,7 @@ func (m *int64HandleMap) Handle(obj *Handled) (handle uint64) {
if obj.count == 0 {
return 0
}
handle = uint64(uintptr(unsafe.Pointer(obj)))
handle >>= 3
handle |= uint64(obj.check) << (48 - 3)
......
......@@ -20,22 +20,6 @@ func markSeen(t *testing.T, substr string) {
}
}
func TestHandleMapDoubleRegister(t *testing.T) {
if unsafe.Sizeof(t) < 8 {
t.Log("skipping test for 32 bits")
return
}
t.Log("TestDoubleRegister")
defer markSeen(t, "already has a handle")
hm := NewHandleMap(false)
obj := &Handled{}
hm.Register(obj)
v := &Handled{}
hm.Register(v)
hm.Register(v)
t.Error("Double register did not panic")
}
func TestHandleMapUnaligned(t *testing.T) {
if unsafe.Sizeof(t) < 8 {
t.Log("skipping test for 32 bits")
......@@ -51,19 +35,46 @@ func TestHandleMapUnaligned(t *testing.T) {
t.Error("Unaligned register did not panic")
}
func TestHandleMapPointerLayout(t *testing.T) {
if unsafe.Sizeof(t) < 8 {
t.Log("skipping test for 32 bits")
return
}
func TestHandleMapLookupCount(t *testing.T) {
for _, portable := range []bool{true, false} {
t.Log("portable:", portable)
v := new(Handled)
hm := NewHandleMap(portable)
h1 := hm.Register(v)
h2 := hm.Register(v)
hm := NewHandleMap(false)
bogus := uint64(1) << uint32((8 * (unsafe.Sizeof(t) - 1)))
p := uintptr(bogus)
v := (*Handled)(unsafe.Pointer(p))
defer markSeen(t, "48")
hm.Register(v)
t.Error("bogus register did not panic")
if h1 != h2 {
t.Fatalf("double register should reuse handle: got %d want %d.", h2, h1)
}
hm.Register(v)
forgotten, obj := hm.Forget(h1, 1)
if forgotten {
t.Fatalf("single forget unref forget object.")
}
if obj != v {
t.Fatalf("should return input object.")
}
if !hm.Has(h1) {
t.Fatalf("handlemap.Has() returned false for live object.")
}
forgotten, obj = hm.Forget(h1, 2)
if !forgotten {
t.Fatalf("unref did not forget object.")
}
if obj != v {
t.Fatalf("should return input object.")
}
if hm.Has(h1) {
t.Fatalf("handlemap.Has() returned false for live object.")
}
}
}
func TestHandleMapBasic(t *testing.T) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment