Пример #1
0
/* 
 * Requires:
 *   None.
 *
 * Effects:
 *   Perform a minimal check of the heap for consistency. 
 */
void
checkheap(bool verbose) 
{
  void *bp;

  if (verbose)
    printf("Heap (%p):\n", heap_listp);

  if (GET_SIZE(HDRP(heap_listp)) != DSIZE ||
      !GET_ALLOC(HDRP(heap_listp)))
    printf("Bad prologue header\n");
  checkblock(heap_listp);

  for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = (void *)NEXT_BLKP(bp)) {
    if (verbose)
      printblock(bp);
    checkblock(bp);
  }

  if (verbose)
    printblock(bp);
  if (GET_SIZE(HDRP(bp)) != 0 || !GET_ALLOC(HDRP(bp)))
    printf("Bad epilogue header\n");

in_heap(bp);
check_coalescing();
check_free_list();
check_free_blocks();
}
Пример #2
0
/*
 * checkheap - functions with multiple checks
 */
int mm_checkheap(int verbose)
{

	char *bp = heap_list_head;

	if (verbose)
	{
		printf("Heap (%p):\n", heap_list_head);
		//printf("Free List (%p):\n", free_list_head);
	}

	if ((GET_SIZE(HDRP(heap_header)) != HEADER_SIZE)
			|| !GET_ALLOC(HDRP(heap_header)))
	{
		//printf("Bad prologue header\n");
		//return 1;
	}
	checkblock(heap_list_head);
	for (bp = heap_list_head; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp))
	{
		if (verbose)
			printblock(bp);
		checkblock(bp);
	}
	if (verbose)
		printblock(bp);
	if ((GET_SIZE(HDRP(bp)) != 0) || !(GET_ALLOC(HDRP(bp))))
	{
		printf("Bad epilogue header\n");
		//return 1;
	}

	return 0;
}
Пример #3
0
//
// mm_checkheap - Check the heap for consistency 
//
void mm_checkheap(int verbose) 
{
  //
  // This provided implementation assumes you're using the structure
  // of the sample solution in the text. If not, omit this code
  // and provide your own mm_checkheap
  //
  void *bp = heap_listp;
  
  if (verbose) {
    printf("Heap (%p):\n", heap_listp);
  }

  if ((GET_SIZE(HDRP(heap_listp)) != DSIZE) || !GET_ALLOC(HDRP(heap_listp))) {
	printf("Bad prologue header\n");
  }
  checkblock(heap_listp);

  for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
    if (verbose)  {
      printblock(bp);
    }
    checkblock(bp);
  }
     
  if (verbose) {
    printblock(bp);
  }

  if ((GET_SIZE(HDRP(bp)) != 0) || !(GET_ALLOC(HDRP(bp)))) {
    printf("Bad epilogue header\n");
  }
}
Пример #4
0
/* 
 * Requires:
 *   None.
 *
 * Effects:
 *   Perform a minimal check of the heap for consistency. 
 */
void
checkheap(bool verbose) 
{
	void *bp;

	if (verbose)
		printf("Heap (%p):\n", heap_listp);

	if (GET_SIZE(HDRP(heap_listp)) != DSIZE)
		printf("Bad prologue header: size\n");
	if (!GET_ALLOC(HDRP(heap_listp)))
		printf("Bad prologue header: alloc\n");
	checkblock(heap_listp);

	for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
		if (verbose)
			printblock(bp);
		checkblock(bp);
	}

	if (verbose)
		printblock(bp);
	if (GET_SIZE(HDRP(bp)) != 0)
		printf("Bad epilogue header: size\n");
	if (!GET_ALLOC(HDRP(bp)))
		printf("Bad epilogue header: alloc\n");
}
Пример #5
0
/*
 * Requires:
 *   None.
 *
 * Effects:
 *   Perform a minimal check of the heap for consistency.
 */
