可以使用calloc或malloc在OSX中仅分配物理内存吗? [英] can calloc or malloc be used to allocate ONLY physical memory in OSX?

查看:114
本文介绍了可以使用calloc或malloc在OSX中仅分配物理内存吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c函数malloc和calloc,我有一些疑问.

我想看看是否可以使用这两个函数仅分配物理内存,我的mac机具有4gb或ram,并且当我使用malloc时,我可以分配的内存大于4gb,这意味着malloc可以分配物理内存和虚拟内存. /p>

我有几个问题:

  1. 有什么我可以使用的功能,所以我只能分配物理内存(不分配虚拟内存)

  2. 在调用malloc和calloc以及指针返回时,有什么方法可以使用指针来确定分配了多少物理内存以及分配了多少虚拟内存?

一个简单的例子将不胜感激:)

感谢您的帮助:)

解决方案

是的,您可以使用mlock()将内存区域仅保留在RAM中,以免页面被分页.通常用于加密,以避免密钥进入磁盘,从而有被攻击者随后检索到的危险.

当然,您可以锁定在RAM中的内存数量是有限的. 它仍将是虚拟内存,但不会进入磁盘,我想这就是您想要的.

malloc/calloc是围绕OS进行内存分配的真实调用的libc包装器(而且我不知道OSX上可能发生了什么),并且它们保留内部缓冲区以避免过于频繁的系统调用.您应该查看这些调用的libc实现,并查看它们在何处保存数据以及采用哪种格式,以便您可以访问它们.

I am playing around with the c functions malloc and calloc and I have some questions.

I want to see if I can use these 2 functions to allocate only physical memory, my mac has 4gb or ram and when I use malloc I can allocate way more than 4gb, which means malloc allocate both physical and virtual memory.

I have a couple of questions:

  1. is there any function I can use, so that I can only allocate the physical memory (w/o allocating the virtual mem)

  2. when calling malloc and calloc and when the pointers return, is there any way I can use the pointers to determine how much physical memory are allocated and how much virtual memory are allocate?

a quick example will be really appreciated :)

thanks for your help :)

解决方案

Yes you can use mlock() to keep an area of memory only in RAM avoiding it to be paged out. It is usually used for encryption to avoid keys to go on the disk with the danger of being retrieved by attackers afterwards.

Of course the amount of memory you can lock in RAM is limited. It will still be virtual memory but will not go to the disk, which i guess is what you want.

malloc/calloc are libc wrappers around the OS real call for allocating memory (and i have no clue of what that might be on OSX), and they keep internal buffers to avoid too frequent system calls. You should see your libc implementation of these calls and see where they keep the data and in which format, so you can access it.

这篇关于可以使用calloc或malloc在OSX中仅分配物理内存吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