Commit ec59b840 authored by Russ Cox's avatar Russ Cox

runtime: coalesce 0-size allocations

Fixes #3996.

R=ken2
CC=golang-dev
https://golang.org/cl/7001052
parent 295a4d8e
......@@ -697,6 +697,13 @@ runtime·new(Type *typ, uint8 *ret)
if(raceenabled)
m->racepc = runtime·getcallerpc(&typ);
if(typ->size == 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);
......@@ -706,6 +713,7 @@ runtime·new(Type *typ, uint8 *ret)
}
runtime·settype(ret, (uintptr)typ | TypeInfo_SingleObject);
}
}
FLUSH(&ret);
}
......@@ -719,6 +727,13 @@ runtime·cnew(Type *typ)
if(raceenabled)
m->racepc = runtime·getcallerpc(&typ);
if(typ->size == 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);
......@@ -728,6 +743,8 @@ runtime·cnew(Type *typ)
}
runtime·settype(ret, (uintptr)typ | TypeInfo_SingleObject);
}
}
return ret;
}
......
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