void Client::sendPlayerPos()
{
	LocalPlayer *myplayer = m_env.getLocalPlayer();
	if(myplayer == NULL)
		return;

	// Save bandwidth by only updating position when something changed
	if(myplayer->last_position == myplayer->getPosition() &&
			myplayer->last_speed == myplayer->getSpeed() &&
			myplayer->last_pitch == myplayer->getPitch() &&
			myplayer->last_yaw == myplayer->getYaw() &&
			myplayer->last_keyPressed == myplayer->keyPressed)
		return;

	myplayer->last_position = myplayer->getPosition();
	myplayer->last_speed = myplayer->getSpeed();
	myplayer->last_pitch = myplayer->getPitch();
	myplayer->last_yaw = myplayer->getYaw();
	myplayer->last_keyPressed = myplayer->keyPressed;

	u16 our_peer_id;
	{
		//MutexAutoLock lock(m_con_mutex); //bulk comment-out
		our_peer_id = m_con.GetPeerID();
	}

	// Set peer id if not set already
	if(myplayer->peer_id == PEER_ID_INEXISTENT)
		myplayer->peer_id = our_peer_id;
	// Check that an existing peer_id is the same as the connection's
	if (myplayer->peer_id != our_peer_id)
		return;

	MSGPACK_PACKET_INIT(TOSERVER_PLAYERPOS, 5);
	PACK(TOSERVER_PLAYERPOS_POSITION, myplayer->getPosition());
	PACK(TOSERVER_PLAYERPOS_SPEED, myplayer->getSpeed());
	PACK(TOSERVER_PLAYERPOS_PITCH, myplayer->getPitch());
	PACK(TOSERVER_PLAYERPOS_YAW, myplayer->getYaw());
	PACK(TOSERVER_PLAYERPOS_KEY_PRESSED, myplayer->keyPressed);
	// Send as unreliable
	Send(0, buffer, false);
}
Exemplo n.º 2
0
/* 
 * Requires:
 *   words: the number of words to increase the heap by
 *   next: next pointer at the end of the linked list, to be set to the 
 	   header of the new block
 * Effects:
 *   Extend the heap with a free block and return that block's address.
 */
static void *
extend_heap(size_t words) 
{
	size_t size;
	void *bp;

	/* Allocate an even number of words to maintain alignment. */
	size = (words % 2) ? (words + 1) * WSIZE : words * WSIZE;

	if ((bp = mem_sbrk(size)) == (void *)-1)  
		return (NULL);
	printf("Before extended block is added to the free list\n");
	checkheap(1);
	/* Initialize the new node pointers, next precedes previous */
	/* The previous point points to the header of the previous block*/	
	
	/*PUT(bp, NULL);
	PUT(bp + WSIZE, HDRP(next));*/

	/* Initialize free block header/footer and the epilogue header. */
	PUT(HDRP(bp), PACK(size, 0));         /* Free block header */
	PUT(FTRP(bp), PACK(size, 0));         /* Free block footer */
	PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* New epilogue header */
	
	/* If list start is NULL, initialize the start to the pointer to the
	 * added block
	 */
	if (list_start == NULL) {
		list_start = (struct node *)bp;
		list_start->next = list_start;
		list_start->previous = list_start;
	}

	printf("For isolation\n");

	add_to_front(bp);
	printf("After extended block is added to the free list\n");
	checkheap(1);
	printf("Entering coalesce from extend_heap\n");
	/* Coalesce if the previous block was free. */
	return (coalesce(bp));
}
/* 
 * free - Free a block 
 */
void free(void *bp)
{
    if (bp == 0) 
        return;

    size_t size = GET_SIZE(HDRP(bp));
    if (heap_listp == 0){
        mm_init();
    }

    PUT(HDRP(bp), PACK(size, 0));
    PUT(FTRP(bp), PACK(size, 0));
    insert_node(bp, size);
    coalesce(bp);
    line_count++;
    if (CHECK && CHECK_FREE) {
    mm_check('f', bp, size);
  }

}
Exemplo n.º 4
0
/**
 * Initialize the memory manager.
 * @param - void no parameter passed in
 * @return - int 0 for success or -1 for failure
 */
