Example #1
0
void resize_maxpool_layer(maxpool_layer *l, int w, int h)
{
    int stride = l->stride;
    l->h = h;
    l->w = w;
    l->inputs = h*w*l->c;

    l->out_w = (w-1)/stride + 1;
    l->out_h = (h-1)/stride + 1;
    l->outputs = l->out_w * l->out_h * l->c;
    int output_size = l->outputs * l->batch;

    l->indexes = realloc(l->indexes, output_size * sizeof(int));
    l->output = realloc(l->output, output_size * sizeof(float));
    l->delta = realloc(l->delta, output_size * sizeof(float));

    #ifdef GPU
    cuda_free((float *)l->indexes_gpu);
    cuda_free(l->output_gpu);
    cuda_free(l->delta_gpu);
    l->indexes_gpu = cuda_make_int_array(output_size);
    l->output_gpu  = cuda_make_array(l->output, output_size);
    l->delta_gpu   = cuda_make_array(l->delta,  output_size);
    #endif
}
//!
//! Function description.
//!
//! @param[in] ipsh pointer to the IP set handler structure
//! @param[in] setname a string pointer to the ipset name
//! @param[in] ipname a string pointer to the ip address
//! @param[in] nmname the netmask address
//!
//! @return
//!
//! @see
//!
//! @pre
//!
//! @post
//!
//! @note
//!
int ips_set_add_net(ips_handler * ipsh, char *setname, char *ipname, int nmname)
{
    ips_set *set = NULL;
    u32 *ip = NULL;
    if (!ipsh || !setname || !ipname || !ipsh->init) {
        return (1);
    }

    set = ips_handler_find_set(ipsh, setname);
    if (!set) {
        return (1);
    }

    ip = ips_set_find_net(ipsh, setname, ipname, nmname);
    if (!ip) {
        set->member_ips = realloc(set->member_ips, sizeof(u32) * (set->max_member_ips + 1));
        if (!set->member_ips) {
            LOGFATAL("out of memory!\n");
            exit(1);
        }
        set->member_nms = realloc(set->member_nms, sizeof(int) * (set->max_member_ips + 1));
        if (!set->member_nms) {
            LOGFATAL("out of memory!\n");
            exit(1);
        }

        bzero(&(set->member_ips[set->max_member_ips]), sizeof(u32));
        bzero(&(set->member_nms[set->max_member_ips]), sizeof(int));
        set->member_ips[set->max_member_ips] = dot2hex(ipname);
        set->member_nms[set->max_member_ips] = nmname;
        set->max_member_ips++;
        set->ref_count++;
    }
    return (0);
}
Example #3
0
static void _os_lacing_expand(ogg_stream_state *os,int needed){
  if(os->lacing_storage<=os->lacing_fill+needed){
    os->lacing_storage+=(needed+32);
    os->lacing_vals=realloc(os->lacing_vals,os->lacing_storage*sizeof(*os->lacing_vals));
    os->granule_vals=realloc(os->granule_vals,os->lacing_storage*sizeof(*os->granule_vals));
  }
}
Example #4
0
INJECTOR_EXPORTS char* get_line(void)
{
	char *buffer = NULL;
	int size = BUF_SIZE;
	int pos = 0;
	int c;

	buffer = realloc(buffer, size);

	while(1) {
		c = getchar();
		if(c == EOF || c == '\n') {
			buffer[pos] = 0;
			return buffer;
		}
		else {
			buffer[pos] = c;
		}
		++pos;

		if(pos >= size) {
			size += BUF_SIZE;
			buffer = realloc(buffer, size);
			if (buffer == NULL) {
				printf("Error: Out of memory.\n");
				break;
			}
		}
	}

	return NULL;
}
Example #5
0
void unlink_action_btn(action *a, bouton *b)
{
    if(a!= NULL && b != NULL && b->action == a)
    {
        int i, j;

        for(i = j = 0; j < a->nb_boutons; i++, j++)
        {
            if(a->boutons[i] == b)
            {
                i--;
            }
            else
            {
                a->boutons[i] = a->boutons[j];
                a->btn_flag[i] = a->btn_flag[j];
            }
        }

        /* Le bouton était bien lié à l'action */
        if(i != j)
        {
            a->nb_boutons--;
            a->boutons = realloc(a->boutons, sizeof(bouton *) * a->nb_boutons);
            a->btn_flag = realloc(a->btn_flag, sizeof(int) * a->nb_boutons);
        }
    }
}
Example #6
0
QSP_CHAR *qspFormatText(QSP_CHAR *txt, QSP_BOOL canReturnSelf)
{
	QSPVariant val;
	QSP_CHAR *newTxt, *lPos, *rPos;
	int oldRefreshCount, len, txtLen, oldTxtLen, bufSize;
	if (qspGetVarNumValue(QSP_FMT("DISABLESUBEX")))
	{
		if (canReturnSelf) return txt;
		return qspGetNewText(txt, -1);
	}
	lPos = qspStrStr(txt, QSP_LSUBEX);
	if (!lPos)
	{
		if (canReturnSelf) return txt;
		return qspGetNewText(txt, -1);
	}
	bufSize = 256;
	newTxt = (QSP_CHAR *)malloc(bufSize * sizeof(QSP_CHAR));
	txtLen = oldTxtLen = 0;
	oldRefreshCount = qspRefreshCount;
	do
	{
		len = (int)(lPos - txt);
		if ((txtLen += len) >= bufSize)
		{
			bufSize = txtLen + 128;
			newTxt = (QSP_CHAR *)realloc(newTxt, bufSize * sizeof(QSP_CHAR));
		}
		qspStrNCopy(newTxt + oldTxtLen, txt, len);
		oldTxtLen = txtLen;
		txt = lPos + QSP_LEN(QSP_LSUBEX);
		rPos = qspStrPos(txt, QSP_RSUBEX, QSP_FALSE);
		if (!rPos)
		{
			qspSetError(QSP_ERR_BRACKNOTFOUND);
			free(newTxt);
			return 0;
		}
		*rPos = 0;
		val = qspExprValue(txt);
		*rPos = QSP_RSUBEX[0];
		if (qspRefreshCount != oldRefreshCount || qspErrorNum)
		{
			free(newTxt);
			return 0;
		}
		qspConvertVariantTo(&val, QSP_TRUE);
		if ((txtLen += qspStrLen(QSP_STR(val))) >= bufSize)
		{
			bufSize = txtLen + 128;
			newTxt = (QSP_CHAR *)realloc(newTxt, bufSize * sizeof(QSP_CHAR));
		}
		qspStrCopy(newTxt + oldTxtLen, QSP_STR(val));
		free(QSP_STR(val));
		oldTxtLen = txtLen;
		txt = rPos + QSP_LEN(QSP_RSUBEX);
		lPos = qspStrStr(txt, QSP_LSUBEX);
	} while (lPos);
	return qspGetAddText(newTxt, txt, txtLen, -1);
}
Example #7
0
int OldCnvMapDecodeTwo(char *name,char *unit,char *type,
                    double st_time,double ed_time,
                    int npnt,int nprm,int pnt,
                    struct RfileData *data,void *ptr) {

  struct CnvMapData *mp;
  mp=(struct CnvMapData *)ptr;

  if (strcmp(name,BLOCK2) !=0) return 0;

  if (pnt==0) {
    if (npnt>0) {
      if (mp->bnd_lon !=NULL) mp->bnd_lon=realloc(mp->bnd_lon,
                               sizeof(double)*npnt);
      else mp->bnd_lon=malloc( sizeof(double)*npnt);
      if (mp->bnd_lat !=NULL) mp->bnd_lat=realloc(mp->bnd_lat,
                               sizeof(double)*npnt);
      else mp->bnd_lat=malloc( sizeof(double)*npnt);
    } else {
      free(mp->bnd_lon);
      mp->bnd_lon=NULL;
      free(mp->bnd_lat);
      mp->bnd_lat=NULL;
    }
    mp->st_time=st_time;
    mp->ed_time=ed_time;
    mp->num_bnd=npnt;
  }

  if (npnt>0) {
    mp->bnd_lat[pnt]=data[1].data.fval;
    mp->bnd_lon[pnt]=data[0].data.fval;
  }
  return 1;
}
Example #8
0
File: pcm.c Project: neinnil/w2m
int
initPcmBuffer (PcmBuffer *pbuf, int nSamples, int nSize)
{
	int rc = -EINVAL;
	if (pbuf)
	{
		pbuf->ch[0] = realloc(pbuf->ch[0], nSamples * nSize);
		if (!pbuf->ch[0])
		{
			return -ENOMEM;;
		}
		pbuf->ch[1] = realloc(pbuf->ch[1], nSamples * nSize);
		if (!pbuf->ch[1])
		{
			free (pbuf->ch[0]);
			pbuf->ch[0] = NULL;
			return -ENOMEM;;
		}
		pbuf->w = nSize;
		pbuf->n = nSamples;
		pbuf->u = 0;
		return 0;
	}
	return rc;
}
Example #9
0
LIBIMOBILEDEVICE_API idevice_error_t idevice_get_device_list(char ***devices, int *count)
{
	usbmuxd_device_info_t *dev_list;

	*devices = NULL;
	*count = 0;

	if (usbmuxd_get_device_list(&dev_list) < 0) {
		debug_info("ERROR: usbmuxd is not running!", __func__);
		return IDEVICE_E_NO_DEVICE;
	}

	char **newlist = NULL;
	int i, newcount = 0;

	for (i = 0; dev_list[i].handle > 0; i++) {
		newlist = realloc(*devices, sizeof(char*) * (newcount+1));
		newlist[newcount++] = strdup(dev_list[i].udid);
		*devices = newlist;
	}
	usbmuxd_device_list_free(&dev_list);

	*count = newcount;
	newlist = realloc(*devices, sizeof(char*) * (newcount+1));
	newlist[newcount] = NULL;
	*devices = newlist;

	return IDEVICE_E_SUCCESS;
}
Example #10
0
/*
   The |try_resize| function tries to resize |*p| so it can hold more
   data. It will always yield a valid, consistent |*p| and |*cap| ---
   |*cap| will never decrease, and no data will be lost from |*p|.
   But if a call to |realloc| fails, |*cap| will be unchanged.
   One important thing to notice: We must never increase |*cap| by
   more than |INT_MAX|, since the second parameter to |fgets| is of
   type |int|.
*/
static void try_resize(char **p, size_t *cap)
{
    /*
       We aren't expecting any really long lines, here. But if the
       current line has exceeded 500 characters, there's probably
       something special going on (like an attempted buffer overflow
       attack), and we'll start increasing the buffer capacity
       geometrically.
    */
    size_t newcap = (*cap < 500)? (*cap + 16):
                    (*cap/2 < INT_MAX)? (*cap + *cap/2):
                    (*cap + INT_MAX);
    char *newp;
    if (newcap < *cap) {
        /* The line length has exceeded |(size_t)-1|. Wow! */
        if (*cap == (size_t)-1) return;
        else newcap = (size_t)-1;
    }

    newp = realloc(*p, newcap);
    /* Maybe we can't get that much memory. Try smaller chunks. */
    while (newp == NULL) {
        newcap = *cap + (newcap - *cap)/2;
        if (newcap == *cap) break;
        newp = realloc(*p, newcap);
    }

    if (newp != NULL)
      *p = newp;

    /* At this point, |*p| hasn't lost any data, and |newcap| is valid. */
    *cap = newcap;
    return;
}
Example #11
0
// Make a texture - doesn't free "pixels"
void jl_gl_maketexture(jlgr_t* jlgr, uint16_t gid, uint16_t id,
	void* pixels, int width, int height, u8_t bytepp)
{
	jl_print_function(jlgr->jl, "GL_MkTex");
	if (!pixels) {
		jl_print(jlgr->jl, "null pixels");
		jl_sg_kill(jlgr->jl);
	}
	if (jlgr->gl.allocatedg < gid + 1) {
		jlgr->gl.textures =
			realloc(jlgr->gl.textures,
				sizeof(uint32_t *) * (gid+1));
		jlgr->gl.tex.uniforms.textures =
			realloc(jlgr->gl.tex.uniforms.textures,
				sizeof(GLint *) * (gid+1));
		jlgr->gl.allocatedg = gid + 1;
		jlgr->gl.allocatedi = 0;
		jlgr->gl.textures[gid] = NULL;
		jlgr->gl.tex.uniforms.textures[gid] = NULL;
	}
	if (jlgr->gl.allocatedi < id + 1) {
		jlgr->gl.textures[gid] =
			realloc(jlgr->gl.textures[gid],
				sizeof(uint32_t) * (id+1));
		jlgr->gl.tex.uniforms.textures[gid] =
			realloc(jlgr->gl.tex.uniforms.textures[gid],
				sizeof(GLint) * (id+1));
		jlgr->gl.allocatedi = id + 1;
	}
	JL_PRINT_DEBUG(jlgr->jl, "generating texture (%d,%d)",width,height);
	// Make the texture.
	jl_gl_texture_new__(jlgr, &jlgr->gl.textures[gid][id], pixels, width,
		height, bytepp);
	jl_print_return(jlgr->jl, "GL_MkTex");
}
Example #12
0
static int
expand_source_tables(int alloc, struct compr_bc *bc)
{
    enum compr_bc_type *t;
    int                *f;
    double             *p, *v, *s;

    t = realloc(bc->type      , alloc * 1           * sizeof *t);
    f = realloc(bc->face      , alloc * 1           * sizeof *f);
    p = realloc(bc->press     , alloc * 1           * sizeof *p);
    v = realloc(bc->flux      , alloc * 1           * sizeof *v);
    s = realloc(bc->saturation, alloc * bc->nphases * sizeof *s);

    if ((t == NULL) || (f == NULL) ||
        (p == NULL) || (v == NULL) || (s == NULL)) {

        free(s);  free(v);  free(p);  free(f);  free(t);

        alloc = 0;
    } else {
        bc->type       = t;  bc->face = f;
        bc->press      = p;  bc->flux = v;
        bc->saturation = s;
    }

    return alloc;

}
Example #13
0
void thresher_init(t_thresher *x)
{
	t_fftease  *fft = x->fft;
	short initialized = fft->initialized;

	fftease_init(fft);
	x->tadv = (t_float) fft->D / (t_float) fft->R ;

	if(!initialized){
		x->mute = 0;
		if(!x->damping_factor){
			x->damping_factor = .95;
		}
		x->first_frame = 1;
		x->move_threshold = .00001 ;
		x->max_hold_time = DEFAULT_HOLD ;
		x->max_hold_frames = x->max_hold_time / x->tadv;
		x->composite_frame = (t_float *) calloc( (fft->N+2), sizeof(t_float));
		x->frames_left = (int *) calloc( (fft->N+2), sizeof(int) );
		
	} else if(initialized == 1){
		x->composite_frame = (t_float *) realloc(x->composite_frame, (fft->N+2) * sizeof(t_float) );
		x->frames_left = (int *) realloc(x->frames_left, (fft->N+2) * sizeof(int) );
	}
}
Example #14
0
static int ssh_poll_ctx_resize(ssh_poll_ctx ctx, size_t new_size) {
  ssh_poll_handle *pollptrs;
  ssh_pollfd_t *pollfds;

  pollptrs = realloc(ctx->pollptrs, sizeof(ssh_poll_handle) * new_size);
  if (pollptrs == NULL) {
    return -1;
  }
  ctx->pollptrs = pollptrs;

  pollfds = realloc(ctx->pollfds, sizeof(ssh_pollfd_t) * new_size);
  if (pollfds == NULL) {
    pollptrs = realloc(ctx->pollptrs, sizeof(ssh_poll_handle) * ctx->polls_allocated);
    if (pollptrs == NULL) {
        return -1;
    }
    ctx->pollptrs = pollptrs;
    return -1;
  }

  ctx->pollfds = pollfds;
  ctx->polls_allocated = new_size;

  return 0;
}
Example #15
0
QSP_CHAR *qspJoinStrs(QSP_CHAR **s, int count, QSP_CHAR *delim)
{
	int i, txtLen = 0, txtRealLen = 0, bufSize = 256, lastIndex = count - 1, delimLen = qspStrLen(delim);
	QSP_CHAR *txt = (QSP_CHAR *)malloc(bufSize * sizeof(QSP_CHAR));
	*txt = 0;
	for (i = 0; i < count; ++i)
	{
		if ((txtLen += qspStrLen(s[i])) >= bufSize)
		{
			bufSize = txtLen + 128;
			txt = (QSP_CHAR *)realloc(txt, bufSize * sizeof(QSP_CHAR));
		}
		qspStrCopy(txt + txtRealLen, s[i]);
		if (i == lastIndex) break;
		txtRealLen = txtLen;
		if ((txtLen += delimLen) >= bufSize)
		{
			bufSize = txtLen + 128;
			txt = (QSP_CHAR *)realloc(txt, bufSize * sizeof(QSP_CHAR));
		}
		qspStrCopy(txt + txtRealLen, delim);
		txtRealLen = txtLen;
	}
	return txt;
}
Example #16
0
void *av_realloc(void *ptr, size_t size)
{
#if CONFIG_MEMALIGN_HACK
    int diff;
#endif

    /* let's disallow possibly ambiguous cases */
    if (size > (max_alloc_size - 32))
        return NULL;

#if CONFIG_MEMALIGN_HACK
    //FIXME this isn't aligned correctly, though it probably isn't needed
    if (!ptr)
        return av_malloc(size);
    diff = ((char *)ptr)[-1];
    av_assert0(diff>0 && diff<=ALIGN);
    ptr = realloc((char *)ptr - diff, size + diff);
    if (ptr)
        ptr = (char *)ptr + diff;
    return ptr;
#elif HAVE_ALIGNED_MALLOC
    return _aligned_realloc(ptr, size + !size, ALIGN);
#else
    return realloc(ptr, size + !size);
#endif
}
Example #17
0
int qspSplitStr(QSP_CHAR *str, QSP_CHAR *delim, QSP_CHAR ***res)
{
	int allocChars, count = 0, bufSize = 8, delimLen = qspStrLen(delim);
	QSP_CHAR *newStr, **ret, *curPos = str, *found = qspStrStr(str, delim);
	ret = (QSP_CHAR **)malloc(bufSize * sizeof(QSP_CHAR *));
	while (found)
	{
		allocChars = (int)(found - curPos);
		newStr = (QSP_CHAR *)malloc((allocChars + 1) * sizeof(QSP_CHAR));
		qspStrNCopy(newStr, curPos, allocChars);
		newStr[allocChars] = 0;
		if (++count > bufSize)
		{
			bufSize += 16;
			ret = (QSP_CHAR **)realloc(ret, bufSize * sizeof(QSP_CHAR *));
		}
		ret[count - 1] = newStr;
		curPos = found + delimLen;
		found = qspStrStr(curPos, delim);
	}
	newStr = (QSP_CHAR *)malloc((qspStrLen(curPos) + 1) * sizeof(QSP_CHAR));
	qspStrCopy(newStr, curPos);
	if (++count > bufSize)
		ret = (QSP_CHAR **)realloc(ret, count * sizeof(QSP_CHAR *));
	ret[count - 1] = newStr;
	*res = ret;
	return count;
}
Example #18
0
File: RGReads.c Project: nh13/BFAST
void RGReadsReallocate(RGReads *reads, int numReads) 
{
	int i;
	if(numReads > 0) {
		/* Remember to free the reads that will be reallocated if we go to less */
		if(numReads < reads->numReads) {
			for(i=numReads;i<reads->numReads;i++) {
				free(reads->reads[i]);
			}
		}
		reads->numReads = numReads;
		reads->reads = realloc(reads->reads, sizeof(char*)*(reads->numReads));
		if(NULL == reads->reads) {
			PrintError("RGReadsReallocate", "reads->reads", "Could not reallocate memory", Exit, MallocMemory);
		}
		reads->readLength = realloc(reads->readLength, sizeof(int32_t)*(reads->numReads));
		if(NULL == reads->readLength) {
			PrintError("RGReadsReallocate", "reads->readLength", "Could not reallocate memory", Exit, MallocMemory);
		}
		reads->offset = realloc(reads->offset, sizeof(int32_t)*(reads->numReads));
		if(NULL == reads->offset) {
			PrintError("RGReadsReallocate", "reads->offset", "Could not reallocate memory", Exit, MallocMemory);
		}
	}
	else {
		RGReadsFree(reads);
	}
}
Example #19
0
void *zrealloc(void *ptr, size_t size) {
#ifndef HAVE_MALLOC_SIZE
    void *realptr;
#endif
    size_t oldsize;
    void *newptr;

    if (ptr == NULL) return zmalloc(size);
#ifdef HAVE_MALLOC_SIZE
    oldsize = zmalloc_size(ptr);
    newptr = realloc(ptr,size);
    if (!newptr) zmalloc_oom_handler(size);

    update_zmalloc_stat_free(oldsize);
    update_zmalloc_stat_alloc(zmalloc_size(newptr));
    return newptr;
#else
    realptr = (char*)ptr-PREFIX_SIZE;
    oldsize = *((size_t*)realptr);
    newptr = realloc(realptr,size+PREFIX_SIZE);
    if (!newptr) zmalloc_oom_handler(size);

    *((size_t*)newptr) = size;
    update_zmalloc_stat_free(oldsize);
    update_zmalloc_stat_alloc(size);
    return (char*)newptr+PREFIX_SIZE;
#endif
}
Example #20
0
//-----------------------------------------------------------------------------
//
//	Surf3 series
//
//-----------------------------------------------------------------------------
long mglGraph::add_spoint(long &pc,mreal **p,mreal **k,mreal **b,mreal **n,
			mreal x,mreal y,mreal z,mreal nx,mreal ny,mreal nz,
			mreal k1,mreal k2,mreal k3,mreal a)
{
	static long pMax=0;
	if(!ScalePoint(x,y,z) || a==0)	return -1;
	mglColor c;
	if(OnCoord)	c = GetC(x,y,z);
	else		c = Pal[100];
	if(!(*p))
	{
		pMax = 100;	pc = 0;
		*p = (mreal *)malloc(3*pMax*sizeof(mreal));
		*n = (mreal *)malloc(3*pMax*sizeof(mreal));
		*k = (mreal *)malloc(3*pMax*sizeof(mreal));
		*b = (mreal *)malloc(4*pMax*sizeof(mreal));
	}
	b[0][4*pc] = c.r;	b[0][4*pc+1] = c.g;	b[0][4*pc+2] = c.b;
	b[0][4*pc+3] = Transparent ? a : 1;
	p[0][3*pc] = x;		p[0][3*pc+1] = y;	p[0][3*pc+2] = z;
	n[0][3*pc] = nx;	n[0][3*pc+1] = ny;	n[0][3*pc+2] = nz;
	k[0][3*pc] = k1;	k[0][3*pc+1] = k2;	k[0][3*pc+2] = k3;
	pc++;
	if(pc>=pMax)
	{
		pMax += 100;
		*p = (mreal *)realloc(*p,3*pMax*sizeof(mreal));
		*n = (mreal *)realloc(*n,3*pMax*sizeof(mreal));
		*k = (mreal *)realloc(*k,3*pMax*sizeof(mreal));
		*b = (mreal *)realloc(*b,4*pMax*sizeof(mreal));
	}
	return pc-1;
}
static void
play_tree_add_basepath(play_tree_t* pt, char* bp) {
    int i,bl = strlen(bp),fl;

    if(pt->child) {
        play_tree_t* i;
        for(i = pt->child ; i != NULL ; i = i->next)
            play_tree_add_basepath(i,bp);
        return;
    }

    if(!pt->files)
        return;

    for(i = 0 ; pt->files[i] != NULL ; i++) {
        fl = strlen(pt->files[i]);
        // if we find a full unix path, url:// or X:\ at the beginning,
        // don't mangle it.
        if(fl <= 0 || strstr(pt->files[i],"://") || (strstr(pt->files[i],":\\") == pt->files[i] + 1) || (pt->files[i][0] == '/') )
            continue;
        // if the path begins with \ then prepend drive letter to it.
        if (pt->files[i][0] == '\\') {
            if (pt->files[i][1] == '\\')
                continue;
            pt->files[i] = (char*)realloc(pt->files[i],2+fl+1);
            memmove(pt->files[i] + 2,pt->files[i],fl+1);
            memcpy(pt->files[i],bp,2);
            continue;
        }
        pt->files[i] = (char*)realloc(pt->files[i],bl+fl+1);
        memmove(pt->files[i] + bl,pt->files[i],fl+1);
        memcpy(pt->files[i],bp,bl);
    }
}
Example #22
0
unsigned char *
file_read (char *pathname)
{
	FILE *		bf;
	unsigned char	c;
	unsigned int	cont_len;
	unsigned char *	cont = NULL;


	bf = fopen (pathname, "r");
	if (bf == NULL)
		return (NULL);

	/* yepp, its slow. f* caches internally though */
	for (cont_len = 0 ; fread (&c, 1, 1, bf) == 1 ; ++cont_len) {
		cont = realloc (cont, cont_len + 1);
		cont[cont_len] = c;
	}
	fclose (bf);

	cont = realloc (cont, cont_len + 1);
	cont[cont_len] = '\0';

	return (cont);
}
Example #23
0
/* allocate initial stack or double stack size, up to YYMAXDEPTH */
static int yygrowstack()
{
    int newsize, i;
    short *newss;
    YYSTYPE *newvs;

    if ((newsize = yystacksize) == 0)
        newsize = YYINITSTACKSIZE;
    else if (newsize >= YYMAXDEPTH)
        return -1;
    else if ((newsize *= 2) > YYMAXDEPTH)
        newsize = YYMAXDEPTH;
    i = yyssp - yyss;
    newss = yyss ? (short *)realloc(yyss, newsize * sizeof *newss) :
            (short *)malloc(newsize * sizeof *newss);
    if (newss == NULL)
        return -1;
    yyss = newss;
    yyssp = newss + i;
    newvs = yyvs ? (YYSTYPE *)realloc(yyvs, newsize * sizeof *newvs) :
            (YYSTYPE *)malloc(newsize * sizeof *newvs);
    if (newvs == NULL)
        return -1;
    yyvs = newvs;
    yyvsp = newvs + i;
    yystacksize = newsize;
    yysslim = yyss + newsize - 1;
    return 0;
}
Example #24
0
File: dns.c Project: aissat/vde2
static const char *
str2lbl(const char *data)
{
   const char *ptr;
   char *ptr2, *buf;
   unsigned int buflen, chunklen;
   
   ptr = data;
   buf = NULL;
   buflen = 0;
   
   while ((ptr2 = strchr(ptr, '.'))) {
      chunklen = ptr2 - ptr;
      if ((chunklen > 63) || (chunklen <= 0)) {
	 DEBUG("Too long or zero-length label");
	 if (buf)
	   free(buf);
	 return NULL;
      }
      buf = realloc(buf, buflen + chunklen + 1);
      buf[buflen] = chunklen;
      memcpy(buf+buflen+1, ptr, chunklen);
      buflen += chunklen + 1;
      ptr = ptr2 + 1;
   }
   chunklen = strlen(ptr);
   buf = realloc(buf, buflen + chunklen + 2);
   buf[buflen] = chunklen;
   memcpy(buf+buflen+1, ptr, chunklen);
   buflen += chunklen+1;
   buf[buflen] = '\0';
   buflen++;
   
   return buf;
}
Example #25
0
void* THRealloc(void *ptr, ptrdiff_t size)
{
  if(!ptr)
    return(THAlloc(size));

  if(size == 0)
  {
    THFree(ptr);
    return NULL;
  }

  if(size < 0)
    THError("$ Torch: invalid memory size -- maybe an overflow?");

  void *newptr = realloc(ptr, size);

  if(!newptr && torchGCFunction) {
    torchGCFunction(torchGCData);
    newptr = realloc(ptr, size);
  }

  if(!newptr)
    THError("$ Torch: not enough memory: you tried to reallocate %dGB. Buy new RAM!", size/1073741824);

  return newptr;
}
Example #26
0
 void AsmList<Scalar>::enlarge()
 {
   cap = !cap ? 128 : cap * 2;
   idx = (int*) realloc(idx, sizeof(int) * cap);
   dof = (int*) realloc(dof, sizeof(int) * cap);
   coef = (Scalar*) realloc(coef, sizeof(Scalar) * cap);
 }
