/** * Inserts new element in the list. If the index is past the end of * the list * it creates the list and add the element as first element of list. * @param new_element the element to be add in the list. * @return 32-bit integer on the success returns 1. */ OSCL_EXPORT_REF int32 Oscl_Linked_List_Base::insert_element(const OsclAny* new_element, int index) { if (index >= num_elements) { return add_element(new_element); } else if (index == 0) { return add_to_front(new_element); } else { OsclAny *tmp, *next, *newelem; int32 ii; if (index < 0) return 0; for (tmp = head, ii = 0; ii < index - 1; ++ii, tmp = pOpaqueType->get_next(tmp)) { } next = pOpaqueType->get_next(tmp); newelem = pOpaqueType->allocate(sizeof_T); if (newelem == NULL) return 0; pOpaqueType->construct(newelem, new_element); pOpaqueType->set_next(tmp, newelem); pOpaqueType->set_next(newelem, next); ++num_elements; return 1; } return 0; }
/* * Requires: * words: the number of words to increase the heap by * next: next pointer at the end of the linked list, to be set to the header of the new block * Effects: * Extend the heap with a free block and return that block's address. */ static void * extend_heap(size_t words) { size_t size; void *bp; /* Allocate an even number of words to maintain alignment. */ size = (words % 2) ? (words + 1) * WSIZE : words * WSIZE; if ((bp = mem_sbrk(size)) == (void *)-1) return (NULL); printf("Before extended block is added to the free list\n"); checkheap(1); /* Initialize the new node pointers, next precedes previous */ /* The previous point points to the header of the previous block*/ /*PUT(bp, NULL); PUT(bp + WSIZE, HDRP(next));*/ /* Initialize free block header/footer and the epilogue header. */ PUT(HDRP(bp), PACK(size, 0)); /* Free block header */ PUT(FTRP(bp), PACK(size, 0)); /* Free block footer */ PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* New epilogue header */ /* If list start is NULL, initialize the start to the pointer to the * added block */ if (list_start == NULL) { list_start = (struct node *)bp; list_start->next = list_start; list_start->previous = list_start; } printf("For isolation\n"); add_to_front(bp); printf("After extended block is added to the free list\n"); checkheap(1); printf("Entering coalesce from extend_heap\n"); /* Coalesce if the previous block was free. */ return (coalesce(bp)); }