int mm_init(void) {
	
	/*return -1 if unable to get heap space*/
	if ((heap_listp = mem_sbrk(2*BLKSIZE)) == NULL) 
		return -1;

	PUT(heap_listp, 0); 								/* alignment padding */ 
	PUT(heap_listp + (1*WSIZE), PACK(BLKSIZE, 1)); 		/* prologue header */
	PUT(heap_listp + (2*WSIZE), 0); 					/* prev pointer */
	PUT(heap_listp + (3*WSIZE), 0); 					/* next pointer */
	PUT(heap_listp + BLKSIZE, PACK(BLKSIZE, 1)); 		/* prologue footer */ 
	PUT(heap_listp+  BLKSIZE + WSIZE, PACK(0, 1)); 		/* epilogue header */ 
	listp = heap_listp + DSIZE; 

	/* Extend the empty heap with a free block of BLKSIZE bytes */
	if (extend_heap(BLKSIZE) == NULL){ 
		return -1;
	}
	return 0;
}
Exemplo n.º 5
0
/* put header, footer and free_list pointers in a free block */
void free_ptr(void *ptr, void *prev, void *next, unsigned size)
{
    unsigned header = PACK(size, 0);
    // header
    PUT_INT(HDRP(ptr), header);
    // footer
    PUT_INT(FTRP(ptr), header);
    // prev and next
    PUT(PREV_PTR(ptr), prev);
    PUT(NEXT_PTR(ptr), next);
}
Exemplo n.º 6
0
static void *extend_heap(size_t words) {
    char *bp;
    size_t size;

    /* Allocate an even number of words to maintain alignment */
    size = (words % 2) ? (words + 1) * WSIZE : words * WSIZE;


    /* call for more memory space */
    if ((bp = mem_sbrk(size)) == (void *)-1)
        return (NULL);

    /* Initialize free block header/footer and the epilogue header */
    SET_HDRP(bp, PACK(size, 0));         /* free block header */
    SET_FTRP(bp, PACK(size, 0));         /* free block footer */
    SET_HDRP(NEXT_BLKP(bp), PACK(0, 1)); /* new epilogue header */

    /* coalesce bp with next and previous blocks */
    return coalesce(bp);
}
Exemplo n.º 7
0
/* 
 * Requires:
 *   "bp" is either the address of an allocated block or NULL.
 *
 * Effects:
 *   Free a block.
 */
void
mm_free(void *bp)
{
	size_t size;

	
	/* Ignore spurious requests. */
	if (bp == NULL)
		return;

	/* Free and coalesce the block. */
	size = GET_SIZE(HDRP(bp));

	/* Reset Header and Footer to be free */
	PUT(HDRP(bp), PACK(size, 0));
	PUT(FTRP(bp), PACK(size, 0));
	coalesce(bp);

	checkheap(1);
}
Exemplo n.º 8
0
Arquivo: mm.c Projeto: js6450/CSO
void mm_free(void *currblock)
{
  size_t size = GETSIZE(HEADER(currblock)); // Size of block 
  
  UNSETRTAG(HEADER(NEXT(currblock)));
  
  PUT(HEADER(currblock), PACK(size, 0));
  PUT(FOOTER(currblock), PACK(size, 0));
  
  insertfree(currblock, size);
  
  coalesce(currblock);
  
  line_count++;
  if (MMCHECK) {
    mm_check('f', currblock, size);
  }

  return;
}
Exemplo n.º 9
0
/*
 * Initialize: return -1 on error, 0 on success.
 */
int mm_init(void) {
    /* Create the initial empty heap */
    if ((heap_listp = mem_sbrk(4*WSIZE)) == (void *)-1) //line:vm:mm:begininit
    return -1;
    PUT(heap_listp, 0);                          /* Alignment padding */
    PUT(heap_listp + (1*WSIZE), PACK(DSIZE, 1)); /* Prologue header */ 
    PUT(heap_listp + (2*WSIZE), PACK(DSIZE, 1)); /* Prologue footer */ 
    PUT(heap_listp + (3*WSIZE), PACK(0, 1));     /* Epilogue header */
    heap_listp += (2*WSIZE); 

    #ifdef NEXT_FIT
    rover = heap_listp;
#endif
/* $begin mminit */

    /* Extend the empty heap with a free block of CHUNKSIZE bytes */
    if (extend_heap(CHUNKSIZE/WSIZE) == NULL) 
        return -1;
    return 0;
}
Exemplo n.º 10
0
void Server::SendHP(u16 peer_id, u8 hp)
{
	DSTACK(FUNCTION_NAME);
	std::ostringstream os(std::ios_base::binary);

	MSGPACK_PACKET_INIT(TOCLIENT_HP, 1);
	PACK(TOCLIENT_HP_HP, hp);

	// Send as reliable
	m_clients.send(peer_id, 0, buffer, true);
}
Exemplo n.º 11
0
static void *extend_heap(size_t words){
#ifdef DEBUG
    printf("call a extend[%d] for %dbyte\n",++exCnt,words*WSIZE);
#endif
    char *bp;
    size_t size;

    size = (words%2)?(words+1)*WSIZE : words*WSIZE;
    if((int)(bp = mem_sbrk(size)) == -1)
        return NULL;
#ifdef DEBUG
    printf("tot heapsize = %d\n",(char*)bp-heap_listp+size+DSIZE);
#endif
    PUT(HDRP(bp), PACK(size, 0));
    PUT(FTRP(bp), PACK(size, 0));
    PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1));/*next block does not exsist, but the header of it does, the epiloge*/
    /*Initialize */
    //printf("bp from exh %x\n",bp);
    return coalesce(bp);
}
Exemplo n.º 12
0
static void *extend_heap(size_t words)
{
        char *bp;
        size_t size;

        /* Allocate an even number of words to maintain aligment */
        size = (words % 2) ? (words+1) * WSIZE : words * WSIZE;
        bp = mem_sbrk(size);
        if ((long)bp == -1)
                return NULL;

        /* Initialize free block header/footer and the epilogue header */
        int prev_alloc = GET_ALLOC_PREV(HDRP(bp));
        PUT(HDRP(bp), PACK(size, prev_alloc)); /* Free block header */
        PUT(FTRP(bp), PACK(size, prev_alloc)); /* Free block footer */
        PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1));  /* New epilogue header */

        /* Coalesce if the previous block was free */
        return coalesce(bp);
}
Exemplo n.º 13
0
int mm_init(void)
{
    /* create the initial empty heap */
    if( (heap_list_ptr = mem_sbrk(6 * WSIZE)) == (void *)-1 )
        return -1;
    PUT( heap_list_ptr + (2 * WSIZE), 0 ); // alignment padding
    PUT( heap_list_ptr + (3 * WSIZE), PACK(DSIZE, 1) ); // prologue HDRPer
    PUT( heap_list_ptr + (4 * WSIZE), PACK(DSIZE, 1) ); // prologue FTRPer
    PUT( heap_list_ptr + (5 * WSIZE), PACK(0, 3) ); // epilogue HDRPer
    heap_list_ptr += (4 * WSIZE);
    /*init the global variables*/
    NULL_POINTER = (void *)0x800000000; 
    root = NULL_POINTER;
    list_for_16 = NULL_POINTER;
    list_for_8 = NULL_POINTER;
    /* Extend the empty heap with a free block of CHUNKSIZE bytes */
    if( extend_heap(MIN_SIZE_IN_TREE)==NULL )
        return -1;
    return 0;
}
Exemplo n.º 14
0
/*
 * mm_init - initialize the malloc package.
 */
