手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux Kernel 3.0.8 内存管理函数

[日期:2012-03-05] 来源:Linux社区  作者:myarrow [字体:大 中 小]
从区域zone中获取一块大小为2^order的物理内存块,返回该内存块的首个页框的描述符page。
  1. static inline  
  2. struct page *buffered_rmqueue(struct zone *preferred_zone,  
  3.             struct zone *zone, int order, gfp_t gfp_flags,  
  4.             int migratetype)  
  5. {  
  6.     unsigned long flags;  
  7.     struct page *page;  
  8.     int cold = !!(gfp_flags & __GFP_COLD);  
  9.   
  10. again:  
  11.     if (likely(order == 0)) { //获取一页物理内存(2^0),从当前cpu的高速缓存内存中申请   
  12.         struct per_cpu_pages *pcp;  
  13.         struct list_head *list;  
  14.   
  15.         local_irq_save(flags);  
  16.         pcp = &this_cpu_ptr(zone->pageset)->pcp; //获取zone的当前处理器的高速缓存内存描述结构指针   
  17.         list = &pcp->lists[migratetype];  
  18.         if (list_empty(list)) { //高速缓存内存为空   
  19.             pcp->count += rmqueue_bulk(zone, 0,//调用此函数从伙伴系统中分配batch空闲内存到高速缓存内存中   
  20.                     pcp->batch, list,  
  21.                     migratetype, cold);  
  22.             if (unlikely(list_empty(list)))  
  23.                 goto failed;  
  24.         }  
  25.                    //我们从pcp->list链表开始的第一个lru起,去寻找相应的struct page结构体   
  26.         if (cold)  
  27.             page = list_entry(list->prev, struct page, lru);  
  28.         else  
  29.             page = list_entry(list->next, struct page, lru);  
  30.                   //由于被分配出去了,所以高速缓存内存中不再包含这页内存,所以从链表里删除这一项。   
  31.         list_del(&page->lru);  
  32.         pcp->count--;  //相应的当前页数也要减少   
  33.     } else { //获取一块物理内存(2^order)   
  34.         if (unlikely(gfp_flags & __GFP_NOFAIL)) {  
  35.             /* 
  36.              * __GFP_NOFAIL is not to be used in new code. 
  37.              * 
  38.              * All __GFP_NOFAIL callers should be fixed so that they 
  39.              * properly detect and handle allocation failures. 
  40.              * 
  41.              * We most definitely don't want callers attempting to 
  42.              * allocate greater than order-1 page units with 
  43.              * __GFP_NOFAIL. 
  44.              */  
  45.             WARN_ON_ONCE(order > 1);  
  46.         }  
  47.         spin_lock_irqsave(&zone->lock, flags);  
  48.         page = __rmqueue(zone, order, migratetype); //调用函数申请内存   
  49.         spin_unlock(&zone->lock);  
  50.         if (!page)  
  51.             goto failed;  
  52.         __mod_zone_page_state(zone, NR_FREE_PAGES, -(1 << order));  
  53.     }  
  54.   
  55.     __count_zone_vm_events(PGALLOC, zone, 1 << order);  
  56.     zone_statistics(preferred_zone, zone, gfp_flags);  
  57.     local_irq_restore(flags);  
  58.   
  59.     VM_BUG_ON(bad_range(zone, page));  
  60.     if (prep_new_page(page, order, gfp_flags))  
  61.         goto again;  
  62.     return page; //返回申请到的内存空间的首页内存页的struct page结构指针   
  63.   
  64. failed:  
  65.     local_irq_restore(flags);  
  66.     return NULL;  
  67. }  

4) rmqueue_bulk

 用于多次(count)内存申请.

  1. /*  
  2.  * Obtain a specified number of elements from the buddy allocator, all under 
  3.  * a single hold of the lock, for efficiency.  Add them to the supplied list. 
  4.  * Returns the number of new pages which were placed at *list. 
  5.  */  
  6. static int rmqueue_bulk(struct zone *zone, unsigned int order,   
  7.             unsigned long count, struct list_head *list,  
  8.             int migratetype, int cold)  
  9. {  
  10.     int i;  
  11.       
  12.     spin_lock(&zone->lock);  
  13.     for (i = 0; i < count; ++i) {  
  14.         struct page *page = __rmqueue(zone, order, migratetype);  
  15.         if (unlikely(page == NULL))  
  16.             break;  
  17.   
  18.         /* 
  19.          * Split buddy pages returned by expand() are received here 
  20.          * in physical page order. The page is added to the callers and 
  21.          * list and the list head then moves forward. From the callers 
  22.          * perspective, the linked list is ordered by page number in 
  23.          * some conditions. This is useful for IO devices that can 
  24.          * merge IO requests if the physical pages are ordered 
  25.          * properly. 
  26.          */  
  27.         if (likely(cold == 0))  
  28.             list_add(&page->lru, list);  
  29.         else  
  30.             list_add_tail(&page->lru, list);  
  31.         set_page_private(page, migratetype);  
  32.         list = &page->lru;  
  33.     }  
  34.     __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));  
  35.     spin_unlock(&zone->lock);  
  36.     return i;  
  37. }   

5) __rmqueue

 用于一次内存申请。

  1. /* 
  2.  * Do the hard work of removing an element from the buddy allocator. 
  3.  * Call me with the zone->lock already held. 
  4.  */  
  5. static struct page *__rmqueue(struct zone *zone, unsigned int order,  
  6.                         int migratetype)  
  7. {  
  8.     struct page *page;  
  9.   
  10. retry_reserve:  
  11.     page = __rmqueue_smallest(zone, order, migratetype);  
  12.   
  13.     if (unlikely(!page) && migratetype != MIGRATE_RESERVE) {  
  14.         page = __rmqueue_fallback(zone, order, migratetype);  
  15.   
  16.         /* 
  17.          * Use MIGRATE_RESERVE rather than fail an allocation. goto 
  18.          * is used because __rmqueue_smallest is an inline function 
  19.          * and we want just one call site 
  20.          */  
  21.         if (!page) {  
  22.             migratetype = MIGRATE_RESERVE;  
  23.             goto retry_reserve;  
  24.         }  
  25.     }  
  26.   
  27.     trace_mm_page_alloc_zone_locked(page, order, migratetype);  
  28.     return page;  
  29. }  

2. 内存释放函数

相关宏定义如下:

  1. #define __free_page(page) __free_pages((page), 0)   
  2. #define free_page(addr) free_pages((addr),0)  
linux
相关资讯       Linux Kernel 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款