Пример #1
0
static void
free_dummycb(void *arg) {
	if (arg) {
		struct dummy *dummy = (struct dummy *)arg;
		if (dummy->mem)
			yfree(dummy->mem);
		yfree(dummy);
	}
}
Пример #2
0
static void
free_arg(void *v) {
	struct targ *ta = (struct targ *)v;
	if (!ta)
		return;
	if (ta->s)
		yfree(ta->s);
	yfree(ta);
}
Пример #3
0
static void
free_result(void *v) {
	struct tres *tr = (struct tres *)v;
	if (!tr)
		return;
	if (tr->s)
		yfree(tr->s);
	yfree(tr);
}
Пример #4
0
void fifo_free ( fifo* q )
{
	fifo_segment* seg;
	while (q->head)
	{
		seg = q->head;
		q->head = q->head->next;
		yfree(seg);
	}
	yfree(q);
}
Пример #5
0
static int
shrink(struct ymempool *mp, int margin) {
	int from, i, j;
	/* start index of empty group */
	from = (mp->fbi - 1) / mp->grpsz + 1 + margin;
	if (from > mp->nrgrp)
		from = mp->nrgrp;
	for (i = from; i < mp->nrgrp; i++) {
		for (j = 0; j < mp->grpsz; j++)
			yfree(mp->fbp[i][j]);
		yfree(mp->fbp[i]);
	}
	mp->nrgrp = from;
	return 0;
}
Пример #6
0
Файл: log.c Проект: yhcting/ylib
/*
 * This function is used for testing and debugging.
 */