int mm_init(void)
{
    /* Create the initial empty heap */
    if((heap_listp = mem_sbrk(MIN_BLCK_SIZE)) == (void *) -1)
        return -1;
    PUT(heap_listp + (0*WSIZE), PACK(3*WSIZE, 1));  /* Prologue header */
    PUT(heap_listp + (1*WSIZE), 0);                 /* Prologue prev */
    PUT(heap_listp + (2*WSIZE), 0);                 /* Prologue next */
    PUT(heap_listp + (3*WSIZE), PACK(0, 1));        /* epilogue header */

    free_listp = heap_listp + (1*WSIZE);
    prologue = heap_listp + (1*WSIZE);
    epilogue = heap_listp + (3*WSIZE);
    mm_check();

    /* Extend the empty heap with a free block of CHUNKSIZE bytes */
    if (extend_heap(CHUNKSIZE/WSIZE) == NULL)
        return (-1);
    return 0;
}
Exemplo n.º 15
0
//Initialize the heap in this block by making the prologue epilogue and providing extra padding.Also the prologue has space for prev/next and an empty padding since minimum block size is 24 which is max(min all block size, min free block size)
int mm_init(void)
{
    if((heap_listp = mem_sbrk(2 * OVERHEAD)) == NULL){                                     
        return -1;
    }

    PUT(heap_listp, 0);                                         
    PUT(heap_listp + WSIZE, PACK(OVERHEAD, 1));                 
    PUT(heap_listp + DSIZE, 0);                                 
    PUT(heap_listp + DSIZE + WSIZE, 0);                         
    PUT(heap_listp + OVERHEAD, PACK(OVERHEAD, 1));              
    PUT(heap_listp + WSIZE + OVERHEAD, PACK(0, 1));             
    head = heap_listp + DSIZE;                                  

    if(extend_heap(CHUNKSIZE / WSIZE) == NULL){                 
        return -1;
    }

    return 0;
}
Exemplo n.º 16
0
Arquivo: mm.c Projeto: dukelv/csci033
/*
 * initializes the dynamic allocator
 * arguments: none
 * returns: 0, if successful
 *          1, if an error occurs
 */
int mm_init(void) {
    if ((heap_listp = mem_sbrk(6 * WSIZE)) == (void *)-1)
    {
	return (-1);
    }
    PUT(heap_listp, 0);                            /* Alignment padding */
    PUT(heap_listp + (1 * WSIZE), PACK(2*DSIZE, 1)); /* Prologue header */
    PUT(heap_listp + (2 * WSIZE), 0);              /*next address is null*/
    PUT(heap_listp + (3 * WSIZE), 0);              /*prev address is null*/
    PUT(heap_listp + (4 * WSIZE), PACK(2*DSIZE, 1)); /* Prologue footer */
    PUT(heap_listp + (5 * WSIZE), PACK(0, 1));     /* Epilogue header */
    heap_listp += (2 * WSIZE);
    free_list = heap_listp;
    /* Extend the empty heap with a free block of CHUNKSIZE bytes. */
    if (extend_heap(CHUNKSIZE / WSIZE) == NULL)
    {
	return (-1);
    }
    return 0;
}
Exemplo n.º 17
0
/*
** Realloc by merging the next if it isn't reserved.
** Return 0 if the realloc has been done, -1 otherwise
*/
static int	realloc_by_merge(t_ptr block, size_t size)
{
    size_t	total_size;
    size_t	real_size;

    if (size <= DWORD_SIZE)
        real_size = size + EXTRA_SIZE;
    else
    {
        real_size = ((size + EXTRA_SIZE + DWORD_SIZE - 1)
                     / DWORD_SIZE) * DWORD_SIZE;
    }
    total_size = GET_B_SIZE(block) + GET_B_SIZE(GET_NEXT(block));
    if (IS_B_ALLOC(GET_NEXT(block)) || total_size < real_size)
        return (-1);
    SET(GET_HEAD(block), PACK(total_size, 0));
    SET(GET_FOOT(block), PACK(total_size, 0));
    heap_reserve(block, real_size);
    return (0);
}
Exemplo n.º 18
0
/*
 * Initialize: return -1 on error, 0 on success.
 */
