/* * Extract next item (in order) from search queue * * Returns a GISTSearchItem or NULL. Caller must pfree item when done with it. * * NOTE: on successful return, so->curTreeItem is the GISTSearchTreeItem that * contained the result item. Callers can use so->curTreeItem->distances as * the distances value for the item. */ static GISTSearchItem * getNextGISTSearchItem(GISTScanOpaque so) { for (;;) { GISTSearchItem *item; /* Update curTreeItem if we don't have one */ if (so->curTreeItem == NULL) { so->curTreeItem = (GISTSearchTreeItem *) rb_leftmost(so->queue); /* Done when tree is empty */ if (so->curTreeItem == NULL) break; } item = so->curTreeItem->head; if (item != NULL) { /* Delink item from chain */ so->curTreeItem->head = item->next; if (item == so->curTreeItem->lastHeap) so->curTreeItem->lastHeap = NULL; /* Return item; caller is responsible to pfree it */ return item; } /* curTreeItem is exhausted, so remove it from rbtree */ rb_delete(so->queue, (RBNode *) so->curTreeItem); so->curTreeItem = NULL; } return NULL; }
static void check_vma_struct(void) { ProcVM *mm = vm_create_proc(); assert(mm != NULL); int step1 = 10, step2 = step1 * 10; int i; for (i = step1; i >= 1; i --) { ProcVMA *vma = vm_create_vma(i * 5, i * 5 + 2, 0); assert(vma != NULL); vm_insert_vma(mm, vma); } for (i = step1 + 1; i <= step2; i ++) { ProcVMA *vma = vm_create_vma(i * 5, i * 5 + 2, 0); assert(vma != NULL); vm_insert_vma(mm, vma); } RBNode *rbn = rb_leftmost(&(mm->mmap_root)); for (i = 1; i <= step2; i ++) { assert(rbn != NULL); ProcVMA *mmap = rbn2vma(rbn, vm_link); assert(mmap->vm_start == i * 5 && mmap->vm_end == i * 5 + 2); rbn = rb_node_next(&(mm->mmap_root), rbn); } for (i = 5; i <= 5 * step2; i += 5) { ProcVMA *vma1 = vm_find_vma(mm, i); assert(vma1 != NULL); ProcVMA *vma2 = vm_find_vma(mm, i+1); assert(vma2 != NULL); ProcVMA *vma3 = vm_find_vma(mm, i+2); assert(vma3 == NULL); ProcVMA *vma4 = vm_find_vma(mm, i+3); assert(vma4 == NULL); ProcVMA *vma5 = vm_find_vma(mm, i+4); assert(vma5 == NULL); assert(vma1->vm_start == i && vma1->vm_end == i + 2); assert(vma2->vm_start == i && vma2->vm_end == i + 2); } for (i =4; i>=0; i--) { ProcVMA *vma_below_5 = vm_find_vma(mm,i); assert(vma_below_5 == NULL); } vm_destroy_proc(mm); printf("[vmm] Passes check_vma_struct test.\n"); }
static void print_vma_struct(ProcVM *proc) { // Find the left-most node. RBNode *start = rb_leftmost(&(proc->mmap_root)); // Print every node. for (; start != NULL;) { ProcVMA *vma = rbn2vma(start, vm_link); printf("VMA start=%08u, end=%08u\n", vma->vm_start, vma->vm_end); start = rb_node_next(&(proc->mmap_root), start); } kfree(proc); }
void vm_destroy_proc(ProcVM *proc) { kassert(vm_get_proc_count(proc) == 0); // Find the left-most node. RBNode *start = rb_leftmost(&(proc->mmap_root)); // Destroy every node. for (; start != NULL;) { RBNode *right = rb_node_next(&(proc->mmap_root), start); kfree(rbn2vma(start, vm_link)); start = right; } kfree(proc); }