131
152
prof = runtime·blockspecial(v);
133
157
// Find size class for v.
134
158
sizeclass = s->sizeclass;
136
160
if(sizeclass == 0) {
138
162
size = s->npages<<PageShift;
139
*(uintptr*)(s->start<<PageShift) = 1; // mark as "needs to be zeroed"
163
*(uintptr*)(s->start<<PageShift) = (uintptr)0xfeedfeedfeedfeedll; // mark as "needs to be zeroed"
140
164
// Must mark v freed before calling unmarkspan and MHeap_Free:
141
165
// they might coalesce v into other spans and change the bitmap further.
142
166
runtime·markfreed(v, size);
143
167
runtime·unmarkspan(v, 1<<PageShift);
144
runtime·MHeap_Free(&runtime·mheap, s, 1);
168
runtime·MHeap_Free(runtime·mheap, s, 1);
147
171
size = runtime·class_to_size[sizeclass];
148
172
if(size > sizeof(uintptr))
149
((uintptr*)v)[1] = 1; // mark as "needs to be zeroed"
173
((uintptr*)v)[1] = (uintptr)0xfeedfeedfeedfeedll; // mark as "needs to be zeroed"
150
174
// Must mark v freed before calling MCache_Free:
151
175
// it might coalesce v and other blocks into a bigger span
152
176
// and change the bitmap further.
274
313
USED(arena_size);
275
314
USED(bitmap_size);
316
if((runtime·mheap = runtime·SysAlloc(sizeof(*runtime·mheap))) == nil)
317
runtime·throw("runtime: cannot allocate heap metadata");
277
319
runtime·InitSizes();
279
limit = runtime·memlimit();
321
// limit = runtime·memlimit();
322
// See https://code.google.com/p/go/issues/detail?id=5049
323
// TODO(rsc): Fix after 1.1.
281
326
// Set up the allocation arena, a contiguous area of memory where
282
327
// allocated data will be found. The arena begins with a bitmap large
283
328
// enough to hold 4 bits per allocated word.
284
329
if(sizeof(void*) == 8 && (limit == 0 || limit > (1<<30))) {
285
330
// On a 64-bit machine, allocate from a single contiguous reservation.
286
// 16 GB should be big enough for now.
331
// 128 GB (MaxMem) should be big enough for now.
288
333
// The code will work with the reservation at any address, but ask
289
// SysReserve to use 0x000000f800000000 if possible.
290
// Allocating a 16 GB region takes away 36 bits, and the amd64
334
// SysReserve to use 0x000000c000000000 if possible.
335
// Allocating a 128 GB region takes away 37 bits, and the amd64
291
336
// doesn't let us choose the top 17 bits, so that leaves the 11 bits
292
// in the middle of 0x00f8 for us to choose. Choosing 0x00f8 means
293
// that the valid memory addresses will begin 0x00f8, 0x00f9, 0x00fa, 0x00fb.
294
// None of the bytes f8 f9 fa fb can appear in valid UTF-8, and
295
// they are otherwise as far from ff (likely a common byte) as possible.
296
// Choosing 0x00 for the leading 6 bits was more arbitrary, but it
297
// is not a common ASCII code point either. Using 0x11f8 instead
337
// in the middle of 0x00c0 for us to choose. Choosing 0x00c0 means
338
// that the valid memory addresses will begin 0x00c0, 0x00c1, ..., 0x0x00df.
339
// In little-endian, that's c0 00, c1 00, ..., df 00. None of those are valid
340
// UTF-8 sequences, and they are otherwise as far away from
341
// ff (likely a common byte) as possible. An earlier attempt to use 0x11f8
298
342
// caused out of memory errors on OS X during thread allocations.
299
343
// These choices are both for debuggability and to reduce the
300
344
// odds of the conservative garbage collector not collecting memory
301
345
// because some non-pointer block of memory had a bit pattern
302
346
// that matched a memory address.
304
// Actually we reserve 17 GB (because the bitmap ends up being 1 GB)
305
// but it hardly matters: fc is not valid UTF-8 either, and we have to
306
// allocate 15 GB before we get that far.
348
// Actually we reserve 136 GB (because the bitmap ends up being 8 GB)
349
// but it hardly matters: e0 00 is not valid UTF-8 either.
308
351
// If this fails we fall back to the 32 bit memory mechanism
309
arena_size = 16LL<<30;
310
353
bitmap_size = arena_size / (sizeof(void*)*8/4);
311
p = runtime·SysReserve((void*)(0x00f8ULL<<32), bitmap_size + arena_size);
354
p = runtime·SysReserve((void*)(0x00c0ULL<<32), bitmap_size + arena_size);
314
357
// On a 32-bit machine, we can't typically get away
354
397
if((uintptr)p & (((uintptr)1<<PageShift)-1))
355
398
runtime·throw("runtime: SysReserve returned unaligned address");
357
runtime·mheap.bitmap = p;
358
runtime·mheap.arena_start = p + bitmap_size;
359
runtime·mheap.arena_used = runtime·mheap.arena_start;
360
runtime·mheap.arena_end = runtime·mheap.arena_start + arena_size;
400
runtime·mheap->bitmap = p;
401
runtime·mheap->arena_start = p + bitmap_size;
402
runtime·mheap->arena_used = runtime·mheap->arena_start;
403
runtime·mheap->arena_end = runtime·mheap->arena_start + arena_size;
362
405
// Initialize the rest of the allocator.
363
runtime·MHeap_Init(&runtime·mheap, runtime·SysAlloc);
406
runtime·MHeap_Init(runtime·mheap, runtime·SysAlloc);
364
407
m->mcache = runtime·allocmcache();
366
409
// See if it works.
420
465
if(h->arena_used > h->arena_end)
421
466
h->arena_end = h->arena_used;
422
467
runtime·MHeap_MapBits(h);
469
runtime·racemapshadow(p, n);
475
static Lock settype_lock;
478
runtime·settype_flush(M *mp, bool sysalloc)
480
uintptr *buf, *endbuf;
481
uintptr size, ofs, j, t;
482
uintptr ntypes, nbytes2, nbytes3;
490
buf = mp->settype_buf;
491
endbuf = buf + mp->settype_bufsize;
493
runtime·lock(&settype_lock);
494
while(buf < endbuf) {
501
// (Manually inlined copy of runtime·MHeap_Lookup)
502
p = (uintptr)v>>PageShift;
503
if(sizeof(void*) == 8)
504
p -= (uintptr)runtime·mheap->arena_start >> PageShift;
505
s = runtime·mheap->map[p];
507
if(s->sizeclass == 0) {
508
s->types.compression = MTypes_Single;
514
ofs = ((uintptr)v - (s->start<<PageShift)) / size;
516
switch(s->types.compression) {
518
ntypes = (s->npages << PageShift) / size;
519
nbytes3 = 8*sizeof(uintptr) + 1*ntypes;
522
data3 = runtime·mallocgc(nbytes3, FlagNoProfiling|FlagNoPointers, 0, 1);
524
data3 = runtime·SysAlloc(nbytes3);
526
runtime·throw("runtime: cannot allocate memory");
527
if(0) runtime·printf("settype(0->3): SysAlloc(%x) --> %p\n", (uint32)nbytes3, data3);
530
s->types.compression = MTypes_Bytes;
531
s->types.sysalloc = sysalloc;
532
s->types.data = (uintptr)data3;
534
((uintptr*)data3)[1] = typ;
535
data3[8*sizeof(uintptr) + ofs] = 1;
539
((uintptr*)s->types.data)[ofs] = typ;
543
data3 = (byte*)s->types.data;
545
if(((uintptr*)data3)[j] == typ) {
548
if(((uintptr*)data3)[j] == 0) {
549
((uintptr*)data3)[j] = typ;
554
data3[8*sizeof(uintptr) + ofs] = j;
556
ntypes = (s->npages << PageShift) / size;
557
nbytes2 = ntypes * sizeof(uintptr);
560
data2 = runtime·mallocgc(nbytes2, FlagNoProfiling|FlagNoPointers, 0, 1);
562
data2 = runtime·SysAlloc(nbytes2);
564
runtime·throw("runtime: cannot allocate memory");
565
if(0) runtime·printf("settype.(3->2): SysAlloc(%x) --> %p\n", (uint32)nbytes2, data2);
568
sysalloc3 = s->types.sysalloc;
570
s->types.compression = MTypes_Words;
571
s->types.sysalloc = sysalloc;
572
s->types.data = (uintptr)data2;
574
// Move the contents of data3 to data2. Then deallocate data3.
575
for(j=0; j<ntypes; j++) {
576
t = data3[8*sizeof(uintptr) + j];
577
t = ((uintptr*)data3)[t];
581
nbytes3 = 8*sizeof(uintptr) + 1*ntypes;
582
if(0) runtime·printf("settype.(3->2): SysFree(%p,%x)\n", data3, (uint32)nbytes3);
583
runtime·SysFree(data3, nbytes3);
591
runtime·unlock(&settype_lock);
593
mp->settype_bufsize = 0;
596
// It is forbidden to use this function if it is possible that
597
// explicit deallocation via calling runtime·free(v) may happen.
599
runtime·settype(void *v, uintptr t)
607
runtime·throw("settype: zero type");
610
buf = mp->settype_buf;
611
i = mp->settype_bufsize;
612
buf[i+0] = (uintptr)v;
615
mp->settype_bufsize = i;
617
if(i == nelem(mp->settype_buf)) {
618
runtime·settype_flush(mp, false);
621
if(DebugTypeAtBlockEnd) {
622
s = runtime·MHeap_Lookup(runtime·mheap, v);
623
*(uintptr*)((uintptr)v+s->elemsize-sizeof(uintptr)) = t;
628
runtime·settype_sysfree(MSpan *s)
630
uintptr ntypes, nbytes;
632
if(!s->types.sysalloc)
635
nbytes = (uintptr)-1;
637
switch (s->types.compression) {
639
ntypes = (s->npages << PageShift) / s->elemsize;
640
nbytes = ntypes * sizeof(uintptr);
643
ntypes = (s->npages << PageShift) / s->elemsize;
644
nbytes = 8*sizeof(uintptr) + 1*ntypes;
648
if(nbytes != (uintptr)-1) {
649
if(0) runtime·printf("settype: SysFree(%p,%x)\n", (void*)s->types.data, (uint32)nbytes);
650
runtime·SysFree((void*)s->types.data, nbytes);
655
runtime·gettype(void *v)
661
s = runtime·MHeap_LookupMaybe(runtime·mheap, v);
664
switch(s->types.compression) {
671
ofs = (uintptr)v - (s->start<<PageShift);
672
t = ((uintptr*)s->types.data)[ofs/s->elemsize];
675
ofs = (uintptr)v - (s->start<<PageShift);
676
data = (byte*)s->types.data;
677
t = data[8*sizeof(uintptr) + ofs/s->elemsize];
678
t = ((uintptr*)data)[t];
681
runtime·throw("runtime·gettype: invalid compression kind");
684
runtime·lock(&settype_lock);
685
runtime·printf("%p -> %d,%X\n", v, (int32)s->types.compression, (int64)t);
686
runtime·unlock(&settype_lock);
428
693
// Runtime stubs.
433
698
return runtime·mallocgc(n, 0, 1, 1);
436
func new(typ *Type) (ret *uint8) {
437
uint32 flag = typ->kind&KindNoPointers ? FlagNoPointers : 0;
438
ret = runtime·mallocgc(typ->size, flag, 1, 1);
703
runtime·new(Type *typ, uint8 *ret)
708
m->racepc = runtime·getcallerpc(&typ);
711
// All 0-length allocations use this pointer.
712
// The language does not require the allocations to
713
// have distinct values.
714
ret = (uint8*)&runtime·zerobase;
716
flag = typ->kind&KindNoPointers ? FlagNoPointers : 0;
717
ret = runtime·mallocgc(typ->size, flag, 1, 1);
719
if(UseSpanType && !flag) {
721
runtime·printf("new %S: %p\n", *typ->string, ret);
722
runtime·settype(ret, (uintptr)typ | TypeInfo_SingleObject);
443
runtime·stackalloc(uint32 n)
445
// Stackalloc must be called on scheduler stack, so that we
446
// never try to grow the stack during the code that stackalloc runs.
447
// Doing so would cause a deadlock (issue 1547).
449
runtime·throw("stackalloc not on scheduler stack");
451
// Stack allocator uses malloc/free most of the time,
452
// but if we're in the middle of malloc and need stack,
453
// we have to do something else to avoid deadlock.
454
// In that case, we fall back on a fixed-size free-list
455
// allocator, assuming that inside malloc all the stack
456
// frames are small, so that all the stack allocations
457
// will be a single size, the minimum (right now, 5k).
458
if(m->mallocing || m->gcing || n == FixedStack) {
459
if(n != FixedStack) {
460
runtime·printf("stackalloc: in malloc, size=%d want %d", FixedStack, n);
461
runtime·throw("stackalloc");
463
return runtime·FixAlloc_Alloc(m->stackalloc);
465
return runtime·mallocgc(n, FlagNoProfiling|FlagNoGC, 0, 0);
469
runtime·stackfree(void *v, uintptr n)
471
if(m->mallocing || m->gcing || n == FixedStack) {
472
runtime·FixAlloc_Free(m->stackalloc, v);
730
cnew(Type *typ, intgo n, int32 objtyp)
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) {
740
// All 0-length allocations use this pointer.
741
// The language does not require the allocations to
742
// have distinct values.
743
return &runtime·zerobase;
745
flag = typ->kind&KindNoPointers ? FlagNoPointers : 0;
746
ret = runtime·mallocgc(typ->size*n, flag, 1, 1);
747
if(UseSpanType && !flag) {
749
runtime·printf("cnew [%D]%S: %p\n", (int64)n, *typ->string, ret);
750
runtime·settype(ret, (uintptr)typ | objtyp);
755
// same as runtime·new, but callable from C
757
runtime·cnew(Type *typ)
759
return cnew(typ, 1, TypeInfo_SingleObject);
763
runtime·cnewarray(Type *typ, intgo n)
765
return cnew(typ, n, TypeInfo_Array);