Please translate

This is a discussion on Please translate within the C Programming forums, part of the General Programming Boards category; I have a huge set of code that I have to translate from C to C++. But this function, I ...

  1. #1
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151

    Please translate

    I have a huge set of code that I have to translate from C to C++. But this function, I cannot make any sense. Please help in explaining.

    Code:
    void myfree(void *ap)
    {
    	Header *bp, *p;
    	bp = (Header *)ap - 1; /* point to block header */
    	printf("bp->s.size %d\n",bp->s.size);
    	for (p = freeptr; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
    		if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
    		{
    			printf("in the corner\n");
    			break; /* freed block at start or end of arena */
    		}
    
        bp->s.check =1;
        if (bp + bp->s.size == p->s.ptr)
    	{
           /* join to upper nbr */
    		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)
    	{ /* join to lower nbr */
    		p->s.size += bp->s.size;
    		p->s.ptr = bp->s.ptr;
    	}
    	 else
    		p->s.ptr = bp;
    	freeptr = p;
    }
    This is the content of the struct.
    Code:
    union header{ /* block header */
    	struct{
    		union header *ptr; /* next block if on free list */
    		unsigned size; /* size of this block */
    		int check;
    	} s;
    	Align x; /* force alignment of blocks */
    };
    
    typedef union header Header;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Ah, such lovely variable names. And your indentation is lying to you, especially with your if/else blocks at the bottom.

    But: do you have a specific question here? Each line is fairly straightforward in and of itself; it would appear the goal is to absorb the content of bp into an appropriate p, with the for-loop at the top being the way to find the appropriate p.

  3. #3
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    I cant make any sense out of it. What is the function parsing through? What is the conditional statement. I was able to understand the mymalloc, but not myfree.

  4. #4
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    What is 'freeptr' ?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Ron View Post
    I cant make any sense out of it. What is the function parsing through? What is the conditional statement. I was able to understand the mymalloc, but not myfree.
    I don't know which conditional statement you mean. There's this one:
    Code:
    	if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
    		{
    			printf("in the corner\n");
    			break; /* freed block at start or end of arena */
    		}
    which if you'll notice is the same criterion for stopping the for-loop -- I guess the point is that these things aren't always in order, so we want to make sure we hit it even if we're going down. But anyway, the loop is that p starts at freeptr (which is whatever it is) and follows the links until we find a p with bp > p (so before the base pointer to be freed) and bp < p->s.ptr (so the next one on the list will be after the base pointer). You say you understand the mymalloc, so you should know how these pointers are linked together, 'cause I don't.

    There's this one:
    Code:
    if (bp + bp->s.size == p->s.ptr)
    which says that "if the base pointer, plus its size, equals the next one on the list"; and there's a similar one underneath that goes the other way.

  6. #6
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    The reason is that there is some error in this code. When I run contents into a list of memory and try to free each node. It comes only to the first if loop, breaks, then goes for the next node. After a few iterations, it stops without actually coming back to the prompt. If I know what is wrong in the code, fix it, then I can start to program it in C++.

  7. #7
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    OK I spoke to someone and was told that I have to keep track of the previous free node by using the pointer field (*ptr)in the header. Searching for previous free blocks in the free function will not be needed then.
    Last edited by Ron; 07-25-2008 at 05:54 PM.

  8. #8
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    How do I do this?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Set the ptr to point to the big list of freed pointers with a name like -- oh, I don't know -- freeptr, maybe, and then set freeptr to point to the current node?

  10. #10
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    I ve removed the last else statement, but the programming is running. But out of 30000 words that are added using malloc 110 is still remaining using the free function. I was just trying hit and trial method, as I can't make any sense out of the code.
    If someone could throw a little more light on the code. Im a lame man.

  11. #11
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    As I said 'I have to keep track of the previous free node by using the pointer field (*ptr)in the header. Searching for previous free blocks in the free function will not be needed then.'
    I cant understand this code nor what I have to do. The rest of my program is working, if I use the standard malloc and free function.

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The rest of my program is working, if I use the standard malloc and free function.
    Then is there actually any point in using mymalloc and myfree? I didn't bother studying that code, but I would assume that it is an attempt at optimization. Perhaps you don't need that optimization. (I think in C++ custom allocators should be available anyway.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Ron View Post
    As I said 'I have to keep track of the previous free node by using the pointer field (*ptr)in the header. Searching for previous free blocks in the free function will not be needed then.'
    That's a bog-standard linked list. freeptr points at the most recently freed block, which points to the next-most recently freed block, which points at the third-most recently freed block, ..., until you run out of freed blocks (which means, in mymalloc, that you have to get a fresh piece of memory instead of recycling old memory).

    So -- set freeptr to NULL initially; when you free a piece of memory, set its ptr to the (old) contents of freeptr and set freeptr to the just-freed memory. Easiest thing in the world.

  14. #14
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    OK i have made the following changes:
    Code:
    void *mymalloc(unsigned nbytes)
    {
    	Header *p, *prevptr;
    	unsigned nunits;
    	nunits = (nbytes+(sizeof(Header))-1)/(sizeof(Header)) + 1;
    
    	if ((prevptr = freeptr) == NULL)
    	{ /* no free list yet */
    		base.s.ptr = (Header *)calloc(MEM_SIZE,sizeof(Header));
    		base.s.size = 0;
    		base.s.check = 0;            X
    		/* allocase a memory from OS and then manage it */
    		base.s.ptr->s.size = MEM_SIZE;
    		base.s.ptr->s.ptr = &base;
    		base.s.ptr->s.check=0;    X
    		freeptr = prevptr = &base;
    	}
    
    	for (p = prevptr->s.ptr; ; prevptr = p, p = p->s.ptr)
    	{
    		if (p->s.size >= nunits)
    		{ /* big enough */
    			if (p->s.size == nunits) /* exactly */
    			{
    				prevptr->s.ptr = p->s.ptr;
    				p->s.check=1;   X
    			}
    			else
    			{ /* allocate tail end */
    				p->s.size -= nunits;
    				p += p->s.size;
    				p->s.size = nunits;
    				p->s.check=1;   X
    			}
    
    			freeptr = prevptr;
    			return (void *)(p+1);
    		}
    	}
    }
    Code:
    void myfree(void *ap)
    {
    	Header *bp, *p;
    	bp = (Header *)ap - 1; /* point to block header */
    	
    	bp -> s.check=0;          X
    	for (p = freeptr; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
    		/*if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
    		{
    			printf("in the corner\n");
    			break;  freed block at start or end of arena
    		}*/
    
    
        if (bp + bp->s.size == p->s.ptr)
    	{
           /* join to upper nbr */
    		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)
    	{ /* join to lower nbr */
    		p->s.size += bp->s.size;
    
    		p->s.ptr = bp->s.ptr;
    	}
    	 else
    
    			p->s.ptr = bp;
    	
    	                freeptr = p;
    }
    But still Im getting the same effect.
    Last edited by Ron; 07-29-2008 at 07:45 PM.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Ron View Post
    OK i have made the following changes:
    Code:
    void *mymalloc(unsigned nbytes)
    {
    	Header *p, *prevptr;
    	unsigned nunits;
    	nunits = (nbytes+(sizeof(Header))-1)/(sizeof(Header)) + 1;
    
    	if ((prevptr = freeptr) == NULL)
    	{ /* no free list yet */
    		base.s.ptr = (Header *)calloc(MEM_SIZE,sizeof(Header));
    		base.s.size = 0;
    		base.s.check = 0;            X
    		/* allocase a memory from OS and then manage it */
    		base.s.ptr->s.size = MEM_SIZE;
    		base.s.ptr->s.ptr = &base;
    		base.s.ptr->s.check=0;    X
    		freeptr = prevptr = &base; /*no changing freeptr!  You're claiming that this block is immediately available for freeing! */
    		/* Also, shouldn't you return now, because you're done? (you don't want to walk the free list, if there isn't one) */
    	}
    
    	for (p = prevptr->s.ptr; ; prevptr = p, p = p->s.ptr)
    	{
    		if (p->s.size >= nunits)
    		{ /* big enough */
    			if (p->s.size == nunits) /* exactly */
    			{
    				prevptr->s.ptr = p->s.ptr;
    				p->s.check=1;   X
    			}
    			else
    			{ /* allocate tail end */
    				p->s.size -= nunits;
    				p += p->s.size;
    				p->s.size = nunits;
    				p->s.check=1;   X
    			}
    
    			freeptr = prevptr;
    			return (void *)(p+1);
    		}
    	}
    	/* What happens if none of the free blocks is large enough by itself? */
    }
    Code:
    void myfree(void *ap)
    {
    	Header *bp, *p;
    	bp = (Header *)ap - 1; /* point to block header */
    	
    	bp -> s.check=0;          X
    	for (p = freeptr; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
    		/*if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
    		{
    			printf("in the corner\n");
    			break;  freed block at start or end of arena
    		}*/
    
    
        if (bp + bp->s.size == p->s.ptr)
    	{
           /* join to upper nbr */
    		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)
    	{ /* join to lower nbr */
    		p->s.size += bp->s.size;
    
    		p->s.ptr = bp->s.ptr;
    	}
    	 else
    
    			p->s.ptr = bp;
    	
    	                freeptr = p;
    }
    But still Im getting the same effect.
    I've made some comments in your mymalloc. And I thought you had said you had rewritten myfree to something like
    Code:
    bp->s.ptr = freeptr;
    freeptr = bp;
    so that you could maintain your linked list?

    I don't remember what check was for, so I can't say whether what you're doing there does the right thing or not.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-28-2008, 12:44 AM
  2. Translate number to sentence
    By alphaoide in forum C++ Programming
    Replies: 1
    Last Post: 09-29-2004, 02:56 AM
  3. Please help me... Translate from c++ to visual basic
    By westcard in forum C++ Programming
    Replies: 6
    Last Post: 08-17-2004, 03:29 PM
  4. can anyone translate this all to aschii characters?
    By tetra in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2003, 10:18 PM
  5. just translate !
    By black in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 07-03-2002, 03:14 AM

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