@@ -717,46 +717,54 @@ runtime·new(Type *typ, uint8 *ret)
717
717
ret = runtime·mallocgc(typ->size, flag, 1, 1);
718
718
719
719
if(UseSpanType && !flag) {
720
- if(false) {
720
+ if(false)
721
721
runtime·printf("new %S: %p\n", *typ->string, ret);
722
- }
723
722
runtime·settype(ret, (uintptr)typ | TypeInfo_SingleObject);
724
723
}
725
724
}
726
725
727
726
FLUSH(&ret);
728
727
}
729
728
730
- // same as runtime·new, but callable from C
731
- void*
732
- runtime·cnew(Type *typ)
729
+ static void*
730
+ cnew(Type *typ, intgo n, int32 objtyp)
733
731
{
734
732
uint32 flag;
735
733
void *ret;
736
734
737
- if(raceenabled)
738
- m->racepc = runtime·getcallerpc(&typ);
739
-
740
- if(typ->size == 0) {
735
+ if((objtyp&(PtrSize-1)) != objtyp)
736
+ runtime·throw("runtime: invalid objtyp");
737
+ if(n < 0 || (typ->size > 0 && n > MaxMem/typ->size))
738
+ runtime·panicstring("runtime: allocation size out of range");
739
+ if(typ->size == 0 || n == 0) {
741
740
// All 0-length allocations use this pointer.
742
741
// The language does not require the allocations to
743
742
// have distinct values.
744
- ret = (uint8*)&runtime·zerobase;
745
- } else {
746
- flag = typ->kind&KindNoPointers ? FlagNoPointers : 0;
747
- ret = runtime·mallocgc(typ->size, flag, 1, 1);
748
-
749
- if(UseSpanType && !flag) {
750
- if(false) {
751
- runtime·printf("new %S: %p\n", *typ->string, ret);
752
- }
753
- runtime·settype(ret, (uintptr)typ | TypeInfo_SingleObject);
754
- }
743
+ return &runtime·zerobase;
744
+ }
745
+ flag = typ->kind&KindNoPointers ? FlagNoPointers : 0;
746
+ ret = runtime·mallocgc(typ->size*n, flag, 1, 1);
747
+ if(UseSpanType && !flag) {
748
+ if(false)
749
+ runtime·printf("cnew [%D]%S: %p\n", (int64)n, *typ->string, ret);
750
+ runtime·settype(ret, (uintptr)typ | objtyp);
755
751
}
756
-
757
752
return ret;
758
753
}
759
754
755
+ // same as runtime·new, but callable from C
756
+ void*
757
+ runtime·cnew(Type *typ)
758
+ {
759
+ return cnew(typ, 1, TypeInfo_SingleObject);
760
+ }
761
+
762
+ void*
763
+ runtime·cnewarray(Type *typ, intgo n)
764
+ {
765
+ return cnew(typ, n, TypeInfo_Array);
766
+ }
767
+
760
768
func GC() {
761
769
runtime·gc(1);
762
770
}
0 commit comments