libubx/ubx.c
static ubx_block_t *ubx_block_clone(ubx_block_t *prot, const char *name)
{
	int i;
	ubx_block_t *newb;
	ubx_port_t *srcport, *tgtport;
	ubx_config_t *srcconf, *tgtconf;

	newb = calloc(1, sizeof(ubx_block_t));

	if (newb == NULL)
		goto out;

	newb->block_state = BLOCK_STATE_PREINIT;

	/* copy attributes of prototype */
	newb->type = prot->type;
	newb->attrs = prot->attrs;

	newb->name = strdup(name);
	if (newb->name == NULL)
		goto out_free;
	newb->meta_data = strdup(prot->meta_data);
	if (newb->meta_data == NULL)
		goto out_free;
	newb->prototype = strdup(prot->name);
	if (newb->prototype == NULL)
		goto out_free;

	/* copy configuration */
	if (prot->configs) {
		i = get_num_configs(prot);

		newb->configs = calloc(i + 1, sizeof(ubx_config_t));
		if (newb->configs == NULL)
			goto out_free;

		for (srcconf = prot->configs, tgtconf = newb->configs;
		    srcconf->name != NULL; srcconf++, tgtconf++) {
			if (ubx_clone_config_data(tgtconf,
						  srcconf->name,
						  srcconf->doc,
						  srcconf->type,
						  srcconf->data_len) != 0)
				goto out_free;
			tgtconf->min = srcconf->min;
			tgtconf->max = srcconf->max;
			tgtconf->block = newb;
		}
	}


	/* do we have ports? */
	if (prot->ports) {
		i = get_num_ports(prot);

		newb->ports = calloc(i + 1, sizeof(ubx_port_t));
		if (newb->ports == NULL)
			goto out_free;

		for (srcport = prot->ports, tgtport = newb->ports;
		     srcport->name != NULL;
		     srcport++, tgtport++) {
			if (ubx_clone_port_data(tgtport, srcport->name, srcport->doc,
						srcport->in_type, srcport->in_data_len,
						srcport->out_type, srcport->out_data_len,
						srcport->state) != 0)
				goto out_free;
			tgtport->block = newb;
		}
	}

	/* ops */
	newb->init = prot->init;
	newb->start = prot->start;
	newb->stop = prot->stop;
	newb->cleanup = prot->cleanup;

	/* copy special ops */
	switch (prot->type) {
	case BLOCK_TYPE_COMPUTATION:
		newb->step = prot->step;
		newb->stat_num_steps = 0;
		break;
	case BLOCK_TYPE_INTERACTION:
		newb->read = prot->read;
		newb->write = prot->write;
		newb->stat_num_reads = 0;
		newb->stat_num_writes = 0;
		break;
	}

	/* all ok */
	return newb;

out_free:
	ubx_block_free(newb);
out:
	return NULL;
}
/**
 * ubx_block_clone - create a copy of an existing block from an existing one.
 *
 * @param prot prototype block which to clone
 * @param name name of new block
 *
 * @return a pointer to the newly allocated block. Must be freed with
 */
create a copy of an existing block from an existing one.
@param prot prototype block which to clone
@param name name of new block

@return a pointer to the newly allocated block. Must be freed with
create a copy of an existing block from an existing one . @param prot prototype block which to clone @param name name of new block @return a pointer to the newly allocated block . Must be freed with
static ubx_block_t *ubx_block_clone(ubx_block_t *prot, const char *name)
{
	int i;
	ubx_block_t *newb;
	ubx_port_t *srcport, *tgtport;
	ubx_config_t *srcconf, *tgtconf;
	newb = calloc(1, sizeof(ubx_block_t));
	if (newb == NULL)
		goto out;
	newb->block_state = BLOCK_STATE_PREINIT;
	newb->type = prot->type;
	newb->attrs = prot->attrs;
	newb->name = strdup(name);
	if (newb->name == NULL)
		goto out_free;
	newb->meta_data = strdup(prot->meta_data);
	if (newb->meta_data == NULL)
		goto out_free;
	newb->prototype = strdup(prot->name);
	if (newb->prototype == NULL)
		goto out_free;
	if (prot->configs) {
		i = get_num_configs(prot);
		newb->configs = calloc(i + 1, sizeof(ubx_config_t));
		if (newb->configs == NULL)
			goto out_free;
		for (srcconf = prot->configs, tgtconf = newb->configs;
		    srcconf->name != NULL; srcconf++, tgtconf++) {
			if (ubx_clone_config_data(tgtconf,
						  srcconf->name,
						  srcconf->doc,
						  srcconf->type,
						  srcconf->data_len) != 0)
				goto out_free;
			tgtconf->min = srcconf->min;
			tgtconf->max = srcconf->max;
			tgtconf->block = newb;
		}
	}
	if (prot->ports) {
		i = get_num_ports(prot);
		newb->ports = calloc(i + 1, sizeof(ubx_port_t));
		if (newb->ports == NULL)
			goto out_free;
		for (srcport = prot->ports, tgtport = newb->ports;
		     srcport->name != NULL;
		     srcport++, tgtport++) {
			if (ubx_clone_port_data(tgtport, srcport->name, srcport->doc,
						srcport->in_type, srcport->in_data_len,
						srcport->out_type, srcport->out_data_len,
						srcport->state) != 0)
				goto out_free;
			tgtport->block = newb;
		}
	}
	newb->init = prot->init;
	newb->start = prot->start;
	newb->stop = prot->stop;
	newb->cleanup = prot->cleanup;
	switch (prot->type) {
	case BLOCK_TYPE_COMPUTATION:
		newb->step = prot->step;
		newb->stat_num_steps = 0;
		break;
	case BLOCK_TYPE_INTERACTION:
		newb->read = prot->read;
		newb->write = prot->write;
		newb->stat_num_reads = 0;
		newb->stat_num_writes = 0;
		break;
	}
	return newb;
out_free:
	ubx_block_free(newb);
out:
	return NULL;
}
static ubx_block_t * ubx_block_clone ( ubx_block_t * prot , const char * name ) { int i ; ubx_block_t * newb ; ubx_port_t * srcport , * tgtport ; ubx_config_t * srcconf , * tgtconf ; newb = calloc ( 1 , sizeof ( ubx_block_t ) ) ; if ( newb == NULL ) goto out ; newb -> block_state = BLOCK_STATE_PREINIT ; newb -> type = prot -> type ; newb -> attrs = prot -> attrs ; newb -> name = strdup ( name ) ; if ( newb -> name == NULL ) goto out_free ; newb -> meta_data = strdup ( prot -> meta_data ) ; if ( newb -> meta_data == NULL ) goto out_free ; newb -> prototype = strdup ( prot -> name ) ; if ( newb -> prototype == NULL ) goto out_free ; if ( prot -> configs ) { i = get_num_configs ( prot ) ; newb -> configs = calloc ( i + 1 , sizeof ( ubx_config_t ) ) ; if ( newb -> configs == NULL ) goto out_free ; for ( srcconf = prot -> configs , tgtconf = newb -> configs ; srcconf -> name != NULL ; srcconf ++ , tgtconf ++ ) { if ( ubx_clone_config_data ( tgtconf , srcconf -> name , srcconf -> doc , srcconf -> type , srcconf -> data_len ) != 0 ) goto out_free ; tgtconf -> min = srcconf -> min ; tgtconf -> max = srcconf -> max ; tgtconf -> block = newb ; } } if ( prot -> ports ) { i = get_num_ports ( prot ) ; newb -> ports = calloc ( i + 1 , sizeof ( ubx_port_t ) ) ; if ( newb -> ports == NULL ) goto out_free ; for ( srcport = prot -> ports , tgtport = newb -> ports ; srcport -> name != NULL ; srcport ++ , tgtport ++ ) { if ( ubx_clone_port_data ( tgtport , srcport -> name , srcport -> doc , srcport -> in_type , srcport -> in_data_len , srcport -> out_type , srcport -> out_data_len , srcport -> state ) != 0 ) goto out_free ; tgtport -> block = newb ; } } newb -> init = prot -> init ; newb -> start = prot -> start ; newb -> stop = prot -> stop ; newb -> cleanup = prot -> cleanup ; switch ( prot -> type ) { case BLOCK_TYPE_COMPUTATION : newb -> step = prot -> step ; newb -> stat_num_steps = 0 ; break ; case BLOCK_TYPE_INTERACTION : newb -> read = prot -> read ; newb -> write = prot -> write ; newb -> stat_num_reads = 0 ; newb -> stat_num_writes = 0 ; break ; } return newb ; out_free : ubx_block_free ( newb ) ; out : return NULL ; }
ubx_block_clone - create a copy of an existing block from an existing one.
ubx_block_clone - create a copy of an existing block from an existing one .
/* copy attributes of prototype */ /* copy configuration */ /* do we have ports? */ /* ops */ /* copy special ops */ /* all ok */