Re: malloc memory region descriptors

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



OK! Now I came to know what exactly your problem is......

malloc is an intelligent function...it keeps record of every byte
returned by it.....what it do is...It takes a bulk amount of memory
from operating system.....and then manage it himself for the
process...

malloc keeps a record of allocated bytes and free ones through a
structure called

struct mem_control_block {
	int is_available;
	int size;
};

and two global variables
void *managed_memory_start;
void *last_valid_address;

when you call malloc(34), it takes bulk amount of memory from the
OS..(lets say 4 KB)...and then put the starting address of this
memory(returned by OS) into both the global variables
'managed_memory_start' and 'last_valid_address'. Then it initialize
the object of above strucure, assigning, is_available=0 and
size=34....it then writes this object at the address pointed by
"last_valid_address"(that is the starting address of returned memory),
then it initialize a local 'void * memory_location'
memory_location=last_valid_address+sizeof(mem_control_block);
then returns this address to the process, hence we can say that
"information about the area allocated to process' by mlloc is kept at
the beginning of allocated area.
it then updates the last_valid_address to
last_valid_address=last_valid_address+sizeof(mem_control_block)
+size;//here last_valid_address is starting address of memory block
returned by OS,size=34 as you given this value.
Then when you call malloc(24);
Then it again initialize the object of  mem_control_block strucure,
assigning, is_available=0 and size=24....it then writes this object at
the address pointed by "last_valid_address"(that is least starting
address of free memory), then it initialize a local 'void *
memory_location'
memory_location=last_valid_address+sizeof(mem_control_block);
then returns this address to the process,
The same thing applies to when u call e=malloc(1024);

when u call free(e);
now 'e' contains the address returned by malloc and we know that
information about the allocated memory resides just before the
returned addres...
so we manipulate this thing...
struct mem_control_block *temp;
temp = (mem_control_block *) ( e - sizeof(mem_control_block));
now temp points to the info block of allocated area (initialized and
stored by malloc(1024));
now it do..
we know
temp->size contains 1024, this is where free(...) come to know about
the size of block.
temp->is_available = 1;//it means this block is free
last_valid_address = last_valid_address - temp->size;

you call malloc(1845);
same thing repeats again..........



I have written this in brief.I hope this will help you little
A lot of other things needs to be discussed.....
like fragmentation problem in it, when it requests to OS for more memory...
like traversing from the starting address to find free blocks
everytime,....old algorithm implemented in malloc, new algorithm to
overcome the limitations of old algorithm  etc..... and lots
more..........

This is a big but simple topic and needs a article of about 20
pages.....I will write a complete aricle over this.....upload it
soon...



On 8/16/10, Prabhu nath <gprabhunath@xxxxxxxxx> wrote:
>
>
>
> On Mon, Aug 16, 2010 at 1:30 PM, Prabhu nath <gprabhunath@xxxxxxxxx> wrote:
> > Dear All,
> >
> >           In linux kernel, for all memory allocation done by vmalloc,
> kernel maintains memory region descriptor (vm_struct) which stores
> information about the linear virtual address range, no. of physical page
> frames allocated... as a linked list headed by vmlist symbol.
> > Can you please give me information about how does kernel maintain
> information about memory allocation done by malloc/calloc invoked by a user
> application ?
> >
> > I understand that kernel maintains a descriptor (vm_area_struct) per
> application to hold information about the virtual address space allocated
> for the heap region.
> >
>
> I would like to elicit my question. Here is a simple code sample
>
> int *b;
> int main()
> {
>     b = malloc(4);
>     printf ("Address of b = 0x%08x \n", b);
>     getchar();
>     return 0;
> }
>
> If I pause the program and inspect the maps file </proc/<pid>/maps>, I get
> to see a whooping 132K of linear virtual address allocated and Address of b
> = 0x0804a008
> Here is a fragment of the maps file.
>
> 08048000-08049000 r-xp 00000000 fd:00 16845753  cSamples/cTests/a.out
> 08049000-0804a000 rw-p 00000000 fd:00 16845753  cSamples/cTests/a.out
> 0804a000-0806b000 rw-p 0804a000 00:00 0          [heap]
> b7f3d000-b7f3e000 rw-p b7f3d000 00:00 0
> b7f4a000-b7f4d000 rw-p b7f4a000 00:00 0
>
> Suppose if have to extend my code as
> c = malloc (34);
> d = malloc(12);
> e = malloc(1024);
> free(e);
> f = malloc(1845);
>
> Assume (c, d, e, f are all declared globally)
>
> Now, when free(e) is executed, kernel should have some information about the
> size of address space allocated to symbol 'e' so that it will only free that
> virtual address region in the heap space that was allocated to e.
>
> Q. Where and how does the kernel maintain information about the size of
> address space allocated to symbol 'e'
>
>
>
> >
> > Regards,
> > Prabhu
> >
> >
>
>

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux