Linux-2.6驱动开发 4 内存分配

标准驱动开发(Linux2.6(cdev) 的开发) - 函数的原型:struct device *device_create(struct class *class, struct device *parent, dev_t devt, void *drvdata, const char *fmt, ...);-- 函数的原型:int cdev_add(struct cdev *cdev, dev_t dev, unsigned count);-- 函数返回值:返回的就是你创建的类结构体, 这个类结构体用于生成设备文件的函数的传参!-- 什么是特殊文件? 阅读详情

4.1 Memory

#include <linux/slab.h>

void *kmalloc(size_t size, int flags);

flags的定义在<linux/gfp.h>,一般使用的选项如下:

GFP_ATOMIC

Used to allocate memory from interrupt handlers and other code outside of a

process context. Never sleeps.

GFP_KERNEL

Normal allocation of kernel memory. May sleep.

GFP_USER

Used to allocate memory for user-space pages; it may sleep.

GFP_HIGHUSER

Like GFP_USER, but allocates from high memory, if any. High memory is

described in the next subsection.

GFP_NOIO

GFP_NOFS

These flags function like GFP_KERNEL, but they add restrictions on what the ker-

nel can do to satisfy the request. A GFP_NOFS allocation is not allowed to perform

any filesystem calls, while GFP_NOIO disallows the initiation of any I/O at all.

They are used primarily in the filesystem and virtual memory code where an allo-

cation may be allowed to sleep, but recursive filesystem calls would be a bad

idea.

以上选项可以与下面的选项用或(|)同时使用:

__GFP_DMA

This flag requests allocation to happen in the DMA-capable memory zone. The

exact meaning is platform-dependent and is explained in the following section.

__GFP_HIGHMEM

This flag indicates that the allocated memory may be located in high memory.

__GFP_COLD

Normally, the memory allocator tries to return “cache warm” pages—pages that

are likely to be found in the processor cache. Instead, this flag requests a “cold”

page, which has not been used in some time. It is useful for allocating pages for

DMA reads, where presence in the processor cache is not useful. See the section

“Direct Memory Access” in Chapter 1 for a full discussion of how to allocate

DMA buffers.

__GFP_NOWARN

This rarely used flag prevents the kernel from issuing warnings (with printk)

when an allocation cannot be satisfied.

__GFP_HIGH

This flag marks a high-priority request, which is allowed to consume even the

last pages of memory set aside by the kernel for emergencies.

__GFP_REPEAT

__GFP_NOFAIL

__GFP_NORETRY

These flags modify how the allocator behaves when it has difficulty satisfying an

allocation. __GFP_REPEAT means “try a little harder” by repeating the attempt—

but the allocation can still fail. The __GFP_NOFAIL flag tells the allocator never to

fail; it works as hard as needed to satisfy the request. Use of __GFP_NOFAIL is very

strongly discouraged; there will probably never be a valid reason to use it in a

device driver. Finally, __GFP_NORETRY tells the allocator to give up immediately if

the requested memory is not available.

 

void kfree(void *obj);

4.2 Cache

#include <linux/slab.h>

kmem_cache_t *kmem_cache_create(const char *name, size_t size,

                                size_t offset,

                                unsigned long flags,

                                void (*constructor)(void *, kmem_cache_t *,

                                                    unsigned long flags),

                                void (*destructor)(void *, kmem_cache_t *,

                                                   unsigned long flags));

flags 的选项如下:

SLAB_NO_REAP

Setting this flag protects the cache from being reduced when the system is loo

ing for memory. Setting this flag is normally a bad idea; it is important to avo

restricting the memory allocator’s freedom of action unnecessarily.

SLAB_HWCACHE_ALIGN

This flag requires each data object to be aligned to a cache line; actual alignme

depends on the cache layout of the host platform. This option can be a goo

choice if your cache contains items that are frequently accessed on SM

machines. The padding required to achieve cache line alignment can end u

wasting significant amounts of memory, however.

SLAB_CACHE_DMA

This flag requires each data object to be allocated in the DMA memory zone.

 

