Пример #1
0
int main(){

	srand(time(0));
	//Creating memory managers
	mm_t *MM = malloc(sizeof(mm_t));
	mm_init(MM,1500,sizeof(node));
	
	mm_t *MM2 = malloc(sizeof(mm_t));
	mm_init(MM2,3,sizeof(dl_list));
	
	//Creating Lists
    dl_list *dl_1 = mm_get(MM2);
    dl_list *dl_2 = mm_get(MM2);
    dl_list *dl_3 = mm_get(MM2);
    
    dl_list_init(dl_1);
    dl_list_init(dl_2);
    dl_list_init(dl_3);
	
	int i;
	for(i = 0; i<500; i++)
		insert_item(dl_1,i,MM);
	
	print_list(dl_1);
	
	for(i = 0; i<500; i++)
		insert_item(dl_2,rand()%500,MM);

	print_list(dl_2);
	
	for(i = 0; i<500; i++)
		insert_item(dl_3,get_element(dl_1,i)+get_element(dl_2,i),MM);
		
	print_list(dl_3);
	
	//Deleting items from list 1
	for(i = 0; i<100; i++)
		delete_item(dl_1,get_element(dl_3,i),MM);
	
	print_list(dl_1);
	
	//Joining lists
	join_lists(dl_2,dl_1);
	join_lists(dl_3,dl_2);
	
	print_list(dl_3);
	
	empty_list(dl_3,MM);
	
	//Deallocating and freeing
	mm_put(MM,dl_1);
	mm_put(MM,dl_2);
	mm_put(MM,dl_3);
	free(MM);
	free(MM2);

    return 0;
}
Пример #2
0
/*
 * Create message from packets ... deallocate packets.
 * Return a pointer to the message on success, or NULL
 */
static char *assemble_message() {

  char *msg;
  int i;
  int msg_len = message.num_packets * sizeof(data_t);

  /* Allocate msg and assemble packets into it */
  msg = (char*)malloc(pkt_total * PACKET_SIZE);
  
  for (i=0; i<pkt_total; i++)
  {
  	packet_t *pkt = message.data[i];
  	memcpy(msg + (pkt->which * PACKET_SIZE), pkt->data, PACKET_SIZE); 
  	mm_put(&mm, pkt);
  }
  	char null = '\0';
  	memcpy(msg + pkt_total * PACKET_SIZE, &null, 1);

  /* reset these for next message */
  pkt_total = 1;
  pkt_cnt = 0;
  message.num_packets = 0;

  return msg;
}
Пример #3
0
void packet_handler(int sig) {
	packet_t pkt;
	fprintf(stderr, "IN PACKET HANDLER, sig=%d %s \t\t pkt counter so far %d\n",
			sig, strsignal(sig), pkt_cnt);

	pkt = get_packet(cnt_msg); // the messages are of variable length. So, the 1st message consists of 1 packet, the 2nd message consists of 2 packets and so on..
	pkt_total = pkt.how_many;

	if (pkt_cnt == 0) { // when the 1st packet arrives, the size of the whole message is allocated.
		message.num_packets = pkt_total;
		message.data = (char*) mm_get(&MM,
				message.num_packets * PACKET_DATA_SIZE + 1); // +1 for ending 0
		fprintf(stderr, "=========Message address start \t %p \n",
				message.data);
//		message.data[pkt_total * PACKET_DATA_SIZE] = 0;
		memset(message.data, 0, message.num_packets * PACKET_DATA_SIZE + 1);
	}

//	fprintf(stderr, "After get %d: \t|%s| \t\t%p\n", cnt_msg, message.data,
//			message.data);

	fprintf(stderr, "CURRENT MESSAGE %d with message total number %d, which %d and stuff %s\n",
			cnt_msg, message.num_packets, pkt.which, pkt.data);

//	fprintf(stderr, "Dest: ~%p~; \t Src: %p \t Size %d \t Offset %d (%d) \t @cnt %d\n",
//			&message.data[0] + (PACKET_DATA_SIZE * pkt.which), pkt.data,
//			PACKET_DATA_SIZE, pkt.which, PACKET_DATA_SIZE * pkt.which,pkt_cnt);

	/* insert your code here ... stick packet in memory, make sure to handle duplicates appropriately */
	if (message.data[PACKET_DATA_SIZE * pkt.which] == (char) 0) {
		memcpy(message.data + (PACKET_DATA_SIZE * pkt.which), pkt.data,
				PACKET_DATA_SIZE);
	} else { // duplicated message deleted, skip this round
		return;
	}

	++pkt_cnt;
	if (pkt_cnt == message.num_packets) {
		int i;
		fputs("--------->",stderr);
		for (i=0;i<(PACKET_DATA_SIZE*message.num_packets);++i){
//			fputs("%d ",message.data[i],stderr);
			fprintf(stderr,"%d ",(int)message.data[i]);
		}
		fputs("\n",stderr);
		/*Print the packets in the correct order.*/
		fprintf(stderr, "MSG %d: \t|%s| \t\t%p\n\n", cnt_msg, message.data,
				message.data);
		mm_put(&MM, (void*) message.data);
	}
	/*Deallocate message*/

}
Пример #4
0
void timer_mm() {

	struct timeval time_start, time_end;
	int j,i;

	j = gettimeofday (&time_start, (void *)NULL);
	mm_t MM;
	if(mm_init (&MM, 10000, 64) == -1){
		perror("failed to initialize mm");
	}
	for (i = 0; i < 10000; i++){
		mm_get (&MM);
	}
	for (i = 0; i < 10000; i++){
		mm_put (&MM, (void*) (MM.blocks + i * MM.size));
	}
	mm_release(&MM);
	j = gettimeofday (&time_end, (void *)NULL);
	fprintf (stderr, "Time taken for MM =  %f msec\n",
		 comp_time (time_start, time_end)/1000.0);
}
Пример #5
0
/* Main driver that tests the
 * clock time for our dynamic
 * memory allocator */
