Commit 92ccb1ff authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Add creation functions for different HandleMaps.

parent fe8805f9
...@@ -55,6 +55,13 @@ type portableHandleMap struct { ...@@ -55,6 +55,13 @@ type portableHandleMap struct {
freeIds []uint64 freeIds []uint64
} }
func newPortableHandleMap() *portableHandleMap {
return &portableHandleMap{
// Avoid handing out ID 0 and 1.
handles: []*Handled{nil, nil},
}
}
func (m *portableHandleMap) Register(obj *Handled) (handle uint64) { func (m *portableHandleMap) Register(obj *Handled) (handle uint64) {
m.Lock() m.Lock()
if obj.count == 0 { if obj.count == 0 {
...@@ -192,6 +199,11 @@ func (m *int32HandleMap) Decode(handle uint64) *Handled { ...@@ -192,6 +199,11 @@ func (m *int32HandleMap) Decode(handle uint64) *Handled {
val := (*Handled)(unsafe.Pointer(uintptr(handle & ((1 << 32) - 1)))) val := (*Handled)(unsafe.Pointer(uintptr(handle & ((1 << 32) - 1))))
return val return val
} }
func newInt32HandleMap() *int32HandleMap {
return &int32HandleMap{
handles: make(map[uint32]*Handled),
}
}
// 64 bits version of HandleMap // 64 bits version of HandleMap
type int64HandleMap struct { type int64HandleMap struct {
...@@ -214,29 +226,28 @@ func (m *int64HandleMap) verify() { ...@@ -214,29 +226,28 @@ func (m *int64HandleMap) verify() {
} }
} }
func newInt64HandleMap() *int64HandleMap {
return &int64HandleMap{
handles: make(map[uint64]*Handled),
nextFree: 1, // to make tests easier.
}
}
// NewHandleMap creates a new HandleMap. If verify is given, we // NewHandleMap creates a new HandleMap. If verify is given, we
// use remaining bits in the handle to store sanity check bits. // use remaining bits in the handle to store sanity check bits.
func NewHandleMap(portable bool) (hm HandleMap) { func NewHandleMap(portable bool) (hm HandleMap) {
if portable { if portable {
return &portableHandleMap{ return newPortableHandleMap()
// Avoid handing out ID 0 and 1.
handles: []*Handled{nil, nil},
}
} }
var obj *Handled var obj *Handled
switch unsafe.Sizeof(obj) { switch unsafe.Sizeof(obj) {
case 8: case 8:
return &int64HandleMap{ return newInt64HandleMap()
handles: make(map[uint64]*Handled),
nextFree: 1, // to make tests easier.
}
case 4: case 4:
return &int32HandleMap{ return newInt32HandleMap()
handles: make(map[uint32]*Handled),
}
default: default:
log.Println("Unknown size.") log.Fatalf("Unknown size.")
} }
return nil return nil
......
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