void *kmem_cache_alloc(kmem_cache_t *cache, int flags);

void kmem_cache_free(kmem_cache_t *cache, const void *obj);

 

int kmem_cache_destroy(kmem_cache_t *cache);

 

应用:

/* declare one cache pointer: use it for all devices */

kmem_cache_t *scullc_cache;

/* scullc_init: create a cache for our quanta */

scullc_cache = kmem_cache_create("scullc", scullc_quantum,

        0, SLAB_HWCACHE_ALIGN, NULL, NULL); /* no ctor/dtor */

if (!scullc_cache) {

    scullc_cleanup( );

    return -ENOMEM;

}

/* Allocate a quantum using the memory cache */

if (!dptr->data[s_pos]) {

    dptr->data[s_pos] = kmem_cache_alloc(scullc_cache, GFP_KERNEL);

    if (!dptr->data[s_pos])

        goto nomem;

    memset(dptr->data[s_pos], 0, scullc_quantum);

}

for (i = 0; i < qset; i++)

if (dptr->data[i])

        kmem_cache_free(scullc_cache, dptr->data[i]);

/* scullc_cleanup: release the cache of our quanta */

if (scullc_cache)

kmem_cache_destroy(scullc_cache);

4.3 Memory Pools

#include <linux/mempool.h>

mempool_t *mempool_create(int min_nr,

                          mempool_alloc_t *alloc_fn,

                          mempool_free_t *free_fn,

                          void *pool_data);

typedef void *(mempool_alloc_t)(int gfp_mask, void *pool_data);

typedef void (mempool_free_t)(void *element, void *pool_data);

int mempool_resize(mempool_t *pool, int new_min_nr, int gfp_mask);

void mempool_destroy(mempool_t *pool);

应用:

cache = kmem_cache_create(. . .);

pool = mempool_create(MY_POOL_MINIMUM,

                      mempool_alloc_slab, mempool_free_slab,

                      cache);

void *mempool_alloc(mempool_t *pool, int gfp_mask);

void mempool_free(void *element, mempool_t *pool);

4.4 get_free_page

如果分配的内存很大,可以使用Page的技术。

get_zeroed_page(unsigned int flags);

Returns a pointer to a new page and fills the page with zeros.

__get_free_page(unsigned int flags);

Similar to get_zeroed_page, but doesn’t clear the page.

__get_free_pages(unsigned int flags, unsigned int order);

Allocates and returns a pointer to the first byte of a memory area that is poten-

tially several (physically contiguous) pages long but doesn’t zero the area.

 

void free_page(unsigned long addr);

void free_pages(unsigned long addr, unsigned long order);

应用:

/* Here's the allocation of a single quantum */

if (!dptr->data[s_pos]) {

    dptr->data[s_pos] =

        (void *)__get_free_pages(GFP_KERNEL, dptr->order);

    if (!dptr->data[s_pos])

        goto nomem;

    memset(dptr->data[s_pos], 0, PAGE_SIZE << dptr->order);

}

/* This code frees a whole quantum-set */

for (i = 0; i < qset; i++)

    if (dptr->data[i])

        free_pages((unsigned long)(dptr->data[i]),

                dptr->order);

4.5 alloc_pages

struct page *alloc_pages_node(int nid, unsigned int flags,

                              unsigned int order);

struct page *alloc_pages(unsigned int flags, unsigned int order);

struct page *alloc_page(unsigned int flags);

void __free_page(struct page *page);

void __free_pages(struct page *page, unsigned int order);

void free_hot_page(struct page *page);

void free_cold_page(struct page *page);

4.6 vmalloc

分配虚拟地址空间

#include <linux/vmalloc.h>

void *vmalloc(unsigned long size);

void vfree(void * addr);

void *ioremap(unsigned long offset, unsigned long size);

void iounmap(void * addr);

应用:

/* Allocate a quantum using virtual addresses */

