Ejemplo n.º 1
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);
}
Ejemplo n.º 2
0
static void
tc3(struct ymsghandler *mh) {
	struct ythreadex *yt;
	struct targ *ta;
	struct tres *tr;
	void *retval;
	/* Normal successful thread */
	ta = ymalloc(sizeof(*ta));
	memset(ta, 0, sizeof(*ta));
	ta->s = ymalloc(16);
	ta->sleep_cnt = 10 + rand() % 20;
	ta->sleep_interval = 10;
	strcpy(ta->s, "ok:arg");
	yt = ythreadex_create("ok",
			      mh,
			      YTHREADEX_NORMAL,
			      &_lis,
			      ta,
			      &free_arg,
			      &free_result,
			      &thread_run);
	yassert(ythreadex_destroy(yt));
	yassert(!ythreadex_start(yt));
	yassert(!ythreadex_cancel(yt, FALSE));
	ythreadex_join(yt, &retval);
	yassert(YTHREADEX_TERMINATED_CANCELLED == ythreadex_get_state(yt));
	tr = ythreadex_get_result(yt);
	yassert(!tr);
	yassert(!ythreadex_destroy(yt));

}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
0
static void plotva(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;
	gp_plotarray( port, GP_PLOT, GP_TITLE "\"average adjf\"" GP_DOTS, NULL, y1, status.curgen );
	yfree( y1 );
}
Ejemplo n.º 5
0
void * yrealloc (mutabor_box_type * box, void * block, size_t newsize)

