"func unsafe.Typeof (i interface { }) (typ interface { })\n"
"func unsafe.Reflect (i interface { }) (typ interface { }, addr unsafe.Pointer)\n"
"func unsafe.Unreflect (typ interface { }, addr unsafe.Pointer) (ret interface { })\n"
+ "func unsafe.New (typ interface { }) (? unsafe.Pointer)\n"
+ "func unsafe.NewArray (typ interface { }, n int) (? unsafe.Pointer)\n"
"\n"
"$$\n";
func Typeof(i interface{}) (typ interface{})
func Reflect(i interface{}) (typ interface{}, addr Pointer)
func Unreflect(typ interface{}, addr Pointer) (ret interface{})
+func New(typ interface{}) Pointer
+func NewArray(typ interface{}, n int) Pointer
"math";
"os";
"reflect";
+ "runtime";
"unsafe";
)
up = decIndirect(up, indir)
}
if *(*unsafe.Pointer)(up) == nil {
- // Allocate object by making a slice of bytes and recording the
- // address of the beginning of the array. TODO(rsc).
- b := make([]byte, rtyp.Size());
- *(*unsafe.Pointer)(up) = unsafe.Pointer(&b[0]);
+ // Allocate object.
+ *(*unsafe.Pointer)(up) = unsafe.New((*runtime.StructType)(unsafe.Pointer(rtyp)))
}
p = *(*uintptr)(up);
}
if indir > 0 {
up := unsafe.Pointer(p);
if *(*unsafe.Pointer)(up) == nil {
- // Allocate the array by making a slice of bytes of the correct size
- // and taking the address of the beginning of the array. TODO(rsc).
- b := make([]byte, atyp.Size());
- *(**byte)(up) = &b[0];
+ // Allocate object.
+ *(*unsafe.Pointer)(up) = unsafe.New(atyp)
}
p = *(*uintptr)(up);
}
}
func decodeSlice(atyp *reflect.SliceType, state *decodeState, p uintptr, elemOp decOp, elemWid uintptr, indir, elemIndir int, ovfl os.ErrorString) os.Error {
- length := uintptr(decodeUint(state));
+ n := int(uintptr(decodeUint(state)));
if indir > 0 {
up := unsafe.Pointer(p);
if *(*unsafe.Pointer)(up) == nil {
// Allocate the slice header.
- *(*unsafe.Pointer)(up) = unsafe.Pointer(new(reflect.SliceHeader))
+ *(*unsafe.Pointer)(up) = unsafe.Pointer(new([]unsafe.Pointer))
}
p = *(*uintptr)(up);
}
// Allocate storage for the slice elements, that is, the underlying array.
- data := make([]byte, length*atyp.Elem().Size());
// Always write a header at p.
hdrp := (*reflect.SliceHeader)(unsafe.Pointer(p));
- hdrp.Data = uintptr(unsafe.Pointer(&data[0]));
- hdrp.Len = int(length);
- hdrp.Cap = int(length);
- return decodeArrayHelper(state, hdrp.Data, elemOp, elemWid, int(length), elemIndir, ovfl);
+ hdrp.Data = uintptr(unsafe.NewArray(atyp.Elem(), n));
+ hdrp.Len = n;
+ hdrp.Cap = n;
+ return decodeArrayHelper(state, hdrp.Data, elemOp, elemWid, n, elemIndir, ovfl);
}
func ignoreSlice(state *decodeState, elemOp decOp) os.Error {
// MakeSlice creates a new zero-initialized slice value
// for the specified slice type, length, and capacity.
func MakeSlice(typ *SliceType, len, cap int) *SliceValue {
- s := new(SliceHeader);
- size := typ.Elem().Size() * uintptr(cap);
- if size == 0 {
- size = 1
- }
- data := make([]uint8, size);
- s.Data = uintptr(addr(&data[0]));
- s.Len = len;
- s.Cap = cap;
+ s := &SliceHeader{
+ Data: uintptr(unsafe.NewArray(typ.Elem(), cap)),
+ Len: len,
+ Cap: cap,
+ };
return newValue(typ, addr(s), true).(*SliceValue);
}
// MakeZero returns a zero Value for the specified Type.
func MakeZero(typ Type) Value {
- // TODO: this will have to move into
- // the runtime proper in order to play nicely
- // with the garbage collector.
- size := typ.Size();
- if size == 0 {
- size = 1
- }
- data := make([]uint8, size);
- return newValue(typ, addr(&data[0]), true);
+ if typ == nil {
+ return nil
+ }
+ return newValue(typ, addr(unsafe.New(typ)), true);
}
#include "runtime.h"
#include "type.h"
+#include "malloc.h"
static void
printiface(Iface i)
}
void
-unsafe·Unreflect(Iface typ, void *addr, Eface e)
+unsafe·Unreflect(Eface typ, void *addr, Eface e)
{
// Reflect library has reinterpreted typ
// as its own kind of type structure.
FLUSH(&e);
}
+
+void
+unsafe·New(Eface typ, void *ret)
+{
+ Type *t;
+
+ // Reflect library has reinterpreted typ
+ // as its own kind of type structure.
+ // We know that the pointer to the original
+ // type structure sits before the data pointer.
+ t = (Type*)((Eface*)typ.data-1);
+
+ if(t->kind&KindNoPointers)
+ ret = mallocgc(t->size, RefNoPointers, 1);
+ else
+ ret = mal(t->size);
+ FLUSH(&ret);
+}
+
+void
+unsafe·NewArray(Eface typ, uint32 n, void *ret)
+{
+ uint64 size;
+ Type *t;
+
+ // Reflect library has reinterpreted typ
+ // as its own kind of type structure.
+ // We know that the pointer to the original
+ // type structure sits before the data pointer.
+ t = (Type*)((Eface*)typ.data-1);
+
+ size = n*t->size;
+ if(t->kind&KindNoPointers)
+ ret = mallocgc(size, RefNoPointers, 1);
+ else
+ ret = mal(size);
+ FLUSH(&ret);
+}
static int32 debug = 0;
+// see also unsafe·NewArray
// makeslice(typ *Type, nel int, cap int) (ary []any);
void
runtime·makeslice(SliceType *t, uint32 nel, uint32 cap, Slice ret)
ret.len = nel;
ret.cap = cap;
- // TODO(rsc): Disabled because reflect and gob cast []byte
- // to data structures with pointers.
- if(0 && (t->elem->kind&KindNoPointers))
+ if((t->elem->kind&KindNoPointers))
ret.array = mallocgc(size, RefNoPointers, 1);
else
ret.array = mal(size);
func Reflect(i interface{}) (typ interface{}, addr uintptr)
// Unreflect inverts Reflect: Given a type and a pointer, it returns an empty interface value
-// with those contents.
+// with those contents. The typ is assumed to contain a pointer to a runtime type;
+// the type information in the interface{} is ignored, so that, for example, both
+// *reflect.StructType and *runtime.StructType can be passed for typ.
func Unreflect(typ interface{}, addr uintptr) (ret interface{})
+
+// New allocates and returns a pointer to memory for a new value of the given type.
+// The typ is assumed to hold a pointer to a runtime type.
+// Callers should use reflect.MakeZero instead of invoking unsafe.New directly.
+func New(typ interface{}) Pointer
+
+// NewArray allocates and returns a pointer to an array of n elements of the given type.
+// The typ is assumed to hold a pointer to a runtime type.
+// Callers should use reflect.MakeSlice instead of invoking unsafe.NewArray directly.
+func NewArray(typ interface{}, n int) Pointer