1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // See malloc.h for overview.
7 // TODO(rsc): double-check stats.
17 #include "interface.h"
20 // Map gccgo field names to gc field names.
21 // Eface aka __go_empty_interface.
22 #define type __type_descriptor
23 // Type aka __go_type_descriptor
25 #define string __reflection
27 // GCCGO SPECIFIC CHANGE
29 // There is a long comment in runtime_mallocinit about where to put the heap
30 // on a 64-bit system. It makes assumptions that are not valid on linux/arm64
31 // -- it assumes user space can choose the lower 47 bits of a pointer, but on
32 // linux/arm64 we can only choose the lower 39 bits. This means the heap is
33 // roughly a quarter of the available address space and we cannot choose a bit
34 // pattern that all pointers will have -- luckily the GC is mostly precise
35 // these days so this doesn't matter all that much. The kernel (as of 3.13)
36 // will allocate address space starting either down from 0x7fffffffff or up
37 // from 0x2000000000, so we put the heap roughly in the middle of these two
38 // addresses to minimize the chance that a non-heap allocation will get in the
41 // This all means that there isn't much point in trying 256 different
42 // locations for the heap on such systems.
44 #define HeapBase(i) ((void*)(uintptr)(0x40ULL<<32))
45 #define HeapBaseOptions 1
47 #define HeapBase(i) ((void*)(uintptr)(i<<40|0x00c0ULL<<32))
48 #define HeapBaseOptions 0x80
50 // END GCCGO SPECIFIC CHANGE
52 // Mark mheap as 'no pointers', it does not contain interesting pointers but occupies ~45K.
56 int32 runtime_checking;
58 extern MStats mstats; // defined in zruntime_def_$GOOS_$GOARCH.go
60 extern volatile intgo runtime_MemProfileRate
61 __asm__ (GOSYM_PREFIX "runtime.MemProfileRate");
63 static MSpan* largealloc(uint32, uintptr*);
64 static void runtime_profilealloc(void *v, uintptr size);
65 static void settype(MSpan *s, void *v, uintptr typ);
67 // Allocate an object of at least size bytes.
68 // Small objects are allocated from the per-thread cache's free lists.
69 // Large objects (> 32 kB) are allocated straight from the heap.
70 // If the block will be freed with runtime_free(), typ must be 0.
72 runtime_mallocgc(uintptr size, uintptr typ, uint32 flag)
77 uintptr tinysize, size1;
86 // All 0-length allocations use this pointer.
87 // The language does not require the allocations to
88 // have distinct values.
89 return &runtime_zerobase;
96 if(m->mcache == nil && m->ncgo > 0) {
97 // For gccgo this case can occur when a cgo or SWIG function
98 // has an interface return type and the function
99 // returns a non-pointer, so memory allocation occurs
100 // after syscall.Cgocall but before syscall.CgocallDone.
101 // We treat it as a callback.
102 runtime_exitsyscall();
105 flag |= FlagNoInvokeGC;
108 if(runtime_gcwaiting() && g != m->g0 && m->locks == 0 && !(flag & FlagNoInvokeGC)) {
113 runtime_throw("malloc/free - deadlock");
114 // Disable preemption during settype.
115 // We can not use m->mallocing for this, because settype calls mallocgc.
119 if(DebugTypeAtBlockEnd)
120 size += sizeof(uintptr);
123 if(!runtime_debug.efence && size <= MaxSmallSize) {
124 if((flag&(FlagNoScan|FlagNoGC)) == FlagNoScan && size < TinySize) {
127 // Tiny allocator combines several tiny allocation requests
128 // into a single memory block. The resulting memory block
129 // is freed when all subobjects are unreachable. The subobjects
130 // must be FlagNoScan (don't have pointers), this ensures that
131 // the amount of potentially wasted memory is bounded.
133 // Size of the memory block used for combining (TinySize) is tunable.
134 // Current setting is 16 bytes, which relates to 2x worst case memory
135 // wastage (when all but one subobjects are unreachable).
136 // 8 bytes would result in no wastage at all, but provides less
137 // opportunities for combining.
138 // 32 bytes provides more opportunities for combining,
139 // but can lead to 4x worst case wastage.
140 // The best case winning is 8x regardless of block size.
142 // Objects obtained from tiny allocator must not be freed explicitly.
143 // So when an object will be freed explicitly, we ensure that
144 // its size >= TinySize.
146 // SetFinalizer has a special case for objects potentially coming
147 // from tiny allocator, it such case it allows to set finalizers
148 // for an inner byte of a memory block.
150 // The main targets of tiny allocator are small strings and
151 // standalone escaping variables. On a json benchmark
152 // the allocator reduces number of allocations by ~12% and
153 // reduces heap size by ~20%.
155 tinysize = c->tinysize;
156 if(size <= tinysize) {
158 // Align tiny pointer for required (conservative) alignment.
160 tiny = (byte*)ROUND((uintptr)tiny, 8);
161 else if((size&3) == 0)
162 tiny = (byte*)ROUND((uintptr)tiny, 4);
163 else if((size&1) == 0)
164 tiny = (byte*)ROUND((uintptr)tiny, 2);
165 size1 = size + (tiny - (byte*)c->tiny);
166 if(size1 <= tinysize) {
167 // The object fits into existing tiny block.
169 c->tiny = (byte*)c->tiny + size1;
170 c->tinysize -= size1;
174 runtime_entersyscall();
178 // Allocate a new TinySize block.
179 s = c->alloc[TinySizeClass];
180 if(s->freelist == nil)
181 s = runtime_MCache_Refill(c, TinySizeClass);
186 if(next != nil) // prefetching nil leads to a DTLB miss
190 // See if we need to replace the existing tiny block with the new one
191 // based on amount of remaining free space.
192 if(TinySize-size > tinysize) {
193 c->tiny = (byte*)v + size;
194 c->tinysize = TinySize - size;
199 // Allocate from mcache free lists.
200 // Inlined version of SizeToClass().
202 sizeclass = runtime_size_to_class8[(size+7)>>3];
204 sizeclass = runtime_size_to_class128[(size-1024+127) >> 7];
205 size = runtime_class_to_size[sizeclass];
206 s = c->alloc[sizeclass];
207 if(s->freelist == nil)
208 s = runtime_MCache_Refill(c, sizeclass);
213 if(next != nil) // prefetching nil leads to a DTLB miss
215 if(!(flag & FlagNoZero)) {
217 // block is zeroed iff second word is zero ...
218 if(size > 2*sizeof(uintptr) && ((uintptr*)v)[1] != 0)
219 runtime_memclr((byte*)v, size);
222 c->local_cachealloc += size;
224 // Allocate directly from heap.
225 s = largealloc(flag, &size);
226 v = (void*)(s->start << PageShift);
231 else if(!(flag & FlagNoScan))
234 if(DebugTypeAtBlockEnd)
235 *(uintptr*)((uintptr)v+size-sizeof(uintptr)) = typ;
238 // TODO: save type even if FlagNoScan? Potentially expensive but might help
239 // heap profiling/tracing.
240 if(UseSpanType && !(flag & FlagNoScan) && typ != 0)
243 if(runtime_debug.allocfreetrace)
244 runtime_tracealloc(v, size, typ);
246 if(!(flag & FlagNoProfiling) && (rate = runtime_MemProfileRate) > 0) {
247 if(size < (uintptr)rate && size < (uintptr)(uint32)c->next_sample)
248 c->next_sample -= size;
250 runtime_profilealloc(v, size);
255 if(!(flag & FlagNoInvokeGC) && mstats.heap_alloc >= mstats.next_gc)
259 runtime_entersyscall();
265 largealloc(uint32 flag, uintptr *sizep)
267 uintptr npages, size;
271 // Allocate directly from heap.
273 if(size + PageSize < size)
274 runtime_throw("out of memory");
275 npages = size >> PageShift;
276 if((size & PageMask) != 0)
278 s = runtime_MHeap_Alloc(&runtime_mheap, npages, 0, 1, !(flag & FlagNoZero));
280 runtime_throw("out of memory");
281 s->limit = (uintptr)((byte*)(s->start<<PageShift) + size);
282 *sizep = npages<<PageShift;
283 v = (void*)(s->start << PageShift);
284 // setup for mark sweep
285 runtime_markspan(v, 0, 0, true);
290 runtime_profilealloc(void *v, uintptr size)
296 c = runtime_m()->mcache;
297 rate = runtime_MemProfileRate;
299 // pick next profile time
300 // If you change this, also change allocmcache.
301 if(rate > 0x3fffffff) // make 2*rate not overflow
303 next = runtime_fastrand1() % (2*rate);
304 // Subtract the "remainder" of the current allocation.
305 // Otherwise objects that are close in size to sampling rate
306 // will be under-sampled, because we consistently discard this remainder.
307 next -= (size - c->next_sample);
310 c->next_sample = next;
312 runtime_MProf_Malloc(v, size);
316 __go_alloc(uintptr size)
318 return runtime_mallocgc(size, 0, FlagNoInvokeGC);
321 // Free the object whose base pointer is v.
334 // If you change this also change mgc0.c:/^sweep,
335 // which has a copy of the guts of free.
339 runtime_throw("malloc/free - deadlock");
342 if(!runtime_mlookup(v, nil, nil, &s)) {
343 runtime_printf("free %p: not an allocated block\n", v);
344 runtime_throw("free runtime_mlookup");
347 sizeclass = s->sizeclass;
348 // Objects that are smaller than TinySize can be allocated using tiny alloc,
349 // if then such object is combined with an object with finalizer, we will crash.
351 runtime_throw("freeing too small block");
353 if(runtime_debug.allocfreetrace)
354 runtime_tracefree(v, size);
356 // Ensure that the span is swept.
357 // If we free into an unswept span, we will corrupt GC bitmaps.
358 runtime_MSpan_EnsureSwept(s);
360 if(s->specials != nil)
361 runtime_freeallspecials(s, v, size);
367 // Must mark v freed before calling unmarkspan and MHeap_Free:
368 // they might coalesce v into other spans and change the bitmap further.
369 runtime_markfreed(v);
370 runtime_unmarkspan(v, 1<<PageShift);
371 // NOTE(rsc,dvyukov): The original implementation of efence
372 // in CL 22060046 used SysFree instead of SysFault, so that
373 // the operating system would eventually give the memory
374 // back to us again, so that an efence program could run
375 // longer without running out of memory. Unfortunately,
376 // calling SysFree here without any kind of adjustment of the
377 // heap data structures means that when the memory does
378 // come back to us, we have the wrong metadata for it, either in
379 // the MSpan structures or in the garbage collection bitmap.
380 // Using SysFault here means that the program will run out of
381 // memory fairly quickly in efence mode, but at least it won't
382 // have mysterious crashes due to confused memory reuse.
383 // It should be possible to switch back to SysFree if we also
384 // implement and then call some kind of MHeap_DeleteSpan.
385 if(runtime_debug.efence)
386 runtime_SysFault((void*)(s->start<<PageShift), size);
388 runtime_MHeap_Free(&runtime_mheap, s, 1);
389 c->local_nlargefree++;
390 c->local_largefree += size;
393 if(size > 2*sizeof(uintptr))
394 ((uintptr*)v)[1] = (uintptr)0xfeedfeedfeedfeedll; // mark as "needs to be zeroed"
395 else if(size > sizeof(uintptr))
396 ((uintptr*)v)[1] = 0;
397 // Must mark v freed before calling MCache_Free:
398 // it might coalesce v and other blocks into a bigger span
399 // and change the bitmap further.
400 c->local_nsmallfree[sizeclass]++;
401 c->local_cachealloc -= size;
402 if(c->alloc[sizeclass] == s) {
403 // We own the span, so we can just add v to the freelist
404 runtime_markfreed(v);
405 ((MLink*)v)->next = s->freelist;
409 // Someone else owns this span. Add to free queue.
410 runtime_MCache_Free(c, v, sizeclass, size);
417 runtime_mlookup(void *v, byte **base, uintptr *size, MSpan **sp)
426 m->mcache->local_nlookup++;
427 if (sizeof(void*) == 4 && m->mcache->local_nlookup >= (1<<30)) {
428 // purge cache stats to prevent overflow
429 runtime_lock(&runtime_mheap);
430 runtime_purgecachedstats(m->mcache);
431 runtime_unlock(&runtime_mheap);
434 s = runtime_MHeap_LookupMaybe(&runtime_mheap, v);
438 runtime_checkfreed(v, 1);
446 p = (byte*)((uintptr)s->start<<PageShift);
447 if(s->sizeclass == 0) {
452 *size = s->npages<<PageShift;
458 i = ((byte*)v - p)/n;
468 runtime_purgecachedstats(MCache *c)
473 // Protected by either heap or GC lock.
475 mstats.heap_alloc += (intptr)c->local_cachealloc;
476 c->local_cachealloc = 0;
477 mstats.nlookup += c->local_nlookup;
478 c->local_nlookup = 0;
479 h->largefree += c->local_largefree;
480 c->local_largefree = 0;
481 h->nlargefree += c->local_nlargefree;
482 c->local_nlargefree = 0;
483 for(i=0; i<(int32)nelem(c->local_nsmallfree); i++) {
484 h->nsmallfree[i] += c->local_nsmallfree[i];
485 c->local_nsmallfree[i] = 0;
489 extern uintptr runtime_sizeof_C_MStats
490 __asm__ (GOSYM_PREFIX "runtime.Sizeof_C_MStats");
492 // Size of the trailing by_size array differs between Go and C,
493 // _NumSizeClasses was changed, but we can not change Go struct because of backward compatibility.
494 // sizeof_C_MStats is what C thinks about size of Go struct.
496 // Initialized in mallocinit because it's defined in go/runtime/mem.go.
498 #define MaxArena32 (2U<<30)
501 runtime_mallocinit(void)
504 uintptr arena_size, bitmap_size, spans_size, p_size;
511 runtime_sizeof_C_MStats = sizeof(MStats) - (_NumSizeClasses - 61) * sizeof(mstats.by_size[0]);
529 if(runtime_class_to_size[TinySizeClass] != TinySize)
530 runtime_throw("bad TinySizeClass");
532 // limit = runtime_memlimit();
533 // See https://code.google.com/p/go/issues/detail?id=5049
534 // TODO(rsc): Fix after 1.1.
537 // Set up the allocation arena, a contiguous area of memory where
538 // allocated data will be found. The arena begins with a bitmap large
539 // enough to hold 4 bits per allocated word.
540 if(sizeof(void*) == 8 && (limit == 0 || limit > (1<<30))) {
541 // On a 64-bit machine, allocate from a single contiguous reservation.
542 // 128 GB (MaxMem) should be big enough for now.
544 // The code will work with the reservation at any address, but ask
545 // SysReserve to use 0x0000XXc000000000 if possible (XX=00...7f).
546 // Allocating a 128 GB region takes away 37 bits, and the amd64
547 // doesn't let us choose the top 17 bits, so that leaves the 11 bits
548 // in the middle of 0x00c0 for us to choose. Choosing 0x00c0 means
549 // that the valid memory addresses will begin 0x00c0, 0x00c1, ..., 0x00df.
550 // In little-endian, that's c0 00, c1 00, ..., df 00. None of those are valid
551 // UTF-8 sequences, and they are otherwise as far away from
552 // ff (likely a common byte) as possible. If that fails, we try other 0xXXc0
553 // addresses. An earlier attempt to use 0x11f8 caused out of memory errors
554 // on OS X during thread allocations. 0x00c0 causes conflicts with
555 // AddressSanitizer which reserves all memory up to 0x0100.
556 // These choices are both for debuggability and to reduce the
557 // odds of the conservative garbage collector not collecting memory
558 // because some non-pointer block of memory had a bit pattern
559 // that matched a memory address.
561 // Actually we reserve 136 GB (because the bitmap ends up being 8 GB)
562 // but it hardly matters: e0 00 is not valid UTF-8 either.
564 // If this fails we fall back to the 32 bit memory mechanism
566 bitmap_size = arena_size / (sizeof(void*)*8/4);
567 spans_size = arena_size / PageSize * sizeof(runtime_mheap.spans[0]);
568 spans_size = ROUND(spans_size, PageSize);
569 for(i = 0; i < HeapBaseOptions; i++) {
571 p_size = bitmap_size + spans_size + arena_size + PageSize;
572 p = runtime_SysReserve(p, p_size, &reserved);
578 // On a 32-bit machine, we can't typically get away
579 // with a giant virtual address space reservation.
580 // Instead we map the memory information bitmap
581 // immediately after the data segment, large enough
582 // to handle another 2GB of mappings (256 MB),
583 // along with a reservation for another 512 MB of memory.
584 // When that gets used up, we'll start asking the kernel
585 // for any memory anywhere and hope it's in the 2GB
586 // following the bitmap (presumably the executable begins
587 // near the bottom of memory, so we'll have to use up
588 // most of memory before the kernel resorts to giving out
589 // memory before the beginning of the text segment).
591 // Alternatively we could reserve 512 MB bitmap, enough
592 // for 4GB of mappings, and then accept any memory the
593 // kernel threw at us, but normally that's a waste of 512 MB
594 // of address space, which is probably too much in a 32-bit world.
595 bitmap_size = MaxArena32 / (sizeof(void*)*8/4);
596 arena_size = 512<<20;
597 spans_size = MaxArena32 / PageSize * sizeof(runtime_mheap.spans[0]);
598 if(limit > 0 && arena_size+bitmap_size+spans_size > limit) {
599 bitmap_size = (limit / 9) & ~((1<<PageShift) - 1);
600 arena_size = bitmap_size * 8;
601 spans_size = arena_size / PageSize * sizeof(runtime_mheap.spans[0]);
603 spans_size = ROUND(spans_size, PageSize);
605 // SysReserve treats the address we ask for, end, as a hint,
606 // not as an absolute requirement. If we ask for the end
607 // of the data segment but the operating system requires
608 // a little more space before we can start allocating, it will
609 // give out a slightly higher pointer. Except QEMU, which
610 // is buggy, as usual: it won't adjust the pointer upward.
611 // So adjust it upward a little bit ourselves: 1/4 MB to get
612 // away from the running binary image and then round up
619 p = (byte*)ROUND(end + (1<<18), 1<<20);
620 p_size = bitmap_size + spans_size + arena_size + PageSize;
621 p = runtime_SysReserve(p, p_size, &reserved);
623 runtime_throw("runtime: cannot reserve arena virtual address space");
626 // PageSize can be larger than OS definition of page size,
627 // so SysReserve can give us a PageSize-unaligned pointer.
628 // To overcome this we ask for PageSize more and round up the pointer.
629 p1 = (byte*)ROUND((uintptr)p, PageSize);
631 runtime_mheap.spans = (MSpan**)p1;
632 runtime_mheap.bitmap = p1 + spans_size;
633 runtime_mheap.arena_start = p1 + spans_size + bitmap_size;
634 runtime_mheap.arena_used = runtime_mheap.arena_start;
635 runtime_mheap.arena_end = p + p_size;
636 runtime_mheap.arena_reserved = reserved;
638 if(((uintptr)runtime_mheap.arena_start & (PageSize-1)) != 0)
639 runtime_throw("misrounded allocation in mallocinit");
641 // Initialize the rest of the allocator.
642 runtime_MHeap_Init(&runtime_mheap);
643 runtime_m()->mcache = runtime_allocmcache();
646 runtime_free(runtime_malloc(TinySize));
650 runtime_MHeap_SysAlloc(MHeap *h, uintptr n)
657 if(n > (uintptr)(h->arena_end - h->arena_used)) {
658 // We are in 32-bit mode, maybe we didn't use all possible address space yet.
659 // Reserve some more space.
662 p_size = ROUND(n + PageSize, 256<<20);
663 new_end = h->arena_end + p_size;
664 if(new_end <= h->arena_start + MaxArena32) {
665 // TODO: It would be bad if part of the arena
666 // is reserved and part is not.
667 p = runtime_SysReserve(h->arena_end, p_size, &reserved);
668 if(p == h->arena_end) {
669 h->arena_end = new_end;
670 h->arena_reserved = reserved;
672 else if(p+p_size <= h->arena_start + MaxArena32) {
673 // Keep everything page-aligned.
674 // Our pages are bigger than hardware pages.
675 h->arena_end = p+p_size;
676 h->arena_used = p + (-(uintptr)p&(PageSize-1));
677 h->arena_reserved = reserved;
681 runtime_SysFree(p, p_size, &stat);
685 if(n <= (uintptr)(h->arena_end - h->arena_used)) {
686 // Keep taking from our reservation.
688 runtime_SysMap(p, n, h->arena_reserved, &mstats.heap_sys);
690 runtime_MHeap_MapBits(h);
691 runtime_MHeap_MapSpans(h);
693 if(((uintptr)p & (PageSize-1)) != 0)
694 runtime_throw("misrounded allocation in MHeap_SysAlloc");
698 // If using 64-bit, our reservation is all we have.
699 if((uintptr)(h->arena_end - h->arena_start) >= MaxArena32)
702 // On 32-bit, once the reservation is gone we can
703 // try to get memory at a location chosen by the OS
704 // and hope that it is in the range we allocated bitmap for.
705 p_size = ROUND(n, PageSize) + PageSize;
706 p = runtime_SysAlloc(p_size, &mstats.heap_sys);
710 if(p < h->arena_start || (uintptr)(p+p_size - h->arena_start) >= MaxArena32) {
711 runtime_printf("runtime: memory allocated by OS (%p) not in usable range [%p,%p)\n",
712 p, h->arena_start, h->arena_start+MaxArena32);
713 runtime_SysFree(p, p_size, &mstats.heap_sys);
718 p += -(uintptr)p & (PageSize-1);
719 if(p+n > h->arena_used) {
721 if(p_end > h->arena_end)
722 h->arena_end = p_end;
723 runtime_MHeap_MapBits(h);
724 runtime_MHeap_MapSpans(h);
727 if(((uintptr)p & (PageSize-1)) != 0)
728 runtime_throw("misrounded allocation in MHeap_SysAlloc");
741 PersistentAllocChunk = 256<<10,
742 PersistentAllocMaxBlock = 64<<10, // VM reservation granularity is 64K on windows
745 // Wrapper around SysAlloc that can allocate small chunks.
746 // There is no associated free operation.
747 // Intended for things like function/type/debug-related persistent data.
748 // If align is 0, uses default align (currently 8).
750 runtime_persistentalloc(uintptr size, uintptr align, uint64 *stat)
756 runtime_throw("persistentalloc: align is not a power of 2");
758 runtime_throw("persistentalloc: align is too large");
761 if(size >= PersistentAllocMaxBlock)
762 return runtime_SysAlloc(size, stat);
763 runtime_lock(&persistent);
764 persistent.pos = (byte*)ROUND((uintptr)persistent.pos, align);
765 if(persistent.pos + size > persistent.end) {
766 persistent.pos = runtime_SysAlloc(PersistentAllocChunk, &mstats.other_sys);
767 if(persistent.pos == nil) {
768 runtime_unlock(&persistent);
769 runtime_throw("runtime: cannot allocate memory");
771 persistent.end = persistent.pos + PersistentAllocChunk;
774 persistent.pos += size;
775 runtime_unlock(&persistent);
776 if(stat != &mstats.other_sys) {
777 // reaccount the allocation against provided stat
778 runtime_xadd64(stat, size);
779 runtime_xadd64(&mstats.other_sys, -(uint64)size);
785 settype(MSpan *s, void *v, uintptr typ)
787 uintptr size, ofs, j, t;
788 uintptr ntypes, nbytes2, nbytes3;
792 if(s->sizeclass == 0) {
793 s->types.compression = MTypes_Single;
798 ofs = ((uintptr)v - (s->start<<PageShift)) / size;
800 switch(s->types.compression) {
802 ntypes = (s->npages << PageShift) / size;
803 nbytes3 = 8*sizeof(uintptr) + 1*ntypes;
804 data3 = runtime_mallocgc(nbytes3, 0, FlagNoProfiling|FlagNoScan|FlagNoInvokeGC);
805 s->types.compression = MTypes_Bytes;
806 s->types.data = (uintptr)data3;
807 ((uintptr*)data3)[1] = typ;
808 data3[8*sizeof(uintptr) + ofs] = 1;
812 ((uintptr*)s->types.data)[ofs] = typ;
816 data3 = (byte*)s->types.data;
818 if(((uintptr*)data3)[j] == typ) {
821 if(((uintptr*)data3)[j] == 0) {
822 ((uintptr*)data3)[j] = typ;
827 data3[8*sizeof(uintptr) + ofs] = j;
829 ntypes = (s->npages << PageShift) / size;
830 nbytes2 = ntypes * sizeof(uintptr);
831 data2 = runtime_mallocgc(nbytes2, 0, FlagNoProfiling|FlagNoScan|FlagNoInvokeGC);
832 s->types.compression = MTypes_Words;
833 s->types.data = (uintptr)data2;
835 // Move the contents of data3 to data2. Then deallocate data3.
836 for(j=0; j<ntypes; j++) {
837 t = data3[8*sizeof(uintptr) + j];
838 t = ((uintptr*)data3)[t];
848 runtime_gettype(void *v)
854 s = runtime_MHeap_LookupMaybe(&runtime_mheap, v);
857 switch(s->types.compression) {
864 ofs = (uintptr)v - (s->start<<PageShift);
865 t = ((uintptr*)s->types.data)[ofs/s->elemsize];
868 ofs = (uintptr)v - (s->start<<PageShift);
869 data = (byte*)s->types.data;
870 t = data[8*sizeof(uintptr) + ofs/s->elemsize];
871 t = ((uintptr*)data)[t];
874 runtime_throw("runtime_gettype: invalid compression kind");
877 runtime_printf("%p -> %d,%X\n", v, (int32)s->types.compression, (int64)t);
887 runtime_mal(uintptr n)
889 return runtime_mallocgc(n, 0, 0);
892 func new(typ *Type) (ret *uint8) {
893 ret = runtime_mallocgc(typ->__size, (uintptr)typ | TypeInfo_SingleObject, typ->kind&kindNoPointers ? FlagNoScan : 0);
897 cnew(const Type *typ, intgo n, int32 objtyp)
899 if((objtyp&(PtrSize-1)) != objtyp)
900 runtime_throw("runtime: invalid objtyp");
901 if(n < 0 || (typ->__size > 0 && (uintptr)n > (MaxMem/typ->__size)))
902 runtime_panicstring("runtime: allocation size out of range");
903 return runtime_mallocgc(typ->__size*n, (uintptr)typ | objtyp, typ->kind&kindNoPointers ? FlagNoScan : 0);
906 // same as runtime_new, but callable from C
908 runtime_cnew(const Type *typ)
910 return cnew(typ, 1, TypeInfo_SingleObject);
914 runtime_cnewarray(const Type *typ, intgo n)
916 return cnew(typ, n, TypeInfo_Array);
920 runtime_gc(2); // force GC and do eager sweep
923 func SetFinalizer(obj Eface, finalizer Eface) {
930 if(obj.__type_descriptor == nil) {
931 runtime_printf("runtime.SetFinalizer: first argument is nil interface\n");
934 if((obj.__type_descriptor->kind&kindMask) != GO_PTR) {
935 runtime_printf("runtime.SetFinalizer: first argument is %S, not pointer\n", *obj.__type_descriptor->__reflection);
938 ot = (const PtrType*)obj.type;
939 // As an implementation detail we do not run finalizers for zero-sized objects,
940 // because we use &runtime_zerobase for all such allocations.
941 if(ot->__element_type != nil && ot->__element_type->__size == 0)
943 // The following check is required for cases when a user passes a pointer to composite literal,
944 // but compiler makes it a pointer to global. For example:
945 // var Foo = &Object{}
947 // runtime.SetFinalizer(Foo, nil)
950 if((byte*)obj.__object < runtime_mheap.arena_start || runtime_mheap.arena_used <= (byte*)obj.__object)
952 if(!runtime_mlookup(obj.__object, &base, &size, nil) || obj.__object != base) {
953 // As an implementation detail we allow to set finalizers for an inner byte
954 // of an object if it could come from tiny alloc (see mallocgc for details).
955 if(ot->__element_type == nil || (ot->__element_type->kind&kindNoPointers) == 0 || ot->__element_type->__size >= TinySize) {
956 runtime_printf("runtime.SetFinalizer: pointer not at beginning of allocated block (%p)\n", obj.__object);
960 if(finalizer.__type_descriptor != nil) {
961 runtime_createfing();
962 if((finalizer.__type_descriptor->kind&kindMask) != GO_FUNC)
964 ft = (const FuncType*)finalizer.__type_descriptor;
965 if(ft->__dotdotdot || ft->__in.__count != 1)
967 fint = *(Type**)ft->__in.__values;
968 if(__go_type_descriptors_equal(fint, obj.__type_descriptor)) {
970 } else if((fint->kind&kindMask) == GO_PTR && (fint->__uncommon == nil || fint->__uncommon->__name == nil || obj.type->__uncommon == nil || obj.type->__uncommon->__name == nil) && __go_type_descriptors_equal(((const PtrType*)fint)->__element_type, ((const PtrType*)obj.type)->__element_type)) {
971 // ok - not same type, but both pointers,
972 // one or the other is unnamed, and same element type, so assignable.
973 } else if((fint->kind&kindMask) == GO_INTERFACE && ((const InterfaceType*)fint)->__methods.__count == 0) {
974 // ok - satisfies empty interface
975 } else if((fint->kind&kindMask) == GO_INTERFACE && __go_convert_interface_2(fint, obj.__type_descriptor, 1) != nil) {
976 // ok - satisfies non-empty interface
980 ot = (const PtrType*)obj.__type_descriptor;
981 if(!runtime_addfinalizer(obj.__object, *(FuncVal**)finalizer.__object, ft, ot)) {
982 runtime_printf("runtime.SetFinalizer: finalizer already set\n");
986 // NOTE: asking to remove a finalizer when there currently isn't one set is OK.
987 runtime_removefinalizer(obj.__object);
992 runtime_printf("runtime.SetFinalizer: cannot pass %S to finalizer %S\n", *obj.__type_descriptor->__reflection, *finalizer.__type_descriptor->__reflection);
994 runtime_throw("runtime.SetFinalizer");
997 func KeepAlive(x Eface) {