void
log_clear(void) {
	void *b = pthread_getspecific(_tkey);
	if (likely(b))
		yfree(b);
	pthread_setspecific(_tkey, NULL);
}
Пример #7
0
char *underline(char linechar, const char *str)
{
	size_t i;
	char *tmp;
	STRBUF *line;
	size_t charlen = charlen_utf8(str);

	if (str[0] == '\0') {
		tmp = ymalloc(1);
		tmp[0] = '\0';
		return tmp;
	}

	line = strbuf_new();
	strbuf_append(line, str);
	strbuf_append(line, "\n");

	tmp = ymalloc(charlen);
	for (i = 0; i < charlen; i++) {
		tmp[i] = linechar;
	}
	strbuf_append_n(line, tmp, charlen);
	yfree(tmp);

	strbuf_append(line, "\n\n");
	return strbuf_spit(line);
}
Пример #8
0
static void plotava(const char *line )
{
	int i;
	plotv *y1, *y2;

	y1 = ymalloc( status.curgen * sizeof(plotv) );
	y2 = ymalloc( status.curgen * sizeof(plotv) );
	for ( i = 0; i < status.curgen; i++ ) {
		y1[i] = run[i].aveadjf;
		y2[i] = run[i].varadjf;
	}
	gp_plotarray( port, GP_PLOT, GP_TITLE "\"average adjf\"" GP_DOTS, NULL, y1, status.curgen );
	gp_plotarray( port, GP_REPLOT, GP_TITLE "\"variance adjf\"" GP_DOTS, NULL, y2, status.curgen );
	yfree( y1 );
	yfree( y2 );
}
Пример #9
0
Файл: o.c Проект: yhcting/ylib
void
yodestroy(struct yo *yo) {
	struct o *o = (struct o *)yo;
        oclear(o);
	if (unlikely(!pool_put(o)))
                yfree(o);
}
Пример #10
0
Файл: o.c Проект: yhcting/ylib
static void
pool_clear(void) {
        struct o *o;
        while ((o = pool_get())) {
                oclear(o);
                yfree(o);
        }
}
Пример #11
0
static void print_regexp_err(int reg_errno, const regex_t *rx)
{
	char *buf = ymalloc(BUF_SZ);

	regerror(reg_errno, rx, buf, BUF_SZ);
	fprintf(stderr, "%s\n", buf);

	yfree(buf);
}
Пример #12
0
static void plotma(const char *line )
{
	int i;
	plotv *y;

	y = ymalloc( status.curgen * sizeof(plotv) );
	for ( i = 0; i < status.curgen; i++ ) y[i] = run[i].maxadjf;
	gp_plotarray( port, GP_PLOT, GP_TITLE "\"max adjf\"" GP_DOTS, NULL, y, status.curgen );
	yfree( y );
}
Пример #13
0
static void ploti(const char *line )
{
	int t, tt, g, n, h, S, E;
	plotv *y, *x, *dx, *dy;
	forecast r;

	if ( (n=sscanf( line, "%*s %d %d %d", &g, &S, &E ))!=EOF ) {
		if ( n == 1 ) {
			S = 0;
			E = datalen( DATA_TARGET );
		} else {
			S = max( 0, S );
			E = min( E, datalen( DATA_TARGET ) );
		}
		if ( 0 < g && g <= status.curgen ) {
			g--;
			n = E - S;
			x = ymalloc( status.length * n * sizeof(plotv) );
			y = ymalloc( status.length * n * sizeof(plotv) );
			dx = ymalloc( n * sizeof(plotv) );
			dy = ymalloc( n * sizeof(plotv) );
			tt = 0;
			for ( t = S; t < E; t++ ) {
				dy[t-S] = getdata( DATA_TARGET, t );
				dx[t-S] = t;
				r = evali( run[g].besti, t );
				for ( h = 0; h < status.length; h++ ) {
					x[tt] = t + h + status.horizon;
					y[tt] = r.v[h];
					tt++;
				}
			}
			gp_plotarray( port, GP_PLOT, GP_TITLE "\"data\"" GP_DOTS, dx, dy, n );
			gp_plotarray( port, GP_REPLOT, GP_TITLE "\"forecast\"" GP_DOTS, x, y, n * status.length );
			yfree( x );
			yfree( y );
			yfree( dx );
			yfree( dy );
		} else
			fprintf( stderr, "individual must lie in [1,%d]\n", status.curgen );
	} else
		p_error( "no individual" );
}
Пример #14
0
static void
destroy_msglooper(void *arg) {
	/* ignore all return value intentionally.
	 * We can't do anything for errors
	 */
	struct ymsglooper *ml = (struct ymsglooper *)arg;
	pthread_mutex_destroy(&ml->state_lock);
	ymsgq_destroy(ml->mq);
	yfree(ml);
}
Пример #15
0
static STRBUF *read_from_zip(const char *zipfile, const char *filename)
{
	int r = 0;
	STRBUF *content = NULL;

#ifdef HAVE_LIBZIP
	int zip_error;
	struct zip *zip = NULL;
	struct zip_stat stat;
	struct zip_file *unzipped = NULL;
	char *buf = NULL;

	if ( !(zip = zip_open(zipfile, 0, &zip_error)) ||
	     (r = zip_name_locate(zip, filename, 0)) < 0 ||
	     (zip_stat_index(zip, r, ZIP_FL_UNCHANGED, &stat) < 0) ||
	     !(unzipped = zip_fopen_index(zip, r, ZIP_FL_UNCHANGED)) ) {
		if (unzipped)
			zip_fclose(unzipped);
		if (zip)
			zip_close(zip);
		r = -1;
	}
#else
	r = kunzip_get_offset_by_name((char*)zipfile, (char*)filename, 3, -1);
#endif

	if(-1 == r) {
		fprintf(stderr,
			"Can't read from %s: Is it an OpenDocument Text?\n", zipfile);
		exit(EXIT_FAILURE);
	}

#ifdef HAVE_LIBZIP
	if ( !(buf = ymalloc(stat.size + 1)) ||
	     (zip_fread(unzipped, buf, stat.size) != stat.size) ||
	     !(content = strbuf_slurp_n(buf, stat.size)) ) {
		if (buf)
			yfree(buf);
		content = NULL;
	}
	zip_fclose(unzipped);
	zip_close(zip);
#else
	content = kunzip_next_tobuf((char*)zipfile, r);
#endif

	if (!content) {
		fprintf(stderr,
			"Can't extract %s from %s.  Maybe the file is corrupted?\n",
			filename, zipfile);
		exit(EXIT_FAILURE);
	}

	return content;
}
Пример #16
0
static int
_sgrow(_cstack * cs)
{
    int i;
    _cstack *dummy;

    dummy = screate(cs->size*2);
    if(!dummy)
        return 0;

    for(i=0; i<cs->size; i++) {
        dummy->_items[i].ckey = cs->_items[i].ckey;
        dummy->_items[i].t0 = cs->_items[i].t0;
    }
    yfree(cs->_items);
    cs->_items = dummy->_items;
    cs->size = dummy->size;
    yfree(dummy);
    return 1;
}
Пример #17
0
/*
 * expand memory pool by 1 group
 */