int mm_init(void) 
{

  if((heapStart = mem_sbrk(4 * WSIZE)) == (void *)-1) return -1 ;
  //allocation failed
  //heapStart contains first byte of heap, will not change
  PUT(heapStart, 0) ; //alignment padding
  PUT(heapStart + WSIZE , PACK(DSIZE, 1)) ; // prologue header
  PUT(heapStart + (2*WSIZE), PACK(DSIZE,1)) ; //prologue footer
  PUT(heapStart + (3*WSIZE), PACK(0,1)); //epilogue
  prologue = heapStart + WSIZE ;
  epilogue = heapStart + (3*WSIZE) ;
  firstFBP = NULL ; // set first free block to epilogue
  //will be free after extend_heap called
  
  extend_heap(CHUNKSIZE*2, EXT_INIT) ;
  dmm_checkheap(319);
  ASSERT(validFreeList(firstFBP)) ;
  return 0;
}
Exemplo n.º 19
0
static void *extend_heap(size_t words)
{
    void *bp;
    size_t size;

    /* Allocate an even number of words to maintain alignment */
    size = (words % 2) ? (words+1) * WSIZE : words * WSIZE;
    if ((long)(bp = mem_sbrk(size)) == -1)
        return NULL;
    //printf("size %d\n",(int)(size));
    /* Initialize free block header/footer and the epilogue header */
    PUT(HDRP(bp), PACK(size, 0));         /* Free block header */
    PUT(FTRP(bp), PACK(size, 0));         /* Free block footer */
    PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* New epilogue header */

    //print_list();

    /* Coalesce if the previous block was free */
    return coalesce((void*)bp);
}
Exemplo n.º 20
0
Arquivo: mm.c Projeto: nik-6947/malloc
static void *
extend_heap(size_t words) 
{
  char *bp;
  size_t size;

  /* Arithmetic to maintain alignment */
  size = (words % 2) ? (words+1) * WSIZE : words * WSIZE;
  
  if ((bp = mem_sbrk(size)) == (void *)-1){ 
    return NULL;
  }
  /* Initialize free block header/footer and the epilogue header */
  PUT(HDRP(bp), PACK(size, 0));         /* free block header */
  PUT(FTRP(bp), PACK(size, 0));         /* free block footer */
  PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* new epilogue header */
  
  /* coalesce if next and previous block is free*/
  return coalesce(bp);
}
Exemplo n.º 21
0
/**********************************************************
 * mm_init
 * Initialize the heap, including "allocation" of the
 * prologue and epilogue
 **********************************************************/
int mm_init(void)
{
	//free_listp = NULL;
	if ((heap_listp = mem_sbrk(4*WSIZE)) == (void *)-1)
		return -1;
	PUT(heap_listp, 0);                         // alignment padding
	PUT(heap_listp + (1 * WSIZE), PACK(DSIZE, 1));   // prologue header
	PUT(heap_listp + (2 * WSIZE), PACK(DSIZE, 1));   // prologue footer
	PUT(heap_listp + (3 * WSIZE), PACK(0, 1));    // epilogue header
	heap_listp += DSIZE;

	//initialize your segregated lists
	int i;
	for(i = 0; i < FREE_SIZE_BUCKETS; i++)
	{
		segregated_list[i] = NULL;
	}

	return 0;
}
Exemplo n.º 22
0
/*
 * Initialize: return -1 on error, 0 on success.
 *
 * mm_init - Called when a new trace starts
 *
 * @return 	-  (0) if all ok
 * 		- (-1) otherwise
 */
int mm_init(void)
{
	heap_ptr = mem_sbrk(4*WSIZE);

	PUT(heap_ptr, 0);	/* Alignment padding OR Heap Prologue */
	PUT( (heap_ptr + (1*WSIZE)), PACK(DSIZE, 1) );	/* Prologue header */
	PUT( (heap_ptr + (2*WSIZE)), PACK(DSIZE, 1) );	/* Prologue footer */
	PUT( (heap_ptr + (3*WSIZE)), PACK(0, 1) );	/* Epilogue header */
	
	heap_ptr += (2*WSIZE);

	if( (long)heap_ptr < 0 )
		return -1;

 	/* Extend the heap with a free block of CHUNK_SIZE bytes */
	if( extend_heap(CHUNK_SIZE) == NULL )
		return -1;

	return 0;
}
Exemplo n.º 23
0
/**********************************************************
 * extend_heap
 * Extend the heap by "words" words, maintaining alignment
 * requirements of course. Free the former epilogue block
 * and reallocate its new header
 **********************************************************/
