Ejemplo n.º 1
0
void *xrealloc (void *ptr, size_t bytes)
#endif
{
    void *cp;

	MALLOC_CHECK();
#ifdef _DEBUG
    if (!ptr)
		cp = _malloc_dbg(bytes?bytes:1,_NORMAL_BLOCK,file,line);
	else
		cp = _realloc_dbg(ptr, bytes,_NORMAL_BLOCK,file,line);
#else
    if (!ptr)
		cp = malloc (bytes?bytes:1);
	else
		cp = realloc(ptr, bytes);
#endif

    if (cp == NULL)
    {
		char buf[80];
		sprintf (buf, "out of memory; can not reallocate %lu bytes",
			(unsigned long) bytes);
		error (1, 0, buf);
    }
	MALLOC_CHECK();
    return (cp);
}
Ejemplo n.º 2
0
void *xmalloc (size_t bytes)
#endif
{
    void *cp;

    /* Parts of CVS try to xmalloc zero bytes and then free it.  Some
       systems have a malloc which returns NULL for zero byte
       allocations but a free which can't handle NULL, so compensate. */
    if (bytes == 0)
		bytes = 1;

	MALLOC_CHECK();
#ifdef _DEBUG
    cp = _malloc_dbg(bytes,_NORMAL_BLOCK,file,line);
#else
    cp = malloc (bytes);
#endif
    if (cp == NULL)
    {
		char buf[80];
		sprintf (buf, "out of memory; can not allocate %lu bytes",
			(unsigned long) bytes);
		error (1, 0, buf);
    }
	MALLOC_CHECK();

    return (cp);
}
Ejemplo n.º 3
0
list_node *add_list_node(list_node *head, void *pdata, size_t len)
{
    list_node *last_node, *new_node;

    /* create new head */
    if (head->data == NULL)
    {
        head->data = malloc(len);
        MALLOC_CHECK(head->data);
        memcpy(head->data, pdata, len);

        new_node = head;
    }
    else
    {
        last_node = head;
        while (last_node->next)
            last_node = last_node->next;

        new_node = (list_node *) calloc(1, sizeof(list_node));
        MALLOC_CHECK(new_node);
        new_node->data = malloc(len);
        MALLOC_CHECK(new_node->data);
        memcpy(new_node->data, pdata, len);

        /* link in new node */
        last_node->next = new_node;
    }
    
    return new_node;
}
//tries to add an entry into the dictionary
void DAdd(void* data, char* key){
   
   int num = hash(key);
   if (dict->start == NULL){//first element; dictionary empty
      MYASSERT(dict->end == NULL);
      dict->start = dict->end = (DNODE *)NEW(DNODE);
      MALLOC_CHECK(dict->start);
      dict->start->prev = dict->start->next = NULL;
      dict->hash[num] = dict->start;
      dict->start->data = data;
      BZERO(dict->start->key, KEY_LENGTH);
      strncpy(dict->start->key, key, KEY_LENGTH);
   }

   else{
      DNODE *d;
 
      if (dict->hash[num] == NULL){//non-collision case
	 d = NEW(DNODE);
	 dict->hash[num] = d;
	 d->next = NULL;
	 d->prev = dict->end;
	 dict->end->next = d;
	 dict->end = d;
	 BZERO(d->key, KEY_LENGTH);
	 strncpy(d->key, key, KEY_LENGTH);
	 d->data = data;
      }
      else {//collision case
	 int unique = TRUE;
	 DNODE *temp = dict->hash[num];
	 //as long as the hashes are the same, keep going
	 while (hash(temp->key) == num){
	    //check for matches
	    if (strncmp(temp->key, key, MAX_URL_LENGTH) == 0)
	       unique = FALSE;
	    if (temp->next == NULL || unique == FALSE)
	       break;//reached the end or we know it's not unique
	    temp = temp->next;
	 }
	 //only add if we didn't find the url in the list
	 if (unique == TRUE){
	    temp = temp->prev;
	    DNODE *d = NEW(DNODE);
	    MALLOC_CHECK(d);
	    d->next = temp->next;
	    d->prev = temp;
	    if (d->next == NULL)//for the case that hash[num] is at the end
	       dict->end = d;
	    else
	       temp->next->prev = d;
	    temp->next = d;
	    BZERO(d->key, KEY_LENGTH);
	    strncpy(d->key, key, KEY_LENGTH);
	    d->data = data;
	 }
      }
   }
}
Ejemplo n.º 5
0
// tweaked version of update index to insert an element into the index along with with the frequency
int insertElementIntoIndex(char* word, int documentId, int frequency, INVERTED_INDEX* index) {
	int hashIndex = hash1(word)%MAX_HASH_SLOT;
	// something is in there already
	if ((index->hash[hashIndex]) != NULL) {
		DocumentNode* init;
		init = (index->hash[hashIndex])->page;
		
		if(init->document_id == documentId) {
			init->page_word_frequency = frequency;
			(index->hash[hashIndex])->page = init;
		}
		else {
			DocumentNode* d;
			
			if(index->hash[hashIndex]) {
				int flag = 0;
				
				d = (index->hash[hashIndex])->page;
				while ((d->next) != NULL) {
					d = d->next;
					if(d->document_id == documentId) {
						//we found the same documentId
						d->page_word_frequency = frequency;
						flag = 1;
						break;
					}
				}
				
				if (flag == 0) {
					// documentId not present
					DocumentNode* dd = (DocumentNode*)malloc(sizeof(DocumentNode));
					MALLOC_CHECK(dd);
					dd->next = NULL;
					dd->document_id = documentId;
					dd->page_word_frequency = frequency;
					
					if (d->next == NULL) {
						d->next = dd;
					}
				}
			}
		}
		
		return 1;
	}
	// nothing is in there already
	else {		
		DocumentNode* doc = malloc(sizeof(DocumentNode));
		MALLOC_CHECK(doc);
		doc->document_id = documentId;
		doc->page_word_frequency = frequency;
		doc->next = NULL;
		
		// add element into the index
		IndexAdd(index, (void*)doc, word);
		return 1;	
	}
}
Ejemplo n.º 6
0
char *
digsyl(int range, int width)
{
	int i,
		base = 1,
		target;
	static char *new_dig = NULL;
	static int last_width = 0;

	if (range <= 0 || width < 0)
		return(NULL);
	
	if (width == 0) 
		{
		base=1;width=1;
		while(base <= range/10)
			{
			width++;
			base *= 10 ;
			}
		} 
	else  
		{
		for (i=0; i < width - 1; i++)
			base *= 10;
		}
	
	if (new_dig == NULL)
		{
		new_dig = (char *)malloc(sizeof(char) * SYL_WIDTH * width + 1);
		MALLOC_CHECK(new_dig);
		last_width = width;
		}
	else
		{
		new_dig[0] = '\0';
		if (last_width < width)
			{
			new_dig = 
				(char *)realloc(new_dig, width * sizeof(char) * SYL_WIDTH + 1);
			MALLOC_CHECK(new_dig);
			last_width = width;
			}
		}

	for (i=0; i < width * SYL_WIDTH; i += SYL_WIDTH)
		{
		target = range - (range % base);
		target /= base;
		strcpy(new_dig + i, digsyllables[target % 10]);
		range -= base * target;
		base /= 10;
		}

	return(new_dig);
}
Ejemplo n.º 7
0
// TODO: move to util
interp_workspace *new_interp_workspace(int upsample_ratio, interp_stats *stats) {
  interp_workspace *w = (interp_workspace *)malloc(sizeof(interp_workspace));
  MALLOC_CHECK(w);
  w->input = gsl_vector_alloc(NUM_STENCIL_POINTS);
  MALLOC_CHECK(w->input);
  w->output = gsl_vector_alloc((upsample_ratio+1)*(upsample_ratio+1));
  MALLOC_CHECK(w->output);
  w->stats = stats;
  return w;
}
Ejemplo n.º 8
0
/* Automatically null the pointer after freeing it */
void xfree_s(void **ptr)
{
	MALLOC_CHECK();
	if(*ptr)
	{
		free(*ptr);
		MALLOC_CHECK();
	}
	*ptr=NULL;
}
Ejemplo n.º 9
0
/*
 * Routine: ProcessNames
 * Purpose: Parse the name vector
 * Algorithm:
 * Data Structures:
 *
 * Params:
 * Returns:
 * Called By:
 * Calls:
 * Assumptions:
 * Side Effects:
 * TODO: None
 */