static int
expand(struct ymempool *mp) {
	int i;
	struct blk ***newfbp;

	if (unlikely(!(newfbp = ymalloc(sizeof(*newfbp) * (mp->nrgrp + 1)))))
		return -ENOMEM;

	/* allocate new fbp group */
	newfbp[mp->nrgrp] = ycalloc(1, sizeof(**newfbp) * mp->grpsz);
	if (unlikely(!newfbp[mp->nrgrp])) {
		yfree(newfbp);
		return -ENOMEM;
	}
	/* now all are NULL */

	/* initialize fbp & block group */
	for (i = 0; i < mp->grpsz; i++) {
		newfbp[mp->nrgrp][i]
			= (struct blk *)ymalloc(blksz(mp));
		if (unlikely(!newfbp[mp->nrgrp][i]))
			goto nomem_blkgrp;
		newfbp[mp->nrgrp][i]->i = sz(mp) + i;
	}

	/* keep previous values */
	memcpy(newfbp, mp->fbp, mp->nrgrp * sizeof(*newfbp));

	/* update mp structure */
	yfree(mp->fbp);
	mp->fbp = newfbp;
	mp->nrgrp++;
	return 0;

 nomem_blkgrp:
	for (i = 0; i < mp->grpsz; i++) {
		if (newfbp[mp->nrgrp][i])
			yfree(newfbp[mp->nrgrp][i]);
	}
	return -ENOMEM;
}
Пример #18
0
struct ymempool *
ymempool_create(int grpsz, int elemsz, int opt) {
	struct ymempool *mp;

	yassert(grpsz > 0 && elemsz > 0);
	if (unlikely(!(mp = ycalloc(1, sizeof(*mp)))))
		return NULL;

	if (unlikely(!(mp->fbp = ymalloc(sizeof(*mp->fbp)))))
		goto nomem;

#ifndef CONFIG_MEMPOOL_DYNAMIC
	if (unlikely(!(mp->grp = ymalloc(sizeof(*mp->grp)))))
		goto nomem;
#endif

	mp->grpsz = grpsz;
	mp->nrgrp = 0;
	mp->esz = elemsz;
	mp->fbi = 0;
	mp->opt = opt;
	init_lock(mp);

	/* allocate 1-block-group for initial state */
	if (expand(mp))
		goto nomem;

	return mp;


 nomem:
#ifndef CONFIG_MEMPOOL_DYNAMIC
	if (mp->grp)
		yfree(mp->grp);
#endif
	if (mp->fbp)
		yfree(mp->fbp);
	yfree(mp);

	return NULL;
}
Пример #19
0
static INLINE void
free_node(struct node *n, void(*vfree)(void *), int recursive) {
	if (recursive) {
		register int i;
		for (i = 0; i < 16; i++)
			if (n->n[i])
				free_node(n->n[i], vfree, TRUE);
	}
	if (n->v && vfree)
		(*vfree)(n->v);
	yfree(n);
}
Пример #20
0
/*
 * expand memory pool by 1 group
 */
