kzalloc, kcalloc, kmalloc, vmalloc

The kernel code base is full of functions which allocate memory with  kmalloc() , then zero it with memset() . Recently, Pekka Enberg concluded that much of this code could be cleaned up by using kcalloc()  instead.  kcalloc()  has this prototype:

    void *kcalloc(size_t n, size_t size, unsigned int __nocast gfp_flags);

This function will allocate an array of n items, and will zero the entire array before returning it to the caller. Pekka's patch converted a number of kmalloc()/memset() pairs over to kcalloc(), but that patch drew a complaint from Andrew Morton:

Notice how every conversion you did passes in `1' in the first argument? And that's going to happen again and again and again. Each callsite needlessly passing that silly third argument, adding more kernel text.

Very few callers actually need to allocate an array of items, so the extra argument is unneeded in most cases. Each instance of that argument adds a bit to the size of the kernel, and, over time, that space adds up. The solution was to create yet another allocation function:

    void *kzalloc(size_t size, unsigned int __nocast gfp_flags);

This function returns a single, zeroed item. It has been added to -mm, with its appearance in the mainline likely to happen for 2.6.14.

kzalloc zeroes the memory before returninga pointer

kcalloc allocates memory for an array, itis not a replacement for kcalloc :

void *kcalloc(size_t n, size_t size, gfp_tflags)

vmalloc is the same as kmalloc, except itallocates memory that is only virtually contiguous. The underling physicalmemory can be discontiguous.

So kmalloc can eventually be replaced by acall to kzalloc, but it is unlikely to solve your problem


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值