{
	void * help = ymalloc (box, newsize);
	memmove (help, block, newsize);
	return help;
}
Ejemplo n.º 6
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);
}
Ejemplo n.º 7
0
wchar_t
*makewstr(const char *handler, const char *variable)
{
    /* TODO: BUFSIZ, skal bli 254 */
    size_t newwcslen = BUFSIZ * sizeof(wchar_t) ;
	wchar_t *w = (wchar_t *) ymalloc(newwcslen,handler,variable);
    memset(w,0,newwcslen ) ;
	return w;
}
Ejemplo n.º 8
0
/* save a string in dynamically allocated memory.           */
char *
savestr(const char *str, const char *handler, const char *variable)
{
	char *s;
	s = (char *)ymalloc((strlen(str) + 1),handler,variable); 

	strcpy(s, str);
	return (s);
}
Ejemplo n.º 9
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" );
}
Ejemplo n.º 10
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 );
}
Ejemplo n.º 11
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;
}
Ejemplo n.º 12
0
wchar_t
*savewstr(const wchar_t * wstr,const char *handler, const char *variable)
{

	wchar_t *w;
    size_t newwcslen = (wcslen(wstr) + 1 ) * sizeof(wchar_t) ;

	w = (wchar_t *) ymalloc(newwcslen,handler,variable) ; 
	wcscpy(w, wstr);
	return (w);
}
Ejemplo n.º 13
0
static int
thread_run(struct ythreadex *threadex, void **result) {
	int i;
	struct tres *tr;
	struct targ *ta = ythreadex_get_arg(threadex);
	ylogv("thread run\n");
	*result = ymalloc(sizeof(*tr));
	tr = (struct tres *)*result;
	memset(tr, 0, sizeof(*tr));
	ythreadex_publish_progress_init(threadex, 100);
	for (i = 0; i < ta->sleep_cnt; i++)
		usleep(ta->sleep_interval * 1000);
	if (ta->retval)
		return ta->retval;
	memset(tr, 0, sizeof(*tr));
	tr->s = ymalloc(32);
	sprintf(tr->s, "res:%d", tr->r);
	*result = tr;
	return 0;
}
Ejemplo n.º 14
0
Archivo: log.c Proyecto: yhcting/ylib
static char *
get_buffer(void) {
	void *b = pthread_getspecific(_tkey);
	if (unlikely(!b)) {
		b = ymalloc(LOG_BUF_SZ);
		if (unlikely(!b))
			return NULL;
		pthread_setspecific(_tkey, b);
	}
	return b;
}
Ejemplo n.º 15
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;
}
Ejemplo n.º 16
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;
}
Ejemplo n.º 17
0
static void
tc2(struct ymsghandler *mh) {
	struct ythreadex *yt;
	struct targ *ta;
	struct tres *tr;
	void *retval;
	/* Normal successful thread */
	ta = ymalloc(sizeof(*ta));
	memset(ta, 0, sizeof(*ta));
	ta->s = ymalloc(16);
	strcpy(ta->s, "ok:arg");
	yt = ythreadex_create("ok",
			      mh,
			      YTHREADEX_NORMAL,
			      &_lis,
			      ta,
			      &free_arg,
			      &free_result,
			      &thread_run);
	yassert(ythreadex_destroy(yt));
	yassert(!ythreadex_start(yt));
	ythreadex_join(yt, &retval);
	tr = ythreadex_get_result(yt);
	/* STARTED, DONE. And PROGRESS_INIT */
	ylogv("%d, %d, %d, %d, %d, %c\n",
	      ta->sti,
	      ta->st[0],
	      ta->st[1],
	      ta->progcnt,
	      tr->r,
	      tr->s[0]);
	yassert(2 == ta->sti
		&& YTHREADEX_STARTED == ta->st[0]
		&& YTHREADEX_DONE == ta->st[1]
		&& 1000 == ta->progcnt
		&& 0 == tr->r
		&& 'r' == tr->s[0]);
	yassert(!ythreadex_destroy(yt));
}
Ejemplo n.º 18
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;
}
Ejemplo n.º 19
0
static _pit_children_info *
_add_child_info(_pit *parent, _pit *current)
{
    _pit_children_info *newci;

    // TODO: Optimize by moving to a freelist?
    newci = ymalloc(sizeof(_pit_children_info));
    newci->index = current->index;
    newci->callcount = 0;
    newci->nonrecursive_callcount = 0;
    newci->ttotal = 0;
    newci->tsubtotal = 0;
    newci->next = (struct _pit_children_info *)parent->children;
    parent->children = (_pit_children_info *)newci;

    return newci;
}
Ejemplo n.º 20
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;
}
Ejemplo n.º 21
0
BOOL
v2v_stream_attach(struct v2v_channel *channel, struct v2v_stream **stream)
{
    struct v2v_stream *work;

    *stream = NULL;

    work = ymalloc(sizeof(*work));
    if (!work)
        return FALSE;
    work->channel = channel;
    work->receive_event = v2v_get_receive_event(channel);
    work->send_event = v2v_get_send_event(channel);
    work->control_event = v2v_get_control_event(channel);
    work->recv_state.queue.tail = &work->recv_state.queue.head;

    *stream = work;
    return TRUE;
}
Ejemplo n.º 22
0
char *image(const char *buf, regmatch_t matches[], size_t nmatch, size_t off)
{
	const int i = 1;
	const char *prefix = "[-- Image: ";
	const char *postfix = " --]";
	size_t pr_len, po_len, len;
	char *match;

	pr_len = strlen(prefix);
	len = matches[i].rm_eo - matches[i].rm_so;
	po_len = strlen(prefix);

	match = ymalloc(pr_len + len + po_len + 1);
	memcpy(match, prefix, pr_len);
	memcpy(match + pr_len, buf + matches[i].rm_so + off, len);
	memcpy(match + pr_len + len, postfix, po_len);
	match[pr_len + len + po_len] = '\0' ;

	return match;
}
Ejemplo n.º 23
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;
}
Ejemplo n.º 24
0
static char *guess_encoding(void)
{
	char *enc;
	char *tmp;

	enc = ymalloc(20);
#ifdef WIN32
	snprintf(enc, 20, "CP%u", GetACP());
#else
	tmp = nl_langinfo(CODESET);
	strncpy(enc, tmp, 20);
#endif
	if(!enc) {
		fprintf(stderr, "warning: Could not detect console "
			"encoding. Assuming ISO-8859-1\n");
		strncpy(enc, "ISO-8859-1", 20);
	}

	return enc;
}
Ejemplo n.º 25
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);
}
Ejemplo n.º 26
0
Archivo: list.c Proyecto: yhcting/ylib
/**
 * 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);
}
Ejemplo n.º 27
0
static void
tc4(struct ymsghandler *mh) {
#define NR_THREADS 10
	int i;
	struct ythreadex *yt[NR_THREADS];
	struct targ *ta;
	struct tres *tr;
	void *retval[NR_THREADS];
	for (i = 0; i < NR_THREADS; i++) {
		/* Normal successful thread */
		ta = ymalloc(sizeof(*ta));
		memset(ta, 0, sizeof(*ta));
		/* sleep enough to get 'cancel' message after start */
		ta->sleep_cnt = 10 + rand() % 20;
		ta->sleep_interval = 10;
		ta->s = ymalloc(16);
		strcpy(ta->s, "ok:arg");
		yt[i] = ythreadex_create("ok",
		                         mh,
		                         YTHREADEX_NORMAL,
		                         &_lis,
		                         ta,
		                         &free_arg,
		                         &free_result,
		                         &thread_run);
	}

	for (i = 0; i < NR_THREADS; i++)
		yassert(!ythreadex_start(yt[i]));

	for (i = 0; i < NR_THREADS; i++) {
		if (i % 3)
			ythreadex_cancel(yt[i], i % 2);
	}
	for (i = 0; i < NR_THREADS; i++)
		ythreadex_join(yt[i], &retval[i]);

	for (i = 0; i < NR_THREADS; i++) {
		if (i % 3) {
			/* This is cancelled thread */
			ylogv("state: %d\n", ythreadex_get_state(yt[i]));
			yassert(YTHREADEX_TERMINATED_CANCELLED == ythreadex_get_state(yt[i]));
		} else {
			yassert(YTHREADEX_TERMINATED == ythreadex_get_state(yt[i]));
			tr = ythreadex_get_result(yt[i]);
			ta = ythreadex_get_arg(yt[i]);
			/* STARTED, DONE. And PROGRESS_INIT */
			ylogv("%d, %d, %d, %d, %d, %c\n",
			      ta->sti,
			      ta->st[0],
			      ta->st[1],
			      ta->progcnt,
			      tr->r,
			      tr->s[0]);
			yassert(2 == ta->sti
				&& YTHREADEX_STARTED == ta->st[0]
				&& YTHREADEX_DONE == ta->st[1]
				&& 1000 == ta->progcnt
				&& 0 == tr->r
				&& 'r' == tr->s[0]);
		}
		yassert(!ythreadex_destroy(yt[i]));
	}
