注册 登录
编程论坛 Linux系统管理

怎样获得Pages 笔记

madfrogme 发布于 2012-07-31 17:58, 1477 次点击
内核通过pages,zones之类来管理内存

内核提供了一个底层的机制来获得内存,

很多函数都被定已在<linux/gfp.h>中

比如300行到304行
程序代码:
300static inline struct page *
 301alloc_pages(gfp_t gfp_mask, unsigned int order)
 302{
 303        return alloc_pages_current(gfp_mask, order);
 304}

这个函数分配2^order个连续physical pages,返回值当然是指向第一个page的指针

如果想把page转换成它的虚拟地址的话用函数用下面的函数,定义在<mm/highmem.c>中

程序代码:
325/**
 326 * page_address - get the mapped virtual address of a page
 327 * @page: &struct page to get the virtual address of
 328 *
 329 * Returns the page's virtual address.
 330 */
 331void *page_address(struct page *page)
 332{
 333        unsigned long flags;
 334        void *ret;
 335        struct page_address_slot *pas;
 336
 337        if (!PageHighMem(page))
 338                return lowmem_page_address(page);
 339
 340        pas = page_slot(page);
 341        ret = NULL;
 342        spin_lock_irqsave(&pas->lock, flags);
 343        if (!list_empty(&pas->lh)) {
 344                struct page_address_map *pam;
 345
 346                list_for_each_entry(pam, &pas->lh, list) {
 347                        if (pam->page == page) {
 348                                ret = pam->virtual;
 349                                goto done;
 350                        }
 351                }
 352        }
 353done:
 354        spin_unlock_irqrestore(&pas->lock, flags);
 355        return ret;
 356}


它返回一个指向logical address的指针

如果你不需要一个实际的 struct page,

(if you have no need for the actual struct page,这句话到底是怎么一回事,不明白)


则可以用下面的函数__get_free_pages()

在mm/page_alloc.c文件中
程序代码:
1969/*
1970 * Common helper functions.
1971 */
1972unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
1973{
1974        struct page *page;
1975
1976        /*
1977         * __get_free_pages() returns a 32-bit address, which cannot represent
1978         * a highmem page
1979         */
1980        VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
1981
1982        page = alloc_pages(gfp_mask, order);
1983        if (!page)
1984                return 0;
1985        return (unsigned long) page_address(page);
1986}

如果只需要一个页的话
则是struct page * alloc_page(gfp_t gfp_mask)

unsigned long __get_free_page(gfp_t gfp_mask)


[ 本帖最后由 madfrogme 于 2012-8-1 02:45 编辑 ]
0 回复
1