int main() { /* Create memory space */ assert(Mem_Init(MEM_SIZE) == 0); printf("Memory Space requested with size: %d\n",MEM_SIZE); Mem_Dump(); /* Ask for simple memory allocation */ void* ptr = Mem_Alloc(ALLOC_SIZE); printf("\nRequest memory block of size: %d\n",ALLOC_SIZE); Mem_Dump(); /* Free memory */ assert(Mem_Free(ptr) == 0); printf("\nFree memory space"); Mem_Dump(); /* Request memory again*/ ptr = Mem_Alloc(ALLOC_SIZE); printf("\nRequest memory block of size: %d\n",ALLOC_SIZE); Mem_Dump(); /* Corrupt pointer */ ptr = NULL; /* Attemp to free memory with bad pointer */ int result = Mem_Free(ptr); printf("Attempted to free memory pointed to by pointer (ptr = %p)\n",ptr); printf("Result from Mem_Free(ptr) = %d\n",result); Mem_Dump(); exit(0); }
int main() { assert(Mem_Init(4096) == 0); void * ptr[4]; ptr[0] = Mem_Alloc(800); assert(ptr[0] != NULL); ptr[1] = Mem_Alloc(800); assert(ptr[1] != NULL); ptr[2] = Mem_Alloc(800); assert(ptr[2] != NULL); ptr[3] = Mem_Alloc(800); assert(ptr[3] != NULL); while (Mem_Alloc(800) != NULL) ; Mem_Dump(); assert(m_error == E_NO_SPACE); assert(Mem_Free(ptr[1]) == 0); Mem_Dump(); assert(Mem_Free(ptr[3]) == 0); Mem_Dump(); assert(Mem_Free(ptr[2]) == 0); Mem_Dump(); ptr[2] = Mem_Alloc(2400); assert(ptr[2] != NULL); Mem_Dump(); exit(0); }
int main() { assert(Mem_Init(4096) == 0); void* ptr = Mem_Alloc(8); Mem_Dump(); assert(ptr != NULL); assert(Mem_Free(ptr) == 0); printf("\nEND\n"); Mem_Dump(); exit(0); }
int main() { assert(Mem_Init(4096) == 0); Mem_Dump(); assert(Mem_Alloc(2048, FIRSTFIT) != NULL); Mem_Dump(); assert(Mem_Alloc(2049, FIRSTFIT) == NULL); Mem_Dump(); assert(m_error == E_NO_SPACE); exit(0); }
int main() { assert(Mem_Init(4096) == 0); assert(Mem_Alloc(4) != NULL); Mem_Dump(); assert(Mem_Alloc(8) != NULL); Mem_Dump(); assert(Mem_Alloc(16) != NULL); Mem_Dump(); assert(Mem_Alloc(4) != NULL); Mem_Dump(); exit(0); }
int main(int argc, char *argv[]) { void *space = Mem_Init(4096); assert(space != NULL); printf("Got space %p\n", space); int *x = Mem_Alloc(sizeof(int)); //int *y = Mem_Alloc(sizeof(int)); //assert(x != NULL && y != NULL); //printf("pre x = %p\n", x); //printf("pre y = %p\n", y); // use x and y *x = 123; //*y = 456; //printf("post x = %p\n", x); //printf("post y = %p\n", y); //Mem_Free(x); //Mem_Free(y); //int64_t *x; // should loop forever while ((x = Mem_Alloc(sizeof(int))) != NULL) { printf("x = %p\n", x); //Mem_Free(x); //Mem_Dump(); } Mem_Dump(); return 0; }
int main() { assert(Mem_Init(4096) == 0); void* ptr = Mem_Alloc(8); assert(ptr != NULL); Mem_Dump(); exit(0); }
int main() { assert(Mem_Init(4096) == 0); void *ptr[4]; void *first, *best, *worst; assert(Mem_Alloc(8, FIRSTFIT) != NULL); ptr[0] = Mem_Alloc(40, FIRSTFIT); assert(Mem_Alloc(8, FIRSTFIT) != NULL); ptr[1] = Mem_Alloc(56, FIRSTFIT); assert(Mem_Alloc(8, FIRSTFIT) != NULL); first = Mem_Alloc(256, FIRSTFIT); assert(Mem_Alloc(8, FIRSTFIT) != NULL); best = Mem_Alloc(128, FIRSTFIT); assert(Mem_Alloc(8, FIRSTFIT) != NULL); ptr[2] = Mem_Alloc(32, FIRSTFIT); assert(Mem_Alloc(8, FIRSTFIT) != NULL); worst = Mem_Alloc(512, FIRSTFIT); assert(Mem_Alloc(8, FIRSTFIT) != NULL); ptr[3] = Mem_Alloc(32, FIRSTFIT); Mem_Dump(); int count = 0; while(Mem_Alloc(128, FIRSTFIT) != NULL) { printf("%d\n", count); count++; Mem_Dump(); } assert(m_error == E_NO_SPACE); assert(Mem_Free(ptr[2]) == 0); assert(Mem_Free(ptr[3]) == 0); Mem_Dump(); assert(Mem_Free(first) == 0); Mem_Dump(); assert(Mem_Free(best) == 0); Mem_Dump(); assert(Mem_Free(ptr[1]) == 0); Mem_Dump(); assert(Mem_Free(worst) == 0); Mem_Dump(); assert(Mem_Free(ptr[0]) == 0); Mem_Dump(); assert(Mem_Alloc(128, BESTFIT) == best); Mem_Dump(); exit(0); }
int main() { /* Make sure the memory initializes correctly */ assert(Mem_Init(memsize) == 0); printf("Allocating %d from initialized memory.\n\n", allocation); /* Make an allocation, get a pointer, and print memory usage */ ptr = Mem_Alloc(allocation); Mem_Dump(); printf("Freeing %d from initialized memory.\n", allocation); /* Free the memory and make sure it exits with status 0 */ assert(Mem_Free(ptr) == 0); Mem_Dump(); puts("Checking if Mem_Free() handles bad pointers..."); /* Check exit status for NULL and bad pointers */ ptr = NULL; assert(Mem_Free(ptr) == -1); ptr = (int *)5; assert(Mem_Free(ptr) == -1); puts("...done."); }
int main() { assert(Mem_Init(4096) == 0); void * ptr[9]; ptr[0] = Mem_Alloc(1); ptr[1] = (Mem_Alloc(5)); ptr[2] = (Mem_Alloc(14)); ptr[3] = (Mem_Alloc(8)); assert(ptr[0] != NULL); assert(ptr[1] != NULL); assert(ptr[2] != NULL); assert(ptr[3] != NULL); assert(Mem_Free(ptr[1]) == 0); assert(Mem_Free(ptr[0]) == 0); assert(Mem_Free(ptr[3]) == 0); Mem_Dump(); ptr[4] = (Mem_Alloc(1)); ptr[5] = (Mem_Alloc(4)); assert(ptr[4] != NULL); assert(ptr[5] != NULL); assert(Mem_Free(ptr[5]) == 0); ptr[6] = (Mem_Alloc(9)); ptr[7] = (Mem_Alloc(33)); assert(ptr[6] != NULL); assert(ptr[7] != NULL); assert(Mem_Free(ptr[4]) == 0); ptr[8] = (Mem_Alloc(55)); assert(ptr[8] != NULL); assert(Mem_Free(ptr[2]) == 0); assert(Mem_Free(ptr[7]) == 0); assert(Mem_Free(ptr[8]) == 0); assert(Mem_Free(ptr[6]) == 0); Mem_Dump(); exit(0); }
int main() { assert(Mem_Init(4096) == 0); void * ptr[6]; ptr[0] = Mem_Alloc(400, BESTFIT); assert(ptr[0] != NULL); ptr[1] = Mem_Alloc(400, BESTFIT); assert(ptr[1] != NULL); ptr[2] = Mem_Alloc(1000, BESTFIT); assert(ptr[2] != NULL); ptr[3] = Mem_Alloc(1000, BESTFIT); assert(ptr[3] != NULL); ptr[4] = Mem_Alloc(400, BESTFIT); assert(ptr[4] != NULL); ptr[5] = Mem_Alloc(400, BESTFIT); assert(ptr[5] != NULL); Mem_Dump(); assert(Mem_Free(ptr[0]) == 0); ptr[0] = NULL; Mem_Dump(); assert(Mem_Free(ptr[2]) == 0); ptr[2] = NULL; Mem_Dump(); assert(Mem_Free(ptr[4]) == 0); ptr[4] = NULL; Mem_Dump(); ptr[0] = Mem_Alloc(360, BESTFIT); assert(ptr[0] != NULL); Mem_Dump(); ptr[2] = Mem_Alloc(360, BESTFIT); assert(ptr[2] != NULL); Mem_Dump(); ptr[4] = Mem_Alloc(960, BESTFIT); assert(ptr[4] != NULL); exit(0); }
int main() { printf("Starting mem testing..."); /* int result = Mem_Init(6012); printf("Called Mem_Init... result=%i\r\n",result); if (result == 0) printf("Successfully called Mem_Init()!\r\n"); int *ptr = Mem_Alloc(2); int *ptr2 = Mem_Alloc(14); int *ptr3 = Mem_Alloc(32); printf("Got allocated memory @%p!\r\n",ptr); result = Mem_Free(ptr); Mem_Dump(); if (result == 0) printf("Successfully called Mem_Free()!\r\n"); Mem_Free(ptr2); Mem_Free(ptr3); printf("Calling Mem_Dump()... "); printf("finishing up testing program.........\r\n"); printf("---------------------------------------------------------\r\n"); */ printf("taking apart free2\r\n"); Mem_Init(4096); void *ptr[4]; ptr[0] = Mem_Alloc(800); ptr[1] = Mem_Alloc(800); ptr[2] = Mem_Alloc(800); ptr[3] = Mem_Alloc(800); Mem_Free(ptr[1]); Mem_Free(ptr[2]); ptr[2] = Mem_Alloc(1600); Mem_Dump(); return 0; }
int main(int argc, char **argv) { // psudeo-random seed unsigned int seed = atoi(argv[1]); // number of operations to perform long long n = atoll(argv[2]); // if true, write data into allocated memory bool writeData = atoi(argv[3]); // maximum number of concurrent allocations to track int max_allocs = 1000; // size for allocation request int min_alloc_size = 1; int max_alloc_size = 128; // allowed constant overhead int slack = 32; // max header size per allocation int header_size = 32; // request size up to 64+32, header up to 32 bytes int max_chunk_size = max_alloc_size + header_size; // most possible space, no more than max_allocs+1 unusable free chunks int region_size = max_allocs * max_chunk_size * 2 + max_chunk_size; void** ptr = calloc(sizeof(void*), max_allocs); int* size = calloc(sizeof(int), max_allocs); void** shadow = calloc(sizeof(void*), max_allocs); /******************************************************************* Please note that random() gives psudeo-random, not true random data. If the seed is set to the same value, the sequence generated by random() will be the same. Using psuedo-random number generators is a common testing technique. *******************************************************************/ srandom(seed); assert(Mem_Init(region_size + slack) == 0); int slot; bool doAlloc; bool doWrite; long long i; for (i=0; i<n; i++) { slot = random() % max_allocs; doAlloc = random() % 4; doWrite = writeData; if (!doAlloc || ptr[slot] != NULL) { assert(Mem_Free(ptr[slot], 1) == 0); free(shadow[slot]); ptr[slot] = NULL; shadow[slot] = NULL; } Mem_Dump(); if (doAlloc) { size[slot] = min_alloc_size + (random() % (max_alloc_size - min_alloc_size + 1)); ptr[slot] = Mem_Alloc(size[slot]); assert(ptr[slot] != NULL); if (doWrite) { shadow[slot] = malloc(size[slot]); int j; for (j=0; j<size[slot]; j++) { char data = random(); *((char*)(ptr[slot] + j)) = data; *((char*)(shadow[slot] + j)) = data; } } } } if (writeData) { for (slot=0; slot<max_allocs; slot++) { if (ptr[slot] != NULL) { assert(memcmp(ptr[slot], shadow[slot], size[slot]) == 0); } } } exit(0); return 0; } // END OF MAIN
int Mem_Free(void *ptr, int coalesce) { printf("Freeing: %p \n", &ptr); Mem_Dump(); if(ptr == NULL) { return 0; } //void * new_block_ptr = ptr - sizeof(struct node_block); struct node_block *ptr_header; struct node_block *cur_block; //struct node_block *prev = free_block_head; // point to block header ptr_header = (struct node_block*) (ptr - sizeof(struct node_block)); // cur block points to free list header cur_block = free_block_head; // if the allocated memory comes before the free list if( (uintptr_t *) ptr_header < (uintptr_t *) cur_block ) { free_block_head->prev = ptr_header; ptr_header->next = free_block_head; ptr_header->prev = NULL; free_block_head = ptr_header; } else if(ptr_header != free_block_head) { // comes after free list while(cur_block->next != NULL) { if( (uintptr_t *) cur_block->next > (uintptr_t *) ptr_header ) { break; } cur_block = cur_block->next; } if(cur_block == free_block_head) { // in front of list ptr_header->prev = cur_block; ptr_header->next = cur_block->next; if(ptr_header->next != NULL) ptr_header->next->prev = ptr_header; cur_block->next = ptr_header; } else if (cur_block->next == NULL) { // end of list ptr_header->prev = cur_block; if(ptr_header->next != NULL) ptr_header->prev->next = ptr_header; ptr_header->next = cur_block->next; cur_block->next = ptr_header; } else if (cur_block != free_block_head) { // middle ptr_header->prev = cur_block; ptr_header->next = cur_block->next; cur_block->next = ptr_header; if(ptr_header->next != NULL) ptr_header->next->prev = ptr_header; } } // check whether need to coalesce if(coalesce != 0) { //cur_block = ptr_header; cur_block = free_block_head; /*// check for left coalesce if( (ptr_header->prev != NULL) && ((void *) ptr_header->prev == (void *) ptr_header - ptr_header->prev->size - sizeof(struct node_block)) ) { ptr_header->prev->size += (ptr_header->size + sizeof(struct node_block)); // update size ptr_header->prev->next = ptr_header->next; if(ptr_header->next != NULL) ptr_header->next->prev = ptr_header->prev; ptr_header = ptr_header->prev; } // check for right coalesce if( ptr_header->next != NULL && (void *) ptr_header->next == (void *) ptr_header + ptr_header->next->size + sizeof(struct node_block)) { ptr_header->size += (ptr_header->next->size + sizeof(struct node_block)); // update size if(ptr_header->next->next != NULL) ptr_header->next->next->prev = ptr_header; ptr_header->next = ptr_header->next->next; // update the next }*/ while(cur_block != NULL && cur_block->next != NULL) { if((void *) cur_block + sizeof(struct node_block) + cur_block->size == (void *) cur_block->next) { // update the size to include the adjacent block and header cur_block->size += (cur_block->next->size + sizeof(struct node_block)); // remove the adjacent block from the link cur_block->next = cur_block->next->next; } else { cur_block = cur_block->next; } } } return 0; } // END OF MEM_FREE
int main() { assert(Mem_Init(4096) == 0); void* ptr[9]; void* test; ptr[0] = Mem_Alloc(300); assert(ptr[0] != NULL); ptr[1] = Mem_Alloc(200); assert(ptr[1] != NULL); ptr[2] = Mem_Alloc(200); assert(ptr[2] != NULL); ptr[3] = Mem_Alloc(100); assert(ptr[3] != NULL); ptr[4] = Mem_Alloc(200); assert(ptr[4] != NULL); ptr[5] = Mem_Alloc(800); assert(ptr[5] != NULL); ptr[6] = Mem_Alloc(500); assert(ptr[6] != NULL); ptr[7] = Mem_Alloc(700); assert(ptr[7] != NULL); ptr[8] = Mem_Alloc(300); assert(ptr[8] != NULL); Mem_Dump(); assert(Mem_Free(ptr[1]) == 0); assert(Mem_Free(ptr[3]) == 0); assert(Mem_Free(ptr[5]) == 0); assert(Mem_Free(ptr[7]) == 0); Mem_Dump(); test = Mem_Alloc(50); Mem_Dump(); assert( (!( ( ((unsigned long int)test >= (unsigned long int)ptr[3]) && ((unsigned long int)test < (unsigned long int)ptr[4]) ) || ( ((unsigned long int)test >= (unsigned long int)ptr[3]) && ((unsigned long int)test < (unsigned long int)ptr[2]) ) )) && (!( ( ((unsigned long int)test >= (unsigned long int)ptr[5]) && ((unsigned long int)test < (unsigned long int)ptr[6]) ) || ( ((unsigned long int)test >= (unsigned long int)ptr[5]) && ((unsigned long int)test < (unsigned long int)ptr[4]) ) )) ); exit(0); }