int main (int argc, char **argv)
{
	int i, j;
	struct timeval times, timee;
	mm_t MM;
	void *chunk = NULL;

	
	
	if (mm_init(&MM,NUM_CHUNKS, CHUNK_SIZE) < 0)
		fprintf(stderr, "Error in mm_init\n");
	j = gettimeofday (&times, (void *)NULL);
	for (i=0; i< ITERS; i++) { 
		chunk = mm_get(&MM);
		mm_put(&MM,chunk);
	}
	
	
	
	j = gettimeofday (&timee, (void *)NULL);
	mm_release(&MM);			/* release stuff on heap*/
	fprintf (stderr, "MM time took %f msec\n",comp_time (times, timee)/1000.0);
	return 0;
}
Пример #6
0
int main()
{
	mm_t mm;
	if(mm_init(&mm, 1000000, 64) == 0)
	{
		printf("success\n");
	}
  	gettimeofday (&time_s, NULL);
	mm_t *mmArray[1000000];
	int i;
	for (i = 0; i < 1000000; i++){
		if ((mmArray[i] = mm_get(&mm)) == NULL){
			printf("Failed to allocate i = %d\n", i);
		}
	}
	while (i > 0){
		i--;
		mm_put(&mm, mmArray[i]);
	}
  	gettimeofday(&time_e, NULL);
	mm_release(&mm);
  	fprintf(stderr, "main_mm duration = %f msec\n", comp_time(time_s, time_e) / 1000.0);
	return 0;
}
Пример #7
0
int main() {

  typedef struct {
    int val;
  } chunk_t;

  mm_t MM;
  mm_t MM1;
  mm_t MM2;

  int i;
  int j;
  int found;

  void* address;

  unsigned long int* addresses = (unsigned long int*) malloc(NUM_CHUNKS * sizeof(unsigned long int));
  unsigned long int* free_addresses = (unsigned long int*) malloc(NUM_CHUNKS * sizeof(unsigned long int));

  srand(time(NULL));


  /*
   * Initialize memory
   */

  if (mm_init(&MM, NUM_CHUNKS, CHUNK_SIZE) == -1) {
	printf("Failed to initialize memory!\n");
        exit(-1);
  }


  /*
   * Allocate all chunks
   */

  for (i = 0; i < NUM_CHUNKS; i++) {
    if (!(address = mm_get(&MM))) {
      printf("Failed to allocate a chunk %d while chunks available!\n", i);
      return 1;
    } else {
      addresses[i] = (unsigned long int) address;
    }
  }



  /*
   * Sort Address in ascending order
   */

  unsigned long int temp;

  for (i = 0; i < NUM_CHUNKS; i++) {
    for (j = i; j < NUM_CHUNKS; j++) {
      if (addresses[i] > addresses[j]) {
        temp = addresses[i];
        addresses[i] = addresses[j];
        addresses[j] = temp;
      }
    }
  }


  /*
   * Addresses are distinct
   */

  for (i = 1; i < NUM_CHUNKS; i++) {
    if (addresses[i] == addresses[i-1]) {
      printf("Not all chunk addresses are Distinct!\n");
      break;
    }
  }


  /*
   * Memory is contiguous
   * Chunk size OK
   */

  for (i = 1; i < NUM_CHUNKS; i++) {
    if (addresses[i] != addresses[i-1] + CHUNK_SIZE) {
      printf("Contiguity and Chunk Size NOT OK!\n");
      break;
    }
  }


  /*
   * Does not allocate more than NUM_CHUNKS
   */

  if (mm_get(&MM) != NULL) {
    printf("Allocating more than %d\n possible chunks!", NUM_CHUNKS);
  }


  /*
   * Deallocate a random number of chunks
   * Reallocate only these
   */

  for (i = 0; i < NUM_CHUNKS; i += (rand() % (NUM_CHUNKS/5)) + 1) {
    mm_put(&MM, (void*) addresses[i]);
    free_addresses[i] = addresses[i];
  }

  while (address == mm_get(&MM)) {
    found = 0;
    for (i = 0; i < NUM_CHUNKS; i++) {
      if (free_addresses[i] == (unsigned long int) address) {
        found = 1;
        break;
      }
    }
    if (found == 0) {
      printf("A chunk in use (or unmanaged) is being Allocated!\n");
      break;
    }
  }

  mm_release(&MM);
  free(free_addresses);
  free(addresses);


  /*
   * What about allocation and
   * deallocation of just a few chunks?
   */

  addresses = (unsigned long int*) malloc(10 * sizeof(unsigned long int));

  if (mm_init(&MM1, 10, 20) == -1) {
	printf("Failed to initialize memory!\n");
        exit(-1);
  }

  for (i = 0; i < 4; i++)
    addresses[i] = (unsigned long int) mm_get(&MM1);

  mm_put(&MM1, (void*) addresses[1]);
  addresses[1] = 0;

  while (address == mm_get(&MM1)) {
    found = 0;
    for (i = 0; i < 4; i++) {
      if (addresses[i] == (unsigned long int) address) {
        printf("A chunk in use is being reallocated!\n");
        break;
      }
    }
  }

  mm_release(&MM1);
  free(addresses);

  /*
   * Finally, Can we use the chunks?
   * Expect no segmentation fault.
   */

  if (mm_init(&MM2, NUM_CHUNKS, sizeof(chunk_t)) == -1) {
	printf("Failed to initialize memory!\n");
        exit(-1);
  }

  chunk_t *my_chunk;
  for (i = 0; i < NUM_CHUNKS; i++) {
    chunk_t new_chunk;
    new_chunk.val = i;
    my_chunk = (chunk_t*) mm_get(&MM2);
    memcpy(my_chunk, &new_chunk, sizeof(chunk_t));
  }

  mm_release(&MM2);

  return 0;
}