if (!dptr->data[s_pos]) {

    dptr->data[s_pos] =

        (void *)vmalloc(PAGE_SIZE << dptr->order);

    if (!dptr->data[s_pos])

        goto nomem;

    memset(dptr->data[s_pos], 0, PAGE_SIZE << dptr->order);

}

/* Release the quantum-set */

for (i = 0; i < qset; i++)

    if (dptr->data[i])

        vfree(dptr->data[i]);

4.7 分配大量连续的空间

#include <linux/bootmem.h>

void *alloc_bootmem(unsigned long size);

void *alloc_bootmem_low(unsigned long size);

void *alloc_bootmem_pages(unsigned long size);

void *alloc_bootmem_low_pages(unsigned long size);

linux驱动开发—基于Device tree机制的驱动编写 前言 Device Tree是一种用来描述硬件的数据结构,类似板级描述语言,起源于OpenFirmware(OF)。在目前广泛使用的Linux kernel 2.6.x版本中,对于不同平台、不同硬件,往往存在着大量的不同的、移植性差的板级描述代码,以达到对这些不同平台和不同硬件特殊适配的需求。但是过多的平台、过的的不同硬件导致了这样的代码越来越多,最终引发了Linux创始人Linus的不满, 阅读详情

相关推荐

Linux-2.6.30.4在2440上的移植之MMC/SD卡驱动

嵌入式Linux之我行,主要讲述和总结了本人在学习嵌入式linux中的每个步骤。一为总结经验,二希望能给想入门嵌入式Linux的朋友提供方便。如有错误之处,谢请指正。 一、移植环境 · 主  机:VMWare--Fedora 9  · 开发板:Mini2440--64MB Nand  · 编译器:arm-linux-gcc-4.3.2 二、移植步骤 1. 添加SD卡设备到系统设备初始化

fgq1990的专栏 1161

JavaScript 内存管理及 V8 优化