void
checkheap(bool verbose)
{
    void *bp;

    if (verbose)
        printf("Heap (%p):\n", heap_listp);

    if (GET_SIZE(HDRP(heap_listp)) != DSIZE ||
        !GET_ALLOC(HDRP(heap_listp)))
        printf("Bad prologue header\n");
    checkblock(heap_listp);

    for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
        if (verbose)
            printblock(bp);
        checkblock(bp);
    }

    if (verbose)
        printblock(bp);
    if (GET_SIZE(HDRP(bp)) != 0 || !GET_ALLOC(HDRP(bp)))
        printf("Bad epilogue header\n");

	 check_is_next_ptr_pointing_to_free(bp);
	 
	 check_contigous(bp);

     check_freelist_freeblock(bp);
}
Пример #6
0
/*
 * mm_checkheap - Check the heap for consistency
 */
void mm_checkheap(int verbose)
{
    char *bp = heap_listp;

    if (verbose) {
        printf("Heap (%p):\n", heap_listp);
        printf("Root (%p):\n", tree_root);
    }

    if ((GET_SIZE(HDRP(heap_listp)) != DSIZE) || !GET_ALLOC(HDRP(heap_listp)))
        printf("Bad prologue header\n");

    checkblock(heap_listp);

    for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
        if (verbose)
            printblock(bp);

        checkblock(bp);
    }

    if (verbose)
        printblock(bp);

    if ((GET_SIZE(HDRP(bp)) != 0) || !(GET_ALLOC(HDRP(bp))))
        printf("Bad epilogue header\n");
}
Пример #7
0
void *luaM_realloc(void *block, int32 size) {
	int32 realsize = HEADER + size + 1;
	int32 realsize = sizeof(int32) + size + sizeof(char);
	if (realsize != (size_t)realsize)
		lua_error("Allocation Error: Block too big");
	if (size == 0) {  // ANSI dosen't need this, but some machines...
		if (block) {
			memset(block, -1, *((int32 *)block - 1));  // erase block
			block = checkblock(block);
			free(block);
		}
		return NULL;
	}
	if (block) {
		block = checkblock(block);
		block = (int32 *)realloc(block, realsize);
	} else
		block = (int32 *)malloc(realsize);
	if (!block)
		lua_error(memEM);
	totalmem += size;
	numblocks++;
	*(int32 *)block = size;
	*(((char *)block) + size + sizeof(int32)) = MARK;
	return (int32 *)block+1;
}
Пример #8
0
//check heap
static int check_my_heap(int verbose){
	if (!verbose){
		return 0;
	}
	else {
		printf("Heap (%p):\n", heap_listp);
		void *bp;
		if ((GET_SIZE(HDRP(heap_listp)) != DSIZE) || !GET_ALLOC(HDRP(heap_listp))){
			printf("Bad prologue header\n");
			return 1;
		}
		if (checkblock(heap_listp)){
			return 1;
		}

		for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
			printblock(bp);
			if (checkblock(bp)){
				return 1;
			}
		}

		if (verbose)
			printblock(bp);
		if ((GET_SIZE(HDRP(bp)) != 0) || !(GET_ALLOC(HDRP(bp)))){
			printf("Bad epilogue header\n");
			return 1;
		}
	}
	return 0;
}
Пример #9
0
// Returns 0 if no errors were found, otherwise returns the error
int mm_checkheap(int verbose) {
  // char *bp = heap_listp;

  if (verbose)
    printf("Heap (%p):\n", heap_listp);

  if ((GET_SIZE(HDRP(heap_listp)) != DSIZE) || !GET_ALLOC(HDRP(heap_listp)))
    printf("Bad prologue header\n");
  checkblock(heap_listp);

  void* list;
  int count_free_in_list = 0;
  int count_free_in_heap = 0;
  for (int i =0; i < 9; i++)
    {
      for (list = (void*)(*(long*)GET_BUCKET(root,i)); list != NULL;
           list =  get_succ(list)  ) {
        if (verbose)
          printblock(list);
        checkblock(list);
        if ( get_succ(list) != NULL && get_pred(list) !=NULL)
          check_succ_pred(list);
        check_address(list);
        count_free_in_list++;
        check_in_correct_bucket(list, i);
      }
    }

  char *bp;

  for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
    if (verbose)
      {
        if (GET_ALLOC(HDRP(bp)) == 0)
          count_free_in_heap ++;
        printblock(bp);
        checkblock(bp);
        check_coalescing( (void*)bp);
      }
  }

  if (count_free_in_heap != count_free_in_list)
    printf ("Number of free block not consistent in heap and list list \n");

  if (verbose)
    printblock(bp);
    if ((GET_SIZE(HDRP(bp)) != 0) || !(GET_ALLOC(HDRP(bp))))
      {
        printf("Bad epilogue header\n");
        if (GET_SIZE(HDRP(bp)) != 0)
          printf ("size is not 0\n");
        if (!(GET_ALLOC(HDRP(bp))) )
           printf ("not allocated properly\n");
     }
    return 0;
}
Пример #10
0
/*
 * checkheap - Check the heap for consistency
 * (iterates all the blocks starting from prologue to epilogue) 
 * 
 */
void _checkheap(void)
{
	char *bp = heap_listp;
	size_t prev_alloc, curr_alloc;
	

	dbg1("\n[CHECK HEAP]\n");
	dbg1("\n[verbose=%d]\n", verbose);

	if (verbose) {
		printf("Heap (starting address:%p):\n", heap_listp);
		printf("-prologue-");
		printblock(bp);
	}

	/* checking prologue block (size, allocate bit) */
	if ((GET_SIZE(HDRP(heap_listp)) != DSIZE) || !GET_ALLOC(HDRP(heap_listp))) {
		printf("Bad prologue header\n");
		printf("-prologue-");
		printblock(bp);
	}
	checkblock(heap_listp);             /* alignment, header/footer */
	prev_alloc = GET_ALLOC(HDRP(bp));
	
	/* checking allocated/free blocks */
	for (bp = NEXT_BLKP(heap_listp); GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
		curr_alloc = GET_PREV_ALLOC(HDRP(bp));
		if (verbose)
			printblock(bp);
		if (!prev_alloc != !curr_alloc) {
			/* previous block's allocate bit should match current block's prev allocate bit */
			printf("prev allocate bit mismatch\n");
			printblock(bp);
			exit(0);
		}
		prev_alloc = GET_ALLOC(HDRP(bp));
		checkblock(bp);
	}
	printf("done\n");

	/* checking epilouge block */
	if ((GET_SIZE(HDRP(bp)) != 0) || !(GET_ALLOC(HDRP(bp)))){
		printf("Bad epilogue header\n");
		printf("-epilogue-");
		printblock(bp);
	}

	checklist();
	
	dbg1("[CHECK DONE]\n\n");
}
Пример #11
0
/*
 * mm_checkheap - Check the heap for consistency
 */
void mm_checkheap(int verbose)
{
    if (verbose)
        printf("Check heap: \n");
    char *bp = heap_listp;
    int free_block_flag = 0;
    int free_block_count = 0;
    
    if (verbose)
        printf("Heap (%p):\n", heap_listp);
    
    // check prologue block
    if ((GET_SIZE(HDRP(heap_listp)) != OVERHEAD) || !GET_ALLOC(HDRP(heap_listp)))
        printf("Bad prologue header\n");
    checkblock(heap_listp);
    
    for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
        if (verbose)
            printblock(bp);
        checkblock(bp);
        
        // check coalescing
        if (!GET_ALLOC(HDRP(bp))) {
            if (free_block_flag == 1) {
                printf("Error: consecutive free blocks %p | %p in the heap.\n", PREV_BLKP(bp),bp);
            }
            free_block_flag = 1;
            free_block_count++;
        } else {
            free_block_flag = 0;
        }
        
    }
    
    if (verbose)
        printblock(bp);
    
    // check epilogue block
    if ((GET_SIZE(HDRP(bp)) != 0) || !(GET_ALLOC(HDRP(bp))))
        printf("Bad epilogue header\n");
    
    // print heap boundaries
    check_heapboundaries(heap_listp-DSIZE, bp-1);

    if (verbose) {
        printfreelist();
    }
    checkfreelist(free_block_count);
}
Пример #12
0
/*
 * Initialize: return -1 on error, 0 on success.
 */
int mm_init(void)
{
	/* Create the initial empty heap */
	if ((bins = mem_sbrk(
		ALIGN(fixed_bin_count * sizeof(block_ptr)) +
		4 * WSIZE)) == (block_ptr)-1)
		return -1;
	memset(bins, 0, fixed_bin_count * sizeof(block_ptr));
	heap_listp = (char *)ALIGN((unsigned long)(bins + fixed_bin_count));
	larger_bin_root = NULL;
	PUTTRUNC(heap_listp, 0);							/* Alignment padding */
	PUTTRUNC(heap_listp + (1 * WSIZE), PACK(DSIZE, 1)); /* Prologue header */
	PUTTRUNC(heap_listp + (2 * WSIZE), PACK(DSIZE, 1)); /* Prologue footer */
	PUTTRUNC(heap_listp + (3 * WSIZE), PACK(0, 1));		/* Epilogue header */
	heap_listp += (2 * WSIZE);
	SET_ALLOC(heap_listp);
#ifdef DEBUG
	{
		printblock(heap_listp);
		checkblock(heap_listp);
	}
	operid = 0;
#endif
	return 0;
}
Пример #13
0
static
void
test567(int testno, unsigned long seed)
{
	static const int sizes[8] = { 13, 17, 69, 176, 433, 871, 1150, 6060 };
	
	void *ptrs[32];
	int psizes[32];
	int i, n, size, failed=0;

	srandom(seed);
	printf("Seeded random number generator with %lu.\n", seed);

	for (i=0; i<32; i++) {
		ptrs[i] = NULL;
		psizes[i] = 0;
	}

	for (i=0; i<100000; i++) {
		n = random()%32;
		if (ptrs[n] == NULL) {
			size = sizes[random()%8];
			ptrs[n] = malloc(size);
			psizes[n] = size;
			if (ptrs[n] == NULL) {
				printf("\nmalloc %u failed\n", size);
				failed = 1;
				break;
			}
			markblock(ptrs[n], size, n, 0);
		}
		else {
			size = psizes[n];
			if (checkblock(ptrs[n], size, n, 0)) {
				failed = 1;
				break;
			}
			free(ptrs[n]);
			ptrs[n] = NULL;
			psizes[n] = 0;
		}
		if (i%256==0) {
			printf(".");
		}
	}
	printf("\n");

	for (i=0; i<32; i++) {
		if (ptrs[i] != NULL) {
			free(ptrs[i]);
		}
	}

	if (failed) {
		printf("FAILED malloc test %d\n", testno);
	}
	else {
		printf("Passed malloc test %d\n", testno);
	}
}
Пример #14
0
static void freeblock (void *block) {
  if (block) {
    size_t size = *blocksize(block);
    block = checkblock(block);
    memset(block, -1, size+HEADER+MARKSIZE);  /* erase block */
    (free)(block);  /* free original block */
  }
}
Пример #15
0
/* 
 * mm_checkheap - Check the heap for consistency 
 */
void mm_checkheap(int verbose) 
{
	//printf("************ CHECK BEGIN ***************\n");
    char *bp = heap_listp;
	char *checkAll = heap_listp;
	int countblocks = -1;

    if (verbose) {
        printf("Heap: (%p)\n", heap_listp);
    }

    if ((GET_SIZE(HDRP(heap_listp)) != OVERHEAD) || !GET_ALLOC(HDRP(heap_listp))) {
        printf("Bad prologue header\n");
    }
    checkblock(heap_listp);
	
	
	 while(GET(SUCC(bp)) != 0){
		if(verbose == 1)
		 	printblock(bp);
		checkblock(bp);
		//countblocks++;
		bp = (char*)GET(SUCC(bp));
	}
	 if(verbose == 1)
		 printblock(bp);

    for (checkAll = heap_listp; GET_SIZE(HDRP(checkAll)) > 0; checkAll = NEXT_BLKP(checkAll)) {
		if(!GET_ALLOC(HDRP(checkAll)) && !block_in_free_list(checkAll)){
			printf("Block is marked free but not in free list\n");		
		}
        checkblock(checkAll);
		countblocks++;
    }

	//printf("Number of free blocks: %d\n", freeblockcount);
	//printf("Total number of blocks: %d\n", countblocks);
    
    if ((GET_SIZE(HDRP(checkAll)) != 0) || !(GET_ALLOC(HDRP(checkAll)))) {
        printf("Bad epilogue header\n");
    }
	//printf("****************** CHECK END ********************\n");
}
Пример #16
0
/*
 * mm_checkheap - Checks the invariants of the heap
 * This function checks the invariants of the heap such as header-footer match,
 * absence of contiguous blocks of free memory and confirmation that the
 * block pointers are within the heaps boundaries.
 */
void mm_checkheap(int verbose)
{
  void *bp = heap_listp; //Points to the first block in the heap
  verbose = verbose;

  /* If first block's header's size or allocation bit is wrong,
   * the prologue header is incorrect. */

  if ((GET_SIZE(HDRP(heap_listp)) != MINBLOCKSIZE) ||
      !GET_ALLOC(HDRP(heap_listp)))
    printf("Bad prologue header\n");
  checkblock(bp);

  for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp))
    {
      if (verbose)
	printblock(bp);
      if(GET_SIZE(HDRP(bp)) == 0)
	printf("Error: block has zero size\n");
      checkblock(bp);

      /* Checking to see if free block in the free list */
      if(!GET_ALLOC(HDRP(bp)))
	checkInFreeList(bp);

    }

  /* If last block's header's size or allocation bit is wrong,
   * the epilogue's header is wrong. We also check the specific location
   * of the epilogue header with respect to the last byte of the heap.
   * mem_heap_hi() and bp(at this point) both point to the same word,
   * but mem_heap_hi() points to the last byte and hence we must add 3 to
   * the address of the header of bp to check this condition.
   */
  if (GET_SIZE(HDRP(bp)) != 0 || (GET_ALLOC(HDRP(bp)) != 1)
      || (HDRP(bp) + WSIZE - 1) != mem_heap_hi())
    printf("Bad epilogue header\n");

  for(bp = free_listp; GET_ALLOC(HDRP(bp)) == 0; bp = NEXT_FREE(bp))
    checkFreeNodes(bp);

}
Пример #17
0
/*
     * checkheap
     * Check the consistency of the memory heap
     * Return nonzero if the heap is consistant.
     */
 void checkheap(bool verbose){

        int i;
        void *bp;  

        for(i = 0; i < TOTAL_LISTS; i++) {
            bp = free_listp[i];       
  
            while (bp) {                       
                      //Checking for a allocated block in free list

                if (GET_ALLOC(HDRP(bp)) == 1 || GET_ALLOC(FTRP(bp)) == 1){
                        printf("Found a allocated block in %dth freelist\n",i);

                        return;
                }     
                bp  = getNext(bp);
            }
        }

            if (verbose)
	            printf("Heap (%p):\n", heaplistp);

            if (GET_SIZE(HDRP(heaplistp)) != DSIZE ||!GET_ALLOC(HDRP(heaplistp)))
                    printf("Bad prologue header\n");

            checkblock(heaplistp);

            for (bp = heaplistp; GET_SIZE(HDRP(bp)) > 0; bp = (void *)NEXT_BLKP(bp)) {

	        if (verbose)
          	      printblock(bp);

                checkblock(bp);
        }
            if (verbose)
		printblock(bp);

            if (GET_SIZE(HDRP(bp)) != 0 || !GET_ALLOC(HDRP(bp)))
           	printf("Bad epilogue header\n");
    }
Пример #18
0
/*
 * malloc
 */
block_ptr malloc(size_t size)
{
	size_t asize;      /* Adjusted block size */
	size_t extendsize; /* Amount to extend heap if no fit */
	char *bp;

	if (heap_listp == 0)
	{
		mm_init();
	}

	/* Ignore spurious requests */
	if (size == 0)
		return NULL;


	/* Adjust block size to include overhead and alignment reqs. */
	if (size <= DSIZE)
		asize = 2 * DSIZE;
	else
		asize = DSIZE * ((size + (WSIZE) + (DSIZE - 1)) / DSIZE);

#ifdef DEBUG
	printf("\nMalloc request: size = %zu, rounded to %zu \033[41;37m[ID:%d]\033[0m\n", size, asize, operid++);
#endif

	/* Search the free list for a fit */
	if ((bp = find_fit(asize)) != NULL)
	{
#ifdef DEBUG
		{
			puts("Found fit!");
			checkblock(bp);
			printblock(bp);
		}
#endif
		place(bp, asize);
		return bp;
	}

	/* No fit found. Get more memory and place the block */
	extendsize = MAX(asize, BLOCKSIZE);
	if ((bp = extend_heap(extendsize / WSIZE)) == NULL)
		return NULL;
	place(bp, asize);
	return bp;
}
Пример #19
0
static
void
test2(void)
{
	volatile unsigned *x;
	size_t size;
	
	printf("Entering malloc test 2.\n");
	printf("Make sure you read and understand the comment in malloctest.c "
	       "that\nexplains the conditions this test assumes.\n\n");

	printf("Testing how much memory we can allocate:\n");
	
	for (size = HUGESIZE; (x = malloc(size))==NULL; size = size/2) {
		printf("  %9lu bytes: failed\n", (unsigned long) size);
	}
	printf("  %9lu bytes: succeeded\n", (unsigned long) size);

	printf("Passed part 1\n");

	printf("Touching all the words in the block.\n");
	markblock(x, size, 0, 1);

	printf("Validating the words in the block.\n");
	if (checkblock(x, size, 0, 1)) {
		printf("FAILED: data corrupt\n");
		return;
	}
	printf("Passed part 2\n");


	printf("Freeing the block\n");
	free((void *)x);
	printf("Passed part 3\n");
	printf("Allocating another block\n");
	
	x = malloc(size);
	if (x==NULL) {
		printf("FAILED: free didn't return the memory?\n");
		return;
	}
	free((void *)x);

	printf("Passed malloc test 2.\n");
}
Пример #20
0
/*---------------------------------------
|					|
|	     gettrace()/2		|
|					|
|   This function gives access to a	|
|   single trace within a 2D data	|
|   set.				|
|					|
+--------------------------------------*/
float *gettrace(int trace, int fpoint)
{
   int	res,
	ok;


   if (interuption)
      return(NULL);
   long_event();

/***********************************************
*  Check to see if the phasefile data need to  *
*  be recalculated from the datafile data.     *
***********************************************/

   if (checkblock(trace, &ok, D_PHASFILE))
      return(NULL);

/**************************************************
*  Get the datafile data and process for display  *
*  according the "dmg" and "dmg1" (if relevant)   *
*  display parameters.                            *
**************************************************/

   if (!ok)
   { /* must calculate from complex or hypercomplex data, includes 1D or 2D */
      if (getdata())
         return(NULL);
      if ( (res = D_markupdated(D_PHASFILE, c_buffer)) )
      {
         D_error(res);
         return(NULL);
      }
   }

   return(c_block.data + (trace - c_first)*(fn/2) + fpoint);
}
Пример #21
0
static
void
test1(void)
{
	volatile unsigned *x;

	printf("*** Malloc test 1 ***\n");
	printf("Allocating %u bytes\n", BIGSIZE);
	x = malloc(BIGSIZE);
	if (x==NULL) {
		printf("FAILED: malloc failed\n");
		return;
	}

	markblock(x, BIGSIZE, 0, 0);
	if (checkblock(x, BIGSIZE, 0, 0)) {
		printf("FAILED: data corrupt\n");
		return;
	}

	free((void *)x);

	printf("Passed malloc test 1.\n");
}
Пример #22
0
void MiniMax::scoreState(tree& state) {

    /* Score the given state */

    int m = state.board.size();                     // num columns
    int n = state.board[0].length() - 1;            // num rows
    int R = state.R;                                // for my convenience
    int d = ((m - R) + (n - R) + 1);                // num diags (usable)
    int s = n - R;                                  // anchor point for diags
    int score = 0;                                  // the score we will assign

    std::vector<std::string> rows;                  // board rows
    std::vector<std::string> diaD;                  // board down diags (usable)
    std::vector<std::string> diaU;                  // board up diags (usable)

    // this prevents multiple resize operations and initializes all values
    // in each vector
    rows.resize(n);
    diaD.resize(d);
    diaU.resize(d);

    // fill in rows, diaD, and diaU
    for (int i = 0; i < m; ++i) {
        for (int j = 0; j < n; ++j) {
            rows[j] += state.board[i][j];
            if ((-1 * s) - (m - n) <= (j - i) && (j - i) <= s) {     // up diags
                diaU[s - (j - i)] += state.board[i][j];
            }
            if (s < (i + j) && (i + j) < (s + d)) {      // down diags
                diaD[(i + j) - (s + 1)] += state.board[i][j];
            }
        }
    }

    int col = state.move;
    if (col < 0 || col >= (int) state.board.size()) {
        state.score = 0;
        return;
    }
    int row = state.board[col].find_last_of("XO");
    int diU = s - (col - row);
    int diD = (col + row) - (s + 1);
    std::string c = state.board[col];
    std::string r = rows[row];
    char placed = c.at(row);
    bool blocked = false;
    std::string D;
    if (diD >= 0 && diD < (int) diaD.size()) {
        D = diaD[diD];
        int x = (col + row >= s + R) ? col - ((col + row) - n) : col;
        blocked |= checkblock(D, placed, x, R);
    }
    std::string U;
    if (diU >=0 && diU < (int) diaU.size()) {
        U = diaU[diU];
        int x = (col - row >= 1) ? col - (col - row) : col;
        blocked |= checkblock(U, placed, x, R);
    }
    blocked |= (
        checkblock(c, placed, row, R) ||
        checkblock(r, placed, col, R)
    );
    if (blocked) {
        state.score =  (placed == 'X') ?
            std::numeric_limits<int>::max() - 2 :
            std::numeric_limits<int>::min() + 2;
        return;
    }

    // score all the things
    for (std::string& column : state.board) {
        int s = scoreLine(column, R);
        if (isWin(s)) {
            state.score = s;
            return;
        }
        score += s;
    }
    for (std::string& row : rows) {
        int s = scoreLine(row, R);
        if (isWin(s)) {
            state.score = s;
            return;
        }
        score += s;
    }
    for (std::string& diag : diaD) {
        int s = scoreLine(diag, R);
        if (isWin(s)) {
            state.score = s;
            return;
        }
        score += s;
    }
    for (std::string& diag : diaU) {
        int s = scoreLine(diag, R);
        if (isWin(s)) {
            state.score = s;
            return;
        }
        score += s;
    }

    state.score = score;

}
Пример #23
0
static void freeblock (void *block) {
  if (block)
    memset(block, -1, *blocksize(block));  /* erase block */
  free(checkblock(block));
}
Пример #24
0
/**
 * checkheap - Function to check the consistency of the heap
 * @param verbose How much information to be printed on the screen
 */
void checkheap(int verbose) 
{
    /* Checks:
     * 1. Check each block for
     *     a. Alignment
     *     b. Header and Footer :
     *         i. Size Match
     *         ii. Header Footer Match Each other
     *     c. In Heap
     * 2. Get next block in free list.
     * 3. Check for no extra block in heap, from the seg lists
     * 4. Check for the cycle in the list.
     * 5. Check Epilogue Block
     * 6. Check Prologue Block
     * 7. Check proper coalescing - no previous and next free blocks
     */
    
    char *bp = heap_listp;
    

    /**
     * run checkheap with verbose =9 to avoid compiler from giving error, 
     * and also making sure that checkheap doesnot run
     */
    if(verbose == 9) {
        return;
    }
    /**
     * Following condition never will be true. 
     * A place function to avoid compiler compalaining
     */
    if(verbose ==10) {
        print_free_block(GET_SEGI(seg_list,0));

    }
    if (verbose){
        dbg_printf("Heap (%p):\n", heap_listp);        
    }

    /*Check for epilogue*/
    if ((GET_SIZE(HDRP(heap_listp)) != DSIZE) || !GET_ALLOC(HDRP(heap_listp))){
        printf("ERROR: Bad prologue header\n");
    }
    
    /*Check header block for issues*/
    checkblock(heap_listp);
    /*Now check the complete heap*/
    for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
        if (verbose) {
            printblock(bp);/*If Verbose , Print the heap*/
        }
        checkblock(bp);
    }

    if (verbose)
        printblock(bp);
    /*Check for Epilogue*/
    if ((GET_SIZE(HDRP(bp)) != 0) || !(GET_ALLOC(HDRP(bp)))) {
        printf("ERROR: Bad epilogue header for block \n");
        printblock(bp);
    }

    /**
     * Checks with respect to seg list
     */
    
    /*Check if all the pointers in all the free lists are aligned*/
    check_seg_pointers();


    /* Check for count of free blocks, iterating over blocks and by 
     * going through next pointers*/
    check_count_free_list();

    /*Check for cycle in the free list*/
    check_cycle();
}
Пример #25
0
static
void
test3(void)
{
	struct test3 *list = NULL, *tmp;
	size_t tot=0;
	int ct=0, failed=0;
	void *x;

	printf("Entering malloc test 3.\n");
	printf("Make sure you read and understand the comment in malloctest.c "
	       "that\nexplains the conditions this test assumes.\n\n");

	printf("Testing how much memory we can allocate:\n");

	while ((tmp = malloc(sizeof(struct test3))) != NULL) {

		tmp->next = list;
		list = tmp;

		tot += sizeof(struct test3);

		markblock(list->junk, sizeof(list->junk), (uintptr_t)list, 0);

		ct++;
		if (ct%128==0) {
			printf(".");
		}
	}

	printf("Allocated %lu bytes\n", (unsigned long) tot);

	printf("Trying some more allocations which I expect to fail...\n");

	x = malloc(SMALLSIZE);
	if (x != NULL) {
		printf("FAILED: malloc(%u) succeeded\n", SMALLSIZE);
		return;
	}

	x = malloc(MEDIUMSIZE);
	if (x != NULL) {
		printf("FAILED: malloc(%u) succeeded\n", MEDIUMSIZE);
		return;
	}

	x = malloc(BIGSIZE);
	if (x != NULL) {
		printf("FAILED: malloc(%u) succeeded\n", BIGSIZE);
		return;
	}

	printf("Ok, now I'm going to free everything...\n");

	while (list != NULL) {
		tmp = list->next;

		if (checkblock(list->junk, sizeof(list->junk), 
			       (uintptr_t)list, 0)) {
			failed = 1;
		}

		free(list);
		list = tmp;
	}

	if (failed) {
		printf("FAILED: data corruption\n");
		return;
	}

	printf("Let me see if I can allocate some more now...\n");

	x = malloc(MEDIUMSIZE);
	if (x == NULL) {
		printf("FAIL: Nope, I couldn't.\n");
		return;
	}
	free(x);
	
	printf("Passed malloc test 3\n");
}
Пример #26
0
/* newspec	flag for whether phase file has been updated */
float *calc_user(int trace, int fpoint, int dcflag, int normok, int *newspec, int file_id)
{
   int		res,
		ok;
   float	datamax,
		*spec_ptr;


/*****************************************************
*  Check to see if data in phasefile block needs to  *
*  be recalculated from data in datafile block.      *
*****************************************************/

   *newspec = FALSE;
   if (interuption)
      return(NULL);
   if (checkblock(trace, &ok, file_id))
      return(NULL);

   if (!ok)
   { /* must calculate from complex or hypercomplex data */
      if (getdata())
         return(NULL);
      *newspec = TRUE;
   }

   spec_ptr = c_block.data + (trace - c_first)*(fn/2);

/*********************
*  Drift correction  *
*********************/

   if (dcflag)
      dodc(spec_ptr, 1, c_block.head->lvl, c_block.head->tlt);

   if ((fabs(c_block.head->lvl - (float)lvl) > 0.01) ||
        (fabs(c_block.head->tlt - (float)tlt) > 0.01))
   {
      leveltilt(spec_ptr, 1, c_block.head->lvl, c_block.head->tlt);
      c_block.head->lvl = (float)lvl;
      c_block.head->tlt = (float)tlt;
      c_block.head->scale = 0;
      c_block.head->ctcount = 0;
      *newspec = TRUE;
   }

/******************
*  Normalization  *
******************/

   if (normok && normflag)
   {
      if (c_block.head->ctcount == 0)
      {
         maxfloat(spec_ptr, fn/2, &datamax);
         setnormfactor(&(c_block.head->ctcount), datamax);
      }
      getnormfactor(&(c_block.head->ctcount), &datamax);
      if (datamax <= 1e-5)
         datamax = 1e-5;
      normalize = 1.0 / datamax;
   }

/*********************************************
*  If a "new spectrum" has been calculated,  *
*  update the internal buffer in which this  *
*  spectrum is stored.                       *
*********************************************/

   if (*newspec)
   {
      if ( (res = D_markupdated(file_id, c_buffer)) )
      {
         D_error(res);
         return(NULL);
      }
   }

   return(spec_ptr + fpoint);
}