void *extend_heap(size_t words)
{
    PRINTDBG (("Extend heap: %ld\n", words * WSIZE));

    char *bp;
    size_t size;

    /* Allocate an even number of words to maintain alignments */
    size = (words % 2) ? (words+1) * WSIZE : words * WSIZE;
    if ((bp = mem_sbrk(size)) == (void *)-1 )
        return NULL;

    /* Initialize free block header/footer and the epilogue header */
    PUT(HDRP(bp), PACK(size, 0));                // free block header
    PUT(FTRP(bp), PACK(size, 0));                // free block footer
    PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1));        // new epilogue header

    /* Coalesce if the previous block was free */
    return coalesce(bp, 0);
}
Exemplo n.º 24
0
Arquivo: mm.c Projeto: Boraz/CSCI-2400
//
// mm_init - Initialize the memory manager
//
int mm_init(void)
{
	CL_init(&free_list);
	
	if ((heap_listp = mem_sbrk(4*WSIZE)) == (void *)-1) { // heap start
		return -1;
    }
    
    PUT(heap_listp, 0);
    PUT(heap_listp + (WSIZE), PACK(DSIZE, 1));
    PUT(heap_listp + (DSIZE), PACK(DSIZE, 1));
    PUT(heap_listp + (3 * WSIZE), PACK(0, 1));
    heap_listp += (DSIZE);
    
    if (extend_heap(CHUNKSIZE / WSIZE) == NULL) {
		return -1;
	}
	
	return 0;
}
Exemplo n.º 25
0
/*
 * Extend_Heap
 */
static void *extend_heap(size_t words)
{
	char *bp;
	size_t size;

	/* Allocate an even number of words to maintain alignement */
	size = (words % 2) ? (words+1) * WSIZE : words * WSIZE;
	if ((long)(bp = mem_sbrk(size)) == -1)
		return NULL;
	size_t previous = GET_PREV_ALLOC(HDRP(bp));

	/* Initialize free block header/footer and the epilogue header */
	PUT(HDRP(bp), PACK(size,previous, 0));
	PUT(FTRP(bp), size);
	epilogueaddr = HDRP(NEXT_BLKP(bp));
	PUT(HDRP(NEXT_BLKP(bp)), PACK(0,2,1));

	/* Coalesce if the previous block was free */
	return coalesce(bp);
}
Exemplo n.º 26
0
/*
 * Extend the heap with a new free block
 */
static void *extend_heap(size_t words){
	char *bp;
	size_t size;
	
	/*Allocate an even number of words to maintain alignment*/
	size = (words % 2) ? (words+1) * WSIZE : words * WSIZE;
	if ((long)(bp = mem_sbrk(size)) == -1){
		fprintf(stderr, "extend_heap: mem_sbrk error\n");
		return NULL;
	}
	/* Initialize free block header/footer and the epilogue header */
	PUT(HDRP(bp), PACK(size, 0)); /* Free block header */
	PUT(FTRP(bp), PACK(size, 0)); /* Free block footer */
	PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* New epilogue header */
	
	dbg_printf( "extend_heap: heap extendted to %d\n",(int)(HDRP(NEXT_BLKP(bp))-heap_listp));
	
	/* Coalesce if the previous block was free */
	return coalesce(bp);
}
Exemplo n.º 27
0
Arquivo: mm.c Projeto: horf31/ece454
/**********************************************************
 * extend_heap
 * Extend the heap by "words" words, maintaining alignment
 * requirements of course. Free the former epilogue block
 * and reallocate its new header
 **********************************************************/
void *extend_heap(size_t words) {
	intptr_t *bp;
	size_t size;

	/* Allocate an even number of words to maintain alignments */
	size = (words % 2) ? (words + 1) * WSIZE : words * WSIZE;
	if ((bp = mem_sbrk(size)) == (void *) -1)
		return NULL;

	/* Initialize free block header/footer and the epilogue header */
	PUT(HDRP(bp), PACK(size, 0));                // free block header
	PUT(FTRP(bp), PACK(size, 0));                // free block footer
	PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1));        // new epilogue header

	/* Coalesce if the previous block was free */
	// Trade-off between util and thru