static int
expand(struct ymempool *mp) {
	int i;
	u8 **newgrp;
	struct blk ***newfbp;
	int bsz;

	/* pre-calulate frequently used value */
	bsz = blksz(mp);

	newgrp = ymalloc(sizeof(*newgrp) * (mp->nrgrp + 1));
	newfbp = ymalloc(sizeof(*newfbp) * (mp->nrgrp + 1));
	if (unlikely(!newgrp || !newfbp))
		goto nomem_expand;

	/* allocate new fbp group */
	newfbp[mp->nrgrp] = ymalloc(sizeof(**newfbp) * mp->grpsz);
	/* allocate new block group */
	newgrp[mp->nrgrp] = ymalloc(sizeof(**newgrp)
				    * mp->grpsz
				    * bsz);
	if (unlikely(!newfbp[mp->nrgrp]
		     || !newgrp[mp->nrgrp]))
		goto nomem_newgrp;

	/* initialize fbp & block group */
	for (i = 0; i < mp->grpsz; i++) {
		newfbp[mp->nrgrp][i]
			= (struct blk *)(newgrp[mp->nrgrp] + i * bsz);
		newfbp[mp->nrgrp][i]->i = sz(mp) + i;
	}

	/* keep previous values */
	memcpy(newgrp, mp->grp, mp->nrgrp * sizeof(*newgrp));
	memcpy(newfbp, mp->fbp, mp->nrgrp * sizeof(*newfbp));

	/* update mp structure */
	yfree(mp->grp);
	yfree(mp->fbp);
	mp->grp = newgrp;
	mp->fbp = newfbp;
	mp->nrgrp++;

	return 0;

 nomem_newgrp:
	if (newfbp[mp->nrgrp])
		yfree(newfbp[mp->nrgrp]);
	if (newgrp[mp->nrgrp])
		yfree(newgrp[mp->nrgrp]);

 nomem_expand:
	if (newgrp)
		yfree(newgrp);
	if (newfbp)
		yfree(newfbp);

	return -ENOMEM;
}
Пример #21
0
static void yarn_launcher ( yarn_launch_data* ld )
{
	TTDGET();
	yarn* active_yarn = ld->active_yarn;
	void (*routine)(void*) = ld->routine;
	void* udata = ld->udata;
	// basically a bootstrap routine for each yarn which runs the routine then cleans up
	yfree(ld);
	routine(udata);
	DEBUG("yarn %p completed, yarn_context_set\n", active_yarn);
	TTD.runtime = UNSCHEDULE;
	PTABLE(active_yarn->pid) = 0;
	yarn_context_set(&(TTD.sched_context));
}
Пример #22
0
// the pit will be cleared by the relevant freelist. we do not free it here.
// we only DECREF the CodeObject or the MethodDescriptive string.
static void
_del_pit(_pit *pit)
{
    _pit_children_info *it,*next;
    it = pit->children;
    while(it) {
        next = (_pit_children_info *)it->next;
        yfree(it);
        it = next;
    }
    pit->children = NULL;
    Py_CLEAR(pit->name);
    Py_CLEAR(pit->modname);
}
Пример #23
0
unsigned long fifo_dequeue ( fifo* q )
{
	unsigned long val;
	fifo_segment* seg;
	if (q->length == 0) return 0;
	val = q->head->elements[q->head->index++];
	if (q->head->index == FIFO_ELEMENTS_PER_SEGMENT)
	{
		seg = q->head;
		q->head = q->head->next;
		yfree(seg);
		if (!q->head) q->tail = 0;
	}
	q->length--;
	return val;
}
Пример #24
0
void
v2v_stream_detach(const struct v2v_stream *_stream)
{
    struct v2v_stream *stream = (struct v2v_stream *)_stream;
    struct queued_message *qm;

    if (!stream)
        return;

    while (stream->recv_state.queue.head) {
        qm = stream->recv_state.queue.head;
        stream->recv_state.queue.head = qm->next;
        HeapFree(GetProcessHeap(), 0, qm);
    }
    yfree(stream);
}
Пример #25
0
static char *headline(char line, const char *buf, regmatch_t matches[],
		      size_t nmatch, size_t off)
{
	const int i = 1;
	char *result;
	size_t len;
	char *match;

	len = matches[i].rm_eo - matches[i].rm_so;
	match = ymalloc(len + 1);

	memcpy(match, buf + matches[i].rm_so + off, len);
	match[len] = '\0' ;

	result = underline(line, match);

	yfree(match);
	return result;
}
Пример #26
0
void
ymempool_destroy(struct ymempool *mp) {
	int i;
	destroy_lock(mp);
#ifdef CONFIG_MEMPOOL_DYNAMIC
	for (i = 0; i < mp->nrgrp; i++) {
		int j;
		for (j = 0; j < mp->grpsz; j++)
			yfree(mp->fbp[i][j]);
		yfree(mp->fbp[i]);
	}
	yfree(mp->fbp);
#else /* CONFIG_MEMPOOL_DYNAMIC */
	for (i = 0; i < mp->nrgrp; i++) {
		yfree(mp->fbp[i]);
		yfree(mp->grp[i]);
	}
	yfree(mp->fbp);
	yfree(mp->grp);
#endif /* CONFIG_MEMPOOL_DYNAMIC */
	yfree(mp);
}
Пример #27
0
static struct ymsglooper *
create_msglooper(pthread_t thread, int msgq_capacity) {
	struct ymsglooper *ml = ymalloc(sizeof(*ml));
	if (unlikely(!ml))
		return NULL;
	ml->mq = ymsgq_create(msgq_capacity);
	if (unlikely(!ml->mq))
		goto free_ml;
	yassert(thread); /* NOT NULL */
	ml->thread = thread;
	if (unlikely(pthread_mutex_init(&ml->state_lock, NULL)))
		goto free_mq;
	return ml;

 free_mq:
	ymsgq_destroy(ml->mq);
 free_ml:
	yfree(ml);
	return NULL;
}
Пример #28
0
static void subst_doc(iconv_t ic, STRBUF *buf)
{
	struct subst *s = substs;
	ICONV_CHAR *in;
	size_t inleft;
	const size_t outbuf_sz = 20;
	char *outbuf;
	char *out;
	size_t outleft;
	size_t r;

	if (opt_subst == SUBST_NONE)
		return;

	outbuf = ymalloc(outbuf_sz);
	while (s->unicode) {
		if (opt_subst == SUBST_ALL) {
			RS_G(s->utf8, s->ascii);
		} else {
			out = outbuf;
			outleft = outbuf_sz;
			in = (ICONV_CHAR*)s->utf8;
			inleft = strlen(in);
			r = iconv(ic, &in, &inleft, &out, &outleft);
			if (r == (size_t)-1) {
				if ((errno == EILSEQ) || (errno == EINVAL)) {
					RS_G(s->utf8, s->ascii);
				} else {
					fprintf(stderr,
						"iconv returned an unexpected error: %s\n",
						strerror(errno));
					exit(EXIT_FAILURE);
				}
			}
		}
		s++;
	}
	yfree(outbuf);
}
Пример #29
0
_cstack *
screate(int size)
{
    int i;
    _cstack *cs;

    cs = (_cstack *)ymalloc(sizeof(_cstack));
    if (!cs)
        return NULL;
    cs->_items = ymalloc(size * sizeof(_cstackitem));
    if (cs->_items == NULL) {
        yfree(cs);
        return NULL;
    }

    for(i=0; i<size; i++) {
        cs->_items[i].ckey = 0;
        cs->_items[i].t0 = 0;
    }

    cs->size = size;
    cs->head = -1;
    return cs;
}
Пример #30
0
/**
 * Linked list test.
 */
