void TestAddMemory()	{

    GameProperties testGame;
    testGame = createGame();
    addMemory(100);
    sput_fail_unless(getAvailableMemory() == 100,"Adding MEmory");
    sput_fail_unless(addMemory(-100) == 0,"Adding Negative Memory");
    free(testGame);
}
示例#2
0
/*
* Does the specified ammount of damage to the specified enemy. Reduces damage by the amount of armour the enemy has first.
* If damage reduces health to less than 0, kills enemy and adds memory equivalent to enemy's max health
*/
void damageEnemy(int damage, int enemyID, int damageType)
{
    Enemy e = getEnemyGroup(NULL)->enemyArray[enemyID];
    if(!isDead(enemyID)) {
      int damageToBeDone;
      
        // modify the damge based on the type of enemy and type of damage
      if(e->eFamily == damageType) {
        damageToBeDone = (damage*TYPE_MATCH_MODIFIER) - e->armour;
      } else {
        damageToBeDone = (damage/TYPE_MISMATCH_MODIFIER) - e->armour;
      }
        
      if(damageToBeDone <= 0) {
        damageToBeDone = 0;
      }
      
      e->health -= damageToBeDone;
      if(e->health<=0 && e->dead != 1)
      {
		  increaseDeathCnt();
		      e->health=0;
          e->dead=1;
          addMemory(e->maxHealth);
          // drawDeath(e->x, e->y);
          //drawKillAll();
      }
    }
}
示例#3
0
文件: my_malloc.c 项目: 4rzael/malloc
void	*malloc(size_t size)
{
  void	*ptr;
  if (start == NULL)
    {
      start = sbrk(0);
      end = start;
    }
  #ifdef DEBUG
  printf("start = %p, end = %p\n", start, end);
  #endif
  ptr = findMemory(start, end, size);
  #ifdef DEBUG
  printf("\nafter findMemory ptr = %p\n", ptr);
  #endif
  if (addMemory(start, &end, &ptr, size) == 0)
    return (0);
  #ifdef DEBUG
  printf("\nafter addMemory ptr = %p\n", ptr);
  printf("after addMemory start = %p, end = %p\n", start, end);
  #endif
  useMemory(ptr, end, size);
  ptr += sizeof(t_metadata);
  #ifdef DEBUG
  printf("\n\n");
  #endif
  return(ptr);
}
void testCheckMem()	{

    GameProperties testGame;
    testGame = getGame(NULL);
    setMemory(0);
    addMemory(10);
    sput_fail_unless(checkMem(10,testGame) == 1,"boundary Testing enough memory");
    useMemory(testGame,10);
    sput_fail_unless(checkMem(50,testGame) == 0,"Testing not enough memory");
    addMemory(100);
    sput_fail_unless(checkMem(100,testGame) == 1,"Testing enough memory");
    setMemory(0);
    test_KillAnEnemy();
    sput_fail_unless(getAvailableMemory() > 0, "Valid: More memory available after killing an enemy");
    freeAllEnemies();
}
示例#5
0
void *debugMalloc(uint32_t length, int32_t tag)
{
	unsigned char *memory;
	struct chunkHeader *chunkHeader;
	struct chunkTrailer *chunkTrailer;
	unsigned char *chunk;

// 	printf("sizeof(struct chunkHeader) = %u, sizeof (struct chunkTrailer) = %u\n", sizeof (struct chunkHeader), sizeof (struct chunkTrailer));

	memory = malloc(length + sizeof(struct chunkHeader) + sizeof(struct chunkTrailer));

	if (memory == NULL)
	{
		fprintf(stderr, "Cannot allocate %u bytes, malloc tag = %d\n", (unsigned int)(length + sizeof(struct chunkHeader) + sizeof(struct chunkTrailer)), tag);
		exit(1);
	}

	chunkHeader = (struct chunkHeader *)memory;
	chunk = memory + sizeof(struct chunkHeader);
	chunkTrailer = (struct chunkTrailer *)(memory + sizeof(struct chunkHeader) + length);

	chunkHeader->length = length;
	chunkHeader->tag = tag;
	chunkHeader->magicNumber = MAGIC_NUMBER;

	chunkTrailer->magicNumber = MAGIC_NUMBER;

	chunkHeader->next = chunkList;
	chunkList = chunkHeader;

#if defined MEMORY_USAGE

	addMemory( length, tag );

#endif

	return chunk;
}
示例#6
0
void *_debugMalloc(uint32_t length, int32_t tag) {
	
	unsigned char *memory;
	struct chunkHeader *chunkHeader;
	struct chunkTrailer *chunkTrailer;
	unsigned char *chunk;

	memory = malloc(length + sizeof(struct chunkHeader) + sizeof(struct chunkTrailer));

	if (memory == NULL)
	{
		dbg( DBGL_SYS, DBGT_ERR, "Cannot allocate %u bytes, malloc tag = %d", 
		     (unsigned int)(length + sizeof(struct chunkHeader) + sizeof(struct chunkTrailer)), tag );
		cleanup_all( -500076 );
	}

	chunkHeader = (struct chunkHeader *)memory;
	chunk = memory + sizeof(struct chunkHeader);
	chunkTrailer = (struct chunkTrailer *)(memory + sizeof(struct chunkHeader) + length);

	chunkHeader->length = length;
	chunkHeader->tag = tag;
	chunkHeader->magicNumber = MAGIC_NUMBER;

	chunkTrailer->magicNumber = MAGIC_NUMBER;

	chunkHeader->next = chunkList;
	chunkList = chunkHeader;

#ifdef MEMORY_USAGE

	addMemory( length, tag );

#endif

	return chunk;
}
MemoryPool::MemoryPool(void* data, size_t size) {
    addMemory(data, size);
}