aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/runtime/malloc.goc
diff options
context:
space:
mode:
authorDmitriy Vyukov <dvyukov@google.com>2013-05-27 11:29:11 +0400
committerDmitriy Vyukov <dvyukov@google.com>2013-05-27 11:29:11 +0400
commit5782f4117dcb4c8fc40f8110a57ac531a2abdb99 (patch)
tree9363a84cf5947c29683782513e36c89b3905cf55 /src/pkg/runtime/malloc.goc
parente017e0cb24f9c5ea4c0e2b7479e4b411e4882dcf (diff)
downloadgo-5782f4117dcb4c8fc40f8110a57ac531a2abdb99.tar.xz
runtime: introduce cnewarray() to simplify allocation of typed arrays
R=golang-dev, dsymonds CC=golang-dev https://golang.org/cl/9648044
Diffstat (limited to 'src/pkg/runtime/malloc.goc')
-rw-r--r--src/pkg/runtime/malloc.goc50
1 files changed, 29 insertions, 21 deletions
diff --git a/src/pkg/runtime/malloc.goc b/src/pkg/runtime/malloc.goc
index 7e691fe9c8..b59693b598 100644
--- a/src/pkg/runtime/malloc.goc
+++ b/src/pkg/runtime/malloc.goc
@@ -729,9 +729,8 @@ runtime·new(Type *typ, uint8 *ret)
ret = runtime·mallocgc(typ->size, flag, 1, 1);
if(UseSpanType && !flag) {
- if(false) {
+ if(false)
runtime·printf("new %S: %p\n", *typ->string, ret);
- }
runtime·settype(ret, (uintptr)typ | TypeInfo_SingleObject);
}
}
@@ -739,36 +738,45 @@ runtime·new(Type *typ, uint8 *ret)
FLUSH(&ret);
}
-// same as runtime·new, but callable from C
-void*
-runtime·cnew(Type *typ)
+static void*
+cnew(Type *typ, intgo n, int32 objtyp)
{
uint32 flag;
void *ret;
- if(raceenabled)
- m->racepc = runtime·getcallerpc(&typ);
-
- if(typ->size == 0) {
+ if((objtyp&(PtrSize-1)) != objtyp)
+ runtime·throw("runtime: invalid objtyp");
+ if(n < 0 || (typ->size > 0 && n > MaxMem/typ->size))
+ runtime·panicstring("runtime: allocation size out of range");
+ if(typ->size == 0 || n == 0) {
// All 0-length allocations use this pointer.
// The language does not require the allocations to
// have distinct values.
- ret = (uint8*)&runtime·zerobase;
- } else {
- flag = typ->kind&KindNoPointers ? FlagNoPointers : 0;
- ret = runtime·mallocgc(typ->size, flag, 1, 1);
-
- if(UseSpanType && !flag) {
- if(false) {
- runtime·printf("new %S: %p\n", *typ->string, ret);
- }
- runtime·settype(ret, (uintptr)typ | TypeInfo_SingleObject);
- }
+ return &runtime·zerobase;
+ }
+ flag = typ->kind&KindNoPointers ? FlagNoPointers : 0;
+ ret = runtime·mallocgc(typ->size*n, flag, 1, 1);
+ if(UseSpanType && !flag) {
+ if(false)
+ runtime·printf("cnew [%D]%S: %p\n", (int64)n, *typ->string, ret);
+ runtime·settype(ret, (uintptr)typ | objtyp);
}
-
return ret;
}
+// same as runtime·new, but callable from C
+void*
+runtime·cnew(Type *typ)
+{
+ return cnew(typ, 1, TypeInfo_SingleObject);
+}
+
+void*
+runtime·cnewarray(Type *typ, intgo n)
+{
+ return cnew(typ, n, TypeInfo_Array);
+}
+
func GC() {
runtime·gc(1);
}