void *malloc()

This is a discussion on void *malloc() within the C Programming forums, part of the General Programming Boards category; This is implementation of malloc, morecore and free on a UNIX. But i cant figure out how does free works. ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    void *malloc()

    This is implementation of malloc, morecore and free on a UNIX.

    But i cant figure out how does free works.

    The book says free scans the free list, starting at freep and looks for the place to insert the free block.

    But i just cant comprehend how does this loop matches the text above;
    Code:
    for(p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr) {
            if(p >= p->s.ptr && (bp > p || bp < p->s.ptr))
                break;
    }
    especially the condition:
    !(bp > p && bp < p->s.ptr)
    and

    if(p >= p->s.ptr && (bp > p || bp < p->s.ptr)) //this condition can ONLY be true if its the last free block? because the last free block points to the first free block, so p's position is bigger then the ptr he points to?

    Code:
    /* 
     * File:   newmain.c
     * Author: root
     *
     * Created on March 14, 2010, 8:23 PM
     */
    #include <stdio.h>
    #include <string.h>
    
    typedef long Align;
    
    union header {
        struct {
            union header *ptr;
            unsigned size;
        } s;
        Align x;
    };
    
    typedef union header Header;
    
    static Header base;
    static Header *freep = NULL;
    
    void *malloc(unsigned nbytes)
    {
        Header *p, *prevp;
        Header *morecore(unsigned);
        unsigned nunits;
    
        nunits = (nbytes + sizeof(Header) - 1)/ sizeof(Header) + 1;
        if((prevp = freep) == NULL) {
            base.s.ptr = freep = prevp = &base;
            base.s.size = 0;
        }
        for(p = prevp->s.ptr; ; prevp = p, p = p->s.ptr) {
            if(p->s.size >= nunits) {
                if(p->s.size == nunits)
                    prevp->s.ptr = p->s.ptr;
                else {
                    p->s.size -= nunits;
                    p += p->s.size;
                    p->s.size = nunits;
                }
                freep = prevp;
                return (void *)(p + 1);
            }
            if(p == freep)
                if((p = morecore(nunits)) == NULL)
                    return NULL;
        }
    }
    
    #define NALLOC 1024
    
    void free(void *ap)
    {
        Header *bp, *p;
    
        bp = (Header *) ap - 1;
        for(p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr) {
            if(p >= p->s.ptr && (bp > p || bp < p->s.ptr))
                break;
        }
        if(bp + bp->s.size == p->s.ptr) {
            bp->s.size += p->s.ptr->s.size;
            bp->s.ptr = p->s.ptr->s.ptr;
        } else
            bp->s.ptr = p->s.ptr;
        if(p + p->s.size == bp) {
            p->s.size += bp->s.size;
            p->s.ptr = bp->s.ptr;
        } else
            p->s.ptr = bp;
        freep = p;
    }
    
    static Header *morecore(unsigned nu)
    {
        char *cp, *sbrk(int);
        Header *up;
    
        if(nu < NALLOC)
            nu = NALLOC;
        cp = sbrk(nu * sizeof(Header));
        if(cp == (char *) - 1)
            return NULL;
        up = (Header *) cp;
        up->s.size = nu;
        free((void *)(up + 1));
        return freep;
    }
    /*
     * 
     */
    int main(int argc, char** argv) {
    
        return 0;
    }
    Last edited by Tool; 03-15-2010 at 04:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Replies: 3
    Last Post: 05-13-2007, 09:55 AM
  3. game window rejected painting !
    By black in forum Windows Programming
    Replies: 4
    Last Post: 03-27-2007, 02:10 AM
  4. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 06:40 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21