Esempio n. 1
0
/*!
 * Free memory chunk
 * \param mpool Memory pool to be used (if NULL default pool is used)
 * \param chunk Chunk location (starting address)
 * \return 0 if successful, -1 otherwise
 */
int ffs_free ( ffs_mpool_t *mpool, void *chunk_to_be_freed )
{
	ffs_hdr_t *chunk, *before, *after;

	ASSERT ( mpool && chunk_to_be_freed );

	chunk = chunk_to_be_freed - sizeof (size_t);
	MARK_FREE ( chunk ); /* mark it as free */

	/* join with left? */
	before = ( (void *) chunk ) - sizeof(size_t);
	if ( CHECK_FREE ( before ) )
	{
		before = GET_HDR ( before );
		ffs_remove_chunk ( mpool, before );
		before->size += chunk->size; /* join */
		chunk = before;
	}

	/* join with right? */
	after = GET_AFTER ( chunk );
	if ( CHECK_FREE ( after ) )
	{
		ffs_remove_chunk ( mpool, after );
		chunk->size += after->size; /* join */
	}

	/* insert chunk in free list */
	ffs_insert_chunk ( mpool, chunk );

	/* set chunk tail */
	CLONE_SIZE_TO_TAIL ( chunk );

	return 0;
}
Esempio n. 2
0
int DestroyFilterI(FILTER *fpPO)
{
    VALIDATE(fpPO,FILTER_ID);
    CHECK_FILE(fpPO->in);
    CHECK_NFILE(fpPO->out,fpPO->outname);
    CHECK_FREE(fpPO->mask);
    CHECK_FREE(fpPO->blk_mask);
    CHECK_WORDLIST(fpPO->wlis);
    FREE(fpPO);
    return(TRUE);
}
Esempio n. 3
0
/*
 * Nakon oslobađanja bloka radi se sortiranje liste tako da je na početku najmanji blok, 
 * a na kraju najveći blok.
 * Blok se dodaje listu koja se:
 *				1. blok spaja sa susjedima ukoliko su slobodni
 *				2. sortira listu
*/
int bf_free ( bf_mpool_t *mpool, void *chunk_to_be_freed )
{
	bf_hdr_t *chunk, *before, *after;

	ASSERT ( mpool && chunk_to_be_freed );

	chunk = chunk_to_be_freed - sizeof (size_t);
	MARK_FREE ( chunk ); /* mark it as free */

	/* join with left? */
	before = ( (void *) chunk ) - sizeof(size_t);
	if ( CHECK_FREE ( before ) )
	{
		before = GET_HDR ( before );
		bf_remove_chunk ( mpool, before );
		before->size += chunk->size; /* join */
		chunk = before;
	}

	/* join with right? */
	after = GET_AFTER ( chunk );
	if ( CHECK_FREE ( after ) )
	{
		bf_remove_chunk ( mpool, after );
		chunk->size += after->size; /* join */
	}

	/* insert chunk in free list */
	//dodaje slobodan blok u listu i sortira
	bf_insert_chunk ( mpool, chunk );

	/* set chunk tail */
	CLONE_SIZE_TO_TAIL ( chunk );
	
	bf_hdr_t *iter;
	iter = mpool->first;
	print("Ispis slobodnih blokova:\n");
	while( iter != NULL )
	{
	    print("%d -> ", iter->size);
	    iter = iter->next;
	}
	print("\n");

	return 0;
}
Esempio n. 4
0
/************************************************************************
*   Set random mask; Handles lines and blocks
*/
int SetRandFilterMaskingI(FILTER *filtPO, DOUB fracD)
{
    int i, line;
    char *tmaskPC;

    LIMIT_NUM(fracD, 0.0, 1.0);
    if( filtPO->l_blk > 1) {
        tmaskPC = (char *)ALLOC(filtPO->n_block, sizeof(char));
        MaskRandSubsetI(tmaskPC, filtPO->n_block, RNUM(fracD));
        for(i=0; i<filtPO->n_block; i++)
        {
            line = filtPO->l_bof + (filtPO->l_blk * i);
            BOG_CHECK(line >= filtPO->n_mask);
            if(tmaskPC[i]) {
                filtPO->mask[line] = 1;
            }
        }
        CHECK_FREE(tmaskPC);
    }
    else {
        MaskRandSubsetI(filtPO->mask, filtPO->n_mask, RNUM(fracD));
    }
    return(TRUE);
}
Esempio n. 5
0
static int 
l_esc_read (GPPort *p, unsigned char *c)
{
	CHECK_NULL (p && c);

	CHECK (gp_port_read (p, c, 1));

	/*
	 * STX, ETX, ENQ, ACK, XOFF, XON, NACK, and ETB have to be masked by
	 * ESC. If we receive one of those (except ETX and ETB) without mask,
	 * we will not report an error, as it will be recovered automatically
	 * later. If we receive ETX or ETB, we reached the end of the packet
	 * and report a transmission error, so that the error can be
	 * recovered.
	 * If the camera sends us ESC (the mask), we will not count this byte
	 * and read a second one. This will be reverted and processed. It
	 * then must be one of STX, ETX, ENQ, ACK, XOFF, XON, NACK, ETB, or
	 * ESC. As before, if it is not one of those, we'll not report an
	 * error, as it will be recovered automatically later.
	 */
	 /* The HP PhotoSmart does not escape every special code, only
	  * some and it gets confused if we do this checks. By relaxing
	  * these, it now downloads images etc.
	  */
#ifndef LESSER_ESCAPES
	if ((*c == STX ) || (*c == ETX) || (*c == ENQ ) || (*c == ACK) ||
	    (*c == XOFF) || (*c == XON) || (*c == NACK) || (*c == ETB)) {
#else /* LESSER_ESCAPES */
	if ((*c == STX ) || (*c == XOFF) || (*c == XON)) {
#endif /* LESSER_ESCAPES */
		GP_DEBUG ("Wrong ESC masking!");
		if ((*c == ETX) || (*c == ETB))
			return (GP_ERROR_CORRUPTED_DATA);
	} else if (*c == ESC) {
		CHECK (gp_port_read (p, c, 1));
		*c = (~*c & 0xff);
		if ((*c != STX ) && (*c != ETX ) && (*c != ENQ) &&
		    (*c != ACK ) && (*c != XOFF) && (*c != XON) &&
		    (*c != NACK) && (*c != ETB ) && (*c != ESC))
			GP_DEBUG ("Wrong ESC masking!");
	}
	return (GP_OK);
}


static int
l_send (GPPort *p, GPContext *context, unsigned char *send_buffer,
	unsigned int send_buffer_size)
{
	unsigned char c;
	unsigned char checksum;
	/**********************************************************************/
	/*  sb: A pointer to the buffer that we will send.                    */
	/* sbs: Its size.		                                      */
	/**********************************************************************/
	unsigned char *sb; 	
	unsigned int sbs;
	int i = 0;

	CHECK_NULL (p && send_buffer);

	/* We need to ping the camera first */
	CHECK (l_ping (p, context));

	/********************************************************/
	/* We will write:			 		*/
	/*  - STX						*/
	/*  - low order byte of (send_buffer_size + 5)		*/
	/*  - high order byte of (send_buffer_size + 5)		*/
	/*  - 'send_buffer_size' bytes data plus ESC quotes	*/
	/*  - ETX						*/
	/*  - 1 byte for checksum plus ESC quotes		*/
	/*							*/
	/* The checksum covers all bytes after STX and before	*/
	/* the checksum byte(s).				*/
	/********************************************************/
	sbs = send_buffer_size + 5;
	sb = malloc (sizeof (char) * sbs);
	sb[0] = STX;
	sb[1] = send_buffer_size;
	sb[2] = send_buffer_size >> 8;
	checksum  = sb[1];
	checksum += sb[2];
	for (i = 3; i < (sbs - 2); i++) {
		checksum += *send_buffer;
		if (	(*send_buffer == STX ) || 
			(*send_buffer == ETX ) ||
			(*send_buffer == ENQ ) ||
			(*send_buffer == ACK ) ||	
			(*send_buffer == XOFF) ||
			(*send_buffer == XON ) ||
			(*send_buffer == NACK) ||
			(*send_buffer == ETB ) ||
			(*send_buffer == ESC )) {
			sb = realloc (sb, ++sbs * sizeof (char));
			sb[  i] = ESC;
			sb[++i] = ~*send_buffer;
		} else  sb[  i] =  *send_buffer;
		send_buffer++;
	}
	sb[sbs - 2] = ETX;
	checksum += ETX;
	if (	(checksum == STX ) || 
		(checksum == ETX ) || 
		(checksum == ENQ ) || 
		(checksum == ACK ) || 
		(checksum == XOFF) ||
		(checksum == XON ) || 
		(checksum == NACK) || 
		(checksum == ETB ) ||
		(checksum == ESC )) {
		sb = realloc (sb, ++sbs * sizeof (char));
		sb[sbs - 2] = ESC;
		sb[sbs - 1] = ~checksum;
	} else  sb[sbs - 1] =  checksum;
	for (i = 0; ; i++) {

		/* Write data as above.	*/
		CHECK_FREE (gp_port_write (p, sb, sbs), sb);
		CHECK_FREE (gp_port_read (p, &c, 1), sb);
		switch (c) {
		case ACK:

			/* ACK received. We can proceed. */
			free (sb);

			/* Write EOT.	*/
			c = EOT;
			CHECK (gp_port_write (p, &c, 1));
			return (GP_OK);

		case NACK:

			/* NACK received. We'll try up to three times. */
			if (i == 2) {
				free (sb);
				return (GP_ERROR_CORRUPTED_DATA);
			} else break;
		default:

			/* Should not happen. */
			return (GP_ERROR_CORRUPTED_DATA);
		}
	}
}
Esempio n. 6
0
/**************************************************************************
*   Rank correlation coefficient
*   ALLOCATES AND FREES TEMP ARRAYS HERE
*/
DOUB RankCorD(void *aPO, void *bPO, int n, int ct)
{
    int i, *iPI,*jPI;
    REAL *iPR,*jPR;
    DOUB rD,*iPD,*jPD;
    DOUB *rank1PD,*rank2PD;
    SCOREC *sc1PO, *sc2PO;
    
    DB_STAT DB_PrI(">> RankCorD, n=%d, ct=%d\n",n,ct);
    /***
    *   Attempt to allocate arrays
    */
    sc1PO = CreateScorecsPO(n);
    sc2PO = CreateScorecsPO(n);
    rank1PD = (DOUB *)ALLOC(n,sizeof(DOUB));
    rank2PD = (DOUB *)ALLOC(n,sizeof(DOUB));
    if( (!sc1PO) || (!sc1PO) || (!rank1PD) || (!rank2PD) )
    {
        printf("Problem allocating temp space for rank correlations!\n");
        CHECK_FREE(sc1PO); CHECK_FREE(sc2PO);
        CHECK_FREE(rank1PD); CHECK_FREE(rank2PD);
        return(-TOO_BIG_R);
    }
    /***
    *   Copy values from passed arrays SCORECs for sorting
    */
    switch(ct)
    {
        case IS_INT:
            iPI = (int *)aPO;
            jPI = (int *)bPO;
            for(i=0;i<n;i++)
            {
                sc1PO[i].sc = DNUM(iPI[i]);
                sc2PO[i].sc = DNUM(jPI[i]);
            }
            break;
        case IS_REAL:
            iPR = (REAL *)aPO;
            jPR = (REAL *)bPO;
            for(i=0;i<n;i++)
            {
                sc1PO[i].sc = DNUM(iPR[i]);
                sc2PO[i].sc = DNUM(jPR[i]);
            }
            break;
        case IS_DOUB:
            iPD = (DOUB *)aPO;
            jPD = (DOUB *)bPO;
            for(i=0;i<n;i++)
            {
                sc1PO[i].sc = iPD[i];
                sc2PO[i].sc = jPD[i];
            }
            break;
        default:
            CHECK_FREE(sc1PO); CHECK_FREE(sc2PO);
            CHECK_FREE(rank1PD); CHECK_FREE(rank2PD);
            printf("Bogus ct code=%d\n",ct);    
            ERR("RankCorD","Bogus ct code");
            return(-TOO_BIG_R);
    }
    /***
    *   Sort arrays and get rank arrays
    */
    SortScorecVals(sc1PO,n,SORT_ASCEND);
    SortScorecVals(sc2PO,n,SORT_ASCEND);
    FillScorecRankArrayI(sc1PO,n,rank1PD);
    FillScorecRankArrayI(sc2PO,n,rank2PD);
    /***
    *   Now get normal coorelation on rank arrays
    */
    rD = PearsonsCorD(rank1PD,rank2PD,n,IS_DOUB);
    /***
    *   Clean up and bail
    */
    CHECK_FREE(sc1PO); CHECK_FREE(sc2PO);
    CHECK_FREE(rank1PD); CHECK_FREE(rank2PD);
    return(rD);
}
Esempio n. 7
0
/* open communication with mainframe under logical number 'id',
   do all necessary initialization and start thread */
int
sy1527Start(unsigned id_nowused, char *ip_address)
{
    int id; ///my:
    char arg[30], userName[20], passwd[30], name[MAX_CAEN_NAME];
    int link, ret;

    pthread_mutex_lock(&global_mutex);

    /* do global initialization on first call */
    if(nmainframes == -1) sy1527Init();

    nmainframes++;///my:
    ///id=nmainframes-1;///my:

    ///if((id+1)>nmainframes)nmainframes=id+1;
    id=id_nowused;///nmainframes-1;///my:

    printf("++++++++++++++++++++++++++++++++++++++++++++++++ nmainframes=%d id=%d\n", nmainframes, id);

    /* lock global mutex to prevent other mainframes to be started
       until we are done with this one */
    /// my: move upward - pthread_mutex_lock(&global_mutex); otherwise many sy1527Init() are possible

    if(nmainframes >= MAX_HVPS)
    {
        printf("ERROR: no more empty slots\n");
        pthread_mutex_unlock(&global_mutex);
        return(CAENHV_SYSERR);
    }

    CHECK_ID(id);
    CHECK_FREE(id);

/// my:  sprintf(name,"sy1527_%03d",id);
    sprintf(name, CRATE_LABEL, id); /// my:
    printf("System Name set as >%s<\n",name);

    strcpy(arg,ip_address);
    printf("TCP/IP address set as >%s<\n",arg);
    link = LINKTYPE_TCPIP;

    /*
    strcpy(userName, "user");
    strcpy(passwd, "user");
    */
    strcpy(userName, "admin");
    strcpy(passwd, "admin");

    ret = CAENHVInitSystem(name, link, arg, userName, passwd);

    printf("my: id_index=%d id=%d name=%s", id, ret, name );
    printf("\nCAENHVInitSystem error: %s (num. %d)\n\n", CAENHVGetError(name),ret);

    if(ret == CAENHV_OK)
    {
        if(strlen(ip_address)>=MAX_CAEN_NAME) {
            printf("too long mainfraime IP: exits now\n");    /// my:
            exit(1);
        }
        strcpy(Measure[id].IPADDR,ip_address); /// my:
        Measure[id].id = ret;
        strcpy(Measure[id].name, name);
        Demand[id].id = ret;
        strcpy(Demand[id].name, name);
    }
    else
    {
        printf("\n CAENHVInitSystem returned %d\n\n",ret);
        pthread_mutex_unlock(&global_mutex);
        return(CAENHV_SYSERR);
    }

    /* init mainframe mutex */
    pthread_mutex_init(&mainframe_mutex[id], &mattr);

    /* start thread */
    stat[id].threadid = id;
    force_exit[id] = 0;
    if(pthread_create(&idth[id],NULL,sy1527MainframeThread,(void *)&stat[id])!=0)
    {
        printf("ERROR: pthread_create(0x%08lx[%d],...) failure\n",idth[id],id);
        pthread_mutex_unlock(&global_mutex);
        return(CAENHV_SYSERR);
    }
    else
    {
        printf("INFO: pthread_create(0x%08lx[%d],...) done\n",idth[id],id);
    }

    /* register mainframe */
    mainframes[nmainframes-1] = id; /// my: removed nmainframes++
    ///mainframes[id] = id;

    printf("====================== 111111111111\n");
    /* get mainframe map */
    sy1527GetMap(id);
    printf("====================== 222222222222\n");

    pthread_mutex_unlock(&global_mutex);

    return(CAENHV_OK);
}
Esempio n. 8
0
/* open communication with mainframe under logical number 'id',
   do all necessary initialization and start thread */
int
sy527Start(unsigned id, char *ip_address)
{
  char arg[30], userName[20], passwd[30], name[MAX_NAME];
  int link, ret;

  printf("123\n");fflush(stdout);

  /* do global initialization on first call */
  if(nmainframes == -1) sy527Init();

  printf("456\n");fflush(stdout);

  /* lock global mutex to prevent other mainframes to be started
     until we are done with this one */
  LOCK_GLOBAL;

  if(nmainframes >= MAX_HVPS)
  {
    printf("ERROR: no more empty slots\n");
    UNLOCK_GLOBAL;

    return(CAENHV_SYSERR);
  }

  CHECK_ID(id);
  CHECK_FREE(id);

  sprintf(name,"sy527_%03d",id);
  printf("System Name set as >%s<\n",name);

  /*strcpy(arg,ip_address); for sy1527*/
  /*printf("TCP/IP address set as >%s<\n",arg);*/

  strcpy(arg,"sy527_0x100000_9");

  /*link = LINKTYPE_TCPIP; for sy1527*/
  link = LINKTYPE_CAENET;

  strcpy(userName, "user");
  strcpy(passwd, "user");

  ret = CAENHVInitSystem(name, link, arg, userName, passwd);

  printf("\nCAENHVInitSystem: %s (num. %d)\n\n", CAENHVGetError(name),ret);

  if(ret == CAENHV_OK)
  {
    Measure[id]->id = ret;
    strcpy(Measure[id]->name, name); 
    Demand[id]->id = ret;
    strcpy(Demand[id]->name, name); 
  }
  else
  {
    printf("\n CAENHVInitSystem returned %d\n\n",ret);    
    UNLOCK_GLOBAL;

    return(CAENHV_SYSERR);
  }


#ifndef vxWorks
  /* init mainframe mutex */
  pthread_mutex_init(&mainframe_mutex[id], &mattr);
#else
  mainframe_mutex[id] = semBCreate(SEM_Q_FIFO, SEM_FULL);
  if(mainframe_mutex[id] == NULL)
  {
    printf("ERROR: could not allocate 'mainframe_mutex' semaphore\n");
    return(0);
  }
#endif


  /* start thread */
  stat[id].threadid = id;
  force_exit[id] = 0;

#ifdef vxWorks
  {
    int iTaskStat;
    iTaskStat = taskSpawn("SY527THRD", 150, 0, 800000, sy527Thread,
                          &stat[id], 0, 0,0,0,0,0,0,0,0);
    printf("taskSpawn(\"SY527THRD\") returns %d\n",iTaskStat);
    if(iTaskStat == ERROR)
    {
	  perror("taskSpawn"); 
      UNLOCK_GLOBAL;
    }
  }
#else
  if(pthread_create(&idth[id],NULL,sy527Thread,(void *)&stat[id])!=0)
  {
    printf("ERROR: pthread_create(0x%08lx[%d],...) failure\n",idth[id],id);
    UNLOCK_GLOBAL;
    return(CAENHV_SYSERR);
  }
  else
  {
    printf("INFO: pthread_create(0x%08lx[%d],...) done\n",idth[id],id);
  }
#endif

  /* register mainframe */
  mainframes[nmainframes++] = id;

  /* get mainframe map */
  sy527GetMap(id);

  UNLOCK_GLOBAL;

  return(CAENHV_OK);
}