static void
test_list(void) {
	int i;
	int *p;
	struct ylist *lst;
	struct ylisti *itr;

	lst = ylist_create(0, &yfree);
	ylist_destroy(lst);

	lst = ylist_create(0, &yfree);
	p = (int *)ymalloc(sizeof(*p));
	*p = 3;
	ylist_add_last(lst, p);

	itr = ylisti_create(lst, YLISTI_FORWARD);
	yassert(ylisti_has_next(itr));
	p = (int *)ylisti_next(itr);
	yassert(3 == *p);
	yassert(!ylisti_has_next(itr));
	ylisti_destroy(itr);

	itr = ylisti_create(lst, YLISTI_FORWARD);
	yassert(ylisti_has_next(itr));
	ylisti_next(itr);
	ylist_remove_current(lst, itr, 1);
	yassert(0 == ylist_size(lst));
	ylisti_destroy(itr);

	for (i = 0; i < 10; i++) {
		p = (int *)ymalloc(sizeof(*p));
		*p = i;
		ylist_add_last(lst, p);
	}
	yassert(10 == ylist_size(lst));
	yassert(ylist_has(lst, p));
	yassert(!ylist_has(lst, lst));

	/* simple iteration */
	i = 0;
	itr = ylisti_create(lst, YLISTI_FORWARD);
	while (ylisti_has_next(itr)) {
		p = ylisti_next(itr);
		yassert(ylist_has(lst, p));
		yassert(i == *p);
		i++;
	}
	ylisti_destroy(itr);

	i = 9;
	itr = ylisti_create(lst, YLISTI_BACKWARD);
	while (ylisti_has_next(itr)) {
		p = ylisti_next(itr);
		yassert(i == *p);
		i--;
	}
	ylisti_destroy(itr);

	/* remove odd numbers - tail is removed. */
	itr = ylisti_create(lst, YLISTI_FORWARD);
	while (ylisti_has_next(itr)) {
		p = ylisti_next(itr);
		if (*p % 2)
			ylist_remove_current(lst, itr, 1);
	}
	ylisti_destroy(itr);

	i = 0;
	itr = ylisti_create(lst, YLISTI_FORWARD);
	while (ylisti_has_next(itr)) {
		p = ylisti_next(itr);
		yassert(i == *p);
		i += 2;
	}
	ylisti_destroy(itr);
	ylist_destroy(lst);


	lst = ylist_create(0, &yfree);
	/* remove even numbers - head is removed. */
	for (i = 0; i < 10; i++) {
		p = (int *)ymalloc(sizeof(*p));
		*p = i;
		ylist_add_last(lst, p);
	}

	itr = ylisti_create(lst, YLISTI_FORWARD);
	while (ylisti_has_next(itr)) {
		p = ylisti_next(itr);
		if (!(*p % 2))
			ylist_remove_current(lst, itr, 1);
	}
	ylisti_destroy(itr);

	i = 1;
	itr = ylisti_create(lst, YLISTI_FORWARD);
	while (ylisti_has_next(itr)) {
		p = ylisti_next(itr);
		yassert(i == *p);
		i += 2;
	}
	ylisti_destroy(itr);
	ylist_destroy(lst);

	{ /* Just Scope */
		struct dummy *dum;
		lst = ylist_create(0, free_dummycb);
		for (i = 0; i < 10; i++) {
			dum = (struct dummy *)ymalloc(sizeof(*dum));
			dum->id = i;
			dum->mem = (int *)ymalloc(sizeof(*dum->mem));
			*(dum->mem) = i;
			ylist_add_last(lst, dum);
		}
		yassert(10 == ylist_size(lst));

		itr = ylisti_create(lst, YLISTI_FORWARD);
		while (ylisti_has_next(itr)) {
			dum = ylisti_next(itr);
			if (5 == dum->id)
				ylist_remove_current(lst, itr, 1);
		}
		ylisti_destroy(itr);
		yassert(9 == ylist_size(lst));

		itr = ylisti_create(lst, YLISTI_FORWARD);
		while (ylisti_has_next(itr)) {
			dum = ylisti_next(itr);
			yassert(5 != dum->id);
		}
		ylisti_destroy(itr);
		ylist_destroy(lst);
	}

	lst = ylist_create(1, &yfree);
	p = (int *)ymalloc(sizeof(*p));
	*p = 0;
	ylist_add_last(lst, p);
	yassert(1 == ylist_size(lst));

	p = (int *)ymalloc(sizeof(*p));
	*p = 1;
	yassert(ylist_add_last(lst, p));
	yassert(1 == ylist_size(lst));
	yfree(p); /* p is fail to insert to the list */

	p = (int *)ylist_peek_last(lst);
	yassert(0 == *p && 1 == ylist_size(lst));
	p = ylist_remove_last(lst, FALSE);
	yassert(ylist_is_empty(lst));
	yfree(p);

	ylist_destroy(lst);
}