Ret buddy_free(Buddy* thiz, size_t offset) { return_val_if_fail(thiz!=NULL && (offset%2)==0, RET_FAIL); int i; for(i=0; i<thiz->size; i++) { Node* iter = foreach_list(thiz->alloc_list[i], (void* )offset, match_block_callback); if(iter == NULL) continue; delete_from_list(&(thiz->alloc_list[i]), iter); free(iter); iter = NULL; Node* buddy_iter = foreach_list(thiz->free_list[i], (void* )offset, match_block_callback); if(buddy_iter == NULL) { return RET_OK; } delete_from_list(&(thiz->free_list[i]), buddy_iter); buddy_iter->size = buddy_iter->size + 1; insert_head(&(thiz->free_list[buddy_iter->size - 1]), buddy_iter); //TODO merge should be recurive. } return RET_FAIL; }
/********************************************************** * main: Prompts the user to enter an operation code, * * then calls a function to perform the requested * * action. Repeats until the user enters the * * command 'q'. Prints an error message if the user * * enters an illegal code. * **********************************************************/ int main(void) { char code; struct player *team_roster = NULL; printf("Operation Code: a for appending to the roster, d for deleteing a player, f for finding a player" ", p for printing the roster; q for quit.\n"); for (;;) { printf("Enter operation code: "); scanf(" %c", &code); while (getchar() != '\n') /* skips to end of line */ ; switch (code) { case 'a': team_roster = append_to_list(team_roster); break; case 'f': find_player(team_roster); break; case 'p': printList(team_roster); break; case 'd': delete_from_list(team_roster); break; case 'q': clearList(team_roster); printf("\n"); return 0; default: printf("Illegal code"); } printf("\n"); } }
/********************************************************** * place * Mark the block as allocated **********************************************************/ void place(void* bp, size_t asize, int from_free_list) { // Get the current block size size_t bsize = GET_SIZE(HDRP(bp)); // bp is not from the operation of the extend_heap, so need to remove from free list if (from_free_list == 1) delete_from_list(bp); void * split_bp; // Split the block if the difference of the sizes is not less than the minimum block size (2*DSIZE) if (asize + 2 * DSIZE <= bsize) { // Mark the footer and header with the requested size PUT(HDRP(bp), PACK(asize, 1)); PUT(FTRP(bp), PACK(asize, 1)); // Find the next block ptr as split ptr split_bp = NEXT_BLKP(bp); // Mark the footer and header of the splitted block PUT(HDRP(split_bp), PACK((bsize-asize), 0)); PUT(FTRP(split_bp), PACK((bsize-asize), 0)); // Insert this free block to the list insert_freelist(split_bp); } else { // Not enough size to split, mark the footer and header PUT(HDRP(bp), PACK(bsize, 1)); PUT(FTRP(bp), PACK(bsize, 1)); } }
void PrivPtr::shrink_ptr(priv_ptr ptr, int current_index, int parent_index, int threadID) { //first, find the first node in the list that appears in the matching if body listnode first = ptr->list->head->next; while(first != ptr->list->tail) { if(first->if_index >= current_index) break; first = first->next; } // second, remove all the nodes located in parallel -- use parent_index listnode tmp = ptr->list->head->next; while(tmp != first) { if(tmp->if_index >= parent_index) break; tmp = tmp->next; } //remove all the nodes from tmp to the node right before first listnode tmp1 = NULL; int index = 0; while(tmp != first) { tmp1 = tmp->next; delete_from_list(&tmp); index++; tmp = tmp1; } if(index > 0) { ptr->size -= index; } return; }
int main() { // Initialize a list struct node *head = malloc(sizeof(struct node)); head->value = 7; for (int i = 7; i > 0; --i) { head = add_to_list(head, i); if (head == NULL) { return EXIT_FAILURE; } } // Delete 1 and 4 delete_from_list(&head, 1); delete_from_list(&head, 4); // Write out the list for (struct node *iter = head; iter != NULL; iter = iter->next) { printf("%d ", iter->value); } printf("\b\n"); // Search 5 struct node *found = search_list(head, 5); if (found != NULL) { printf("%d found\n", found->value); } // Search 10 found = search_list(head, 10); if (found == NULL) { printf("10 not found\n"); } // Count 7 and 2 printf("The list contains %d seven\n", count_occurrencies(head, 7)); printf("The list contains %d two\n", count_occurrencies(head, 2)); // Find last 7 struct node *last = find_last(head, 7); if (last->next != NULL) { fprintf(stderr, "That seven is not the last one\n"); } else { printf("Found %d\n", last->value); } // Find 1 last = find_last(head, 1); if (last != NULL) { fprintf(stderr, "Found an inexistent element?!\n"); } else { printf("1 not found\n"); } // Free memory clear_list(head); return EXIT_SUCCESS; }
void PrivPtr::clear_list(dlist* list) { listnode tmp = (*list)->head->next; listnode tmp1; while(tmp != (*list)->tail) { tmp1 = tmp->next; delete_from_list(&tmp); tmp = tmp1; } }
/************************************************************************** * Public Functions **************************************************************************/ void catchChild(int signum)//child return or terminate callback { //printf("Child died!%d\n",signum); pid_t pid; int status; delete_from_list(waitpid(-1,&status,WNOHANG)); while((pid = waitpid(-1,&status,WNOHANG|WUNTRACED)) != -1) { delete_from_list(pid); return; // if (WIFEXITED(status)) // printf("Child ended normally.n"); // else if (WIFSIGNALED(status)) // printf("Child ended because of an uncaught signal.n"); // else if (WIFSTOPPED(status)) // printf("Child process has stopped.n"); } //siglongjmp( env, 1 ); }
/********************************************************** * coalesce * Covers the 4 cases discussed in the text: * - both neighbours are allocated * - the next block is available for coalescing * - the previous block is available for coalescing * - both neighbours are available for coalescing **********************************************************/ void *coalesce(void *bp) { size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp))); size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp))); size_t size = GET_SIZE(HDRP(bp)); /* Case 1: |ALLOC|<bp>|ALLOC| cannot coalesce */ if (prev_alloc && next_alloc) { return bp; } /* Case 2: |ALLOC|<bp>|FREE| can coalesce with the next block */ else if (prev_alloc && !next_alloc) { delete_from_list(NEXT_BLKP(bp)); // Remove the free block from the list size += GET_SIZE(HDRP(NEXT_BLKP(bp))); // Calculate the combined total size PUT(HDRP(bp), PACK(size, 0)); // Set the header PUT(FTRP(bp), PACK(size, 0)); // Set the footer return (bp); } /* Case 3: |FREE|<bp>|ALLOC| can coalesce with the prev block */ else if (!prev_alloc && next_alloc) { delete_from_list(PREV_BLKP(bp)); // Remove the free block from the list size += GET_SIZE(HDRP(PREV_BLKP(bp))); // Calculate the combined total size PUT(FTRP(bp), PACK(size, 0)); // Set the header PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0)); // Set the footer return (PREV_BLKP(bp)); } /* Case 4: |FREE|<bp>|FREE| both sides are free, can coalesce both */ else { delete_from_list(PREV_BLKP(bp)); // Remove the free blocks from the list delete_from_list(NEXT_BLKP(bp)); size += GET_SIZE(HDRP(PREV_BLKP(bp))) + GET_SIZE(FTRP(NEXT_BLKP(bp))); // Calculate the combined total size PUT(HDRP(PREV_BLKP(bp)), PACK(size,0)); // Set the header PUT(FTRP(NEXT_BLKP(bp)), PACK(size,0)); // Set the footer return (PREV_BLKP(bp)); } }
int main() { mylist *head = NULL; mylist *found; mylist *previous; int element; int c; printf("hi,This program does basic linklist manupulation.\n"); printf("what would you like to do?\n"); printf("Press:\nc for creating a list."); printf("\na for appending an element to the list."); printf("\ns for searching for an element."); printf("\nd for deleting an element."); printf("\np for printing the list."); printf("\ne for exiting program.\n"); while ((c = getchar())) { switch (c) { case 'c': head = create_list(); printf("list created\n"); break; case 'a': printf ("type in element to add\n"); scanf ("%d",&element); added = add_element(&head , element); break; case 's': printf ("type in element to search for\n"); scanf ("%d",&element); found = search_for_element(&head , element , &previous); if (found) printf("found: %d\n",found->var); else printf("not found\n"); break; case 'd': printf ("type in element to delete\n"); scanf ("%d",&element); delete_from_list(&head , element); break; case 'p': print_list(&head); break; case 'e': return 0; } } return 0; }
int main(void) { int i = 0, ret = 0; struct test_struct *ptr = NULL; print_list(); for(i = 5; i<10; i++) add_to_list(i,true); print_list(); for(i = 4; i>0; i--) add_to_list(i,false); print_list(); for(i = 1; i<10; i += 4) { ptr = search_in_list(i, NULL); if(NULL == ptr) { printf("\n Search [val = %d] failed, no such element found\n",i); } else { printf("\n Search passed [val = %d]\n",ptr->val); } print_list(); ret = delete_from_list(i); if(ret != 0) { printf("\n delete [val = %d] failed, no such element found\n",i); } else { printf("\n delete [val = %d] passed \n",i); } print_list(); } return 0; }
int main (void) { int num = 5; struct node *my_node = malloc(sizeof(struct node)); my_node->value = num; my_node->next = NULL; read_numbers(); printf("my_node add: %p\n", (void *)my_node); print_list(top); top = insert_into_ordered_list(top, my_node); print_list(top); print_list(find_last(top, 2)); delete_from_list(&top, num); print_list(top); return 0; }
/******************************************************************* DELETE_FROM_LIST(b,NEWLIST) = NEWLIST; DELETE_FROM_LIST(b,ADDLIST(t,l)) = if b=t then l else ADDLIST(DELETE_FROM_LIST(b,l)); *******************************************************************/ BOXLIST *delete_from_list(BOX *box,BOXLIST *boxlistpointer) { if(boxlistpointer==NULL) return NULL; if(box==boxlistpointer->box) { scrub_box(box); scrub_boxlist(boxlistpointer); return boxlistpointer->link; } else { boxlistpointer->link=delete_from_list(box, boxlistpointer->link); return boxlistpointer; } }
/* * place - transform a free block to an allocated block, * then delete old free block from its free list * and insert the rest part to the corresponding free list */ static void place(void *bp, size_t alloc_block_size) { size_t size; delete_from_list(bp); size = block_size(bp); dbg_printf("want to place a %d size allocated block from a %d size free block\n", (int)alloc_block_size, (int)size); if (size - alloc_block_size >= 4 * WSIZE) { /* have rest part for a new free block */ if (last_block == bp) { last_block = bp + alloc_block_size; } mark(bp, alloc_block_size, block_prev_alloc(bp), 1); bp += alloc_block_size; mark(bp, size - alloc_block_size, 1, 0); insert_to_list(bp); } else { /* have no rest part for a new free block */ mark(bp, size, block_prev_alloc(bp), 1); mark(next_block(bp), block_size(next_block(bp)), 1, block_alloc(next_block(bp))); } }
/* * coalesce - boundary tag coalescing * will delete coalesced old blocks from the free list, and * will insert the new block to the free list * return pointer to coalesced block */ static void *coalesce(void *bp) { size_t prev_alloc, next_alloc; size_t size; prev_alloc = block_prev_alloc(bp); next_alloc = block_alloc(next_block(bp)); size = block_size(bp); dbg_printf("want to coalesce %d size block, prev alloc: %d, next alloc: %d\n", (int)size, (int)prev_alloc, (int)next_alloc); if (prev_alloc && next_alloc) { // alloc - free - alloc return bp; } else if (prev_alloc && !next_alloc) { // alloc - free - free delete_from_list(bp); delete_from_list(next_block(bp)); if (next_block(bp) == last_block) { last_block = bp; } size += block_size(next_block(bp)); mark(bp, size, prev_alloc, 0); insert_to_list(bp); } else if (!prev_alloc && next_alloc) { // free - free (- alloc) delete_from_list(prev_block(bp)); delete_from_list(bp); if (bp == last_block) { last_block = prev_block(bp); } size += block_size(prev_block(bp)); bp = prev_block(bp); mark(bp, size, block_prev_alloc(bp), 0); insert_to_list(bp); } else if (!prev_alloc && !next_alloc) { // free - free - free delete_from_list(prev_block(bp)); delete_from_list(bp); delete_from_list(next_block(bp)); if (next_block(bp) == last_block) { last_block = prev_block(bp); } size += block_size(prev_block(bp)) + block_size(next_block(bp)); bp = prev_block(bp); mark(bp, size, block_prev_alloc(bp), 0); insert_to_list(bp); } return bp; }
int main () { list l = create_list(); prepend (&l, (void*)2); append (&l, (void*)17); insert_in_list (&l, 3, (void*)1); insert_in_list (&l, 2, (void*)14); display_list(l); printf ("\n"); display_list_reverse(l); printf ("\n"); delete_from_list (&l,2); display_list(l); printf ("\n"); display_list_reverse(l); printf ("\n"); set (l, 3, (void*)5); display_list(l); printf ("\n"); display_list_reverse(l); return 0; }
// Desc : 연결 리스트를 전체 삭제 // Param : head - 리스트의 헤드 포인터 void delete_list(ListNode *head) { while (head != NULL) head = delete_from_list(head, head, 1); }
/********************************************************** * mm_realloc * Implemented simply in terms of mm_malloc and mm_free *********************************************************/ void *mm_realloc(void *ptr, size_t size) { /* Case 1: If size == 0 then this is just free, and we return NULL. */ if (size == 0) { mm_free(ptr); return NULL; } /* Case 2: If oldptr is NULL, then this is just malloc. */ if (ptr == NULL) { return (mm_malloc(size)); } /* Case 3: Size is equal or smaller than the original size, return or split */ size_t old_size = GET_SIZE(HDRP(ptr)); /* current block size */ size_t asize; /* adjusted requested block size */ // Adjust block size to include overhead and alignment reqs. if (size <= DSIZE) asize = 2 * DSIZE; else asize = DSIZE * ((size + (DSIZE) + (DSIZE - 1)) / DSIZE); // If old_size is greater than the minimum block size and new size, then split if (old_size >= asize + 2 * DSIZE) { split(ptr, asize, old_size); return ptr; } else if (old_size >= asize) { // If not enough space to split, just return return ptr; } /* Case 4: Check next neighboring block, if free, and the combined size is equal to or greater than size, combine */ size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(ptr))); /* allocative state of the next block */ size_t next_size = GET_SIZE(HDRP(NEXT_BLKP(ptr))); /* size of the next block */ size_t remainder_size; if ((int) next_alloc == 0 && old_size + next_size >= asize) { delete_from_list(NEXT_BLKP(ptr)); // Remove from the free list // If the combined space is too large, split if (old_size + next_size > asize + 2 * DSIZE) { PUT(HDRP(ptr), PACK(asize, 1)); // Mark the footer and header of the realloc'd block PUT(FTRP(ptr), PACK(asize, 1)); remainder_size = old_size + next_size - asize; // Calculate the remainder size of the splitted block PUT(HDRP(NEXT_BLKP(ptr)), PACK(remainder_size, 0)); // Mark the footer and header for the splitted block PUT(FTRP(NEXT_BLKP(ptr)), PACK(remainder_size, 0)); insert_freelist((intptr_t *) NEXT_BLKP(ptr)); } else { // No enough space to split PUT(HDRP(ptr), PACK(old_size+next_size, 1)); // Mark the footer and header of the realloc'd block PUT(FTRP(ptr), PACK(old_size+next_size, 1)); } return ptr; } /* Case 5: Current block is at the end of the heap, just extend heap */ // Check if the next block ptr is at the end of the heap if (NEXT_BLKP(ptr) > (char*)mem_heap_hi() ) { size_t extendsize = asize - old_size; // Calculate the size needed to extend intptr_t * bp; if ((bp = extend_heap(extendsize / WSIZE)) == NULL) // Extend the heap return NULL; PUT(HDRP(ptr), PACK(asize, 1)); // Mark the footer and header PUT(FTRP(ptr), PACK(asize, 1)); return ptr; } /* Case 6: Check previous neighboring block, if free, and the combined size is equal to or greater than size, combine and move the content */ intptr_t * prev_p = (intptr_t *)PREV_BLKP(ptr); /* previous block's ptr */ size_t prev_alloc = GET_ALLOC(HDRP(prev_p)); /* allocative state of the prev block */ size_t prev_size = GET_SIZE(HDRP(prev_p)); /* size of the next block */ if ((int) prev_alloc == 0 && old_size + prev_size >= asize) { delete_from_list(PREV_BLKP(ptr)); // Remove from the free list memmove(prev_p, ptr, asize); // Move the content to the new starting ptr // If the combined space is too large, split if (old_size + prev_size > asize + 2 * DSIZE) { PUT(HDRP(prev_p), PACK(asize, 1)); // Mark the footer and header of the realloc'd block PUT(FTRP(prev_p), PACK(asize, 1)); remainder_size = old_size + prev_size - asize; // Calculate the remainder size of the splitted block PUT(HDRP(NEXT_BLKP(prev_p)), PACK(remainder_size, 0)); // Mark the footer and header for the splitted block PUT(FTRP(NEXT_BLKP(prev_p)), PACK(remainder_size, 0)); insert_freelist((intptr_t *) NEXT_BLKP(prev_p)); } else { // No enough space to split PUT(HDRP(prev_p), PACK(old_size+prev_size, 1)); // Mark the footer and header of the realloc'd block PUT(FTRP(prev_p), PACK(old_size+prev_size, 1)); } return prev_p; } /* Case 7: Malloc a new block, copy the content, free the old block */ void *oldptr = ptr; void *newptr; newptr = mm_malloc(size); // Malloc a new block if (newptr == NULL) return NULL; // Copy the old data. if (size < old_size) old_size = size; memcpy(newptr, oldptr, old_size); mm_free(oldptr); // Free the old block return newptr; }
void delete_from_alloc_list(const void *ptr) { assert(ptr); HeapStruct *node = delete_from_list(alloc_list, ptr); assert(node); free(node); }
void NMEnumerationCallback( void *inContext, NMEnumerationCommand inCommand, NMEnumerationItem *item) { struct list_item_data *list_data= (struct list_item_data *) inContext; short index; switch(inCommand) { case kNMEnumAdd: for(index= 0; index<list_data->count; ++index) { if (list_data->host[index]==item->id) DEBUG_PRINT("Got multiple adds for id %d", item->id); } if (index==list_data->count) { list_data->host[list_data->count++]= item->id; index= add_text_to_list(list_data->dialog, iGameList, item->name); op_assert(index==list_data->count-1); } break; case kNMEnumDelete: { NMBoolean found = false; for(index= 0; index<list_data->count; ++index) { if (list_data->host[index]==item->id) { found = true; // remove from list. delete_from_list(list_data->dialog, iGameList, index); // from cache memmove(&list_data->host[index], &list_data->host[index+1], list_data->count-index-1); list_data->count--; break; } } if (!found) { DEBUG_PRINT("Got a remove for something I don't have in my list?? (%d %s)",item->id, item->name); } break; } case kNMEnumClear: empty_list(list_data->dialog, iGameList); list_data->count= 0; break; default: DEBUG_PRINT("Command %d unrecognized in NMEnumerationCallback", inCommand); break; } return; }
int main(int argc, char*argv[], char*envp[]){ char next_char; int i, fd; copy_envp(envp); char changepath[1024]; head = (struct node *) malloc( sizeof(struct node) ); head->next = NULL; head->val = 0; struct node *ptr = (struct node *)malloc(sizeof(struct node *)); ptr->val=0; ptr->next = NULL; head = current = ptr; /** clears screen for RSI **/ if(fork() == 0) { execvp("clear", argv); exit(1); } else { wait(NULL); } /** Prints prompt for first time **/ strcpy(cwd,""); if (getcwd(cwd, sizeof(cwd)) == NULL){ perror("getcwd() error"); } printf("RSI: %s > ", cwd); fflush(stdout); char word[24] = "\0"; char* args[256]; int m = 0; int child_pid; int h; char command[24]; static char arg_buffer[256] = "\0"; /* while user has not pushed ctrl-d */ while(next_char != EOF){ /* gets next char typed in cmd line, adds to words. */ /* puts the words (arguments) into argv *************/ next_char=getchar(); if(isspace(next_char) == 0){ strncat(word, &next_char, 1); } else{ strcpy(command, word); args[m] = (char *)malloc(sizeof(char) * 100); strcpy(args[m], command); strcpy(word, ""); m++; } /* if the line is done, ...*/ if(next_char == '\n'){ args[m] = (char *)malloc(sizeof(char) * 2); args[m] = NULL; int r = 0; int j = 0; int wpid; /* if statements check if cmd is bg, bglist, or cd. */ /* if not, it executes the command, waits for the child to complete, then prompts again */ if (strcmp(args[0], "cd" ) == 0){ change_directory(args, changepath, envp); } else if(strcmp(args[0], "\0")==0){ printf("\nNo Command Entered.\n"); } else if(strcmp(args[0], "bglist") == 0){ bglist(); } /** if it's a bg process, execute process, add info to list, then continue. **/ else if(strcmp(args[0], "bg") == 0){ for(r = 0; r<m-1; r++){ bzero(args[r], sizeof(args[r])); strcpy(args[r],args[r+1]); } bzero(args[m-1], sizeof(args[m-1])); args[m-1] = NULL; if ((child_pid=fork()) == 0) { h = execvp(args[0], args); printf("errno is %d\n", errno); } else { int c = 1; /* combine separate args to single string for node and future printout at bglist */ for(c=1; c<m-1; c++){ strcat(arg_buffer, args[c]); strcat(arg_buffer, " "); } add_to_list(child_pid, args[0], arg_buffer); bzero(arg_buffer, 256); } } /* execute process, then wait until process has finished to continue */ else { pid_t num; if ((num = fork()) == 0){ h = execvp(args[0], args); printf("errno is %d\n", errno); } else { waitpid(num, NULL, 0); } } m = 0; /* gets cwd and checks for finished processes. If there are, remove them from the list of bg processes. */ strcpy(cwd,""); if (getcwd(cwd, sizeof(cwd)) == NULL){ perror("getcwd() error"); } int status; int pid; /* waitpid() returns a PID on success*/ for(;;){ pid = waitpid(-1, &status, WNOHANG); if(pid <= 0){ break; } printf("[proc %d exited with code %d]\n", pid, WEXITSTATUS(status)); delete_from_list(pid); } /* prints prompt */ printf("\nRSI: %s > ", cwd); fflush(stdout); } } }