// auto generated by go tool dist
// goos=linux goarch=arm

#include "runtime.h"
#include "arch_GOARCH.h"
#include "stack.h"
#include "malloc.h"
#include "defs_GOOS_GOARCH.h"
#include "type.h"
#pragma dataflag 16 /* mark mheap as 'no pointers', hiding from garbage collector */

#line 18 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
MHeap runtime·mheap; 
#line 20 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
extern MStats mstats; 
#line 22 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
extern volatile int32 runtime·MemProfileRate; 
#line 27 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
void* 
runtime·mallocgc ( uintptr size , uint32 flag , int32 dogc , int32 zeroed ) 
{ 
int32 sizeclass , rate; 
MCache *c; 
uintptr npages; 
MSpan *s; 
void *v; 
#line 36 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( runtime·gcwaiting && g != m->g0 && m->locks == 0 ) 
runtime·gosched ( ) ; 
if ( m->mallocing ) 
runtime·throw ( "malloc/free - deadlock" ) ; 
m->mallocing = 1; 
if ( size == 0 ) 
size = 1; 
#line 44 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
c = m->mcache; 
c->local_nmalloc++; 
if ( size <= MaxSmallSize ) { 
#line 48 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
sizeclass = runtime·SizeToClass ( size ) ; 
size = runtime·class_to_size[sizeclass]; 
v = runtime·MCache_Alloc ( c , sizeclass , size , zeroed ) ; 
if ( v == nil ) 
runtime·throw ( "out of memory" ) ; 
c->local_alloc += size; 
c->local_total_alloc += size; 
c->local_by_size[sizeclass].nmalloc++; 
} else { 
#line 60 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
npages = size >> PageShift; 
if ( ( size & PageMask ) != 0 ) 
npages++; 
s = runtime·MHeap_Alloc ( &runtime·mheap , npages , 0 , 1 ) ; 
if ( s == nil ) 
runtime·throw ( "out of memory" ) ; 
size = npages<<PageShift; 
c->local_alloc += size; 
c->local_total_alloc += size; 
v = ( void* ) ( s->start << PageShift ) ; 
#line 72 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
runtime·markspan ( v , 0 , 0 , true ) ; 
} 
if ( ! ( flag & FlagNoGC ) ) 
runtime·markallocated ( v , size , ( flag&FlagNoPointers ) != 0 ) ; 
#line 77 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
m->mallocing = 0; 
#line 79 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( ! ( flag & FlagNoProfiling ) && ( rate = runtime·MemProfileRate ) > 0 ) { 
if ( size >= rate ) 
goto profile; 
if ( m->mcache->next_sample > size ) 
m->mcache->next_sample -= size; 
else { 
#line 87 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( rate > 0x3fffffff ) 
rate = 0x3fffffff; 
m->mcache->next_sample = runtime·fastrand1 ( ) % ( 2*rate ) ; 
profile: 
runtime·setblockspecial ( v , true ) ; 
runtime·MProf_Malloc ( v , size ) ; 
} 
} 
#line 96 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( dogc && mstats.heap_alloc >= mstats.next_gc ) 
runtime·gc ( 0 ) ; 
return v; 
} 
#line 101 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
void* 
runtime·malloc ( uintptr size ) 
{ 
return runtime·mallocgc ( size , 0 , 0 , 1 ) ; 
} 
#line 108 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
void 
runtime·free ( void *v ) 
{ 
int32 sizeclass; 
MSpan *s; 
MCache *c; 
uint32 prof; 
uintptr size; 
#line 117 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( v == nil ) 
return; 
#line 123 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( m->mallocing ) 
runtime·throw ( "malloc/free - deadlock" ) ; 
m->mallocing = 1; 
#line 127 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( !runtime·mlookup ( v , nil , nil , &s ) ) { 
runtime·printf ( "free %p: not an allocated block\n" , v ) ; 
runtime·throw ( "free runtime·mlookup" ) ; 
} 
prof = runtime·blockspecial ( v ) ; 
#line 134 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
sizeclass = s->sizeclass; 
c = m->mcache; 
if ( sizeclass == 0 ) { 
#line 138 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
size = s->npages<<PageShift; 
* ( uintptr* ) ( s->start<<PageShift ) = 1; 
#line 142 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
runtime·markfreed ( v , size ) ; 
runtime·unmarkspan ( v , 1<<PageShift ) ; 
runtime·MHeap_Free ( &runtime·mheap , s , 1 ) ; 
} else { 
#line 147 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
size = runtime·class_to_size[sizeclass]; 
if ( size > sizeof ( uintptr ) ) 
( ( uintptr* ) v ) [1] = 1; 
#line 153 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
runtime·markfreed ( v , size ) ; 
c->local_by_size[sizeclass].nfree++; 
runtime·MCache_Free ( c , v , sizeclass , size ) ; 
} 
c->local_alloc -= size; 
if ( prof ) 
runtime·MProf_Free ( v , size ) ; 
m->mallocing = 0; 
} 
#line 163 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
int32 
runtime·mlookup ( void *v , byte **base , uintptr *size , MSpan **sp ) 
{ 
uintptr n , i; 
byte *p; 
MSpan *s; 
#line 170 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
m->mcache->local_nlookup++; 
s = runtime·MHeap_LookupMaybe ( &runtime·mheap , v ) ; 
if ( sp ) 
*sp = s; 
if ( s == nil ) { 
runtime·checkfreed ( v , 1 ) ; 
if ( base ) 
*base = nil; 
if ( size ) 
*size = 0; 
return 0; 
} 
#line 183 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
p = ( byte* ) ( ( uintptr ) s->start<<PageShift ) ; 
if ( s->sizeclass == 0 ) { 
#line 186 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( base ) 
*base = p; 
if ( size ) 
*size = s->npages<<PageShift; 
return 1; 
} 
#line 193 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( ( byte* ) v >= ( byte* ) s->limit ) { 
#line 195 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
return 0; 
} 
#line 198 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
n = runtime·class_to_size[s->sizeclass]; 
if ( base ) { 
i = ( ( byte* ) v - p ) /n; 
*base = p + i*n; 
} 
if ( size ) 
*size = n; 
#line 206 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
return 1; 
} 
#line 209 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
MCache* 
runtime·allocmcache ( void ) 
{ 
int32 rate; 
MCache *c; 
#line 215 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
runtime·lock ( &runtime·mheap ) ; 
c = runtime·FixAlloc_Alloc ( &runtime·mheap.cachealloc ) ; 
mstats.mcache_inuse = runtime·mheap.cachealloc.inuse; 
mstats.mcache_sys = runtime·mheap.cachealloc.sys; 
runtime·unlock ( &runtime·mheap ) ; 
#line 222 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
rate = runtime·MemProfileRate; 
if ( rate > 0x3fffffff ) 
rate = 0x3fffffff; 
if ( rate != 0 ) 
c->next_sample = runtime·fastrand1 ( ) % ( 2*rate ) ; 
#line 228 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
return c; 
} 
#line 231 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
void 
runtime·purgecachedstats ( M* m ) 
{ 
MCache *c; 
#line 237 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
c = m->mcache; 
mstats.heap_alloc += c->local_cachealloc; 
c->local_cachealloc = 0; 
mstats.heap_objects += c->local_objects; 
c->local_objects = 0; 
mstats.nmalloc += c->local_nmalloc; 
c->local_nmalloc = 0; 
mstats.nfree += c->local_nfree; 
c->local_nfree = 0; 
mstats.nlookup += c->local_nlookup; 
c->local_nlookup = 0; 
mstats.alloc += c->local_alloc; 
c->local_alloc= 0; 
mstats.total_alloc += c->local_total_alloc; 
c->local_total_alloc= 0; 
} 
#line 254 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
uintptr runtime·sizeof_C_MStats = sizeof ( MStats ) ; 
#line 256 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
#define MaxArena32 ( 2U<<30 ) 
#line 258 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
void 
runtime·mallocinit ( void ) 
{ 
byte *p; 
uintptr arena_size , bitmap_size; 
extern byte end[]; 
byte *want; 
uintptr limit; 
#line 267 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
p = nil; 
arena_size = 0; 
bitmap_size = 0; 
#line 272 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
USED ( p ) ; 
USED ( arena_size ) ; 
USED ( bitmap_size ) ; 
#line 276 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
runtime·InitSizes ( ) ; 
#line 278 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
limit = runtime·memlimit ( ) ; 
#line 283 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( sizeof ( void* ) == 8 && ( limit == 0 || limit > ( 1<<30 ) ) ) { 
#line 308 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
arena_size = 16LL<<30; 
bitmap_size = arena_size / ( sizeof ( void* ) *8/4 ) ; 
p = runtime·SysReserve ( ( void* ) ( 0x00f8ULL<<32 ) , bitmap_size + arena_size ) ; 
} 
if ( p == nil ) { 
#line 330 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
bitmap_size = MaxArena32 / ( sizeof ( void* ) *8/4 ) ; 
arena_size = 512<<20; 
if ( limit > 0 && arena_size+bitmap_size > limit ) { 
bitmap_size = ( limit / 9 ) & ~ ( ( 1<<PageShift ) - 1 ) ; 
arena_size = bitmap_size * 8; 
} 
#line 346 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
want = ( byte* ) ( ( ( uintptr ) end + ( 1<<18 ) + ( 1<<20 ) - 1 ) &~ ( ( 1<<20 ) -1 ) ) ; 
p = runtime·SysReserve ( want , bitmap_size + arena_size ) ; 
if ( p == nil ) 
runtime·throw ( "runtime: cannot reserve arena virtual address space" ) ; 
if ( ( uintptr ) p & ( ( ( uintptr ) 1<<PageShift ) -1 ) ) 
runtime·printf ( "runtime: SysReserve returned unaligned address %p; asked for %p" , p , bitmap_size+arena_size ) ; 
} 
if ( ( uintptr ) p & ( ( ( uintptr ) 1<<PageShift ) -1 ) ) 
runtime·throw ( "runtime: SysReserve returned unaligned address" ) ; 
#line 356 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
runtime·mheap.bitmap = p; 
runtime·mheap.arena_start = p + bitmap_size; 
runtime·mheap.arena_used = runtime·mheap.arena_start; 
runtime·mheap.arena_end = runtime·mheap.arena_start + arena_size; 
#line 362 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
runtime·MHeap_Init ( &runtime·mheap , runtime·SysAlloc ) ; 
m->mcache = runtime·allocmcache ( ) ; 
#line 366 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
runtime·free ( runtime·malloc ( 1 ) ) ; 
} 
#line 369 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
void* 
runtime·MHeap_SysAlloc ( MHeap *h , uintptr n ) 
{ 
byte *p; 
#line 374 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( n > h->arena_end - h->arena_used ) { 
#line 377 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
byte *new_end; 
uintptr needed; 
#line 380 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
needed = ( uintptr ) h->arena_used + n - ( uintptr ) h->arena_end; 
#line 382 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
needed = ( needed + ( 256<<20 ) - 1 ) & ~ ( ( 256<<20 ) -1 ) ; 
new_end = h->arena_end + needed; 
if ( new_end <= h->arena_start + MaxArena32 ) { 
p = runtime·SysReserve ( h->arena_end , new_end - h->arena_end ) ; 
if ( p == h->arena_end ) 
h->arena_end = new_end; 
} 
} 
if ( n <= h->arena_end - h->arena_used ) { 
#line 392 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
p = h->arena_used; 
runtime·SysMap ( p , n ) ; 
h->arena_used += n; 
runtime·MHeap_MapBits ( h ) ; 
return p; 
} 
#line 400 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( sizeof ( void* ) == 8 && ( uintptr ) h->bitmap >= 0xffffffffU ) 
return nil; 
#line 406 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
p = runtime·SysAlloc ( n ) ; 
if ( p == nil ) 
return nil; 
#line 410 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( p < h->arena_start || p+n - h->arena_start >= MaxArena32 ) { 
runtime·printf ( "runtime: memory allocated by OS (%p) not in usable range [%p,%p)\n" , 
p , h->arena_start , h->arena_start+MaxArena32 ) ; 
runtime·SysFree ( p , n ) ; 
return nil; 
} 
#line 417 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( p+n > h->arena_used ) { 
h->arena_used = p+n; 
if ( h->arena_used > h->arena_end ) 
h->arena_end = h->arena_used; 
runtime·MHeap_MapBits ( h ) ; 
} 
#line 424 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
return p; 
} 
#line 429 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
void* 
runtime·mal ( uintptr n ) 
{ 
return runtime·mallocgc ( n , 0 , 1 , 1 ) ; 
} 
void
runtime·new(Type* typ, uint8* ret)
{
#line 435 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"

	uint32 flag = typ->kind&KindNoPointers ? FlagNoPointers : 0;
	ret = runtime·mallocgc(typ->size, flag, 1, 1);
	FLUSH(&ret);
	FLUSH(&ret);
}