extern "C" void CVC_GetConcreteModel(CVC_VC vc, long *psize,
                                     CVC_Exp **pvars, CVC_Exp **pvals)
{
  GetVC;
  try {
    CVC3::ExprMap<CVC3::Expr> model;
    cvc->getConcreteModel(model);

    // make sure we have room to store the model.
    unsigned needed = sizeof(CVC_Exp) * model.size();
    model_vars = (CVC_Exp*) realloc(model_vars, needed);
    model_vals = (CVC_Exp*) realloc(model_vals, needed);
    model_size = model.size();

    unsigned n = 0;
    CVC3::ExprMap<CVC3::Expr>::iterator it = model.begin();
    CVC3::ExprMap<CVC3::Expr>::iterator end = model.end();
    for (; it != end; it++) {
      model_vars[n] = ToExp(it->first);
      model_vals[n] = ToExp(it->second);
      n++;
    }
    Assert(n == model.size());

    *psize = model.size();
    *pvars = model_vars;
    *pvals = model_vals;
  } catch (CVC3::Exception ex) {
    ExceptionFail(ex);
  }
}
Example #28
0
// Resemblance of method Console.ReadLine() from C#
// USAGE: This function can virtually read everything from user input, no matter the length of it
char *ReadLine(size_t size, FILE *file) {
    char *str;
    int ch;
    size_t len = 0;
    str = (char*) malloc(sizeof (char)*size);
    if (str == NULL) {
        printf("\nUnable to allocate memory.\n");
        free(str);
        return NULL;
    }
    while ((ch = fgetc(file)) != '\n') {
        str[len++] = ch;
        if (len == size) {
            str = (char*) realloc(str, sizeof (char)*(size += 16));
            if (str == NULL) {
                printf("\nUnable to allocate memory.\n");
                free(str);
                return NULL;
            }
        }
    }
    str[len++] = '\0';

    return (char*) realloc(str, sizeof (char)*len);
}
Example #29
0
void dispatch_add(DispatchPtr table, int fd, short events, DispatchReadyFunc readyfn, DispatchErrorFunc errorfn, DispatchIndexFunc indexfn, void *arg) {
    if (table && fd >= 0) {
        log_debug("Adding %d to dispatch queue", fd);
        if (table->allocentries < table->numentries + 1) {
            table->allocentries += DISPATCH_ALLOC_INCREASE;
            table->entries = realloc(table->entries, sizeof(struct DispatchEntry) * table->allocentries);
            table->fds = realloc(table->fds, sizeof(struct pollfd) * table->allocentries);
        }
        if (table->entries && table->fds) {
            size_t index = table->numentries;
            table->entries[index].arg = arg;
            table->entries[index].readyfn = readyfn;
            table->entries[index].errorfn = errorfn;
            table->entries[index].indexfn = indexfn;
            table->fds[index].fd = fd;
            if (events == -1) {
                table->fds[index].events = POLLIN;
            } else {
                table->fds[index].events = events;
            }
            log_debug("Poll events for fd %d are: 0x%x", fd, table->fds[index].events);
            table->fds[index].revents = 0;
            if (table->entries[index].indexfn) {
                table->entries[index].indexfn(table->entries[index].arg, index);
            }
            table->numentries++;
        }
    }
}
SVECTOR *read_sparse_phi2(char *file_name, STRUCT_LEARN_PARM *sparm){
    
    int scanned;
    WORD *words = NULL;
    char feature_file[1000];
    sprintf(feature_file, "%s", file_name);
    FILE *fp = fopen(feature_file, "r");
    
    int length = 0;
    while(!feof(fp)){
        length++;
        words = (WORD *) realloc(words, length*sizeof(WORD));
        if(!words) die("Memory error."); 
        scanned = fscanf(fp, " %d:%f", &words[length-1].wnum, &words[length-1].weight);
        if(scanned < 2) {
            words[length-1].wnum = 0;
            words[length-1].weight = 0.0;
        }
    }
    fclose(fp);

    if(words[length-1].wnum) {
      length++;
      words = (WORD *) realloc(words,length*sizeof(WORD));
      words[length-1].wnum = 0;
      words[length-1].weight = 0.0;
    }

  SVECTOR *fvec = create_svector(words,"",1);
  free(words);

  return fvec;
}