In article <11**********************@22g2000hsm.googlegroups. com>,
Nehil <ne***********@gmail.comwrote:
>I've the following requirement :
>i'm developing a Garbage Collector for C. so i'd do develop a complete
memory manager ie i'd to allocate and de-allocate. for allocation i'm
using sbrk(int mem).
I am not clear what your portability requirements are? A number
of pointer operations which are formally unspecified or undefined
behaviour in C are well defined and simple if you are allowed to
assume particular operating environments -- but can be a large
large nuisance if you are trying to work portably.
>acc. to man page it returns a void pointer to the memory chunk of mem
bytes. but actually it is returning a void pointer to a memory chunk
of 4kb (4096 bytes).
I'd like to emphasize that sbrk() is not considered portable,
being not a part of C nor of POSIX.1. It is part of the
Single Unix Specification Version 2, but there are still quite
a number of useful Unix operating systems which are not certified
to Version 2 of the SUS. Even the SUS lists them as LEGACY and says,
"The use of malloc() is now preferred because it can be used
portably with all other memory allocation functions and with any
function that uses other allocation functions."
http://www.opengroup.org/onlinepubs/...9/xsh/brk.html
"It is unspecified whether the pointer returned by sbrk() is
aligned suitably for any purpose."
Which is equivilent to a flaming red arrow saying THIS IS NOT PORTABLE.
>Now, at the start of program this whole chunk can be treated as a free
block of memery. so i do as follows :
Header *ptr_free = (Header *)sbrk(4096);
Header *ptr_alloc = NULL;
>when, user requests for memory of certain bytes, let say 20 byets. so
i do as follows : (i'm considering only the very 1st request)
And if the user requests 4096 - sizeof(struct header) or more,
you have problems. The memory your allocation routines hands over
to the users must be consequative, so you cannot hand over multiple
chunks each with an embedded header.
>ptr_alloc = ptr_free;
ptr_free = ptr_free + 20 + sizeof(Header);
void *memory = ptr_alloc + sizeof(Header);
return memory;
>i.e. ptr_free is now pointing to the start of un-allocated part of
that big chunk. and memory points to the allocated part, but leaves
the starting header.
>Now, ptr_free should have size of "rest of the block". but it is a
single node so it's not possible. plz correct me if i'm wrong.
To keep the record of free blocks i need to break the big chunk into
parts : Allocated and Free.
How can i break the node into two parts so that it can have size
information for allocated and free parts.
If you do not have enough C experience to figure this out on your
own (and it is not difficult) then you do not have enough C experience
to implement a garbage collector. This bit is trivial compared to
figuring out whether a particular block of memory is still in
use or not!
--
Programming is what happens while you're busy making other plans.