#line 441 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
void* 
runtime·stackalloc ( uint32 n ) 
{ 
#line 447 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( g != m->g0 ) 
runtime·throw ( "stackalloc not on scheduler stack" ) ; 
#line 457 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
if ( m->mallocing || m->gcing || n == FixedStack ) { 
if ( n != FixedStack ) { 
runtime·printf ( "stackalloc: in malloc, size=%d want %d" , FixedStack , n ) ; 
runtime·throw ( "stackalloc" ) ; 
} 
return runtime·FixAlloc_Alloc ( m->stackalloc ) ; 
} 
return runtime·mallocgc ( n , FlagNoProfiling|FlagNoGC , 0 , 0 ) ; 
} 
#line 467 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"
void 
runtime·stackfree ( void *v , uintptr n ) 
{ 
if ( m->mallocing || m->gcing || n == FixedStack ) { 
runtime·FixAlloc_Free ( m->stackalloc , v ) ; 
return; 
} 
runtime·free ( v ) ; 
} 
void
runtime·GC()
{
#line 477 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"

	runtime·gc(1);
}
void
runtime·SetFinalizer(Eface obj, Eface finalizer)
{
#line 481 "/root/fast/go.build.hg/src/pkg/runtime/malloc.goc"

	byte *base;
	uintptr size;
	FuncType *ft;
	int32 i, nret;
	Type *t;

	if(obj.type == nil) {
		runtime·printf("runtime.SetFinalizer: first argument is nil interface\n");
		goto throw;
	}
	if(obj.type->kind != KindPtr) {
		runtime·printf("runtime.SetFinalizer: first argument is %S, not pointer\n", *obj.type->string);
		goto throw;
	}
	if(!runtime·mlookup(obj.data, &base, &size, nil) || obj.data != base) {
		runtime·printf("runtime.SetFinalizer: pointer not at beginning of allocated block\n");
		goto throw;
	}
	nret = 0;
	if(finalizer.type != nil) {
		if(finalizer.type->kind != KindFunc)
			goto badfunc;
		ft = (FuncType*)finalizer.type;
		if(ft->dotdotdot || ft->in.len != 1 || *(Type**)ft->in.array != obj.type)
			goto badfunc;

		// compute size needed for return parameters
		for(i=0; i<ft->out.len; i++) {
			t = ((Type**)ft->out.array)[i];
			nret = (nret + t->align - 1) & ~(t->align - 1);
			nret += t->size;
		}
		nret = (nret + sizeof(void*)-1) & ~(sizeof(void*)-1);
	}
	
	if(!runtime·addfinalizer(obj.data, finalizer.data, nret)) {
		runtime·printf("runtime.SetFinalizer: finalizer already set\n");
		goto throw;
	}
	return;

badfunc:
	runtime·printf("runtime.SetFinalizer: second argument is %S, not func(%S)\n", *finalizer.type->string, *obj.type->string);
throw:
	runtime·throw("runtime.SetFinalizer");
}
