内存管理API之page_cache_get_speculative

inline int page_cache_get_speculative(struct page *page)主要用于锁定page,如果page的引用计数是1的话,就返回0,如果引用计数已经是零,
就增加page的引用计数后,返回1
其源码分析如下:

static inline int page_cache_get_speculative(struct page *page)
{
#ifdef CONFIG_TINY_RCU
# ifdef CONFIG_PREEMPT_COUNT
	VM_BUG_ON(!in_atomic() && !irqs_disabled());
# endif
	/*
	 * Preempt must be disabled here - we rely on rcu_read_lock doing
	 * this for us.
	 *
	 * Pagecache won't be truncated from interrupt context, so if we have
	 * found a page in the radix tree here, we have pinned its refcount by
	 * disabling preempt, and hence no need for the "speculative get" that
	 * SMP requires.
	 */
	#在这种case下 下线断言page_count 不能为零,然后增加page的引用计数
	VM_BUG_ON_PAGE(page_count(page) == 0, page);
	page_ref_inc(page);

#else
	#get_page_unless_zero线判断page的引用计数是否为零,如果为零就增加引用计数,否则啥也不做
	if (unlikely(!get_page_unless_zero(page))) {
		/*
		 * Either the page has been freed, or will be freed.
		 * In either case, retry here and the caller should
		 * do the right thing (see comments above).
		 */
		#这种case是page的引用计数是1,没有增加引用计数的case
		return 0;
	}
#endif
	VM_BUG_ON_PAGE(PageTail(page), page);
	#这种case就增加page的引用计数
	return 1;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值