| 1 |
747 |
jeremybenn |
// 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.
|
| 4 |
|
|
|
| 5 |
|
|
// See malloc.h for overview.
|
| 6 |
|
|
//
|
| 7 |
|
|
// TODO(rsc): double-check stats.
|
| 8 |
|
|
|
| 9 |
|
|
package runtime
|
| 10 |
|
|
#include
|
| 11 |
|
|
#include
|
| 12 |
|
|
#include
|
| 13 |
|
|
#include "go-alloc.h"
|
| 14 |
|
|
#include "runtime.h"
|
| 15 |
|
|
#include "arch.h"
|
| 16 |
|
|
#include "malloc.h"
|
| 17 |
|
|
#include "go-string.h"
|
| 18 |
|
|
#include "interface.h"
|
| 19 |
|
|
#include "go-type.h"
|
| 20 |
|
|
|
| 21 |
|
|
MHeap runtime_mheap;
|
| 22 |
|
|
extern MStats mstats; // defined in extern.go
|
| 23 |
|
|
|
| 24 |
|
|
extern volatile int32 runtime_MemProfileRate
|
| 25 |
|
|
__asm__ ("libgo_runtime.runtime.MemProfileRate");
|
| 26 |
|
|
|
| 27 |
|
|
// Allocate an object of at least size bytes.
|
| 28 |
|
|
// Small objects are allocated from the per-thread cache's free lists.
|
| 29 |
|
|
// Large objects (> 32 kB) are allocated straight from the heap.
|
| 30 |
|
|
void*
|
| 31 |
|
|
runtime_mallocgc(uintptr size, uint32 flag, int32 dogc, int32 zeroed)
|
| 32 |
|
|
{
|
| 33 |
|
|
M *m;
|
| 34 |
|
|
G *g;
|
| 35 |
|
|
int32 sizeclass, rate;
|
| 36 |
|
|
MCache *c;
|
| 37 |
|
|
uintptr npages;
|
| 38 |
|
|
MSpan *s;
|
| 39 |
|
|
void *v;
|
| 40 |
|
|
|
| 41 |
|
|
m = runtime_m();
|
| 42 |
|
|
g = runtime_g();
|
| 43 |
|
|
if(g->status == Gsyscall)
|
| 44 |
|
|
dogc = 0;
|
| 45 |
|
|
if(runtime_gcwaiting && g != m->g0 && m->locks == 0 && g->status != Gsyscall) {
|
| 46 |
|
|
runtime_gosched();
|
| 47 |
|
|
m = runtime_m();
|
| 48 |
|
|
}
|
| 49 |
|
|
if(m->mallocing)
|
| 50 |
|
|
runtime_throw("malloc/free - deadlock");
|
| 51 |
|
|
m->mallocing = 1;
|
| 52 |
|
|
if(size == 0)
|
| 53 |
|
|
size = 1;
|
| 54 |
|
|
|
| 55 |
|
|
c = m->mcache;
|
| 56 |
|
|
c->local_nmalloc++;
|
| 57 |
|
|
if(size <= MaxSmallSize) {
|
| 58 |
|
|
// Allocate from mcache free lists.
|
| 59 |
|
|
sizeclass = runtime_SizeToClass(size);
|
| 60 |
|
|
size = runtime_class_to_size[sizeclass];
|
| 61 |
|
|
v = runtime_MCache_Alloc(c, sizeclass, size, zeroed);
|
| 62 |
|
|
if(v == nil)
|
| 63 |
|
|
runtime_throw("out of memory");
|
| 64 |
|
|
c->local_alloc += size;
|
| 65 |
|
|
c->local_total_alloc += size;
|
| 66 |
|
|
c->local_by_size[sizeclass].nmalloc++;
|
| 67 |
|
|
} else {
|
| 68 |
|
|
// TODO(rsc): Report tracebacks for very large allocations.
|
| 69 |
|
|
|
| 70 |
|
|
// Allocate directly from heap.
|
| 71 |
|
|
npages = size >> PageShift;
|
| 72 |
|
|
if((size & PageMask) != 0)
|
| 73 |
|
|
npages++;
|
| 74 |
|
|
s = runtime_MHeap_Alloc(&runtime_mheap, npages, 0, !(flag & FlagNoGC));
|
| 75 |
|
|
if(s == nil)
|
| 76 |
|
|
runtime_throw("out of memory");
|
| 77 |
|
|
size = npages<
|
| 78 |
|
|
c->local_alloc += size;
|
| 79 |
|
|
c->local_total_alloc += size;
|
| 80 |
|
|
v = (void*)(s->start << PageShift);
|
| 81 |
|
|
|
| 82 |
|
|
// setup for mark sweep
|
| 83 |
|
|
runtime_markspan(v, 0, 0, true);
|
| 84 |
|
|
}
|
| 85 |
|
|
if(!(flag & FlagNoGC))
|
| 86 |
|
|
runtime_markallocated(v, size, (flag&FlagNoPointers) != 0);
|
| 87 |
|
|
|
| 88 |
|
|
m->mallocing = 0;
|
| 89 |
|
|
|
| 90 |
|
|
if(!(flag & FlagNoProfiling) && (rate = runtime_MemProfileRate) > 0) {
|
| 91 |
|
|
if(size >= (uint32) rate)
|
| 92 |
|
|
goto profile;
|
| 93 |
|
|
if((uint32) m->mcache->next_sample > size)
|
| 94 |
|
|
m->mcache->next_sample -= size;
|
| 95 |
|
|
else {
|
| 96 |
|
|
// pick next profile time
|
| 97 |
|
|
// If you change this, also change allocmcache.
|
| 98 |
|
|
if(rate > 0x3fffffff) // make 2*rate not overflow
|
| 99 |
|
|
rate = 0x3fffffff;
|
| 100 |
|
|
m->mcache->next_sample = runtime_fastrand1() % (2*rate);
|
| 101 |
|
|
profile:
|
| 102 |
|
|
runtime_setblockspecial(v, true);
|
| 103 |
|
|
runtime_MProf_Malloc(v, size);
|
| 104 |
|
|
}
|
| 105 |
|
|
}
|
| 106 |
|
|
|
| 107 |
|
|
if(dogc && mstats.heap_alloc >= mstats.next_gc)
|
| 108 |
|
|
runtime_gc(0);
|
| 109 |
|
|
return v;
|
| 110 |
|
|
}
|
| 111 |
|
|
|
| 112 |
|
|
void*
|
| 113 |
|
|
__go_alloc(uintptr size)
|
| 114 |
|
|
{
|
| 115 |
|
|
return runtime_mallocgc(size, 0, 0, 1);
|
| 116 |
|
|
}
|
| 117 |
|
|
|
| 118 |
|
|
// Free the object whose base pointer is v.
|
| 119 |
|
|
void
|
| 120 |
|
|
__go_free(void *v)
|
| 121 |
|
|
{
|
| 122 |
|
|
M *m;
|
| 123 |
|
|
int32 sizeclass;
|
| 124 |
|
|
MSpan *s;
|
| 125 |
|
|
MCache *c;
|
| 126 |
|
|
uint32 prof;
|
| 127 |
|
|
uintptr size;
|
| 128 |
|
|
|
| 129 |
|
|
if(v == nil)
|
| 130 |
|
|
return;
|
| 131 |
|
|
|
| 132 |
|
|
// If you change this also change mgc0.c:/^sweep,
|
| 133 |
|
|
// which has a copy of the guts of free.
|
| 134 |
|
|
|
| 135 |
|
|
m = runtime_m();
|
| 136 |
|
|
if(m->mallocing)
|
| 137 |
|
|
runtime_throw("malloc/free - deadlock");
|
| 138 |
|
|
m->mallocing = 1;
|
| 139 |
|
|
|
| 140 |
|
|
if(!runtime_mlookup(v, nil, nil, &s)) {
|
| 141 |
|
|
// runtime_printf("free %p: not an allocated block\n", v);
|
| 142 |
|
|
runtime_throw("free runtime_mlookup");
|
| 143 |
|
|
}
|
| 144 |
|
|
prof = runtime_blockspecial(v);
|
| 145 |
|
|
|
| 146 |
|
|
// Find size class for v.
|
| 147 |
|
|
sizeclass = s->sizeclass;
|
| 148 |
|
|
c = m->mcache;
|
| 149 |
|
|
if(sizeclass == 0) {
|
| 150 |
|
|
// Large object.
|
| 151 |
|
|
size = s->npages<
|
| 152 |
|
|
*(uintptr*)(s->start<
|
| 153 |
|
|
// Must mark v freed before calling unmarkspan and MHeap_Free:
|
| 154 |
|
|
// they might coalesce v into other spans and change the bitmap further.
|
| 155 |
|
|
runtime_markfreed(v, size);
|
| 156 |
|
|
runtime_unmarkspan(v, 1<
|
| 157 |
|
|
runtime_MHeap_Free(&runtime_mheap, s, 1);
|
| 158 |
|
|
} else {
|
| 159 |
|
|
// Small object.
|
| 160 |
|
|
size = runtime_class_to_size[sizeclass];
|
| 161 |
|
|
if(size > sizeof(uintptr))
|
| 162 |
|
|
((uintptr*)v)[1] = 1; // mark as "needs to be zeroed"
|
| 163 |
|
|
// Must mark v freed before calling MCache_Free:
|
| 164 |
|
|
// it might coalesce v and other blocks into a bigger span
|
| 165 |
|
|
// and change the bitmap further.
|
| 166 |
|
|
runtime_markfreed(v, size);
|
| 167 |
|
|
c->local_by_size[sizeclass].nfree++;
|
| 168 |
|
|
runtime_MCache_Free(c, v, sizeclass, size);
|
| 169 |
|
|
}
|
| 170 |
|
|
c->local_alloc -= size;
|
| 171 |
|
|
if(prof)
|
| 172 |
|
|
runtime_MProf_Free(v, size);
|
| 173 |
|
|
m->mallocing = 0;
|
| 174 |
|
|
}
|
| 175 |
|
|
|
| 176 |
|
|
int32
|
| 177 |
|
|
runtime_mlookup(void *v, byte **base, uintptr *size, MSpan **sp)
|
| 178 |
|
|
{
|
| 179 |
|
|
uintptr n, i;
|
| 180 |
|
|
byte *p;
|
| 181 |
|
|
MSpan *s;
|
| 182 |
|
|
|
| 183 |
|
|
runtime_m()->mcache->local_nlookup++;
|
| 184 |
|
|
s = runtime_MHeap_LookupMaybe(&runtime_mheap, v);
|
| 185 |
|
|
if(sp)
|
| 186 |
|
|
*sp = s;
|
| 187 |
|
|
if(s == nil) {
|
| 188 |
|
|
runtime_checkfreed(v, 1);
|
| 189 |
|
|
if(base)
|
| 190 |
|
|
*base = nil;
|
| 191 |
|
|
if(size)
|
| 192 |
|
|
*size = 0;
|
| 193 |
|
|
return 0;
|
| 194 |
|
|
}
|
| 195 |
|
|
|
| 196 |
|
|
p = (byte*)((uintptr)s->start<
|
| 197 |
|
|
if(s->sizeclass == 0) {
|
| 198 |
|
|
// Large object.
|
| 199 |
|
|
if(base)
|
| 200 |
|
|
*base = p;
|
| 201 |
|
|
if(size)
|
| 202 |
|
|
*size = s->npages<
|
| 203 |
|
|
return 1;
|
| 204 |
|
|
}
|
| 205 |
|
|
|
| 206 |
|
|
if((byte*)v >= (byte*)s->limit) {
|
| 207 |
|
|
// pointers past the last block do not count as pointers.
|
| 208 |
|
|
return 0;
|
| 209 |
|
|
}
|
| 210 |
|
|
|
| 211 |
|
|
n = runtime_class_to_size[s->sizeclass];
|
| 212 |
|
|
if(base) {
|
| 213 |
|
|
i = ((byte*)v - p)/n;
|
| 214 |
|
|
*base = p + i*n;
|
| 215 |
|
|
}
|
| 216 |
|
|
if(size)
|
| 217 |
|
|
*size = n;
|
| 218 |
|
|
|
| 219 |
|
|
return 1;
|
| 220 |
|
|
}
|
| 221 |
|
|
|
| 222 |
|
|
MCache*
|
| 223 |
|
|
runtime_allocmcache(void)
|
| 224 |
|
|
{
|
| 225 |
|
|
int32 rate;
|
| 226 |
|
|
MCache *c;
|
| 227 |
|
|
|
| 228 |
|
|
runtime_lock(&runtime_mheap);
|
| 229 |
|
|
c = runtime_FixAlloc_Alloc(&runtime_mheap.cachealloc);
|
| 230 |
|
|
mstats.mcache_inuse = runtime_mheap.cachealloc.inuse;
|
| 231 |
|
|
mstats.mcache_sys = runtime_mheap.cachealloc.sys;
|
| 232 |
|
|
runtime_unlock(&runtime_mheap);
|
| 233 |
|
|
|
| 234 |
|
|
// Set first allocation sample size.
|
| 235 |
|
|
rate = runtime_MemProfileRate;
|
| 236 |
|
|
if(rate > 0x3fffffff) // make 2*rate not overflow
|
| 237 |
|
|
rate = 0x3fffffff;
|
| 238 |
|
|
if(rate != 0)
|
| 239 |
|
|
c->next_sample = runtime_fastrand1() % (2*rate);
|
| 240 |
|
|
|
| 241 |
|
|
return c;
|
| 242 |
|
|
}
|
| 243 |
|
|
|
| 244 |
|
|
void
|
| 245 |
|
|
runtime_purgecachedstats(M* m)
|
| 246 |
|
|
{
|
| 247 |
|
|
MCache *c;
|
| 248 |
|
|
|
| 249 |
|
|
// Protected by either heap or GC lock.
|
| 250 |
|
|
c = m->mcache;
|
| 251 |
|
|
mstats.heap_alloc += c->local_cachealloc;
|
| 252 |
|
|
c->local_cachealloc = 0;
|
| 253 |
|
|
mstats.heap_objects += c->local_objects;
|
| 254 |
|
|
c->local_objects = 0;
|
| 255 |
|
|
mstats.nmalloc += c->local_nmalloc;
|
| 256 |
|
|
c->local_nmalloc = 0;
|
| 257 |
|
|
mstats.nfree += c->local_nfree;
|
| 258 |
|
|
c->local_nfree = 0;
|
| 259 |
|
|
mstats.nlookup += c->local_nlookup;
|
| 260 |
|
|
c->local_nlookup = 0;
|
| 261 |
|
|
mstats.alloc += c->local_alloc;
|
| 262 |
|
|
c->local_alloc= 0;
|
| 263 |
|
|
mstats.total_alloc += c->local_total_alloc;
|
| 264 |
|
|
c->local_total_alloc= 0;
|
| 265 |
|
|
}
|
| 266 |
|
|
|
| 267 |
|
|
extern uintptr runtime_sizeof_C_MStats
|
| 268 |
|
|
__asm__ ("libgo_runtime.runtime.Sizeof_C_MStats");
|
| 269 |
|
|
|
| 270 |
|
|
#define MaxArena32 (2U<<30)
|
| 271 |
|
|
|
| 272 |
|
|
void
|
| 273 |
|
|
runtime_mallocinit(void)
|
| 274 |
|
|
{
|
| 275 |
|
|
byte *p;
|
| 276 |
|
|
uintptr arena_size, bitmap_size;
|
| 277 |
|
|
extern byte end[];
|
| 278 |
|
|
byte *want;
|
| 279 |
|
|
|
| 280 |
|
|
runtime_sizeof_C_MStats = sizeof(MStats);
|
| 281 |
|
|
|
| 282 |
|
|
runtime_InitSizes();
|
| 283 |
|
|
|
| 284 |
|
|
// Set up the allocation arena, a contiguous area of memory where
|
| 285 |
|
|
// allocated data will be found. The arena begins with a bitmap large
|
| 286 |
|
|
// enough to hold 4 bits per allocated word.
|
| 287 |
|
|
if(sizeof(void*) == 8) {
|
| 288 |
|
|
// On a 64-bit machine, allocate from a single contiguous reservation.
|
| 289 |
|
|
// 16 GB should be big enough for now.
|
| 290 |
|
|
//
|
| 291 |
|
|
// The code will work with the reservation at any address, but ask
|
| 292 |
|
|
// SysReserve to use 0x000000f800000000 if possible.
|
| 293 |
|
|
// Allocating a 16 GB region takes away 36 bits, and the amd64
|
| 294 |
|
|
// doesn't let us choose the top 17 bits, so that leaves the 11 bits
|
| 295 |
|
|
// in the middle of 0x00f8 for us to choose. Choosing 0x00f8 means
|
| 296 |
|
|
// that the valid memory addresses will begin 0x00f8, 0x00f9, 0x00fa, 0x00fb.
|
| 297 |
|
|
// None of the bytes f8 f9 fa fb can appear in valid UTF-8, and
|
| 298 |
|
|
// they are otherwise as far from ff (likely a common byte) as possible.
|
| 299 |
|
|
// Choosing 0x00 for the leading 6 bits was more arbitrary, but it
|
| 300 |
|
|
// is not a common ASCII code point either. Using 0x11f8 instead
|
| 301 |
|
|
// caused out of memory errors on OS X during thread allocations.
|
| 302 |
|
|
// These choices are both for debuggability and to reduce the
|
| 303 |
|
|
// odds of the conservative garbage collector not collecting memory
|
| 304 |
|
|
// because some non-pointer block of memory had a bit pattern
|
| 305 |
|
|
// that matched a memory address.
|
| 306 |
|
|
//
|
| 307 |
|
|
// Actually we reserve 17 GB (because the bitmap ends up being 1 GB)
|
| 308 |
|
|
// but it hardly matters: fc is not valid UTF-8 either, and we have to
|
| 309 |
|
|
// allocate 15 GB before we get that far.
|
| 310 |
|
|
arena_size = (uintptr)(16LL<<30);
|
| 311 |
|
|
bitmap_size = arena_size / (sizeof(void*)*8/4);
|
| 312 |
|
|
p = runtime_SysReserve((void*)(0x00f8ULL<<32), bitmap_size + arena_size);
|
| 313 |
|
|
if(p == nil)
|
| 314 |
|
|
runtime_throw("runtime: cannot reserve arena virtual address space");
|
| 315 |
|
|
} else {
|
| 316 |
|
|
// On a 32-bit machine, we can't typically get away
|
| 317 |
|
|
// with a giant virtual address space reservation.
|
| 318 |
|
|
// Instead we map the memory information bitmap
|
| 319 |
|
|
// immediately after the data segment, large enough
|
| 320 |
|
|
// to handle another 2GB of mappings (256 MB),
|
| 321 |
|
|
// along with a reservation for another 512 MB of memory.
|
| 322 |
|
|
// When that gets used up, we'll start asking the kernel
|
| 323 |
|
|
// for any memory anywhere and hope it's in the 2GB
|
| 324 |
|
|
// following the bitmap (presumably the executable begins
|
| 325 |
|
|
// near the bottom of memory, so we'll have to use up
|
| 326 |
|
|
// most of memory before the kernel resorts to giving out
|
| 327 |
|
|
// memory before the beginning of the text segment).
|
| 328 |
|
|
//
|
| 329 |
|
|
// Alternatively we could reserve 512 MB bitmap, enough
|
| 330 |
|
|
// for 4GB of mappings, and then accept any memory the
|
| 331 |
|
|
// kernel threw at us, but normally that's a waste of 512 MB
|
| 332 |
|
|
// of address space, which is probably too much in a 32-bit world.
|
| 333 |
|
|
bitmap_size = MaxArena32 / (sizeof(void*)*8/4);
|
| 334 |
|
|
arena_size = 512<<20;
|
| 335 |
|
|
|
| 336 |
|
|
// SysReserve treats the address we ask for, end, as a hint,
|
| 337 |
|
|
// not as an absolute requirement. If we ask for the end
|
| 338 |
|
|
// of the data segment but the operating system requires
|
| 339 |
|
|
// a little more space before we can start allocating, it will
|
| 340 |
|
|
// give out a slightly higher pointer. Except QEMU, which
|
| 341 |
|
|
// is buggy, as usual: it won't adjust the pointer upward.
|
| 342 |
|
|
// So adjust it upward a little bit ourselves: 1/4 MB to get
|
| 343 |
|
|
// away from the running binary image and then round up
|
| 344 |
|
|
// to a MB boundary.
|
| 345 |
|
|
want = (byte*)(((uintptr)end + (1<<18) + (1<<20) - 1)&~((1<<20)-1));
|
| 346 |
|
|
if(0xffffffff - (uintptr)want <= bitmap_size + arena_size)
|
| 347 |
|
|
want = 0;
|
| 348 |
|
|
p = runtime_SysReserve(want, bitmap_size + arena_size);
|
| 349 |
|
|
if(p == nil)
|
| 350 |
|
|
runtime_throw("runtime: cannot reserve arena virtual address space");
|
| 351 |
|
|
}
|
| 352 |
|
|
if((uintptr)p & (((uintptr)1<
|
| 353 |
|
|
runtime_throw("runtime: SysReserve returned unaligned address");
|
| 354 |
|
|
|
| 355 |
|
|
runtime_mheap.bitmap = p;
|
| 356 |
|
|
runtime_mheap.arena_start = p + bitmap_size;
|
| 357 |
|
|
runtime_mheap.arena_used = runtime_mheap.arena_start;
|
| 358 |
|
|
runtime_mheap.arena_end = runtime_mheap.arena_start + arena_size;
|
| 359 |
|
|
|
| 360 |
|
|
// Initialize the rest of the allocator.
|
| 361 |
|
|
runtime_MHeap_Init(&runtime_mheap, runtime_SysAlloc);
|
| 362 |
|
|
runtime_m()->mcache = runtime_allocmcache();
|
| 363 |
|
|
|
| 364 |
|
|
// See if it works.
|
| 365 |
|
|
runtime_free(runtime_malloc(1));
|
| 366 |
|
|
}
|
| 367 |
|
|
|
| 368 |
|
|
void*
|
| 369 |
|
|
runtime_MHeap_SysAlloc(MHeap *h, uintptr n)
|
| 370 |
|
|
{
|
| 371 |
|
|
byte *p;
|
| 372 |
|
|
|
| 373 |
|
|
if(n <= (uintptr)(h->arena_end - h->arena_used)) {
|
| 374 |
|
|
// Keep taking from our reservation.
|
| 375 |
|
|
p = h->arena_used;
|
| 376 |
|
|
runtime_SysMap(p, n);
|
| 377 |
|
|
h->arena_used += n;
|
| 378 |
|
|
runtime_MHeap_MapBits(h);
|
| 379 |
|
|
return p;
|
| 380 |
|
|
}
|
| 381 |
|
|
|
| 382 |
|
|
// On 64-bit, our reservation is all we have.
|
| 383 |
|
|
if(sizeof(void*) == 8)
|
| 384 |
|
|
return nil;
|
| 385 |
|
|
|
| 386 |
|
|
// On 32-bit, once the reservation is gone we can
|
| 387 |
|
|
// try to get memory at a location chosen by the OS
|
| 388 |
|
|
// and hope that it is in the range we allocated bitmap for.
|
| 389 |
|
|
p = runtime_SysAlloc(n);
|
| 390 |
|
|
if(p == nil)
|
| 391 |
|
|
return nil;
|
| 392 |
|
|
|
| 393 |
|
|
if(p < h->arena_start || (uintptr)(p+n - h->arena_start) >= MaxArena32) {
|
| 394 |
|
|
runtime_printf("runtime: memory allocated by OS not in usable range\n");
|
| 395 |
|
|
runtime_SysFree(p, n);
|
| 396 |
|
|
return nil;
|
| 397 |
|
|
}
|
| 398 |
|
|
|
| 399 |
|
|
if(p+n > h->arena_used) {
|
| 400 |
|
|
h->arena_used = p+n;
|
| 401 |
|
|
if(h->arena_used > h->arena_end)
|
| 402 |
|
|
h->arena_end = h->arena_used;
|
| 403 |
|
|
runtime_MHeap_MapBits(h);
|
| 404 |
|
|
}
|
| 405 |
|
|
|
| 406 |
|
|
return p;
|
| 407 |
|
|
}
|
| 408 |
|
|
|
| 409 |
|
|
// Runtime stubs.
|
| 410 |
|
|
|
| 411 |
|
|
void*
|
| 412 |
|
|
runtime_mal(uintptr n)
|
| 413 |
|
|
{
|
| 414 |
|
|
return runtime_mallocgc(n, 0, 1, 1);
|
| 415 |
|
|
}
|
| 416 |
|
|
|
| 417 |
|
|
func new(typ *Type) (ret *uint8) {
|
| 418 |
|
|
uint32 flag = typ->__code&GO_NO_POINTERS ? FlagNoPointers : 0;
|
| 419 |
|
|
ret = runtime_mallocgc(typ->__size, flag, 1, 1);
|
| 420 |
|
|
}
|
| 421 |
|
|
|
| 422 |
|
|
func Alloc(n uintptr) (p *byte) {
|
| 423 |
|
|
p = runtime_malloc(n);
|
| 424 |
|
|
}
|
| 425 |
|
|
|
| 426 |
|
|
func Free(p *byte) {
|
| 427 |
|
|
runtime_free(p);
|
| 428 |
|
|
}
|
| 429 |
|
|
|
| 430 |
|
|
func Lookup(p *byte) (base *byte, size uintptr) {
|
| 431 |
|
|
runtime_mlookup(p, &base, &size, nil);
|
| 432 |
|
|
}
|
| 433 |
|
|
|
| 434 |
|
|
func GC() {
|
| 435 |
|
|
runtime_gc(1);
|
| 436 |
|
|
}
|
| 437 |
|
|
|
| 438 |
|
|
func SetFinalizer(obj Eface, finalizer Eface) {
|
| 439 |
|
|
byte *base;
|
| 440 |
|
|
uintptr size;
|
| 441 |
|
|
const FuncType *ft;
|
| 442 |
|
|
|
| 443 |
|
|
if(obj.__type_descriptor == nil) {
|
| 444 |
|
|
// runtime·printf("runtime.SetFinalizer: first argument is nil interface\n");
|
| 445 |
|
|
goto throw;
|
| 446 |
|
|
}
|
| 447 |
|
|
if(obj.__type_descriptor->__code != GO_PTR) {
|
| 448 |
|
|
// runtime_printf("runtime.SetFinalizer: first argument is %S, not pointer\n", *obj.type->string);
|
| 449 |
|
|
goto throw;
|
| 450 |
|
|
}
|
| 451 |
|
|
if(!runtime_mlookup(obj.__object, &base, &size, nil) || obj.__object != base) {
|
| 452 |
|
|
// runtime_printf("runtime.SetFinalizer: pointer not at beginning of allocated block\n");
|
| 453 |
|
|
goto throw;
|
| 454 |
|
|
}
|
| 455 |
|
|
ft = nil;
|
| 456 |
|
|
if(finalizer.__type_descriptor != nil) {
|
| 457 |
|
|
if(finalizer.__type_descriptor->__code != GO_FUNC)
|
| 458 |
|
|
goto badfunc;
|
| 459 |
|
|
ft = (const FuncType*)finalizer.__type_descriptor;
|
| 460 |
|
|
if(ft->__dotdotdot || ft->__in.__count != 1 || !__go_type_descriptors_equal(*(Type**)ft->__in.__values, obj.__type_descriptor))
|
| 461 |
|
|
goto badfunc;
|
| 462 |
|
|
}
|
| 463 |
|
|
|
| 464 |
|
|
if(!runtime_addfinalizer(obj.__object, finalizer.__type_descriptor != nil ? *(void**)finalizer.__object : nil, ft)) {
|
| 465 |
|
|
runtime_printf("runtime.SetFinalizer: finalizer already set\n");
|
| 466 |
|
|
goto throw;
|
| 467 |
|
|
}
|
| 468 |
|
|
return;
|
| 469 |
|
|
|
| 470 |
|
|
badfunc:
|
| 471 |
|
|
// runtime_printf("runtime.SetFinalizer: second argument is %S, not func(%S)\n", *finalizer.type->string, *obj.type->string);
|
| 472 |
|
|
throw:
|
| 473 |
|
|
runtime_throw("runtime.SetFinalizer");
|
| 474 |
|
|
}
|