Thread (3 messages) 3 messages, 3 authors, 2016-05-25

free(), the size to free?

From: 慕冬亮 <hidden>
Date: 2016-05-25 19:24:58

2016-05-25 14:17 GMT-04:00 Greg KH [off-list ref]:
On Wed, May 25, 2016 at 02:00:41PM -0400, Wenda Ni wrote:
quoted
The prototype is void kfree(const void *).

As the input pointer is void *, how does the kernel know the size of memory to
be freed? There should be some metadata recorded under the hood, right?
Yes there is.  See the memory allocation code for the details.

Same goes for the C language implementation of free(), look at any libc
implementation for more details about that if you are curious, the
basics are the same for within the kernel.
Take heap allocation in glibc for example,

1. An allocated chunk is represented as follows:

chunk -> | Size of previous chunk, if allocated |
              | Size of chunk, in bytes         |A|M|P|
mem ->   | User data starts here                     |
              |                                                     |

2. An chunk that was at one time allocated but has been free()d,

chunk -> | Size of previous chunk, if allocated  |
              | Size of chunk, in bytes          |A|  |P|
mem ->   | Forward pointer to next chunk in list |
              | Back pointer to next chunk in list     |
              | Unused space                                |

The pointer (argument in free or kfree) is a pointer to a complicated
data structure. The information you seek is all in that data
structure. All you need to do is to find documents that explains this
data structure.

My best regards to you.

     No System Is Safe!
     mudongliang
thanks,

greg k-h

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help