int
ProcessNames (char *stmt, token_t * tokens)
{
    char *szResult = NULL;
    char *cp;
    int nCount = 0,
        nWordLength = 0;

    /* get the names */
    while ((cp = strtok (NULL, "=( ,);:")) != NULL)
    {
        if (nCount == 0)
        {
            nWordLength = strlen(cp);
            szResult = malloc(nWordLength + 1);
            MALLOC_CHECK(szResult);
            nCount = nWordLength + 1;
            strcpy(szResult, cp);
        }
        else
        {
            nWordLength = strlen(cp);
            szResult = realloc(szResult, nCount + nWordLength + 1);
            strcpy(szResult + nCount, cp);
            nCount += nWordLength + 1;

        }
    }

    pCurrentIndexEntry->dist->names = szResult;
    return (nCount);
}
Ejemplo n.º 10
0
// Function to write the webpage to a file.
int WriteFile(WebPage *wp, char *path, int pageID) {
	// Declare and initialize a filename variable.
	char *filename = (char *)malloc(20);
    	MALLOC_CHECK(stderr,filename);
    
    	// Write to the filename variable.
	if (sprintf(filename, "%s/%d", path, pageID) == EOF) {
    		printf("There is an error with the directory path.\n");
    		return 0;
    	}
    
    	// Declare and open a filepath with the filename.
    	FILE *fp;
    	fp = fopen(filename, "w+"); // Open a new file by the filename.
	
	// Check that there were no errors opening the file.
   	if (fp == NULL) {
    		printf("Error reading file.\n");
    		return 0;
    	}
    
    	// Write to each file of an html.
    	fprintf(fp, "%s\n%d\n%s", wp->url, wp->depth, wp->html);
    
    	// Cleanup.
	fclose(fp);
	free(filename);
	return 1;
}
//takes in an array of search terms, gets the docID, score, and which list it should be added in
//and passes the information to the function addScoreToList to be added to the list
//
//PSEUDO CODE
//for all searchterms
//    get the docIDs associated with the word and add the score to the list but factor in a WEIGHT
//    if the prev searchterm is not OR
//	 add to list with weight
//    else
//	 add to the list
void processSearchTerms(INVERTED_INDEX* index, char* searchterms) {
    int docID;
    int score;
    char* prevterm = NULL;
    char* currentterm;
    int pos;
    DOCNODE* d;
    while (searchterms != NULL) {
        currentterm = searchterms;
        pos = 0;
        if(isSearchTerm(currentterm) == TRUE) { //if it's a search term, normalize it and search for it
            NormalizeWord(currentterm);
            while((d = getDoc(index, currentterm, &pos)) != NULL) {
                docID = d->doc_id;
                score = d->page_word_freq;
                if(isNotOR(prevterm) == TRUE) { //add with weighteded score because it must be ADD
                    addScoreToList(querylist, TRUE, docID, (score*WEIGHT));
                }
                else//add with regular score
                    addScoreToList(querylist, FALSE, docID, score);
            }
        }
        prevterm = currentterm;
        searchterms = strtok(NULL, " "); //get next searchterm
    }
    if (querylist->start != NULL) {
        slist = NEW(SORTLIST);
        MALLOC_CHECK(slist);
        BZERO(slist, sizeof(SORTLIST));
        sortList(slist, querylist);
        printList(slist);
    }
}
Ejemplo n.º 12
0
Archivo: driver.c Proyecto: YIwama/bcb
/*
* re-set default output file names 
*/
int
set_files (int i, int pload)
{
	char line[80], *new_name;
	
	if (table & (1 << i))
child_table:
	{
		if (pload != -1)
			sprintf (line, "%s.%d", tdefs[i].name, pload);
		else
		{
			printf ("Enter new destination for %s data: ",
				tdefs[i].name);
			if (fgets (line, sizeof (line), stdin) == NULL)
				return (-1);;
			if ((new_name = strchr (line, '\n')) != NULL)
				*new_name = '\0';
			if (strlen (line) == 0)
				return (0);
		}
		new_name = (char *) malloc (strlen (line) + 1);
		MALLOC_CHECK (new_name);
		strcpy (new_name, line);
		tdefs[i].name = new_name;
		if (tdefs[i].child != NONE)
		{
			i = tdefs[i].child;
			tdefs[i].child = NONE;
			goto child_table;
		}
	}
	
	return (0);
}
Ejemplo n.º 13
0
Archivo: permute.c Proyecto: YIwama/bcb
long *
permute_dist(distribution *d, long stream)
	{
	static distribution *dist = NULL;
	int i;
	
	if (d != NULL)
		{
		if (d->permute == (long *)NULL)
			{
			d->permute = (long *)malloc(sizeof(long) * (DIST_SIZE(d)));
			MALLOC_CHECK(d->permute);
			for (i=0; i < (DIST_SIZE(d)); i++) 
				*(d->permute + i) = i;
			}
		dist = d;
		return(permute(dist->permute, DIST_SIZE(dist), stream));
		}
	
	
	if (dist != NULL)
		return(permute(NULL, DIST_SIZE(dist), stream));
	else
		INTERNAL_ERROR("Bad call to permute_dist");	
	}
Ejemplo n.º 14
0
/*
* Routine: MakePermutation(int nSize)
* Purpose: Permute the integers in [1..nSize]
* Algorithm:
* Data Structures:
*
* Params:
* Returns:
* Called By: 
* Calls: 
* Assumptions:
* Side Effects:
* TODO: None
*/
int *
makePermutation(int *nNumberSet, int nSize, int nStream)
{
	int i,
		nTemp,
		nIndex,
		*pInt;

	if (nSize <= 0)
		return(NULL);

	if (!nNumberSet)
	{
		nNumberSet = (int *)malloc(nSize * sizeof(int));
		MALLOC_CHECK(nNumberSet);
		pInt = nNumberSet;
		for (i=0; i < nSize; i++)
			*pInt++ = i;
	}

	for (i=0; i < nSize; i++)
	{
		nIndex = genrand_integer(NULL, DIST_UNIFORM, 0, nSize - 1, 0, nStream);
		nTemp = nNumberSet[i];
		nNumberSet[i] = nNumberSet[nIndex];
		nNumberSet[nIndex] = nTemp;
	}

	return(nNumberSet);
}
//initializes the querylist
void initQueryList() {
    querylist = NEW(QUERYLIST);
    MALLOC_CHECK(querylist);
    BZERO(querylist, sizeof(QUERYLIST));
    querylist->start = querylist->end = NULL;
    for (int i = 0; i < MAX_HASH_SLOT; i++)
        querylist->hash[i] = NULL;
}
Ejemplo n.º 16
0
int
getopt(int ac, char **av, char *opt)
{
    static char *nextchar = NULL;
    char *cp;
    char hold;

    if (optarg == NULL)
        {
        optarg = (char *)malloc(BUFSIZ);
        MALLOC_CHECK(optarg);
        }

    if (!nextchar || *nextchar == '\0')
        {
        optind++;
        if (optind == ac)
            return(-1);
        nextchar = av[optind];
        if (*nextchar != '-')
            return(-1);
        nextchar +=1;
        }

    if (nextchar && *nextchar == '-')   /* -- termination */
        {
        optind++;
        return(-1);
        }
    else        /* found an option */
        {
        cp = strchr(opt, *nextchar);
        nextchar += 1;
        if (cp == NULL) /* not defined for this run */
            return('?');
        if (*(cp + 1) == ':')   /* option takes an argument */
            {
            if (*nextchar)
                {
                hold = *cp;
                cp = optarg;
                while (*nextchar)
                    *cp++ = *nextchar++;
                *cp = '\0';
                *cp = hold;
                }
            else        /* white space separated, use next arg */
                {
                if (++optind == ac)
                    return('?');
                strcpy(optarg, av[optind]);
                }
            nextchar = NULL;
            }
        return(*cp);
        }
}
Ejemplo n.º 17
0
// initializes the index
INVERTED_INDEX* InitIndex() {
	INVERTED_INDEX* index = (INVERTED_INDEX*)malloc(sizeof(INVERTED_INDEX));
	MALLOC_CHECK(index);
	index->start = index->end = NULL;
	
	for (int i = 0; i< MAX_HASH_SLOT; i++) {
		index->hash[i] = NULL;
	}
	return index;
}
Ejemplo n.º 18
0
DocumentNode *DNode(int docID, int freq) {
    DocumentNode *node = (DocumentNode *)calloc(1, sizeof(DocumentNode));
    MALLOC_CHECK(stderr, node);
    
    node->next = NULL;
    node->docID = docID;
    node->freq = freq;
    
    return node;
    
}
Ejemplo n.º 19
0
// updateIndex takes a word, a document_id, and an index.  It adds the document to the index,
// and the word itself if it's not already contained in the index.  Returns 0 if success, 1 if failure.
int updateIndex(char* word, int document_id, INVERTED_INDEX* in_index)
{
	DocumentNode* docnode;
	WordNode* wordnode;
	DocumentNode* current_doc_node;

	int page_node_exists;

	page_node_exists = 0;

// creates a DocumentNode from the doc_id
	docnode = malloc(sizeof(DocumentNode));
  	MALLOC_CHECK(docnode);
  	docnode->document_id = document_id;
  	docnode->page_word_frequency = 1;
	docnode->next = NULL;

// makes it lower case (necessary for the query system)
	NormalizeWord(word);

	if(addData(in_index, docnode, word))	// if the wordnode already exists
	{
		wordnode = getData(in_index, word);

		if(wordnode != NULL)
		{
			current_doc_node = wordnode->data;

			while(current_doc_node != NULL)
			{
				if((current_doc_node->document_id) == document_id)
				{
					page_node_exists = 1;
					current_doc_node->page_word_frequency = (current_doc_node->page_word_frequency)+1;
					free(docnode);
					break;
				}
				else
					if(current_doc_node->next == NULL)
						break;
					else
						current_doc_node = current_doc_node->next;
			}

			if(!page_node_exists)
			{
				current_doc_node->next = docnode;
			}
		}			
	}

	return 0;
}
Ejemplo n.º 20
0
WordNode *WNode(DocumentNode *docNode, char *word) {
    WordNode *wordNode = (WordNode *)calloc(1, sizeof(WordNode));
    MALLOC_CHECK(stderr, wordNode);
    
    //wordNode->next = NULL;
    wordNode->page = docNode;
    
    wordNode->word = (char *)calloc(strlen(word) + 1, sizeof(char));       // null terminator
    
    strcpy(wordNode->word, word);
    return wordNode;
}
//takes in QUERYLIST and adds a QUERYNODE
//to the ANDORLIST if the docID is unique, or updates the score of th QUERYNODE containing
//the docID if the docID already exists in the list
void addScoreToList(QUERYLIST* querylist, int isAND, int docID, int score) {
    char ID[DOCLEN];
    sprintf(ID, "%d", docID);
    int num = hash(ID);

    QUERYNODE* l = querylist->hash[num];
    int addNew = TRUE;
    while(1) {
        if (l == NULL)
            break;
        if (strncmp(ID, l->docID, DOCLEN) == 0) {
            addNew = FALSE;
            break;
        }
        else if (l->next == NULL || hash(l->docID) != num)
            break;
        l = l->next;
    }
    if (addNew == TRUE) {
        QUERYNODE* ll = NEW(QUERYNODE);
        MALLOC_CHECK(ll);
        BZERO(ll, sizeof(QUERYNODE));
        ll->score = score;
        strncpy(ll->docID, ID, DOCLEN);

        if (querylist->start == NULL) { //first node case
            querylist->start = querylist->end = ll;
            ll->next = ll->prev = NULL;
        }
        else if (l != NULL && l->next != NULL) { //this and the next are collision cases
            ll->prev = l;
            ll->next = l->next;
            l->next->prev = ll;
            l->next = ll;
        }
        else if (l != NULL) {
            ll->prev = querylist->end;
            ll->next = NULL;
            querylist->end = ll;
        }
        else { //inserting into new hash slot
            querylist->hash[num] = ll;
            ll->prev = querylist->end;
            ll->next = NULL;
            querylist->end->next = ll;
            querylist->end = ll;
        }
    }
    else
        l->score = score + l->score;
}
Ejemplo n.º 22
0
bit_array_t *upsample(double **grid, int ny, int nx, double alpha, int M, int upsample_ratio, interp_stats *stats) {
  gsl_matrix *interp = create_interp_matrix(alpha, M, upsample_ratio);
  bit_array_t *upsampled = new_bit_array((ny-1)*upsample_ratio+1, (nx-1)*upsample_ratio+1);
  MALLOC_CHECK(upsampled);
  int r,c,x,y;
  interp_workspace *w = new_interp_workspace(upsample_ratio, stats);
  for (r = 0 ; r < ny - 3 ; r++) {
    for (c = 0 ; c < nx - 3 ; c++) {
      interpolate(grid, upsampled, r, c, ny, nx, upsample_ratio, interp, w);
    }
  }
  free_interp_workspace(w);
  return upsampled;
}
//initializes the dictionary
DICTIONARY* initDict(){
   DICTIONARY* dict = NEW(DICTIONARY);
   MALLOC_CHECK(dict);
   BZERO(dict, sizeof(DICTIONARY));

   //dict->start = dict->end = malloc(sizeof(DNODE));
   //MALLOC_CHECK(dict->start);
   //dict->start->prev = dict->start->next = NULL;
   dict->start = dict->end = NULL;
   for (int i = 0; i < MAX_HASH_SLOT; i++)
      dict->hash[i] = NULL;

   return dict;
}
Ejemplo n.º 24
0
DocumentNode *addDocNode(int DocId, int frequency){
  
  //create Document Node
  DocumentNode *dNew;
  dNew = (DocumentNode *)malloc(sizeof(DocumentNode));
  MALLOC_CHECK(dNew);
  BZERO(dNew,sizeof(DocumentNode));
  
  //update Document Node
  dNew->document_id = DocId;
  dNew->page_word_frequency = frequency;
 
  //return the new DocNode
  return dNew; 

}
Ejemplo n.º 25
0
//
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
int srt_server_sock(unsigned int port)
{
  //malloc new TCB entry at first NULL entry
	int sockfd = 0;
	while (sockfd < MAX_TRANSPORT_CONNECTIONS){
		if (server_TCB_Table[sockfd] == NULL) {

			//malloc new client tcb entry
			server_TCB_Table[sockfd] = malloc(sizeof(svr_tcb_t));
			MALLOC_CHECK(server_TCB_Table[sockfd]);
			memset(server_TCB_Table[sockfd], 0, sizeof(svr_tcb_t));

			//initialize TCB entry, malloc recv buffer and mutex
			server_TCB_Table[sockfd]->svr_portNum = port;
			server_TCB_Table[sockfd]->state = CLOSED;
			server_TCB_Table[sockfd]->usedBufLen = 0;
			server_TCB_Table[sockfd]->expect_seqNum = 0;
			server_TCB_Table[sockfd]->recvBuf = malloc(RECEIVE_BUF_SIZE);
			memset(server_TCB_Table[sockfd]->recvBuf, 0, RECEIVE_BUF_SIZE);
			server_TCB_Table[sockfd]->bufMutex = malloc(sizeof(pthread_mutex_t));
			memset(server_TCB_Table[sockfd]->bufMutex, 0, sizeof(pthread_mutex_t));
			server_TCB_Table[sockfd]->svr_nodeID = topology_getMyNodeID();
			printf("My nodeID is %u.\n", server_TCB_Table[sockfd]->svr_nodeID);

			//initialize mutex
			if (pthread_mutex_init(server_TCB_Table[sockfd]->bufMutex, NULL) != 0) {
			    printf("\n mutex init failed\n");
			    return -1;
			}

			break;
		} else {
			sockfd++;
		}
	}

	if (sockfd == MAX_TRANSPORT_CONNECTIONS) {
		printf("You've reached the maximum number of transport connections.\n");
		return -1;
	} else {
		printf("Created new TCB server entry with sockfd %d.\n", sockfd);
		return sockfd;
	}
}
Ejemplo n.º 26
0
/*
 * Routine: 
 * Purpose: 
 * Algorithm:
 * Data Structures:
 *
 * Params:
 * Returns:
 * Called By: 
 * Calls: 
 * Assumptions:
 * Side Effects:
 * TODO: None
 */
substitution_t *
findSubstitution(template_t *t, char *name, int *nUse)
{
	int nChars,
		nUseCount;
	substitution_t *pSub;
	static substitution_t tempSubstitution;
	static int bInit = 0;

	if (!bInit)
	{
		memset(&tempSubstitution, 0, sizeof(struct SUBSTITUTION_T));
		tempSubstitution.name = malloc(100 * sizeof(char));
		MALLOC_CHECK(tempSubstitution.name);
		bInit = 1;
	}
	
	/* exclude any numeric suffix from search, but update nUses */
	nChars = strcspn(name, "0123456789");
	if (strlen(name) > 100)
		tempSubstitution.name = realloc(tempSubstitution.name, strlen(name) + 1);
	strncpy(tempSubstitution.name, name, nChars);
	tempSubstitution.name[nChars] = '\0';
	pSub = findList(t->SubstitutionList, (void *)&tempSubstitution);
	if (!pSub) /* the substitution could be global; add a local reference */
	{
		pSub = findList(g_Template->SubstitutionList, (void *)&tempSubstitution);
		if (pSub)
			addList(t->SubstitutionList, pSub);
	}
	if (pSub)
	{
		nUseCount = atoi(name + nChars);
		if (nUseCount == 0)
			nUseCount = 1;
		if (nUseCount > pSub->nUse)
			pSub->nUse = nUseCount;
		if (nUse)	/* we're interested in the usage number */
			*nUse = nUseCount;
		return(pSub);
	}
	
	return(NULL);
}
Ejemplo n.º 27
0
/*
* Routine: expr_t *makeExpr(void)
* Purpose: 
* Algorithm:
* Data Structures:
*
* Params:
* Returns:
* Called By: 
* Calls: 
* Assumptions:
* Side Effects:
* TODO: None
*/
expr_t *
makeExpr(void)
{
	expr_t *pResult;

	pResult = (expr_t *)malloc(sizeof(struct EXPR_T));
	MALLOC_CHECK(pResult);
	if (pResult == NULL)
		ReportError(QERR_NO_MEMORY, "in MakeReplacement()", 1);
	memset(pResult, 0, sizeof(struct EXPR_T));
	pResult->nValueCount = 1;
#ifdef MEM_TEST
	fprintf(stderr, "MakeExpr value %x\n", pResult);
#endif
	pResult->Value.pBuf = InitBuffer(10, 10);
	if (pResult->Value.pBuf == NULL)
		ReportError(QERR_NO_MEMORY, "in MakeReplacement()", 1);

	return(pResult);
}
Ejemplo n.º 28
0
int TestgetWord1(){
  START_TEST_CASE;

  char string[] = "";
  char *result;
  int pos = 0;

  result = (char *)malloc(1000);
  MALLOC_CHECK(result);
  BZERO(result,1000);

  pos = getWord(string,result,pos);
  

  SHOULD_BE( pos == -1 );
  SHOULD_BE( strcmp(result,"") == 0);
  
  free(result);
  
  END_TEST_CASE;
}
Ejemplo n.º 29
0
long *
permute_dist(distribution *d, long stream, 
             DSS_HUGE& source, distribution* cd)
{
  static bool bInit = false;
  static distribution *dist = NULL;
	
  if (d != NULL) {
    if (d->permute == (long *)NULL) {
      d->permute = (long *)malloc(sizeof(long) * DIST_SIZE(d));
      MALLOC_CHECK(d->permute);
      //IP: permute does the same 
      //for (int i=0; i < DIST_SIZE(d); i++) {
      //   *(d->permute + i) = i;
      //}
    }

    while (!bInit) {
      dist = d;
      bInit = true;
    }
    // IP: This will not work in general, but afaict from the code
    //     this function (permute_dist) is called only by mk_part
    //     so 'dist' will never have to change its value. 
    //     This assertion ensures that 'dist' will have a single 
    //     value.    
    assert (dist == d);
    return (permute(dist->permute, DIST_SIZE(dist), stream, 
                    source, cd->permute));
  }
		
  if (dist != NULL) {
    return (permute(NULL, DIST_SIZE(dist), stream, source, cd->permute));
  }
  else {
    INTERNAL_ERROR("Bad call to permute_dist");	
  }

  return (NULL);
}
Ejemplo n.º 30
0
bit_array_t *createScaledMaskFromBilliard(Billiard *b, double xl, double xh, double yl, double yh, double dx, double upsample_ratio, double scale, int ny, int nx) {
    int nx_should_be, ny_should_be;
    if (xh - xl == 0.0) {
        ny_should_be = ceil((b->yh - b->yl + 0.9) / dx) * upsample_ratio + 1;
        nx_should_be = ceil((b->xh - b->xl + 0.9) / dx) * upsample_ratio + 1;
    } else {
        ny_should_be = (int)((yh - yl) / dx + 0.9) * upsample_ratio + 1;
        nx_should_be = (int)((xh - xl) / dx + 0.9) * upsample_ratio + 1;
    }
    if ((float)fabs(nx-nx_should_be)/nx_should_be > DIMENSION_ERROR_MARGIN || (float)fabs(ny-ny_should_be)/ny_should_be > DIMENSION_ERROR_MARGIN) {
        ERROR("Mask dimensions do not match expected dimesnions. Given %d x %d, expected %d x %d", ny, nx, ny_should_be, nx_should_be);
        exit(DIMENSION_ERR);
    }


    bit_array_t *counted = new_bit_array(ny, nx);
    MALLOC_CHECK(counted);

    int i, j;
    for (i = 0 ; i < ny ; i++) {
        for (j = 0 ; j < nx ; j++) {
            if (i == 0 || j == 0) {
                bit_array_set(counted, j, i); // mask out first row and column
            }
            if (!inside_billiard(j * (dx / upsample_ratio) / scale, i * (dx / upsample_ratio) / scale, b)) {
                bit_array_set(counted, j, i); // bit array is zeroed when allocated
            }
        }
    }

    // special case for qugrs billiard: mask out the two points at the tip
    if (b->type == QU_GEN_RECT_SINAI) {
        bit_array_set(counted, nx-1, ny-1);
        bit_array_set(counted, nx-2, ny-2);
    }

    return counted;
}