JavaScript 内存管理 JavaScript 具有垃圾自动回收机制(Garbage Collection)简称 GC。垃圾回收机制会中断整个代码执行,释放不可能再被使用的变量,释放内存,这个工作机制是周期性的,我们会在下文详细探讨。 可释放对象 function fn1() { var obj1 = { name: 'xiao...

weixin_33841503的博客 244

AI人工智能与模拟退火:碰撞出的智慧火花

本文旨在为读者全面介绍模拟退火算法在人工智能领域的应用。我们将涵盖从基础理论到实际实现的完整知识体系,帮助读者理解这一受自然界启发的优化技术如何解决复杂的计算问题。文章首先介绍模拟退火的基本概念,然后深入其工作原理和数学基础,接着通过Python实现展示具体应用,最后讨论实际应用场景和未来发展方向。模拟退火(Simulated Annealing, SA):一种受物理退火过程启发的概率性优化算法能量函数(Energy Function):评估系统状态的函数,对应优化问题的目标函数。

AGI×大数据,开启智能时代的认知跃迁;解码AGI,赋能数据驱动的智能革命。 454

CPU高速缓存行对齐(cache line)

 转载:http://blog.csdn.net/snowwalf/article/details/6784014 CPU的高速缓存一般分为一级缓存和二级缓存,现今更多的CPU更是提供了三级缓存。CPU在运行时首先从一级缓存读取数据,如果读取失败则会从二级缓存读取数据,如果仍然失败则再从内存中存读取数据。而CPU从一级缓存或二级缓存或主内存中最终读取到数据所耗费的时钟周期差距是非常

陆业聪 1万+

SAP MM 物料描述MAKTX 40个字符不够用的问题处理

SAP物料描述表,长度40个字符一般情况下,40个字符在描述中也够用了,但是要加上规格之类的额外描述就不一定了,特别是对于辅助件-刀具。解决方案一:对maktx增强,换成CHAR80或者CHAR100的,但是风险较大,涉及到好多地方,另外SAP+ 算号器 也不支持。解决方案二:增强字段。内部顾问,建议在 那就在这里加一个字段,具体过程如下:1、创建域2、创建架构3、附加架构4、GUI 设置 5、后台设置6、MASS/MM17字段新增。过程1、创建域 SE112、创建结构 se11 3、MARA附加架构

GAVIN 4733

2021哈工大网安837考研初试经验贴

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 欢迎使用Markdown编辑器 你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Mar

weixin_41797880的博客 6524

HIVE 内存溢出常见问题

MapReduce作业运行过程中内存溢出错误分类 1、Mapper/Reducer阶段JVM内存溢出(一般都是堆) 1)JVM堆(Heap)内存溢出:堆内存不足时,一般会抛出如下异常: 第一种:“java.lang.OutOfMemoryError:” GC overhead limit exceeded; 第二种:“Error: Java heapspace”异常信息; 第三种:“ru...

yisun123456的博客 3万+

Android11车机系统海外项目资源包RRO方案优化

本文介绍了汽车设置应用如何通过Android RRO(运行时资源叠加)方案实现多车型差异化资源管理。针对不同车型(国内/海外、轿车/SUV)和配置(方向盘、轮毂等),采用产品变体(productFlavors)构建不同APK版本,并通过JSON配置文件动态启用对应资源包。系统根据车辆下线配置自动匹配适用的资源叠加包,解决了硬编码判断带来的维护问题。该方案实现了资源的高效复用,同时支持灵活配置多种车型组合,为汽车行业多车型共平台开发提供了可扩展的技术实现。

shusuanly的博客 471

linux驱动静态分配内存,Linux-2.6驱动开发 4 内存分配

4.1 Memory#include void *kmalloc(size_t size, int flags);flags的定义在,一般使用的选项如下:GFP_ATOMICUsed to allocate memory from interrupt handlers and other code outside of aprocess context. Never sleeps.GFP_KERN...

weixin_39875629的博客 205

AMBA-CHI协议详解(二)

读事务,写事务,原子事务,Stash事务,Dataless事务,DVM事务,Retry事务。

Crazzy_M的博客 6660

Deep SHAP 深度学习模型的可解释性分析

Deep SHAP 算法是一种用于解释深度学习模型的技术, 该算法通过将每个特征的重要性与输出值相关联来进行解释。本博客对深度学习的模型可解释性进行了分析。

cnjs1994的博客 5121

驱动字符设备开发之Linux2.6开发

字符设备驱动开发三大方法之一Linux2.6开发

Charary的博客 1030

Cesium圆形波纹/雷达扩散效果扩展类CircleWaveMaterialProperty

本文是基于原创内容进行部分修改,原文出处:https://blog.csdn.net/chudage11/article/details/101632482 SuperMapCesium支持该类似扩展类,以下是原生Cesium调用扩展类CircleWaveMaterialProperty说明 1.扩展类CircleWaveMaterialProperty.js import*asCesiumfrom'cesium' varCircleWaveMaterial={ Circl...

一碗老面的博客 3001

Linux2.6内核驱动与2.4的区别

随着Linux2.6的发布,由于2.6内核做了新的改动,各个设备的驱动程序在不同程度上要进行改写。为了方便各位Linux爱好者我把自己整理的这分文档share出来。该文当列举了2.6内核同以前版本的绝大多数变化,可惜的是由于时间和精力有限没有详细列出各个函数的用法。 1、 使用新的入口 必须包含 module_init(your_init_func); module_exit

shenbin1430的专栏 2974

96.在 Vue 3 中使用 OpenLayers 探究加载 Point、Polygon 的极限

在前端 GIS(地理信息系统)开发中,OpenLayers 作为一个功能强大的 Web 地图库,广泛用于加载地图、渲染矢量数据(点、线、面)、叠加图层等场景。然而,当涉及到。

小彭的博客 1159

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Linux内核与驱动层的区别,Linux2.6与2.4内核驱动程序的区别

为了彻底防止对正在被使用的内核模块进行错误操作,Linux2.6内核在加载和导出内核模块方面都较2.4内核有所改进,避免了用户执行将导致系统崩溃的操作(例如强制删除模块等)。同时,当驱动程序需要在多个文件中包含头文件时,不必定义宏来检查内核的版本。与2.4内核相比,2.6内核在可扩展性、吞吐率等方面有较大提升,其新特性主要包括:使用了新的调度器算法;内核抢占功能显著地降低了用户交互式应用程序;多媒...

weixin_29774679的博客 393

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

移植之前所有驱动(基于linux-2.6.22.6)到linux-4.12内核

    之前编写的驱动都是基于linux-2.6.22.6内核的,下面将所有驱动移植到linux-4.12内核上,linux-4.12使用的是移植linux-4.12到JZ2440里移植的内核,使用2.4 制作补丁的方法获得该移植好的linux-4.12内核。 目录 1. 移植网卡驱动     1.1 准备文件    1.2 解决错误        1.2.1 解决错误1          ...

墨、白的博客 3976

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

从 2.4 到 2.6:Linux 内核可装载模块机制的改变对设备驱动的影响

转贴: 原地址:http://www.ibm.com/developerworks/cn/linux/l-module26/从 2.4 到 2.6,Linux 内核在可装载模块机制、设备模型、一些核心 API 等方面发生较大改变,设备驱动开发人员面临着将驱动从 2.4 移植到 2.6 内核,或是使驱动同时支持2.4 与 2.6 内核的任务。站在设备驱动开发人员的角度,驱动由一个或几个外

pottichu的专栏 7537

linux2.6 添加驱动,Linux2.6 驱动设计――从 2.4 到 2.6

Linux2.6 驱动设计――从 2.4 到 2.6作者 RayRTEMS版权所有,转载请注明来源ray@rtems" target="_blank">www.rtems.net,作者ray@rtemsLinux 2.6 和 2.4 的比较我不想废话,总体来说 2.6 功能更强,但是资源消耗更多。由于 2.6 内核在驱动框架,底层调用上和 2.4 内核有很多差别,所以本文主要是为程序员提供 ...

weixin_32115669的博客 156

linux2.4和2.6在注册设备驱动函数…

2.4内核注册驱动要用: int register_chrdev (unsigned int major, const char *name, struct file_operations *fops);2.4内核注销驱动要用: int unregister_chrdev( unsigned int major, const char *name );2.4内核驱动注册完后,要用以下代码创建设备文

Snail239的专栏 858

Linux2.6设备驱动开发

linux2.6驱动开发方式

jcf5832的博客 865

Linux2.6驱动开发

目前Linux2.6并不能直接生成设备文件,需要手动生成,接下来就是如何解决这一问题。

有人住高楼,有人在深沟;有人光万丈,有人一身锈。 782

Linux 驱动开发之WIFI设备分析4

Linux 驱动开发之WIFI设备分析4(基于Linux6.6)---USB接口WiFi介绍

ListQ的博客 1871

Linux 2.6以上版本驱动程序如何开发

    Linux内核版本若高于2.4,其驱动程序在高版本上移植开发,会有很多不同。    在2.4版本里面,可以简单的利用GCC生成。O文件,然后用insmod加载到内核中,但是在2.6版本上会不可以,会报格式不正确的错误,如何解决这个问题,由于版本差异,在2.6版本上运行驱动程序是以.ko格式存在的   下面以那个经典的例子:     先在/home里创建一个新目录hell

my2005lb的专栏 667

Linux 2.6 驱动设计快速入门!

作者 Ray来源www.rtems.net,作者ray@rtemsLinux 2.6 和 2.4 的比较我不想废话,总体来说 2.6 功能更强,但是资源消耗更多。 由于 2.6 内核在驱动框架,底层调用上和 2.4 内核有很多差别,所以本文主要是为程序员提供 2.4 到 2.6 迁移的指导。 2.6 和 2.4 主要的不同在于 •  内核的 API 变化,增加了不少新功能(例如 mem pool

Ray的嵌入式专栏 2240
上一篇: Linux-2.6驱动开发 3 I/O
下一篇: Linux-2.6驱动开发 5 时间,延时
skdev
博客等级 码龄20年 139粉丝 84原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值