void exampleList() { // Use pooling for efficiency, if you don't want to use pooling // then comment out this line. pool_list(16); List* L = newList(); list_add(L, "a"); list_add(L, "b"); list_add(L, "c"); list_add(L, "d"); list_add(L, "e"); list_add(L, "f"); // Display the current list displayStrings(L); //ABCDEF // Remove first item list_removeFirst(L); displayStrings(L); //BCDEF // Add first item back list_addFirst(L, "a"); displayStrings(L); list_clear(L); if (list_isEmpty(L)) printf("List was cleared.\n"); // Add some strings and remove all that begin with - int nums[] = {1, 2, 3, 4, 6, 7, 8}; int x; for (x = 0; x < 7; x++) list_add(L, &nums[x]); displayIntegers(L); list_start(L); while (list_hasNext(L)) { // get does not move the current node x = *((int*)list_peek(L)); if (x % 2 == 0) // remove will remove the node from the list altogether and // return the data removed. list_remove(L); else // next will just goto the next node and return the data. list_next(L); } // Print out the odd numbers displayIntegers(L); // Try removing all while traversing list_start(L); while (list_hasNext(L)) list_remove(L); displayIntegers(L); if (L->first == NULL && L->last == NULL && L->size == 0) printf("All cleaned up!\n"); // Traverse through an array of strings and find any that start // with . and after it add 0 and add one before it that is - list_add(L, ".1"); list_add(L, " two "); list_add(L, ".3"); list_add(L, " four "); list_add(L, ".5"); displayStrings(L); list_start(L); char* c; while (list_hasNext(L)) { c = (char*)list_peek(L); if (c[0] == '.') { list_insertBefore(L, "-"); list_insertAfter(L, "0"); } list_next(L); } displayStrings(L); char* first = (char*)L->first->data; char* last = (char*)L->last->data; if (first[0] == '-' && last[0] == '+') printf("Insertions correct.\n"); // This will clear the list of any nodes and pool them and then free // the list itself from memory list_free(L); // If you're not using pooling this can be commented out. This will // free all pooled nodes from memory. Always call this at the end // of using any List. unpool_list(); }
int new_thread( int type, struct process *proc, struct thread *parent, const char name[NAME_LENGTH], uint32_t entry, int priority, uint32_t data1, uint32_t data2, uint32_t data3, uint32_t data4 ) { struct thread *tr = (struct thread*)malloc( sizeof(struct thread) ); if ( tr == NULL ) return -1; strncpy( tr->name, name, NAME_LENGTH ); tr->tid = next_tid( proc ); tr->state = THREAD_SUSPENDED; tr->process = proc; tr->sched_state = -1; // architecture ... // tr->stack_user = page_find_free( proc->map, sK_BASE, sK_CEILING, sK_PAGES_THREAD + 1 ); tr->stack_user += (sK_PAGES_THREAD + 1) * PAGE_SIZE; // We're leaving a guard page at the bottom (and, // therefore, the top) of every thread stack. user_ensure( proc, tr->stack_user - (sK_PAGES_THREAD * PAGE_SIZE), sK_PAGES_THREAD ); tr->stack_kernel = (uint32_t) memory_alloc( sK_PAGES_KERNEL ); tr->stack_kernel += sK_PAGES_KERNEL * PAGE_SIZE; tr->cpu = -1; tr->cpu_affinity = -1; // No preference. tr->math_state = -1; tr->math = memory_alloc( 1 ); tr->stack = tr->stack_kernel; // ------------ if ( type == 1 ) { // kernel thread pushl( tr, GDT_SYSTEMDATA ); pushl( tr, tr->stack_user ); pushl( tr, EFLAG_BASE | EFLAG_INTERRUPT ); pushl( tr, GDT_SYSTEMCODE ); } else { // user thread pushl( tr, GDT_USERDATA | 3 ); pushl( tr, tr->stack_user ); pushl( tr, EFLAG_BASE | EFLAG_INTERRUPT ); pushl( tr, GDT_USERCODE | 3 ); } pushl( tr, entry ); pushl( tr, GDT_USERDATA | 3 ); // gs pushl( tr, GDT_USERDATA | 3 ); // fs pushl( tr, GDT_USERDATA | 3 ); // ds pushl( tr, GDT_USERDATA | 3 ); // es pushl( tr, data1 ); // eax pushl( tr, data3 ); // ecx pushl( tr, data4 ); // edx pushl( tr, data2 ); // ebx pushl( tr, 0 ); // temp esp pushl( tr, 0 ); // ebp pushl( tr, 0 ); // esi pushl( tr, 0 ); // edi FAMILY_INIT( tr ); LIST_INIT( tr ); // .... if ( proc->threads == NULL ) { proc->threads = tr; } else { if ( parent == NULL ) { family_add_sibling( &(parent->family_tree), &(tr->family_tree) ); list_insertAfter( &(proc->threads->list), &(tr->list) ); } else { family_add_child( &(parent->family_tree), &(tr->family_tree) ); list_insertAfter( &(proc->threads->list), &(tr->list) ); } } htable_insert( proc->threads_htable, tr ); proc->thread_count += 1; return tr->tid; }