Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allocation of memory by zalloc

Tags:

perf

I am going through the perf source in linux kernel source to find out how user space probing is implemented. At many places, I encountered this :

zalloc(sizeof(struct __event_package) * npevs);

I think its located in zlib library (for fedora 18). Can anybody tell me how this zalloc helps in allocating memory? Thanks in advance...

like image 411
Hemant Kumar Avatar asked Mar 09 '26 17:03

Hemant Kumar


1 Answers

You can refer this link:

The allocation is the same as any other heap allocation. In the kernel space, heap is divided into many freelists, and each freelist has blocks of same sizes connected in a linked list.

For eg:
Freelist1 - 4 bytes/block x 10 blocks
Freelist2 - 8 bytes/block x 10 blocks
Freelist3 - 16 bytes/block x 10 blocks
....
Freelist10 - 1024 bytes/block x 10 blocks

Each free list represents slabs (slab allocator) and make use of buddy system

So, when one does a zalloc, it first decides which size freelist can fulfill this request and then finds a free blocks from it.

In some custom kernel implementations, heap is divided amongst kernel & other services. In that case, *alloc needs to know which heap to access to fulfill the request.

like image 175
brokenfoot Avatar answered Mar 14 '26 10:03

brokenfoot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!