#undef NR_THREADS
}
Ejemplo n.º 28
0
int main(int argc, const char **argv)
{
	struct stat st;
	iconv_t ic;
	STRBUF *wbuf;
	STRBUF *docbuf;
	STRBUF *outbuf;
	int i = 1;

	(void)setlocale(LC_ALL, "");

	while (argv[i]) {
		if (!strcmp(argv[i], "--raw")) {
			opt_raw = 1;
			i++; continue;
		} else if (!strcmp(argv[i], "--raw-input")) {
			opt_raw_input = 1;
			i++; continue;
		} else if (!strncmp(argv[i], "--encoding=", 11)) {
			size_t arglen = strlen(argv[i]) - 10;
#ifdef iconvlist
			if (!strcmp(argv[i] + 11, "list")) {
				show_iconvlist();
			}
#endif
			opt_encoding = ymalloc(arglen);
			memcpy(opt_encoding, argv[i] + 11, arglen);
			i++; continue;
		} else if (!strncmp(argv[i], "--width=", 8)) {
			opt_width = atoi(argv[i] + 8);
			if(opt_width < 3 && opt_width != -1) {
				fprintf(stderr, "Invalid value for width: %s\n",
					argv[i] + 8);
				exit(EXIT_FAILURE);
			}
			i++; continue;
		} else if (!strcmp(argv[i], "--force")) {
			// ignore this setting
			i++; continue;
		} else if (!strncmp(argv[i], "--output=", 9)) {
			if (*(argv[i] + 9) != '-') {
				size_t arglen = strlen(argv[i]) - 8;
				opt_output = ymalloc(arglen);
				memcpy(opt_output, argv[i] + 9, arglen);
			}
			i++; continue;
		} else if (!strncmp(argv[i], "--subst=", 8)) {
			if (!strcmp(argv[i] + 8, "none"))
				opt_subst = SUBST_NONE;
			else if (!strcmp(argv[i] + 8, "some"))
				opt_subst = SUBST_SOME;
			else if (!strcmp(argv[i] + 8, "all"))
				opt_subst = SUBST_ALL;
			else {
				fprintf(stderr, "Invalid value for --subst: %s\n",
					argv[i] + 8);
				exit(EXIT_FAILURE);
			}
			i++; continue;
		} else if (!strcmp(argv[i], "--help")) {
			usage();
		} else if (!strcmp(argv[i], "--version")
			   || !strcmp(argv[i], "-v")) {
			version_info();
		} else if (!strcmp(argv[i], "-")) {
			usage();
		} else {
			if(opt_filename)
				usage();
			opt_filename = argv[i];
			i++; continue;
		}
	}

	if(opt_encoding && !strcmp("show", opt_encoding)) {
		yfree(opt_encoding);
		opt_encoding = guess_encoding();
		printf("%s\n", opt_encoding);
		yfree(opt_encoding);
		exit(EXIT_SUCCESS);
	}

	if(opt_raw)
		opt_width = -1;

	if(!opt_filename)
		usage();

	if(!opt_encoding) {
		opt_encoding = guess_encoding();
	}

	ic = init_conv("UTF-8", opt_encoding);

	if (0 != stat(opt_filename, &st)) {
		fprintf(stderr, "%s: %s\n",
			opt_filename, strerror(errno));
		exit(EXIT_FAILURE);
	}

	/* read content.xml */
	docbuf = opt_raw_input ?
		read_from_xml(opt_filename, "content.xml") :
		read_from_zip(opt_filename, "content.xml");

	if (!opt_raw) {
		subst_doc(ic, docbuf);
		format_doc(docbuf, opt_raw_input);
	}

	wbuf = wrap(docbuf, opt_width);

	/* remove all trailing whitespace */
	(void) regex_subst(wbuf, " +\n", _REG_GLOBAL, "\n");

	outbuf = conv(ic, wbuf);

	if (opt_output)
		write_to_file(outbuf, opt_output);
	else
		fwrite(strbuf_get(outbuf), strbuf_len(outbuf), 1, stdout);

	finish_conv(ic);
	strbuf_free(wbuf);
	strbuf_free(docbuf);
	strbuf_free(outbuf);
#ifndef NO_ICONV
	yfree(opt_encoding);
#endif
	if (opt_output)
		yfree(opt_output);

	return EXIT_SUCCESS;
}
Ejemplo n.º 29
0
int
ystpr_distgraph(int fd,
		const double *vs,
		u32 vsz,
		u32 w,
		u32 h,
		u32 isp,
		char barc) {
	/*
	 * Comment text will be something like
	 *   "-2s", "-s", "-3s", "u", "s", "2s" ...
	 * And min/max value - print double value.
	 * So, 64 should be enough
	 */
#define _MAX_CMT_LEN 64
#define _PRSTR_ALL_SAME_VALUES "All values are same.\n"
#define _dist_index(v) (((v) - min) * (double)w / interval)
#define _get_dist_index(out, v)				\
	do {							\
		tmp = _dist_index(v);				\
		if (unlikely(tmp > w - 1))			\
			tmp = w - 1;				\
		(out) = tmp;					\
	} while (0);

	const double *d;
	double min, max, var, sigma, mean, interval, *dist;
	u32 tmp, r, i, j, sigminus, sigplus, nrcmt, *idxs;
	char **cmts;
	const double * const de = vs + vsz;

	/* verify input and calculate required values. */
	if (unlikely(r = stats_double(&min, &max, &mean, &var, vs, vsz)))
		return r;

	sigma = sqrt(var);
	/* all values are same. */
	if (min >= max) {
		/* ignore IO error. '- 1' to remove trailing 0 */
		r = write(fd,
			  _PRSTR_ALL_SAME_VALUES,
			  sizeof(_PRSTR_ALL_SAME_VALUES) - 1);
		return 0;
	}

	/* allocates memories required */
	if (unlikely(!(dist = (double *)ycalloc(w, sizeof(*dist)))))
		return -ENOMEM;

	/* calculate distribution */
	interval = max - min;
	for (d = vs; d < de; d++) {
		_get_dist_index(i, *d);
		dist[i]++;
	}

	/* calculate 'u - Ns' that can be shown at the graph */
	i = 0;
	while (0 <= _dist_index(mean - ++i * sigma));
	sigminus = i - 1;
	i = 0;
	while (w > _dist_index(mean + ++i * sigma));
	sigplus = i - 1;

	/* + 1 for 'mean', + 2 for min/max */
	nrcmt = sigminus + sigplus + 1 + 2;
	if (unlikely(!(cmts = ymalloc(sizeof(*cmts) * nrcmt)))) {
		yfree(dist);
		return -ENOMEM;
	}
	if (unlikely(!(idxs = ymalloc(sizeof(*idxs) * nrcmt)))) {
		yfree(dist);
		yfree(cmts);
		return -ENOMEM;
	}

	for (i = 0; i < nrcmt; i++) {
		cmts[i] = (char *)ymalloc(sizeof(*cmts[i])
					  * _MAX_CMT_LEN);
		if (unlikely(!cmts[i])) {
			for (j = 0; j < i; j++)
				yfree(cmts[j]);
			yfree(idxs);
			yfree(dist);
			return -ENOMEM;
		}
		cmts[i][_MAX_CMT_LEN - 1] = 0; /* trailing 0 */
	}

	j = 0;

	/* add comment for min */
	idxs[j] = 0;
	snprintf(cmts[j++], _MAX_CMT_LEN, "%f", min);
	for (i = sigminus; i > 0; i--) {
		_get_dist_index(idxs[j], mean - i * sigma);
		/* ignore return value intentionally */
		snprintf(cmts[j++], _MAX_CMT_LEN, "-%ds", i);
	}

	_get_dist_index(idxs[j], mean);
	snprintf(cmts[j++], _MAX_CMT_LEN, "u");
	for (i = 1; i <= sigplus; i++) {
		_get_dist_index(idxs[j], mean + i * sigma);
		/* ignore return value intentionally */
		snprintf(cmts[j++], _MAX_CMT_LEN, "%ds", i);
	}
	/* add comment for max */
	idxs[j] = w - 1;
	snprintf(cmts[j++], _MAX_CMT_LEN, "%f", max);

	r = ystpr_bargraph(fd,
			   dist,
			   w,
			   h,
			   idxs,
			   (const char * const *)cmts,
			   nrcmt,
			   isp,
			   barc);

	yfree(idxs);
	yfree(dist);
	for (i = 0; i < nrcmt; i++)
		yfree(cmts[i]);
	yfree(cmts);

	return r;

#undef _get_dist_index
#undef _dist_index
#undef _PRSTR_ALL_SAME_VALUES
#undef _MAX_CMT_LEN
}
Ejemplo n.º 30
0
static STRBUF *conv(iconv_t ic, STRBUF *buf)
{
	/* FIXME: This functionality belongs into strbuf.c */
	ICONV_CHAR *doc;
	char *out, *outbuf;
	size_t inleft, outleft = 0;
	size_t r;
	size_t outlen = 0;
	const size_t alloc_step = 4096;
	STRBUF *output;

	inleft = strbuf_len(buf);
	doc = (ICONV_CHAR*)strbuf_get(buf);
	outlen = alloc_step; outleft = alloc_step;
	outbuf = ymalloc(alloc_step);
	out = outbuf;
	outleft = alloc_step;

	do {
		if (!outleft) {
			outlen += alloc_step; outleft += alloc_step;
			yrealloc_buf(&outbuf, &out, outlen);
		}
		r = iconv(ic, &doc, &inleft, &out, &outleft);
		if (r == (size_t)-1) {
			if(errno == E2BIG) {
				outlen += alloc_step; outleft += alloc_step;
				if (outlen > (strbuf_len(buf) << 3)) {
					fprintf(stderr, "Buffer grew to much. "
						"Corrupted document?\n");
					exit(EXIT_FAILURE);
				}
				yrealloc_buf(&outbuf, &out, outlen);
				continue;
			} else if ((errno == EILSEQ) || (errno == EINVAL)) {
				char skip = 1;

				/* advance in source buffer */
				if ((unsigned char)*doc > 0x80)
					skip += utf8_length[(unsigned char)*doc - 0x80];
				doc += skip;
				inleft -= skip;

				/* advance in output buffer */
				*out = '?';
				out++;
				outleft--;

				continue;
			}
			fprintf(stderr, "iconv returned: %s\n", strerror(errno));
			exit(EXIT_FAILURE);
		}
	} while(inleft != 0);

	if (!outleft) {
		outbuf = yrealloc(outbuf, outlen + 1);
	}
	*out = '\0';

	output = strbuf_slurp_n(outbuf, (size_t)(out - outbuf));
	strbuf_setopt(output, STRBUF_NULLOK);
	return output;
}