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 ...@@ -124,7 +124,7 @@ func (m *portableHandleMap) Forget(h uint64, count int) (forgotten bool, obj *Ha
m.used-- m.used--
forgotten = true forgotten = true
obj.handle = 0 obj.handle = 0
} }
m.Unlock() m.Unlock()
return forgotten, obj return forgotten, obj
} }
...@@ -166,7 +166,7 @@ func (m *int32HandleMap) Handle(obj *Handled) uint64 { ...@@ -166,7 +166,7 @@ func (m *int32HandleMap) Handle(obj *Handled) uint64 {
if obj.count == 0 { if obj.count == 0 {
return 0 return 0
} }
h := uint32(uintptr(unsafe.Pointer(obj))) h := uint32(uintptr(unsafe.Pointer(obj)))
return uint64(h) return uint64(h)
} }
...@@ -245,7 +245,7 @@ func NewHandleMap(portable bool) (hm HandleMap) { ...@@ -245,7 +245,7 @@ func NewHandleMap(portable bool) (hm HandleMap) {
case 8: case 8:
return newInt64HandleMap() return newInt64HandleMap()
case 4: case 4:
return newInt32HandleMap() return newInt32HandleMap()
default: default:
log.Fatalf("Unknown size.") log.Fatalf("Unknown size.")
} }
...@@ -285,6 +285,7 @@ func (m *int64HandleMap) Register(obj *Handled) (handle uint64) { ...@@ -285,6 +285,7 @@ func (m *int64HandleMap) Register(obj *Handled) (handle uint64) {
panic(_ALREADY_MSG) panic(_ALREADY_MSG)
} }
obj.check = check obj.check = check
obj.handle = handle
m.handles[handle] = obj m.handles[handle] = obj
} else { } else {
handle = m.Handle(obj) handle = m.Handle(obj)
...@@ -299,7 +300,7 @@ func (m *int64HandleMap) Handle(obj *Handled) (handle uint64) { ...@@ -299,7 +300,7 @@ func (m *int64HandleMap) Handle(obj *Handled) (handle uint64) {
if obj.count == 0 { if obj.count == 0 {
return 0 return 0
} }
handle = uint64(uintptr(unsafe.Pointer(obj))) handle = uint64(uintptr(unsafe.Pointer(obj)))
handle >>= 3 handle >>= 3
handle |= uint64(obj.check) << (48 - 3) handle |= uint64(obj.check) << (48 - 3)
......
...@@ -20,22 +20,6 @@ func markSeen(t *testing.T, substr string) { ...@@ -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) { func TestHandleMapUnaligned(t *testing.T) {
if unsafe.Sizeof(t) < 8 { if unsafe.Sizeof(t) < 8 {
t.Log("skipping test for 32 bits") t.Log("skipping test for 32 bits")
...@@ -51,19 +35,46 @@ func TestHandleMapUnaligned(t *testing.T) { ...@@ -51,19 +35,46 @@ func TestHandleMapUnaligned(t *testing.T) {
t.Error("Unaligned register did not panic") t.Error("Unaligned register did not panic")
} }
func TestHandleMapPointerLayout(t *testing.T) { func TestHandleMapLookupCount(t *testing.T) {
if unsafe.Sizeof(t) < 8 { for _, portable := range []bool{true, false} {
t.Log("skipping test for 32 bits") t.Log("portable:", portable)
return v := new(Handled)
} hm := NewHandleMap(portable)
h1 := hm.Register(v)
h2 := hm.Register(v)
hm := NewHandleMap(false) if h1 != h2 {
bogus := uint64(1) << uint32((8 * (unsafe.Sizeof(t) - 1))) t.Fatalf("double register should reuse handle: got %d want %d.", h2, h1)
p := uintptr(bogus) }
v := (*Handled)(unsafe.Pointer(p))
defer markSeen(t, "48") hm.Register(v)
hm.Register(v)
t.Error("bogus register did not panic") 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) { 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