/* Removes the first item held by the list. */ void l_remove_front(linked_list list) { assert(l_length(list) > 0); //If l_remove() is implemented propertly below, just call //it with the pointer to the front link. l_remove(list, list->front); }
static void l_resort(Timer * t) { /* Remove the timer from its old list. */ l_remove(t); /* Recompute the hash. */ t->hash = hash(t); /* And add it back in to its new list, sorted correctly. */ l_add(t); }
void tmr_cancel( Timer* t ) { /* Remove it from its active list. */ l_remove( t ); /* And put it on the free list. */ t->next = free_timers; free_timers = t; t->prev = (Timer*) 0; }
/* remove the nth element */ void l_remove_nth(list_t * list, unsigned n, void **data) { element_t *np = list->head; /* traverse to correct element */ while (n--) { if ((np = np->next) == NULL) /* return */ abort(); } l_remove(list, np, data); }
/* destroy the whole list */ void l_destroy(list_t * list) { void *data; if (list == NULL) return; /* remove every element */ while (list->size) { l_remove(list, NULL, &data); free(data); } free(list); }
int main(void) { List *tp, *root_p = NULL; int i, ins[] = {4,2,6,1,3,4,7}; for(i = 0; i < 7; i++) if(!(root_p = l_insert(root_p, ins[i]))) err(1, "Malloc error"); // can't insert for(i = 0; i < 9; i++){ tp = l_search(root_p, i, NULL, NULL); printf("%d ", i); if(!tp) printf("not "); printf("found\n"); } printf("now delete items 1, 4 and 8\n"); l_remove(&root_p, 1); l_remove(&root_p, 4); l_remove(&root_p, 8); for(i = 0; i < 9; i++){ tp = l_search(root_p, i, NULL, NULL); printf("%d ", i); if(!tp) printf("not "); printf("found\n"); } return 0; }
void tmr_cancel(Timer *t) { if (!t) return; /* Remove it from its active list. */ l_remove(t); --active_count; /* And put it on the free list. */ t->prev = NULL; t->next = free_timers; free_timers = t; ++free_count; }
/* concatenate list src with list dst */ int l_concat(list_t *dst, list_t **src) { void *data; element_t *n; n = (*src)->head; while (n) { l_remove(*src, NULL, &data); if (l_append(dst, NULL, data) == -1) return -1; n = (*src)->head; } l_destroy(*src); *src = NULL; return 0; }
void dl_remove(List list, int index) { assert_argument_not_null(list); dl_assert_element_size(list); l_remove(list, index); }
FUNCTION objend () { register Ocb * o; extern int aggressive; Debug o = xqting_ocb; destroy_message_vector (o); if ( o->centry == DCRT || o->centry == DDES ) { unmark_macro ( o->ci ); } else { register Msgh * n; for ( n = fstigb ( o->ci ); n; n = nxtigb ( n ) ) { if ( n->mtype != DYNDSMSG ) unmark_macro ( n ); } } if ( o->centry == INIT || o->centry == DCRT || o->centry == DDES ) { #ifdef SOM /* Update the phase's highest-seen ept field to the Ept of the event just completed. Also update the amount of total unrolled back work done by the phase. */ o->Ept = o->sb->Ept; #endif SOM save_state (o); } if ( o->centry == EVENT ) { o->stats.numecomp++; o->eventTimePermitted -= o->sb->effectWork; #ifdef SOM /* Update the phase's highest-seen ept field to the Ept of the event just completed. Also update the amount of total unrolled back work done by the phase. */ o->Ept = o->sb->Ept; #endif SOM save_state ( o ); } else if ( o->centry == TERM ) { #ifdef RBC if ( o->uses_rbc ) l_destroy ( o->sb ); else /* destroy_state and rollback chip don't mix */ #endif destroy_state ( o->sb ); o->sb = NULL; l_destroy ( o->stk ); o->stk = NULL; #ifdef RBC if ( o->uses_rbc && rollback_op ( o, 1, posinfPlus1 ) ) { printf ( "weird error term objend for %s\n", o->name ); tester(); } #endif o->ci = NULL; o->co = NULL; o->control = EDGE; o->runstat = BLKINF; if ( ! aggressive ) cancel_omsgs ( o, o->svt, o->phase_end ); l_remove ( o ); o->svt = posinfPlus1; l_insert ( l_prev_macro ( _prqhd ), o ); dispatch (); return; } go_forward ( o ) ; dispatch (); }
void big_test() { linked_list list = l_create(); l_add_front(list, 3); length_test(list, 1); front_test(list, 3); back_test(list, 3); //list = 3 l_add_back(list, 4); length_test(list, 2); front_test(list, 3); back_test(list, 4); //list = 3, 4 l_add_front(list, 2); length_test(list, 3); front_test(list, 2); back_test(list, 4); //list = 2, 3, 4 int array1[] = {2, 3, 4}; print_test(list, array1, 3); l_add_front(list, 1); l_add_back(list, 5); length_test(list, 5); front_test(list, 1); back_test(list, 5); //list = 1, 2, 3, 4, 5 find_test(list, 3, 1); link l = l_find(list, 3); l_remove(list, l); find_test(list, 3, 0); length_test(list, 4); front_test(list, 1); back_test(list, 5); //list = 1, 2, 4, 5 int array2[] = {1, 2, 4, 5}; print_test(list, array2, 4); l_remove_back(list); length_test(list, 3); back_test(list, 4); //list = 1, 2, 4 l_remove_front(list); length_test(list, 2); front_test(list, 2); //list = 2, 4 l_destroy(list); }
/* Removes the last item held by the list. */ void l_remove_back(linked_list list) { assert(l_length(list) > 0); l_remove(list, list->back); }
/* dregion [!][x][,x,...] */ bool handler__dregion(globals_t * vars, char **argv, unsigned argc) { unsigned id; bool invert = false; char *end = NULL, *idstr = NULL, *block = NULL; element_t *np, *pp; list_t *keep = NULL; region_t *save; /* need an argument */ if (argc < 2) { show_error("expected an argument, see `help dregion`.\n"); return false; } /* check that there is a process known */ if (vars->target == 0) { show_error("no target specified, see `help pid`\n"); return false; } /* check for an inverted match */ if (*argv[1] == '!') { invert = true; /* create a copy of the argument for strtok(), +1 to skip '!' */ block = strdupa(argv[1] + 1); /* check for lone '!' */ if (*block == '\0') { show_error("inverting an empty set, maybe try `reset` instead?\n"); return false; } /* create a list to keep the specified regions */ if ((keep = l_init()) == NULL) { show_error("memory allocation error.\n"); return false; } } else { invert = false; block = strdupa(argv[1]); } /* loop for every number specified, eg "1,2,3,4,5" */ while ((idstr = strtok(block, ",")) != NULL) { region_t *r = NULL; /* set block to NULL for strtok() */ block = NULL; /* attempt to parse as a regionid */ id = strtoul(idstr, &end, 0x00); /* check that worked, "1,abc,4,,5,6foo" */ if (*end != '\0' || *idstr == '\0') { show_error("could not parse argument %s.\n", idstr); if (invert) { if (l_concat(vars->regions, &keep) == -1) { show_error("there was a problem restoring saved regions.\n"); l_destroy(vars->regions); l_destroy(keep); return false; } } assert(keep == NULL); return false; } /* initialise list pointers */ np = vars->regions->head; pp = NULL; /* find the correct region node */ while (np) { r = np->data; /* compare the node id to the id the user specified */ if (r->id == id) break; pp = np; /* keep track of prev for l_remove() */ np = np->next; } /* check if a match was found */ if (np == NULL) { show_error("no region matching %u, or already moved.\n", id); if (invert) { if (l_concat(vars->regions, &keep) == -1) { show_error("there was a problem restoring saved regions.\n"); l_destroy(vars->regions); l_destroy(keep); return false; } } if (keep) l_destroy(keep); return false; } np = pp; /* save this region if the match is inverted */ if (invert) { assert(keep != NULL); l_remove(vars->regions, np, (void *) &save); if (l_append(keep, keep->tail, save) == -1) { show_error("sorry, there was an internal memory error.\n"); free(save); return false; } continue; } /* check for any affected matches before removing it */ if(vars->num_matches > 0) { region_t *s; /* determine the correct pointer we're supposed to be checking */ if (np) { assert(np->next); s = np->next->data; } else { /* head of list */ s = vars->regions->head->data; } if (!(vars->matches = delete_by_region(vars->matches, &vars->num_matches, s, false))) { show_error("memory allocation error while deleting matches\n"); } } l_remove(vars->regions, np, NULL); } if (invert) { region_t *s = keep->head->data; if (vars->num_matches > 0) { if (!(vars->matches = delete_by_region(vars->matches, &vars->num_matches, s, true))) { show_error("memory allocation error while deleting matches\n"); } } /* okay, done with the regions list */ l_destroy(vars->regions); /* and switch to the keep list */ vars->regions = keep; } return true; }