//    bp = coalesce(bp);

	return bp;
}
Exemplo n.º 28
0
static void *extend_heap(size_t words) {

    size_t size;
    size = (words & 1) ? (words + 1) : words;
    size = size * WSIZE;

    char *bp;
    bp = hmm_mem_sbrk(size);
    if (bp == (void *)-1)
        return NULL;

    // The new block header and footer
    PUT(HDRP(bp), PACK(size, 0));
    PUT(FTRP(bp), PACK(size, 0));

    // Epilogue header
    PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1));

    return coalesce(bp);
}
Exemplo n.º 29
0
/*split the block if the given free block has larger size than the required size*/
void* splitBlock(void* bp, size_t asize) {
    size_t totalsize = GET_SIZE(HDRP(bp));
    size_t delta = totalsize - asize;
    //if the remaining free block larger than min block size we can split
    //we know that both bp size and required size are aligned with 8 
    //so delta is also aligned with 8

    if (delta >= MIN_BLOCK_SIZE) {
        //split
        PUT(HDRP(bp), PACK(asize, 0));
        PUT(FTRP(bp), PACK(asize, 0)); //footer
        //insert the rest free block to free list
        PUT(HDRP(NEXT_BLKP(bp)), PACK(delta, 0));
        PUT(FTRP(NEXT_BLKP(bp)), PACK(delta, 0));
        insertToFreeList(NEXT_BLKP(bp));

        return bp;
    }
    return bp;
}
Exemplo n.º 30
0
/// Updates the lookup table
void CvDistanceMapTurns::Update()
{
	//performance optimization, reduce pathfinding range
	int iMaxTurns = GC.getGame().getElapsedGameTurns() == 0 ? 7 : 12;
	int iVeryFar = iMaxTurns * 6;

	const CvMap& map = GC.getMap();
	int nPlots = map.numPlots();

	m_vDistance = std::vector<int>(nPlots, iVeryFar);
	m_vClosestFeature = std::vector<int>(nPlots,0);
	m_bArrayAllocated = true;
		
	for (int i = 0; i < MAX_PLAYERS; i++)
	{
		//if we have a set player, ignore all others
		if (m_ePlayer!=NO_PLAYER && m_ePlayer!=i)
			continue;

		// for each city
		CvPlayer& thisPlayer = GET_PLAYER((PlayerTypes)i);
		int iCityIndex = 0;
		for(CvCity* pLoopCity = thisPlayer.firstCity(&iCityIndex); pLoopCity != NULL; pLoopCity = thisPlayer.nextCity(&iCityIndex))
		{
			//slow update only for plots close to the city
			ReachablePlots turnsFromCity;
			SPathFinderUserData data(m_ePlayer, PT_GENERIC_REACHABLE_PLOTS, -1, iMaxTurns);
			turnsFromCity = GC.GetStepFinder().GetPlotsInReach(pLoopCity->plot(), data);

			for (ReachablePlots::iterator it = turnsFromCity.begin(); it != turnsFromCity.end(); ++it)
			{
				int iTurns = it->iTurns;
				bool bUpdate = (iTurns < m_vDistance[it->iPlotIndex]);
					
				//in case of equal distance, take care not to prefer the player with the lower ID
				if (iTurns == m_vDistance[it->iPlotIndex])
				{
					PlayerTypes currentOwner = (PlayerTypes)UNPACK_OWNER(m_vClosestFeature[it->iPlotIndex]);
					CvCity* pCurrentCity = GET_PLAYER(currentOwner).getCity(UNPACK_ID(m_vClosestFeature[it->iPlotIndex]));
					//it can happen that there is no current city if the plot has never been updated because it's very remote
					bUpdate = (pCurrentCity==NULL) || (pCurrentCity->getGameTurnFounded() > pLoopCity->getGameTurnFounded());
				}

				if (bUpdate)
				{
					m_vDistance[it->iPlotIndex] = iTurns;
					m_vClosestFeature[it->iPlotIndex] = PACK(pLoopCity->getOwner(), pLoopCity->GetID());
				}
			}
		}
	}

	m_bDirty = false;
}