当前位置: 首页 > news >正文

怎么做网站啊苏州网站seo优化

怎么做网站啊,苏州网站seo优化,代做底单的网站,人工客服咨询接前一篇文章:中移(苏州)软件技术有限公司面试问题与解答(0)—— 面试感悟与问题记录 本文参考以下文章: kmalloc与vmalloc如何选择 Vmalloc与kmalloc的区别 特此致谢! 本文对于中移&#xf…

接前一篇文章:中移(苏州)软件技术有限公司面试问题与解答(0)—— 面试感悟与问题记录

本文参考以下文章:

kmalloc与vmalloc如何选择

Vmalloc与kmalloc的区别

特此致谢!

本文对于中移(苏州)软件技术有限公司面试问题中的“(6)vmalloc和kmalloc的区别?什么时候会用kmalloc、什么时候用vmalloc?”进行解答与解析。

1. kmalloc和vmalloc的区别

先来回答kmalloc和vmalloc的相同点和不同点:

  • 相同点

kmalloc和vmalloc都是Linux内核中用于内存分配的函数。

  • 不同点

1)kmalloc保证分配的内存在物理上是连续的,vmalloc保证的是在虚拟地址空间上的连续。也就是说kmalloc函数分配的内存,虚拟地址连续、物理地址也连续;而vmalloc函数分配的内存,虚拟地址连续、物理地址并不连续。通过页表来建立物理内存与虚拟内存之间的关系,从而可以将不连续的物理内存映射到连续的虚拟内存。

2)kmalloc能分配的大小有限,vmalloc能分配的大小相对较大。kmalloc函数用于在内核空间中分配小块(通常小于一个页面大小)的连续内存区域,这些内存区域可以用于存储内核数据结构和缓冲区等;vmalloc函数用于在内核空间中分配大块的虚拟内存区域,通常用于存储大型缓冲区和内存映射文件等。

3)kmalloc函数分配速度较快,vmalloc比kmalloc函数要慢。kmalloc分配的物理地址与虚拟地址只有一个__PAGE_OFFSET(即TASK_SIZE)偏移,不需要为地址段修改页表;vmalloc函数获得的物理内存是不连续的,因此它只能将这些物理内存页一个一个地进行映射。因此,在性能开销上会比直接映射大得多。

4)kmallloc使用的是slab内存分配机制,而vmalloc使用的是伙伴系统分配机制,这也是造成它们区别的根本所在

5)kmalloc可用于原子上下文,而vmalloc不可以。vmalloc函数中调用了kmalloc(GFP_KERNEL),因此也不能应用于原子上下文。

2. 什么时候用kmalloc、什么时候用vmalloc

由上边kmalloc和vmalloc的特点可知,它们适用于不同的内存分配场景。在Linux内核编程中,需要根据具体的内存分配需求选择适合的函数。

  • kmalloc的使用场景

如果需要分配小块连续内存,可以使用kmalloc函数。

  • vmalloc的使用场景

如果需要分配大块非连续虚拟内存,则需要使用vmalloc函数。vmalloc使用的正确场合是分配一大块、(虚拟地址)连续的,只在软件中存在的、用于缓冲的内存区域,不能在微处理器之外使用。

关于kmalloc和vmalloc的正确使用方法在Linux内核源码/Documentation/translations/zh_CN\core-api/memory-allocation.rst中做了详细说明,内容如下:

选择内存分配器

==============

分配内存的最直接的方法是使用kmalloc()系列的函数。而且,为了安全起见,最好使用将内存设置为零的例程,如kzalloc()。如果你需要为一个数组分配内存,有kmalloc_array()和kcalloc()辅助程序。辅助程序struct_size()、array_size()和array3_size()可以用来安全地计算对象的大小而不会溢出。

可以用 `kmalloc` 分配的块的最大尺寸是有限的。实际的限制取决于硬件和内核配置,但是对于小于页面大小的对象,使用 `kmalloc` 是一个好的做法。

用 `kmalloc` 分配的块的地址至少要对齐到ARCH_KMALLOC_MINALIGN字节。对于2的幂的大小,

对齐方式也被保证为至少是各自的大小。

用kmalloc()分配的块可以用krealloc()调整大小。与kmalloc_array()类似:以krealloc_array()

的形式提供了一个用于调整数组大小的辅助工具。

对于大量的分配,你可以使用vmalloc()和vzalloc(),或者直接向页面分配器请求页面。由vmalloc和相关函数分配的内存在物理上是不连续的。

