Skip to content

Instantly share code, notes, and snippets.

@chewxy
Created December 10, 2013 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chewxy/7886554 to your computer and use it in GitHub Desktop.
Save chewxy/7886554 to your computer and use it in GitHub Desktop.
package pointerTagging
import (
"unsafe"
"math/rand"
"time"
)
type vector struct {
x int
y int
}
var interfaceSlice []interface{}
var uintSlice []uint
var tmpInterfaceSlice []interface{}
var tmpUintSlice []uint
var InterfaceCollectionBox []interface{}
var TaggingCollectionBox []interface{}
func init() {
rand.Seed(time.Now().UnixNano())
initializingConventional()
tmpInterfaceSlice = interfaceSlice
initializingTagging()
tmpUintSlice = uintSlice
}
func TagPointer(ptr *uint, i uint) {
x := (*ptr | i)
*ptr = x
}
func ReturnPointer(ptr *uint) uint {
tag := *ptr & uint(7)
*ptr = (*ptr & ^uint(7))
return tag
}
func initializingConventional() {
interfaceSlice = make([]interface{}, 0)
for i := 0; i < 1000; i++ {
random := rand.Intn(5)
switch random {
case 0:
a := "Hello World"
interfaceSlice = append(interfaceSlice, &a)
case 1:
a := 65535
interfaceSlice = append(interfaceSlice, &a)
case 2:
a := 3.14159
interfaceSlice = append(interfaceSlice, &a)
case 3:
a := &vector{1,2}
interfaceSlice = append(interfaceSlice, a)
case 4:
a := uint(65535)
interfaceSlice = append(interfaceSlice, &a)
}
}
}
func initializingTagging() {
uintSlice = make([]uint, 0)
for i := 0; i < 1000; i++ {
random := rand.Intn(5)
switch random {
case 0:
a := "Hello World"
u := uint(uintptr(unsafe.Pointer(&a)))
TagPointer(&u, uint(1))
uintSlice = append(uintSlice, u)
case 1:
a := 65535
u := uint(uintptr(unsafe.Pointer(&a)))
TagPointer(&u, uint(2))
uintSlice = append(uintSlice, u)
case 2:
a := 3.14159
u := uint(uintptr(unsafe.Pointer(&a)))
TagPointer(&u, uint(3))
uintSlice = append(uintSlice, u)
case 3:
a := &vector{1,2}
u := uint(uintptr(unsafe.Pointer(a)))
TagPointer(&u, uint(4))
uintSlice = append(uintSlice, u)
case 4:
a := uint(65535)
u := uint(uintptr(unsafe.Pointer(&a)))
TagPointer(&u, uint(5))
uintSlice = append(uintSlice, u)
}
}
}
func RetrievingTagging() {
TaggingCollectionBox = make([]interface{}, 0)
for _, v := range tmpUintSlice {
t := ReturnPointer(&v)
switch t {
case 1:
TaggingCollectionBox = append(TaggingCollectionBox, *(*string)(unsafe.Pointer(uintptr(v))))
case 2:
TaggingCollectionBox = append(TaggingCollectionBox, *(*int)(unsafe.Pointer(uintptr(v))))
case 3:
TaggingCollectionBox = append(TaggingCollectionBox, *(*float64)(unsafe.Pointer(uintptr(v))))
case 4:
TaggingCollectionBox = append(TaggingCollectionBox, (*vector)(unsafe.Pointer(uintptr(v))))
case 5:
TaggingCollectionBox = append(TaggingCollectionBox, *(*uint)(unsafe.Pointer(uintptr(v))))
}
}
}
func InterfaceRetrieval() {
InterfaceCollectionBox = make([]interface{}, 0)
for _, v := range tmpInterfaceSlice {
switch t := v.(type) {
case *string:
InterfaceCollectionBox = append(InterfaceCollectionBox, *t)
case *int:
InterfaceCollectionBox = append(InterfaceCollectionBox, *t)
case *float64:
InterfaceCollectionBox = append(InterfaceCollectionBox, *t)
case *vector:
InterfaceCollectionBox = append(InterfaceCollectionBox, t)
case *uint:
InterfaceCollectionBox = append(InterfaceCollectionBox, *t)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment