void* p9malloc(ulong n) { void *v; if(n == 0) n++; lock(&malloclock); v = malloc(n); unlock(&malloclock); print("p9malloc %lud => %p; pc %lux\n", n, v, getcallerpc(&n)); return v; }
uintptr paddr(void *v) { uintptr va; va = (uintptr)v; if(va >= KZERO) return va-KZERO; if(va >= VMAP) return va-VMAP; panic("paddr: va=%#p pc=%#p", va, getcallerpc(&v)); return 0; }
void* emallocz(uint32_t size, int zero) { void *x; x = malloc(size); if(x == nil) sysfatal("malloc: %r"); if(zero) memset(x, 0, size); setmalloctag(x, getcallerpc()); return x; }
Rune* erunestrdup(Rune *r) { void *p; if(r == nil) return nil; p = runestrdup(r); if(p == nil) error("runestrdup failed"); setmalloctag(p, getcallerpc(&r)); return p; }
void* emalloc9p(uint32_t sz) { void *v; if((v = malloc(sz)) == nil) { fprint(2, "out of memory allocating %lud\n", sz); exits("mem"); } memset(v, 0, sz); setmalloctag(v, getcallerpc()); return v; }
int decref(Ref *r) { int x; lock(&r->l); x = --r->ref; unlock(&r->l); if(x < 0) panic("decref pc=%#p", getcallerpc()); return x; }
VtBlock * vtfileblock(VtFile *r, uint32_t bn, int mode) { VtBlock *b, *bb; int index[VtPointerDepth+1]; VtEntry e; int i; int m; assert(ISLOCKED(r)); assert(bn != NilBlock); b = fileload(r, &e); if(b == nil) return nil; i = mkindices(&e, bn, index); if(i < 0) goto Err; if(i > DEPTH(e.type)){ if(mode == VtOREAD){ werrstr("bad address 0x%lux", (uint32_t)bn); goto Err; } index[i] = 0; if(growdepth(r, b, &e, i) < 0) goto Err; } assert(b->type == VtDirType); index[DEPTH(e.type)] = r->offset % r->epb; /* mode for intermediate block */ m = mode; if(m == VtOWRITE) m = VtORDWR; for(i=DEPTH(e.type); i>=0; i--){ bb = blockwalk(b, index[i], r->c, i==0 ? mode : m, &e); if(bb == nil) goto Err; vtblockput(b); b = bb; } b->pc = getcallerpc(); return b; Err: vtblockput(b); return nil; }
// convert an mpint into a big endian byte array (most significant byte first) // return number of bytes converted // if p == nil, allocate and result array int mptobe(mpint *b, uchar *p, uint n, uchar **pp) { int i, j, suppress; mpdigit x; uchar *e, *s, c; if(p == nil) { n = (b->top+1)*Dbytes; p = malloc(n); setmalloctag(p, getcallerpc(&b)); } if(p == nil) return -1; if(pp != nil) *pp = p; memset(p, 0, n); // special case 0 if(b->top == 0) { if(n < 1) return -1; else return 1; } s = p; e = s+n; suppress = 1; for(i = b->top-1; i >= 0; i--) { x = b->p[i]; for(j = Dbits-8; j >= 0; j -= 8) { c = x>>j; if(c == 0 && suppress) continue; if(p >= e) return -1; *p++ = c; suppress = 0; } } // guarantee at least one byte if(s == p) { if(p >= e) return -1; *p++ = 0; } return p - s; }
Attr* _parseattr(char *s) { char *p, *t, *tok[256]; int i, ntok, type; Attr *a; s = strdup(s); if(s == nil) sysfatal("_parseattr strdup: %r"); ntok = tokenize(s, tok, nelem(tok)); a = nil; for(i=ntok-1; i>=0; i--) { t = tok[i]; if((p = strchr(t, '='))) { *p++ = '\0'; // if(p-2 >= t && p[-2] == ':'){ // p[-2] = '\0'; // type = AttrDefault; // }else type = AttrNameval; a = _mkattr(type, t, p, a); setmalloctag(a, getcallerpc(&s)); } else if(t[strlen(t)-1] == '?') { t[strlen(t)-1] = '\0'; a = _mkattr(AttrQuery, t, "", a); setmalloctag(a, getcallerpc(&s)); } else { /* really a syntax error, but better to provide some indication */ a = _mkattr(AttrNameval, t, "", a); setmalloctag(a, getcallerpc(&s)); } } free(s); return cleanattr(a); }
Packet * packetdup(Packet *p, int offset, int n) { Frag *f, *ff; Packet *pp; NOTFREE(p); if(offset < 0 || n < 0 || offset+n > p->size) { werrstr(EBadSize); return nil; } pp = packetalloc(); pp->pc = getcallerpc(&p); if(n == 0){ NOTFREE(pp); return pp; } pp->size = n; /* skip offset */ for(f=p->first; offset >= FRAGSIZE(f); f=f->next) offset -= FRAGSIZE(f); /* first frag */ ff = fragdup(pp, f); ff->rp += offset; pp->first = ff; n -= FRAGSIZE(ff); pp->asize += FRAGASIZE(ff); /* the remaining */ while(n > 0) { f = f->next; ff->next = fragdup(pp, f); ff = ff->next; n -= FRAGSIZE(ff); pp->asize += FRAGASIZE(ff); } /* fix up last frag: note n <= 0 */ ff->wp += n; ff->next = nil; pp->last = ff; NOTFREE(pp); NOTFREE(p); return pp; }
Block* allocb(int size) { Block *b; /* * Check in a process and wait until successful. * Can still error out of here, though. */ if(up == nil) panic("allocb without up: %#p", getcallerpc(&size)); if((b = _allocb(size)) == nil){ splhi(); xsummary(); mallocsummary(); delay(500); panic("allocb: no memory for %d bytes; caller %#p", size, getcallerpc(&size)); } setmalloctag(b, getcallerpc(&size)); return b; }
void runtime·selectrecv2(Select* sel, Hchan* c, byte* elem, bool* received, bool selected) { selected = 0; FLUSH(&selected); #line 523 "/home/14/ren/source/golang/go/src/pkg/runtime/chan.goc" selected = false; // nil cases do not compete if(c != nil) selectrecv(sel, c, runtime·getcallerpc(&sel), elem, received, (byte*)&selected - (byte*)&sel); FLUSH(&selected); }
Packet * packetsplit(Packet *p, int n) { Packet *pp; Frag *f, *ff; NOTFREE(p); if(n < 0 || n > p->size) { werrstr(EPacketSize); return nil; } pp = packetalloc(); pp->pc = getcallerpc(&p); if(n == 0){ NOTFREE(pp); return pp; } pp->size = n; p->size -= n; ff = nil; for(f=p->first; n > 0 && n >= FRAGSIZE(f); f=f->next) { n -= FRAGSIZE(f); p->asize -= FRAGASIZE(f); pp->asize += FRAGASIZE(f); f->p = pp; ff = f; } /* split shared frag */ if(n > 0) { f->p = pp; ff = f; f = fragdup(p, ff); pp->asize += FRAGASIZE(ff); ff->wp = ff->rp + n; f->rp += n; } pp->first = p->first; pp->last = ff; ff->next = nil; p->first = f; if(f == nil || f->next == nil) p->last = f; NOTFREE(pp); NOTFREE(p); return pp; }
uintmem PADDR(void* va) { uintmem pa; pa = PTR2UINT(va); if(pa >= KSEG0 && pa < KSEG0+TMFM) return pa-KSEG0; if(pa > KSEG2) return pa-KSEG2; panic("PADDR: va %#p pa #%p @ %#p\n", va, _PADDR(va), getcallerpc()); return 0; }
void* emalloc(uint32_t n) { void *v; v = mallocz(n, 1); if(v == nil) { abort(); sysfatal("out of memory"); } setmalloctag(v, getcallerpc(&n)); return v; }
char* estrdup(char *s) { int l; char *t; if (s == nil) return nil; l = strlen(s)+1; t = emalloc(l); memcpy(t, s, l); setmalloctag(t, getcallerpc()); return t; }
void* p9calloc(ulong a, ulong b) { void *v; if(a*b == 0) a = b = 1; lock(&malloclock); v = calloc(a*b, 1); unlock(&malloclock); print("p9calloc %lud %lud => %p; pc %lux\n", a, b, v, getcallerpc(&a)); return v; }
int canqlock(QLock *q) { if(!canlock(&q->use)) return 0; if(q->locked){ unlock(&q->use); return 0; } q->locked = 1; q->qpc = getcallerpc(&q); unlock(&q->use); return 1; }
void runtime·selectrecv2(Select* sel, Hchan* c, byte* elem, bool* received, bool selected) { selected = 0; FLUSH(&selected); #line 521 "/tmp/makerelease402042453/go/src/pkg/runtime/chan.goc" selected = false; // nil cases do not compete if(c != nil) selectrecv(sel, c, runtime·getcallerpc(&sel), elem, received, (byte*)&selected - (byte*)&sel); FLUSH(&selected); }
char* esmprint(char *fmt, ...) { va_list args; char *p; va_start(args, fmt); p = vsmprint(fmt, args); va_end(args); if (p == nil) sysfatal("esmprint: out of memory: %r"); setmalloctag(p, getcallerpc()); return p; }
void rlock(RWlock *q) { Proc *up = externup(); Proc *p; uint64_t t0; cycles(&t0); if(!canlock(&q->use)){ lock(&q->use); slockstat(getcallerpc(&q), t0); } qlockstats.rlock++; if(q->writer == 0 && q->head == nil){ /* no writer, go for it */ q->readers++; unlock(&q->use); return; } qlockstats.rlockq++; p = q->tail; if(up == nil) panic("rlock"); if(p == 0) q->head = up; else p->qnext = up; q->tail = up; up->qnext = 0; up->state = QueueingR; if(up->trace) proctrace(up, SLock, 0); unlock(&q->use); sched(); lockstat(getcallerpc(&q), t0); }
void qlock(QLock *q) { Proc *p; void (*pt)(Proc*, int, vlong, vlong); if(m->ilockdepth != 0) print("qlock: %#p: ilockdepth %d", getcallerpc(&q), m->ilockdepth); if(up != nil && up->nlocks) print("qlock: %#p: nlocks %d", getcallerpc(&q), up->nlocks); lock(&q->use); rwstats.qlock++; if(!q->locked) { q->locked = 1; q->qpc = getcallerpc(&q); unlock(&q->use); return; } if(up == nil) panic("qlock"); rwstats.qlockq++; p = q->tail; if(p == 0) q->head = up; else p->qnext = up; q->tail = up; up->qnext = 0; up->state = Queueing; up->qpc = getcallerpc(&q); if(up->trace && (pt = proctrace) != nil) pt(up, SSleep, 0, Queueing | (up->qpc<<8)); unlock(&q->use); sched(); q->qpc = getcallerpc(&q); }
void wlock(RWlock *q) { Proc *p; uintptr pc; void (*pt)(Proc*, int, vlong, vlong); lock(&q->use); rwstats.wlock++; if(q->readers == 0 && q->writer == 0){ /* noone waiting, go for it */ q->wpc = getcallerpc(&q); q->wproc = up; q->writer = 1; unlock(&q->use); return; } /* wait */ rwstats.wlockq++; p = q->tail; if(up == nil) panic("wlock"); if(p == nil) q->head = up; else p->qnext = up; q->tail = up; up->qnext = 0; up->state = QueueingW; if(up->trace && (pt = proctrace) != nil){ pc = getcallerpc(&q); pt(up, SSleep, 0, QueueingW|(pc<<8)); } unlock(&q->use); sched(); }
/* * parse a single tuple */ char* _ndbparsetuple(char *cp, Ndbtuple **tp) { char *p; int len; Ndbtuple *t; /* a '#' starts a comment lasting till new line */ EATWHITE(cp); if(*cp == '#' || *cp == '\n') return 0; t = ndbnew(nil, nil); setmalloctag(t, getcallerpc()); *tp = t; /* parse attribute */ p = cp; while(*cp != '=' && !ISWHITE(*cp) && *cp != '\n') cp++; len = cp - p; if(len >= Ndbalen) len = Ndbalen-1; strncpy(t->attr, p, len); /* parse value */ EATWHITE(cp); if(*cp == '='){ cp++; if(*cp == '"'){ p = ++cp; while(*cp != '\n' && *cp != '"') cp++; len = cp - p; if(*cp == '"') cp++; } else if(*cp == '#'){ len = 0; } else { p = cp; while(!ISWHITE(*cp) && *cp != '\n') cp++; len = cp - p; } ndbsetval(t, p, len); } return cp; }
char* estrdupn(char *s, int n) { int l; char *t; l = strlen(s); if(l > n) l = n; t = emalloc(l+1); memmove(t, s, l); t[l] = '\0'; setmalloctag(t, getcallerpc()); return t; }
void qlock(QLock *q) { Proc *p; if(m->ilockdepth != 0) print("qlock: %#p: ilockdepth %d\n", getcallerpc(&q), m->ilockdepth); if(up != nil && up->nlocks.ref) print("qlock: %#p: nlocks %lud\n", getcallerpc(&q), up->nlocks.ref); if(q->use.key == 0x55555555) panic("qlock: q %#p, key 5*\n", q); lock(&q->use); rwstats.qlock++; if(!q->locked) { q->locked = 1; q->qpc = getcallerpc(&q); unlock(&q->use); return; } if(up == 0) panic("qlock"); rwstats.qlockq++; p = q->tail; if(p == 0) q->head = up; else p->qnext = up; q->tail = up; up->qnext = 0; up->state = Queueing; up->qpc = getcallerpc(&q); unlock(&q->use); sched(); q->qpc = getcallerpc(&q); }
void priqlock(QLock *q) { Proc *p; void (*pt)(Proc*, int, vlong, vlong); if(m->ilockdepth != 0) print("qlock: %#p: ilockdepth %d\n", getcallerpc(&q), m->ilockdepth); if(up != nil && up->nlocks) print("qlock: %#p: nlocks %d\n", getcallerpc(&q), up->nlocks); lock(&q->use); if(!q->locked) { //q->p = up; q->locked = 1; q->qpc = getcallerpc(&q); unlock(&q->use); return; } if(up == nil) panic("priqlock"); // if(q->p == up) // panic("qlock deadlock. pid=%ld cpc=%lux qpc=%lux\n", up->pid, getcallerpc(&q), up->qpc); p = up->qnext = q->head; if(p == nil) q->tail = up; q->head = up; up->state = Queueing; up->qpc = getcallerpc(&q); if(up->trace && (pt = proctrace) != nil) pt(up, SSleep, 0, Queueing | (up->qpc<<8)); // if(kproflock) // kproflock(up->qpc); unlock(&q->use); sched(); }
void iunlock(Lock *l) { Proc *up = externup(); Mpl pl; uint64_t x; if(LOCKCYCLES){ cycles(&x); l->lockcycles = x - l->lockcycles; if(l->lockcycles > maxilockcycles){ maxilockcycles = l->lockcycles; maxilockpc = l->_pc; } } if(l->key == 0) print("iunlock: not locked: pc %#p\n", getcallerpc()); if(!l->isilock) print("iunlock of lock: pc %#p, held by %#p\n", getcallerpc(), l->_pc); if(islo()) print("iunlock while lo: pc %#p, held by %#p\n", getcallerpc(), l->_pc); if(l->m != machp()){ print("iunlock by cpu%d, locked by cpu%d: pc %#p, held by %#p\n", machp()->machno, l->m->machno, getcallerpc(), l->_pc); } pl = l->pl; l->m = nil; l->key = 0; coherence(); machp()->ilockdepth--; if(up) up->lastilock = nil; splx(pl); }
void powerkproc(void*) { ulong xlink, xlink1; while(waserror()) ; for(;;){ while(powerflag == 0) sleep(&powerr, powerdown, 0); xlink = getcallerpc(&xlink); // iprint("call deepsleep, pc = 0x%lux, sp = 0x%lux\n", xlink, &xlink); deepsleep(); xlink1 = getcallerpc(&xlink1); delay(2000); // iprint("deepsleep returned, pc = 0x%lux, sp = 0x%lux\n", xlink1, &xlink); powerflag = 0; } }
Attr* _copyattr(Attr *a) { Attr **la, *na; na = nil; la = &na; for(; a; a=a->next) { *la = _mkattr(a->type, a->name, a->val, nil); setmalloctag(*la, getcallerpc(&a)); la = &(*la)->next; } *la = nil; return na; }