kzalloc()_kcalloc()

定义在/include/linux/slab.h中

1. kzalloc()
 --- kmalloc + memset 0;

[cpp]  view plain copy
  1. /** 
  2.  * kzalloc - allocate memory. The memory is set to zero. 
  3.  * @size: how many bytes of memory are required. 
  4.  * @flags: the type of memory to allocate (see kmalloc). 
  5.  */  
  6. static inline void *kzalloc(size_t size, gfp_t flags)  
  7. {  
  8.     return kmalloc(size, flags | __GFP_ZERO);  
  9. }  

2. kcalloc()  ---  分配n块内存,kmalloc + memset 0
[cpp]  view plain copy
  1. /** 
  2.  * kcalloc - allocate memory for an array. The memory is set to zero. 
  3.  * @n: number of elements. 
  4.  * @size: element size. 
  5.  * @flags: the type of memory to allocate. 
  6.  * 
  7.  * The @flags argument may be one of: 
  8.  * 
  9.  * %GFP_USER - Allocate memory on behalf of user. May sleep. 
  10.  * 
  11.  * %GFP_KERNEL - Allocate normal kernel ram. May sleep. 
  12.  * 
  13.  * %GFP_ATOMIC - Allocation will not sleep. May use emergency pools. 
  14.  * For example, use this inside interrupt handlers. 
  15.  * 
  16.  * %GFP_HIGHUSER - Allocate pages from high memory. 
  17.  * 
  18.  * %GFP_NOIO - Do not do any I/O at all while trying to get memory. 
  19.  * 
  20.  * %GFP_NOFS - Do not make any fs calls while trying to get memory. 
  21.  * 
  22.  * %GFP_NOWAIT - Allocation will not sleep. 
  23.  * 
  24.  * %GFP_THISNODE - Allocate node-local memory only. 
  25.  * 
  26.  * %GFP_DMA - Allocation suitable for DMA. 
  27.  * Should only be used for kmalloc() caches. Otherwise, use a 
  28.  * slab created with SLAB_DMA. 
  29.  * 
  30.  * Also it is possible to set different flags by OR'ing 
  31.  * in one or more of the following additional @flags: 
  32.  * 
  33.  * %__GFP_COLD - Request cache-cold pages instead of 
  34.  * trying to return cache-warm pages. 
  35.  * 
  36.  * %__GFP_HIGH - This allocation has high priority and may use emergency pools. 
  37.  * 
  38.  * %__GFP_NOFAIL - Indicate that this allocation is in no way allowed to fail 
  39.  * (think twice before using). 
  40.  * 
  41.  * %__GFP_NORETRY - If memory is not immediately available, 
  42.  * then give up at once. 
  43.  * 
  44.  * %__GFP_NOWARN - If allocation fails, don't issue any warnings. 
  45.  * 
  46.  * %__GFP_REPEAT - If allocation fails initially, try once more before failing. 
  47.  * 
  48.  * There are other flags available as well, but these are not intended 
  49.  * for general use, and so are not documented here. For a full list of 
  50.  * potential flags, always refer to linux/gfp.h. 
  51.  */  
  52. static inline void *kcalloc(size_t n, size_t size, gfp_t flags)  
  53. {  
  54.     if (size != 0 && n > ULONG_MAX / size)  
  55.         return NULL;  
  56.     return __kmalloc(n * size, flags | __GFP_ZERO);  
  57. }  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值