int main() { addfront(1); display(); addfront(2); display(); addfront(3); display(); addfront(4); display(); addrear(5); display(); addrear(6); display(); addrear(7); display(); addrear(8); display(); addrear(9); display(); deletelist(); display(); deletelist(); display(); /* addafter(10, 5); display(); addafter(20,10); display(); addafter(20, 9); display(); addafter(21, 20); display(); addrear(22); display(); addbefore(30, 3); display(); addbefore(31, 4); display(); addbefore(32, 5); display(); addbefore(33, 299); display(); addnth(40, 10); display(); addnth(41, 0); display(); addnth(42, 1); display(); addnth(43, length()); display(); addnth(44, length()+1); display(); */ return 0; }
struct page *region_get_mem(size_t s) { size_t request_bytes; void *mem; struct page *newp; /* Don't get less than K * RPAGESIZE extra memory (K * RPAGESIZE is the minimum useful size for something on unused_pages) */ if (s + K * RPAGESIZE < MINIMUM_MEM_REQUEST) request_bytes = MINIMUM_MEM_REQUEST; else request_bytes = s; #if 0 request_bytes = ALIGN(request_bytes, 65536); #endif mem = (struct page *)MMAP(0, request_bytes+RPAGESIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE); if (!mem) { out_of_memory(); abort(); } VALGRIND_MALLOCLIKE_BLOCK(mem, request_bytes+RPAGESIZE, 0, 0); // VALGRIND_MAKE_NOACCESS(mem, request_bytes+RPAGESIZE); newp = PALIGN(mem, RPAGESIZE); VALGRIND_MAKE_WRITABLE(newp, sizeof(struct page)); memset(newp, 0, sizeof(struct page)); if (mem == newp) /* Maybe we were lucky! */ request_bytes += RPAGESIZE; addbyaddress(newp); /* Add the new memory to unused_pages */ #ifndef NMEMDEBUG set_region_range(newp, (char *)newp + s, FREEPAGE); #endif total_page_count += request_bytes >> RPAGELOG; newp->pagecount = request_bytes >> RPAGELOG; assert(newp->pagecount > 0); newp->free = 1; addfront(&unused_pages, newp); return newp; }
static void coalesce(struct page *p) { struct page *prev = p->prev_address, *next; p->free = 1; assert(p->pagecount > 0); if (p->pagecount) { // mark data part of page non-accessible // VALGRIND_MAKE_NOACCESS(p + sizeof(struct page), // (p->pagecount << RPAGELOG) - sizeof(struct page)); } /* Coalesce with predecessor ? */ if (prev->free && (char *)prev + (prev->pagecount << RPAGELOG) == (char *)p) { // fprintf(stderr, // "## coalesce p=%p (pagecount=%d) with prev=%p (pagecount=%d)\n", // p, p->pagecount, prev, prev->pagecount); assert(prev->pagecount > 0); prev->pagecount += p->pagecount; unlink_address(p); p = prev; } else /* No, add to free pages list */ { // fprintf(stderr, // "## coalesce p=%p (pagecount=%d) => unused_pages\n", // p, p->pagecount); addfront(&unused_pages, p); } next = p->next_address; /* Coalesce with successor ? */ if (next->free && (char *)p + (p->pagecount << RPAGELOG) == (char *)next) { // fprintf(stderr, // "## coalesce p=%p (pagecount=%d) with next=%p (pagecount=%d)\n", // p, p->pagecount, next, next->pagecount); unlink_page(&unused_pages, next); p->pagecount += next->pagecount; unlink_address(next); } }
struct page *region_get_mem(size_t s) { size_t request_bytes; void *mem; struct page *newp; /* Don't get less than K * RPAGESIZE extra memory (K * RPAGESIZE is the minimum useful size for something on unused_pages) */ if (s + K * RPAGESIZE < MINIMUM_MEM_REQUEST) request_bytes = MINIMUM_MEM_REQUEST; else request_bytes = s; mem = xmalloc(request_bytes + RPAGESIZE); // memset(mem, 0, request_bytes + RPAGESIZE); // VALGRIND_MAKE_NOACCESS(mem, request_bytes + RPAGESIZE); newp = PALIGN(mem, RPAGESIZE); // fprintf(stderr, "## region_get_mem: s=%d, request_bytes=%d, mem=%p, newp=%p\n", s, request_bytes, mem, newp); // VALGRIND_MAKE_WRITABLE(newp, sizeof(struct page)); memset(newp, 0, sizeof(struct page)); if (mem == newp) /* Maybe we were lucky! */ request_bytes += RPAGESIZE; addbyaddress(newp); /* Add the new memory to unused_pages */ #ifndef NMEMDEBUG set_region_range(newp, (char *)newp + request_bytes, FREEPAGE); #endif total_page_count += request_bytes >> RPAGELOG; newp->pagecount = request_bytes >> RPAGELOG; assert(newp->pagecount > 0); newp->free = 1; addfront(&unused_pages, newp); return newp; }
int main(){ struct node *head = NULL; int n; char ch, fakech, quit = 1; do{ printf("\n"); printf("1 - Insert\n"); printf("2 - Display\n"); printf("3 - Find\n"); printf("4 - Delete\n"); printf("5 - Delete All\n"); printf("0 - Quit\n"); printf("\n"); printf("Enter choice: "); scanf("%c", &ch); scanf("%c", &fakech); switch(ch){ case '1': printf("Insert Element: "); scanf("%d", &n); scanf("%c", &fakech); printf("Add front or back?(f/b): "); scanf("%c", &ch); scanf("%c", &fakech); if(ch == 'f'){ head = addfront(head, n); } else{ head = addback(head, n); } break; case '2': display(head); break; case '3': printf("Find: "); scanf("%d", &n); scanf("%c", &fakech); printf("Result: %p\n", find(head, n)); break; case '4': printf("Delete: "); scanf("%d", &n); scanf("%c", &fakech); struct node *p; if((p = find(head, n)) != NULL){ head = delnode(head, p); } else{ printf("Not found in the list!\n"); } break; case '5': freelist(head); head = NULL; break; case '0': quit = 0; break; } } while(quit); return 0; }