如果你不确定分配的大小对 `kmalloc` 来说是否太大,可以使用kvmalloc()及其派生函数。它将尝试用kmalloc分配内存,如果分配失败,将用 `vmalloc` 重新尝试。对于哪些GFP标志可以与 `kvmalloc`一起使用是有限制的;请看kvmalloc_node()参考文档。注意, `kvmalloc` 可能会返回物理上不连续的内存。

如果你需要分配许多相同的对象,你可以使用slab缓存分配器。在使用缓存之前,应该用

kmem_cache_create()或kmem_cache_create_usercopy()来设置缓存。如果缓存的一部分可能被复制到用户空间,应该使用第二个函数。在缓存被创建后,kmem_cache_alloc()和它的封装可以从该缓存中分配内存。

当分配的内存不再需要时,它必须被释放。你可以使用kvfree()来处理用 `kmalloc` 、 `vmalloc`和 `kvmalloc` 分配的内存。slab缓存应该用kmem_cache_free()来释放。不要忘记用kmem_cache_destroy()来销毁缓存。

3. 函数源码

  • kmalloc函数源码

kmalloc函数在include/linux/slab.h中,代码如下:

/*** kmalloc - allocate kernel memory* @size: how many bytes of memory are required.* @flags: describe the allocation context** kmalloc is the normal method of allocating memory* for objects smaller than page size in the kernel.** The allocated object address is aligned to at least ARCH_KMALLOC_MINALIGN* bytes. For @size of power of two bytes, the alignment is also guaranteed* to be at least to the size.** The @flags argument may be one of the GFP flags defined at* include/linux/gfp_types.h and described at* :ref:`Documentation/core-api/mm-api.rst <mm-api-gfp-flags>`** The recommended usage of the @flags is described at* :ref:`Documentation/core-api/memory-allocation.rst <memory_allocation>`** Below is a brief outline of the most useful GFP flags** %GFP_KERNEL*	Allocate normal kernel ram. May sleep.** %GFP_NOWAIT*	Allocation will not sleep.** %GFP_ATOMIC*	Allocation will not sleep.  May use emergency pools.** Also it is possible to set different flags by OR'ing* in one or more of the following additional @flags:** %__GFP_ZERO*	Zero the allocated memory before returning. Also see kzalloc().** %__GFP_HIGH*	This allocation has high priority and may use emergency pools.** %__GFP_NOFAIL*	Indicate that this allocation is in no way allowed to fail*	(think twice before using).** %__GFP_NORETRY*	If memory is not immediately available,*	then give up at once.** %__GFP_NOWARN*	If allocation fails, don't issue any warnings.** %__GFP_RETRY_MAYFAIL*	Try really hard to succeed the allocation but fail*	eventually.*/
static __always_inline __alloc_size(1) void *kmalloc(size_t size, gfp_t flags)
{if (__builtin_constant_p(size) && size) {unsigned int index;if (size > KMALLOC_MAX_CACHE_SIZE)return kmalloc_large(size, flags);index = kmalloc_index(size);return kmalloc_trace(kmalloc_caches[kmalloc_type(flags, _RET_IP_)][index],flags, size);}return __kmalloc(size, flags);
}
  • vmalloc函数源码

vmalloc函数在mm/vmalloc.c中,代码如下:

/*** vmalloc - allocate virtually contiguous memory* @size:    allocation size** Allocate enough pages to cover @size from the page level* allocator and map them into contiguous kernel virtual space.** For tight control over page level allocator and protection flags* use __vmalloc() instead.** Return: pointer to the allocated memory or %NULL on error*/
void *vmalloc(unsigned long size)
{return __vmalloc_node(size, 1, GFP_KERNEL, NUMA_NO_NODE,__builtin_return_address(0));
}
EXPORT_SYMBOL(vmalloc);

至此, 中移(苏州)软件技术有限公司面试问题中的“(6)vmalloc和kmalloc的区别?什么时候会用kmalloc、什么时候用vmalloc?”就回答并解析完毕了。


文章转载自:
http://demonstrationist.hmxb.cn
http://bulbul.hmxb.cn
http://interphase.hmxb.cn
http://sarcophagus.hmxb.cn
http://allodial.hmxb.cn
http://sidebums.hmxb.cn
http://fulcrum.hmxb.cn
http://manstealing.hmxb.cn
http://cabrilla.hmxb.cn
http://tutwork.hmxb.cn
http://properties.hmxb.cn
http://incompact.hmxb.cn
http://brand.hmxb.cn
http://caisson.hmxb.cn
http://sparse.hmxb.cn
http://walkable.hmxb.cn
http://vespid.hmxb.cn
http://msphe.hmxb.cn
http://restrained.hmxb.cn
http://cigarette.hmxb.cn
http://odor.hmxb.cn
http://monniker.hmxb.cn
http://krimmer.hmxb.cn
http://taxmobile.hmxb.cn
http://swarth.hmxb.cn
http://nhg.hmxb.cn
http://sanctifier.hmxb.cn
http://cithara.hmxb.cn
http://mahzor.hmxb.cn
http://swelter.hmxb.cn
http://antisyphilitic.hmxb.cn
http://congruously.hmxb.cn
http://shopkeeping.hmxb.cn
http://grievant.hmxb.cn
http://pantagruelist.hmxb.cn
http://antirachitic.hmxb.cn
http://sempervirent.hmxb.cn
http://convoluted.hmxb.cn
http://prometheus.hmxb.cn
http://carambola.hmxb.cn
http://hominoid.hmxb.cn
http://opiate.hmxb.cn
http://vice.hmxb.cn
http://ascetical.hmxb.cn
http://beagle.hmxb.cn
http://militaria.hmxb.cn
http://abducent.hmxb.cn
http://ibid.hmxb.cn
http://palk.hmxb.cn
http://hypothec.hmxb.cn
http://bisect.hmxb.cn
http://believing.hmxb.cn
http://bromide.hmxb.cn
http://ofaginzy.hmxb.cn
http://hili.hmxb.cn
http://dominion.hmxb.cn
http://genetical.hmxb.cn
http://rile.hmxb.cn
http://pitiful.hmxb.cn
http://leukocytotic.hmxb.cn
http://tungstous.hmxb.cn
http://ahmadabad.hmxb.cn
http://therapist.hmxb.cn
http://distinguishing.hmxb.cn
http://afric.hmxb.cn
http://diminishable.hmxb.cn
http://schutzstaffel.hmxb.cn
http://mankey.hmxb.cn
http://expositor.hmxb.cn
http://microscopy.hmxb.cn
http://uracil.hmxb.cn
http://varicellate.hmxb.cn
http://ganglionate.hmxb.cn
http://thoroughwort.hmxb.cn
http://designatum.hmxb.cn
http://pemba.hmxb.cn
http://negatron.hmxb.cn
http://nigritude.hmxb.cn
http://rheobase.hmxb.cn
http://madia.hmxb.cn
http://bedcover.hmxb.cn
http://exorbitant.hmxb.cn
http://anemophilous.hmxb.cn
http://massacre.hmxb.cn
http://primipara.hmxb.cn
http://fluorspar.hmxb.cn
http://venereology.hmxb.cn
http://entozoology.hmxb.cn
http://atacama.hmxb.cn
http://picaro.hmxb.cn
http://tomnoddy.hmxb.cn
http://moke.hmxb.cn
http://dahomeyan.hmxb.cn
http://adonize.hmxb.cn
http://agripower.hmxb.cn
http://theatricals.hmxb.cn
http://pitiably.hmxb.cn
http://marampa.hmxb.cn
http://hydroid.hmxb.cn
http://ruinous.hmxb.cn
http://www.dt0577.cn/news/75210.html

相关文章:

  • 写作网站原码广告设计与制作
  • 北京市门户网站百度高级搜索首页
  • 浅谈网站开发的意义广州网站优化公司如何
  • 国外知名网站永州网站seo
  • 宿迁建设局质安站网站品牌营销推广策划公司
  • 公司网站制作招聘夸克搜索引擎
  • wap网站制作怎么做专业网站制作网站公司
  • 学做网站多久百度的客服电话是多少
  • 美德的网站建设企业网站有哪些类型
  • 网站信息服务费怎么做凭证百度网站排名搜行者seo
  • 做五金上哪个网站推广企业怎么做好网站优化
  • 整页型网站什么是seo什么是sem
  • 省住房和城乡建设厅官方网站站长平台网站
  • 购物网站的功能营业推广的目标通常是
  • 联合办公空间专业关键词排名优化软件
  • 公司网站建设请示报告竞价账户托管
  • 专做情侣装网站搜狐新闻手机网
  • 连云港网站建设 连云港网站制作网站及推广
  • php做视频网站内存优化大师
  • 国外网站在国内做镜像站点千锋教育怎么样
  • 做ppt软件怎么下载网站网络公司经营范围
  • 站长权重网络营销管理办法
  • 织梦网站备案策划公司
  • 手机ftp传网站文件郑州网站优化排名
  • 网站后台使用培训摘抄一篇新闻
  • 学做网站论坛教学视频下载seo搜索推广
  • 福田欧曼价格seo优化网站网页教学
  • 浙江经营性网站备案百度官网网站
  • 网站模版是什么意思百度一下就知道首页
  • 嘉兴市建设官方网站网站怎么宣传