void *lmem_alloc(long nbytes, const char *file, int line) { struct descriptor *bp; void *ptr; lassert(nbytes > 0); nbytes = ((nbytes + sizeof(union align) - 1)/ \ (sizeof (union align))) * ( sizeof (union align)); for (bp = freelist.free; bp; bp = bp->free){ if (bp->size > nbytes){ bp->size -= nbytes; ptr = (char *)bp->ptr + bp->size; if ((bp = dalloc(ptr, nbytes, file, line)) != NULL){ unsigned h = hash(ptr, htab); bp->link = htab[h]; htab[h] = bp; return ptr; }else{ lexcept_raise(&mem_failed, file, line); } } if (bp == &freelist){ struct descriptor *newptr; if ((ptr = malloc(nbytes + NALLOC)) == NULL || (newptr = dalloc(ptr, nbytes + NALLOC, __FILE__, __LINE__)) == NULL) lexcept_raise(&mem_failed, file, line); newptr->free = freelist.free; freelist.free = newptr; } } lassert(0); return NULL; }
rtree_t * rtree_new(unsigned bits, rtree_alloc_t *alloc, rtree_dalloc_t *dalloc, pool_t *pool) { rtree_t *ret; unsigned bits_per_level, bits_in_leaf, height, i; assert(bits > 0 && bits <= (sizeof(uintptr_t) << 3)); bits_per_level = jemalloc_ffs(pow2_ceil((RTREE_NODESIZE / sizeof(void *)))) - 1; bits_in_leaf = jemalloc_ffs(pow2_ceil((RTREE_NODESIZE / sizeof(uint8_t)))) - 1; if (bits > bits_in_leaf) { height = 1 + (bits - bits_in_leaf) / bits_per_level; if ((height-1) * bits_per_level + bits_in_leaf != bits) height++; } else { height = 1; } assert((height-1) * bits_per_level + bits_in_leaf >= bits); ret = (rtree_t*)alloc(pool, offsetof(rtree_t, level2bits) + (sizeof(unsigned) * height)); if (ret == NULL) return (NULL); memset(ret, 0, offsetof(rtree_t, level2bits) + (sizeof(unsigned) * height)); ret->alloc = alloc; ret->dalloc = dalloc; ret->pool = pool; if (malloc_mutex_init(&ret->mutex)) { if (dalloc != NULL) dalloc(pool, ret); return (NULL); } ret->height = height; if (height > 1) { if ((height-1) * bits_per_level + bits_in_leaf > bits) { ret->level2bits[0] = (bits - bits_in_leaf) % bits_per_level; } else ret->level2bits[0] = bits_per_level; for (i = 1; i < height-1; i++) ret->level2bits[i] = bits_per_level; ret->level2bits[height-1] = bits_in_leaf; } else ret->level2bits[0] = bits; ret->root = (void**)alloc(pool, sizeof(void *) << ret->level2bits[0]); if (ret->root == NULL) { if (dalloc != NULL) dalloc(pool, ret); return (NULL); } memset(ret->root, 0, sizeof(void *) << ret->level2bits[0]); return (ret); }
void *Mem_alloc(long nbytes,const char *file,int line) { struct descriptor *bp; void *ptr; assert(nbytes>0); //对nbytes做字节对齐扩充到合适字节,避免空间浪费 nbytes=((nbytes+sizeof(union align)-1)/(sizeof(union align)))*(sizeof(union align)); //从空闲块的首地址的下一块开始寻找合适的空间,freelist是一个循环链表,这里所谓的空闲块 //并不是真正都是空闲的,其实是所有当前已经申请过的块,每一块都有至少(NALLOC)大。经过多次分别后 //块中有的是空闲的,而有的可能已经被用满。 for(bp=freelist.free;bp;bp=bp->free) { if(bp->size>nbytes) //如果找到合适的空闲块 { bp->size-=nbytes; ptr=(char *)bp->ptr+bp->size; //得到分配空间的首地址 if((bp=dalloc(ptr,nbytes,file,line))!=NULL) //申请一个存储新ptr的结构体bp { unsigned h=hash(ptr,htab); //然后将这个结构体加入到hash表中 bp->link=htab[h]; //表明这块内存已经被占用了。 htab[h]=bp; return ptr; } else { if(file==NULL) RAISE(Mem_Failed); else Except_raise(&Mem_Failed,file,line); } } if(bp==&freelist) //没有合适的空闲块,那么分配一个新块 { struct descriptor *newptr; if((ptr=malloc(nbytes+NALLOC))==NULL //新块大小由固定宏和nbytes共同决定,意味着所有的块都将有大小上的一些差异 ||(newptr=dalloc(ptr,nbytes+NALLOC,__FILE__,__LINE__))==NULL) { if(file==NULL) RAISE(Mem_Failed); else Except_raise(&Mem_Failed,file,line); } newptr->free=freelist.free; freelist.free=newptr; } } assert(0); return NULL; }
/** * @parm: nbytes 内存字节数 * return 内存指针 **/ static void *mem_alloc(long nbytes) { struct memnode *bp; void *ptr; struct memnode *newptr; //空闲内存不够时,重新malloc unsigned int h; //哈希值 bp = NULL; ptr = NULL; assert(nbytes > 0); // //使用“first match”算法在空闲的内存块链表中找到一个空闲块 for(bp = freelist.free; bp; bp = bp->free) { /*当前的空闲块的内存节点大小满足需求* * 如果不满足继续for循环查找,直到遍历完整链表 * 如果未找到,则需要重新malloc分配节点,加入freelist中 * 再次进入下次循环*/ if(bp->size > nbytes) { bp->size -= nbytes; ptr = (char *)bp->ptr + bp->size; //移动指针 //将memnode 加入哈希表(htab)中 if((bp = dalloc(ptr, nbytes)) != NULL) { h = HASH(ptr, htab); bp->link = htab[h]; htab[h] = bp; return ptr; //内存分配成功,并返回 }else { assert(0); //失败 } }/*if*/ /*循环完整个空闲块,未找到空闲块,此时重新分配内存*/ if(bp == &freelist) { //ptr 为实际内存地址 if((ptr = malloc(nbytes + NALLOC)) == NULL\ || ((newptr = dalloc(ptr, nbytes + NALLOC)) == NULL)) { assert(!"malloc failed\n"); }/*if*/ //将新分配的节点,头插法加入到freelist链表中 newptr->free = freelist.free; freelist.free = newptr; }/*if*/ }/*for*/ assert(!"no happend\n"); return NULL; }
dlist *dlistInsertNode(dlist *list, dlistNode *old_node, void *value, int after) { dlistNode *node; if ((node = dalloc(sizeof(*node))) == NULL) return NULL; node->value = value; if (after) { node->prev = old_node; node->next = old_node->next; if (list->tail == old_node) { list->tail = node; } } else { node->next = old_node; node->prev = old_node->prev; if (list->head == old_node) { list->head = node; } } if (node->prev != NULL) { node->prev->next = node; } if (node->next != NULL) { node->next->prev = node; } list->len++; return list; }
static trace_data * init_trace(trace_data *T, GEN S, nflift_t *L, GEN q) { long e = gexpo(S), i,j, l,h; GEN qgood, S1, invd; if (e < 0) return NULL; /* S = 0 */ qgood = int2n(e - 32); /* single precision check */ if (cmpii(qgood, q) > 0) q = qgood; S1 = gdivround(S, q); if (gcmp0(S1)) return NULL; invd = ginv(itor(L->den, DEFAULTPREC)); T->dPinvS = gmul(L->iprk, S); l = lg(S); h = lg(T->dPinvS[1]); T->PinvSdbl = (double**)cgetg(l, t_MAT); init_dalloc(); for (j = 1; j < l; j++) { double *t = dalloc(h * sizeof(double)); GEN c = gel(T->dPinvS,j); pari_sp av = avma; T->PinvSdbl[j] = t; for (i=1; i < h; i++) t[i] = rtodbl(mpmul(invd, gel(c,i))); avma = av; } T->d = L->den; T->P1 = gdivround(L->prk, q); T->S1 = S1; return T; }
int print(int argc, char**argv){ if(argc==0){ fprintf(stderr,"print FILE [-r chrName]\n"); exit(0); } char *base = *argv; char* outnames_bin = append(base,BIN); char* outnames_idx = append(base,IDX); fprintf(stderr,"Assuming binfile:%s and indexfile:%s\n",outnames_bin,outnames_idx); myMap mm = getMap(outnames_idx); writemap(stderr,mm); BGZF *fp = bgzf_open(outnames_bin,"r"); --argc;++argv; // fprintf(stderr,"argc=%d\n",argc); int argP =0; char *chr=NULL; while(argP<argc){ // fprintf(stderr,"args=%s\n",argv[argP]); if(argP==argc){ fprintf(stderr,"incomplete arguments list\n"); exit(0); } if(strcmp("-r",argv[argP])==0) chr = argv[argP+1]; else { fprintf(stderr,"Unknown argument:%s\n",argv[argP]); exit(0); } argP +=2; } if(chr!=NULL){ myMap::iterator it = mm.find(chr); if(it==mm.end()){ fprintf(stderr,"Problem finding chr: %s in index\n",chr); exit(0); } datum d = it->second; bgzf_seek(fp,d.fpos,SEEK_SET); } while(1){ perChr pc = getPerChr(fp); if(pc.nSites==0) break; fprintf(stderr,"pc.chr=%s pc.nSites=%zu firstpos=%d lastpos=%d\n",pc.chr,pc.nSites,pc.posi[0],pc.posi[pc.nSites-1]); print_main(pc,stdout); if(chr!=NULL) break; dalloc(pc); } return 0; }
void *mem_alloc(long nbytes, const char *file, int line) { struct descriptor *bp; void *ptr; assert(nbytes > 0); nbytes = ((nbytes + sizeof(union align) - 1)/(sizeof(union align)))*sizeof(union align); for (bp = freelist.free; bp; bp = bp->free) { if (bp->size > nbytes) { bp->size -= nbytes; ptr = (char *)bp->ptr + bp->size; if ((bp = dalloc(ptr, nbytes, file, line)) != NULL) { unsigned h = hash(ptr, htab); bp->link = htab[h]; htab[h] = bp; return ptr; } else { printf("FAIL OF ALLOC #1 at %s:%d\n", file, line); exit(2); } } if (bp == &freelist) { struct descriptor *newptr; if ((ptr = malloc(nbytes + NALLOC)) == NULL || (newptr = dalloc(ptr, nbytes + NALLOC, __FILE__, __LINE__)) == NULL) { printf("FAIL OF ALLOC #! at %s:%d\n", file, line); exit(3); } newptr->free = freelist.free; freelist.free = newptr; } } assert(0); return NULL; }
robj *createObject(int type, void *ptr) { robj *o = dalloc(sizeof(*o)); o->type = type; o->encoding = OBJ_ENCODING_RAW; o->ptr = ptr; o->constant = 0; o->refcount = -1; o->lru = 0; return o; }
/* The member object is stored in the zs->dict. * You can use the member in the zs->zsl just * when zs->dict was not released. */ robj *createZsetObject(void) { zset *zs = dalloc(sizeof(*zs)); robj *o; zs->dict = dictCreate(&zsetDictType,NULL); zs->zsl = zslCreate(); o = createObject(OBJ_ZSET,zs); o->encoding = OBJ_ENCODING_SKIPLIST; return o; }
dbuf_t * dalloc_ptr(void *data, size_t size) { dbuf_t *d = dalloc(1); free(d->buf); d->buf = data; d->savebuf = d->buf; d->size = size; d->dsize = size; return d; }
void * Mem_alloc(long nbytes, const char *file, int line) { assert(nbytes > 0); nbytes = ((nbytes + sizeof(union align) - 1) / (sizeof(union align))) * (sizeof(union align)); for (struct descriptor *bp = freelist.free; bp; bp = bp->free) { if (bp->size > nbytes) { bp->size -= nbytes; void *ptr = (char *)bp->ptr + bp->size; if ((bp = dalloc(ptr, nbytes, file, line)) != NULL) { unsigned h = hash(ptr, htab); bp->link = htab[h]; htab[h] = bp; return ptr; } else { if (file == NULL) RAISE(Mem_Failed); else Except_raise(&Mem_Failed, file, line); } } if (bp == &freelist) { void *ptr = malloc(nbytes + NALLOC); struct descriptor *newptr = dalloc(ptr, nbytes + NALLOC, __FILE__, __LINE__); if (ptr == NULL || newptr == NULL) { if (file == NULL) RAISE(Mem_Failed); else Except_raise(&Mem_Failed, file, line); } newptr->free = freelist.free; freelist.free = newptr; } // exercise 5.2 if (bp->free->size == sizeof(union align)) { struct descriptor *temp = bp->free; bp->free = temp->free; temp->free = NULL; } } assert(0); return NULL; }
dbuf_t * dsubstrcpy(dbuf_t *d, int start, int howmany) { dbuf_t *temp = NULL; if ((start < d->dsize) && (start + howmany <= d->dsize)) { temp = dalloc(howmany); memcpy(temp->buf, &d->buf[start], howmany); temp->dsize = howmany; } return temp; }
dbuf_t * dstrcpy(const char *str) { dbuf_t *temp = dalloc(strlen(str) + 1); if(temp) { memcpy((void *) temp->buf, str, temp->size); temp->dsize = temp->size - 1; } return temp; }
dbuf_t * dalloczf(int size) { dbuf_t *d = dalloc(size); if(d) { d->dsize = d->size; bzero(d->buf, d->size); } return d; }
void* dalloc_z(uint32 size) { void *ret; ret = dalloc(size); if(ret) { memset(ret, 0, size); } return ret; }
/* Returns a list iterator 'iter'. After the initialization every * call to dlistNext() will return the next element of the list. * * This function can't fail. */ dlistIter *dlistGetIterator(dlist *list, int direction) { dlistIter *iter; if ((iter = dalloc(sizeof(*iter))) == NULL) return NULL; if (direction == AL_START_HEAD) iter->next = list->head; else iter->next = list->tail; iter->direction = direction; return iter; }
dbuf_t *dtry_reasm_timed(void *pile, uint32_t xid, char *data, uint16_t len, uint16_t offs, int more, time_t now) { reasm_pile_struct_t *rp = (void *)(((dbuf_t *)pile)->buf); reasm_chunk_t *chk; if (now > 0) { check_timeouts(rp, now); } if(offs + len > rp->mtu) { debug(DBG_REASM, 10, "Offset + length (%d + %d) of fragment > MTU (%d), discard", offs, len, rp->mtu); return NULL; } if ((offs > 0) && (offs < HOLE_MIN_LENGTH)) { debug(DBG_REASM, 10, "Offset %d less than min hole length %d\n", offs, HOLE_MIN_LENGTH); return NULL; } chk = hfind(rp->chs, &xid, sizeof(xid)); if (!chk) { debug(DBG_REASM, 10, "Reasm chunk %lu not found, creating", xid); chk = malloc(sizeof(reasm_chunk_t)); chk->xid = xid; chk->maxfrags = rp->maxfrags; chk->hole = 0; chk->esize = rp->ftu; chk->d = dalloc(chk->esize); chk->deadline = now + rp->reasm_timeout; memset(chk->d->buf, 0xaa, chk->d->size); hole_set(chk, 0, chk->esize, 0); hinsert(rp->chs, &xid, sizeof(xid), chk, chk_destructor, NULL, NULL, NULL); TAILQ_INSERT_TAIL(&rp->lru, chk, entries); } else { debug(DBG_REASM, 10, "Reasm chunk %lu found", xid); } debug(DBG_REASM, 100, "Chunk data (hole: %d, esize: %d):", chk->hole, chk->esize); debug_dump(DBG_REASM, 100, chk->d->buf, chk->d->size); if(offs + len > chk->d->size) { debug(DBG_REASM, 10, "Reasm chunk %lu overflow - %d + %d > %d", xid, offs, len, chk->d->size); /* We already checked the MTU overflow above, so we can safely reallocate here */ int oldsize = chk->d->size; dgrow(chk->d, rp->mtu - chk->d->size); hole_grow(chk, oldsize-1, chk->d->size); chk->esize = chk->d->size; debug(DBG_REASM, 100, "Fresh chunk data after growth to MTU:"); debug_dump(DBG_REASM, 100, chk->d->buf, chk->d->size); } chk->atime = now; return dperform_reasm(rp, chk, xid, data, len, offs, more); }
/* Create a new list. The created list can be freed with * AlFreeList(), but private value of every node need to be freed * by the user before to call AlFreeList(). * * On error, NULL is returned. Otherwise the pointer to the new list. */ dlist *dlistCreate(void) { struct dlist *list; if ((list = dalloc(sizeof(*list))) == NULL) return NULL; list->head = list->tail = NULL; list->len = 0; list->dup = NULL; list->free = NULL; list->match = NULL; return list; }
multiReader::~multiReader(){ if(revMap){ for(aMap::iterator it=revMap->begin();it!=revMap->end();++it) free((char*)it->first); } dalloc(hd); delete revMap; free(fai); switch(type){ case INPUT_PILEUP:{ delete mpil; break; } case INPUT_VCF_GP:{ delete myvcf; break; } case INPUT_VCF_GL:{ delete myvcf; break; } case INPUT_GLF:{ delete myglf; break; } case INPUT_BEAGLE:{ delete bglObj; break; } default:{ break; } } if(gz!=Z_NULL) gzclose(gz); free(fname); for(unsigned i=0;i<args->nams.size();i++) free(args->nams[i]); delete [] args->usedArgs; free(args->outfiles); if(args->argumentFile!=stderr) fclose(args->argumentFile); delete args; }
int val_bed(int argc, char**argv){ if(argc!=1){ fprintf(stderr,"val_bed FILE.gz \n"); exit(0); } char *base = *argv; char* outnames_bin = append(base,BIN); char* outnames_gz = base; fprintf(stderr,"Assuming binfile:%s and gzfile:%s\n",outnames_bin,outnames_gz); BGZF *fp = bgzf_open(outnames_bin,"r"); gzFile gz =gzopen(outnames_gz,"r"); char buf[4096]; gzgets(gz,buf,4096); while(1){ perChr pc = getPerChr(fp); if(pc.nSites==0) break; fprintf(stderr,"pc.chr=%s pc.nSites=%zu firstpos=%d lastpos=%d\n",pc.chr,pc.nSites,pc.posi[0],pc.posi[pc.nSites-1]); for(size_t i=0;i<pc.nSites;i++){ gzgets(gz,buf,4096); char *chr = strtok(buf,"\n\t "); if(strcmp(chr,pc.chr)!=0){ fprintf(stderr,"Problem with nonmatching chromosome: \'%s\' vs \'%s\'\n",chr,pc.chr); exit(0); } int posi =atoi(strtok(NULL,"\t\n ")); if(posi!=pc.posi[i]){ fprintf(stderr,"Problem with nonmatching position\n"); exit(0); } float tW = atof(strtok(NULL,"\t\n ")); float tP = atof(strtok(NULL,"\t\n ")); float tF = atof(strtok(NULL,"\t\n ")); float tH = atof(strtok(NULL,"\t\n ")); float tL = atof(strtok(NULL,"\t\n ")); fun(tW,pc.tW[i]); fun(tP,pc.tP[i]); fun(tF,pc.tF[i]); fun(tH,pc.tH[i]); fun(tL,pc.tL[i]); } fprintf(stderr,"FILE: %s chr: %s OK\n",base,pc.chr); dalloc(pc); } fprintf(stderr,"ALL OK: %s\n",base); return 0; }
static Dentry* maked(long a, int s, long qpath) { Iobuf *p; Dentry *d, *d1; p = xtag(a, Tdir, qpath); if(!p) return 0; d = getdir(p, s); d1 = dalloc(sizeof(Dentry)); memmove(d1, d, sizeof(Dentry)); putbuf(p); return d1; }
void CClientMgr::AddToObjectMap(uint16 id) { LTRecord *newMap; uint32 newSize; // Make sure the object map is large enough. if (m_ObjectMapSize <= id) { newSize = id + 100; LT_MEM_TRACK_ALLOC(newMap = (LTRecord*)dalloc(sizeof(LTRecord) * newSize),LT_MEM_TYPE_OBJECT); memset(newMap, 0, sizeof(LTRecord) * newSize); memcpy(newMap, m_ObjectMap, sizeof(LTRecord) * m_ObjectMapSize); dfree(m_ObjectMap); m_ObjectMap = newMap; m_ObjectMapSize = newSize; } }
static int lua_fn_dsend(lua_State *L) { int n = lua_gettop(L); int idx = lua_tonumber(L, 1); size_t len; int nwrote; char *data = (void *)lua_tolstring(L, 2, &len); dbuf_t *d = dalloc(len); memcpy(d->buf, data, len); d->dsize = len; sock_write_data(idx, d, &nwrote); dunlock(d); lua_pushnumber(L, nwrote); return 1; }
void *make_reasm_pile() { reasm_pile_struct_t *rp; dbuf_t *d = dalloc(sizeof(reasm_pile_struct_t)); rp = (void*) d->buf; TAILQ_INIT(&rp->lru); rp->chs = halloc(4, 16); rp->maxfrags = 32; rp->reasm_timeout = 20; rp->mtu = 500* 8; rp->ftu = 250* 8; rp->mtu = 50* 8; rp->ftu = 25* 8; return d; }
/* Add a new node to the list, to tail, containing the specified 'value' * pointer as value. * * On error, NULL is returned and no operation is performed (i.e. the * list remains unaltered). * On success the 'list' pointer you pass to the function is returned. */ dlist *dlistAddNodeTail(dlist *list, void *value) { dlistNode *node; if ((node = dalloc(sizeof(*node))) == NULL) return NULL; node->value = value; if (list->len == 0) { list->head = list->tail = node; node->prev = node->next = NULL; } else { node->prev = list->tail; node->next = NULL; list->tail->next = node; list->tail = node; } list->len++; return list; }
/* * format the inode blocks */ int iformat(Icache *ic, int f, uint32_t nino, char *name, int bsize, int psize) { int nib; uint32_t bno, i2b, i; Bbuf *bb; Dinode *bi; /* * first format disk allocation */ if(dformat(ic, f, name, bsize, psize) < 0) return -1; fprint(2, "formatting inodes\n"); i2b = (bsize - sizeof(Dihdr))/sizeof(Inode); nib = (nino + i2b - 1)/i2b; for(bno = ic->nab; bno < ic->nab + nib; bno++){ if(dalloc(ic, 0) == Notabno){ fprint(2, "iformat: balloc failed\n"); return -1; } bb = bcalloc(ic, bno); if(bb == 0){ fprint(2, "iformat: bcalloc failed\n"); return -1; } bi = (Dinode*)bb->data; bi->magic = Imagic; bi->nino = nino; for(i = 0; i < i2b; i++) bi->inode[i].inuse = 0; bcmark(ic, bb); } bcsync(ic); return iinit(ic, f, psize, name); }
int main_sites(int argc,char **argv){ #if 0 for(int i=0;i<argc;i++) fprintf(stderr,"argv[%d]:%s\n",i,argv[i]); #endif if(argc==1){ fprintf(stderr,"\tsites print filename\t\tPrint index file\n\tsites index filename [-r offset -compl doCompl]\tgenerate binary index file\n"); return 0; } --argc;++argv; if(!strcasecmp(*argv,"index")){ filt_init(--argc,++argv); }else if(!strcasecmp(*argv,"print")){ filt *f = filt_read(*++argv); filt_print(stdout,f,NULL); dalloc(f); }else fprintf(stderr,"Unknown option: \'%s\'\n",*argv); return 0; }
static void dsi_GetDLLModes(char *pDLLName, RMode **pMyList) { RMode *pMyMode; RMode *pListHead, *pCur; pListHead = rdll_GetSupportedModes(); // Copy the mode list. pCur = pListHead; while (pCur) { LT_MEM_TRACK_ALLOC(pMyMode = (RMode*)dalloc(sizeof(RMode)),LT_MEM_TYPE_MISC); memcpy(pMyMode, pCur, sizeof(RMode)); pMyMode->m_pNext = *pMyList; *pMyList = pMyMode; pCur = pCur->m_pNext; } rdll_FreeModeList(pListHead); }
dlockqueue *dlockqueue_create(void) { dlockqueue *lqueue; lqueue = dalloc(sizeof(*lqueue)); if (lqueue == NULL) { return NULL; } lqueue->maxlen = -1; lqueue->maxlen_policy = MAX_LENGTH_POLICY_REJECT; pthread_mutex_init(&lqueue->lmutex,NULL); lqueue->l = dlistCreate(); if (lqueue->l == NULL) { dlockqueue_destroy(lqueue); return NULL; } return lqueue; }