- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
由于一些与这个问题无关的模糊原因,我需要求助于使用 MAP_FIXED 以获得接近 libc 的文本部分在内存中的位置的页面。
在阅读 mmap(2)(我一开始就应该这样做)之前,如果我使用 MAP_FIXED 调用 mmap 并且基地址与已映射区域重叠,我预计会出现错误。
然而事实并非如此。例如,这是某些进程的/proc/maps 的一部分
7ffff7299000-7ffff744c000 r-xp 00000000 08:05 654098 /lib/x86_64-linux-gnu/libc-2.15.so
在进行以下 mmap 调用之后...
mmap(0x7ffff731b000,
getpagesize(),
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED,
0,
0);
... 变成:
7ffff7299000-7ffff731b000 r-xp 00000000 08:05 654098 /lib/x86_64-linux-gnu/libc-2.15.so
7ffff731b000-7ffff731c000 rwxp 00000000 00:00 0
7ffff731c000-7ffff744c000 r-xp 00083000 08:05 654098 /lib/x86_64-linux-gnu/libc-2.15.so
这意味着我已经用我自己的页面覆盖了一部分专用于 libc 的虚拟地址空间。显然不是我想要的...
在 mmap(2) 手册的 MAP_FIXED 部分,它明确指出:
If the memory region specified by addr and len overlaps pages of any existing mapping(s), then the overlapped part of the existing mapping(s) will be discarded.
这解释了我所看到的,但我有几个问题:
最佳答案
使用 page = sysconf(SC_PAGE_SIZE)
找出页面大小,然后使用 msync(addr, page, 0)
扫描您希望检查的每个页面大小的 block 。 (使用 (unsigned long)addr % page == 0
,即 addr
与页面对齐)。如果它返回 -1
和 errno == ENOMEM
,则该页面未映射。
编辑:正如 fons 在下面评论的那样,mincore(addr,page,&dummy)
优于 msync()
。 (系统调用的实现在 Linux 内核源代码的 mm/mincore.c
中,C 库通常提供更新 errno
的包装器。当系统调用执行映射时在确保 addr
是页面对齐后立即检查,它在未映射的情况下是最佳的 (ENOMEM
)。如果页面已经映射,它会做一些工作,所以如果性能是最重要的,请尽量避免检查您知道已映射的页面。
您必须针对每个页面单独执行此操作,因为对于大于单个页面的区域,ENOMEM
表示该区域未完全映射;它可能仍被部分映射。映射始终精细到页面大小的单位。
据我所知,如果该区域已映射或包含已映射的页面,则无法告诉 mmap()
失败。 (这同样适用于 mremap()
,因此您不能创建一个映射,然后将其移动到所需的区域。)
这意味着您面临竞争条件的风险。最好自己执行实际的系统调用,而不是 C 库包装器,以防它们在内部进行内存分配或更改内存映射:
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
static size_t page = 0;
static inline size_t page_size(void)
{
if (!page)
page = (size_t)sysconf(_SC_PAGESIZE);
return page;
}
static inline int raw_msync(void *addr, size_t length, int flags)
{
return syscall(SYS_msync, addr, length, flags);
}
static inline void *raw_mmap(void *addr, size_t length, int prot, int flags)
{
return (void *)syscall(SYS_mmap, addr, length, prot, flags, -1, (off_t)0);
}
但是,我怀疑无论您尝试做什么,您最终都需要解析 /proc/self/maps
。
我建议完全避免使用标准 I/O stdio.h
(因为各种操作会动态分配内存,从而改变映射),而是使用较低级别的 unistd.h
接口(interface),这不太可能影响映射。这是一组简单粗暴的函数,您可以使用它们来找出每个映射区域和该区域中启用的保护(并丢弃其他信息)。在实践中,它使用大约 1 KB 的代码,少于堆栈中的代码,因此即使在有限的架构(例如,嵌入式设备)上也非常有用。
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#ifndef INPUT_BUFFER
#define INPUT_BUFFER 512
#endif /* INPUT_BUFFER */
#ifndef INPUT_EOF
#define INPUT_EOF -256
#endif /* INPUT_EOF */
#define PERM_PRIVATE 16
#define PERM_SHARED 8
#define PERM_READ 4
#define PERM_WRITE 2
#define PERM_EXEC 1
typedef struct {
int descriptor;
int status;
unsigned char *next;
unsigned char *ends;
unsigned char buffer[INPUT_BUFFER + 16];
} input_buffer;
/* Refill input buffer. Returns the number of new bytes.
* Sets status to ENODATA at EOF.
*/
static size_t input_refill(input_buffer *const input)
{
ssize_t n;
if (input->status)
return (size_t)0;
if (input->next > input->buffer) {
if (input->ends > input->next) {
memmove(input->buffer, input->next,
(size_t)(input->ends - input->next));
input->ends = input->buffer + (size_t)(input->ends - input->next);
input->next = input->buffer;
} else {
input->ends = input->buffer;
input->next = input->buffer;
}
}
do {
n = read(input->descriptor, input->ends,
INPUT_BUFFER - (size_t)(input->ends - input->buffer));
} while (n == (ssize_t)-1 && errno == EINTR);
if (n > (ssize_t)0) {
input->ends += n;
return (size_t)n;
} else
if (n == (ssize_t)0) {
input->status = ENODATA;
return (size_t)0;
}
if (n == (ssize_t)-1)
input->status = errno;
else
input->status = EIO;
return (size_t)0;
}
/* Low-lever getchar() equivalent.
*/
static inline int input_next(input_buffer *const input)
{
if (input->next < input->ends)
return *(input->next++);
else
if (input_refill(input) > 0)
return *(input->next++);
else
return INPUT_EOF;
}
/* Low-level ungetc() equivalent.
*/
static inline int input_back(input_buffer *const input, const int c)
{
if (c < 0 || c > 255)
return INPUT_EOF;
else
if (input->next > input->buffer)
return *(--input->next) = c;
else
if (input->ends >= input->buffer + sizeof input->buffer)
return INPUT_EOF;
memmove(input->next + 1, input->next, (size_t)(input->ends - input->next));
input->ends++;
return *(input->next) = c;
}
/* Low-level fopen() equivalent.
*/
static int input_open(input_buffer *const input, const char *const filename)
{
if (!input)
return errno = EINVAL;
input->descriptor = -1;
input->status = 0;
input->next = input->buffer;
input->ends = input->buffer;
if (!filename || !*filename)
return errno = input->status = EINVAL;
do {
input->descriptor = open(filename, O_RDONLY | O_NOCTTY);
} while (input->descriptor == -1 && errno == EINTR);
if (input->descriptor == -1)
return input->status = errno;
return 0;
}
/* Low-level fclose() equivalent.
*/
static int input_close(input_buffer *const input)
{
int result;
if (!input)
return errno = EINVAL;
/* EOF is not an error; we use ENODATA for that. */
if (input->status == ENODATA)
input->status = 0;
if (input->descriptor != -1) {
do {
result = close(input->descriptor);
} while (result == -1 && errno == EINTR);
if (result == -1 && !input->status)
input->status = errno;
}
input->descriptor = -1;
input->next = input->buffer;
input->ends = input->buffer;
return errno = input->status;
}
/* Read /proc/self/maps, and fill in the arrays corresponding to the fields.
* The function will return the number of mappings, even if not all are saved.
*/
size_t read_maps(size_t const n,
void **const ptr, size_t *const len,
unsigned char *const mode)
{
input_buffer input;
size_t i = 0;
unsigned long curr_start, curr_end;
unsigned char curr_mode;
int c;
errno = 0;
if (input_open(&input, "/proc/self/maps"))
return (size_t)0; /* errno already set. */
c = input_next(&input);
while (c >= 0) {
/* Skip leading controls and whitespace */
while (c >= 0 && c <= 32)
c = input_next(&input);
/* EOF? */
if (c < 0)
break;
curr_start = 0UL;
curr_end = 0UL;
curr_mode = 0U;
/* Start of address range. */
while (1)
if (c >= '0' && c <= '9') {
curr_start = (16UL * curr_start) + c - '0';
c = input_next(&input);
} else
if (c >= 'A' && c <= 'F') {
curr_start = (16UL * curr_start) + c - 'A' + 10;
c = input_next(&input);
} else
if (c >= 'a' && c <= 'f') {
curr_start = (16UL * curr_start) + c - 'a' + 10;
c = input_next(&input);
} else
break;
if (c == '-')
c = input_next(&input);
else {
errno = EIO;
return (size_t)0;
}
/* End of address range. */
while (1)
if (c >= '0' && c <= '9') {
curr_end = (16UL * curr_end) + c - '0';
c = input_next(&input);
} else
if (c >= 'A' && c <= 'F') {
curr_end = (16UL * curr_end) + c - 'A' + 10;
c = input_next(&input);
} else
if (c >= 'a' && c <= 'f') {
curr_end = (16UL * curr_end) + c - 'a' + 10;
c = input_next(&input);
} else
break;
if (c == ' ')
c = input_next(&input);
else {
errno = EIO;
return (size_t)0;
}
/* Permissions. */
while (1)
if (c == 'r') {
curr_mode |= PERM_READ;
c = input_next(&input);
} else
if (c == 'w') {
curr_mode |= PERM_WRITE;
c = input_next(&input);
} else
if (c == 'x') {
curr_mode |= PERM_EXEC;
c = input_next(&input);
} else
if (c == 's') {
curr_mode |= PERM_SHARED;
c = input_next(&input);
} else
if (c == 'p') {
curr_mode |= PERM_PRIVATE;
c = input_next(&input);
} else
if (c == '-') {
c = input_next(&input);
} else
break;
if (c == ' ')
c = input_next(&input);
else {
errno = EIO;
return (size_t)0;
}
/* Skip the rest of the line. */
while (c >= 0 && c != '\n')
c = input_next(&input);
/* Add to arrays, if possible. */
if (i < n) {
if (ptr) ptr[i] = (void *)curr_start;
if (len) len[i] = (size_t)(curr_end - curr_start);
if (mode) mode[i] = curr_mode;
}
i++;
}
if (input_close(&input))
return (size_t)0; /* errno already set. */
errno = 0;
return i;
}
read_maps()
函数最多读取n
个区域,起始地址为void *
到ptr
数组,len
数组的长度,mode
数组的权限,返回 map 的总数(可能大于 n
),如果发生错误,则设置 errno
为零。
很可能对上面的低级 I/O 使用系统调用,这样您就不会使用任何 C 库功能,但我认为完全没有必要。 (据我所知,C 库在实际系统调用周围使用非常简单的包装器。)
我希望你觉得这很有用。
关于c - 使用 mmap 重叠页面 (MAP_FIXED),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14943990/
我对具有 2 个轴的数据有交叉识别问题,例如 A = array([['x0', 'y0', 'data0', 'data0'], ['x0', 'y0', 'data0', '
我知道这是代码有点傻,但有人可以解释为什么 isList [42]返回 True而isList2 [42]打印 False ,以及如何防止这种情况?我想更好地理解一些更晦涩的 GHC 类型扩展,我认为
我正在使用memmove(),但目标似乎正在覆盖源,或者也许我不明白覆盖是什么。我有一个 char 数组(目标),然后是一个指向目标的指针,该指针位于 vector 内部。 char destinat
以下AS3代码有时会导致音频多次播放,就像疯狂的回声一样,几乎同时播放。通常使用该URL都可以,但是当我使用https://soundcloud.com url时,它总是会发疯。在极少数情况下,我认为
我正在尝试在 android 2.2 中实现类似操作栏的东西。这是我的 main.xml
如何避免第一个值的重叠问题 而且,我怎样才能看到最后一个被剪裁的值? 最佳答案 我认为您在修改轴上的样式和调整视口(viewport)之间有几种选择。 我会尝试: 禁用左轴,启用右轴 chart.le
我正在构建一个简单的应用程序,您可以在其中使用纸娃娃之类的工具来描述您的外观。 Check out this image.计划是有 4 个水平 ScrollView :第一个用于发型,第二个用于面部毛
我有一个问题...我在绝对布局中有两个 ScrollView 。换句话说,它们是全屏的并且相互重叠 上面的scrollview是水平滚动的,下面的是垂直滚动的scrollview。 当我水平滚动时,我
我看了一些类似的问题,但我不太明白在我的层次结构中我应该做什么? 我有 用于屏幕底部的标签菜单 和 对于其他将创建的 fragment 。 我有 9 个标签菜单,每个都是 fragment 。 一
在我的 Android 应用程序中,我有一个编辑文本和一个按钮,单击该按钮会向我的主要 Activity 添加一个 fragment ,其中包含在我的编辑文本中写入的消息。问题是,当我更改消息并单击按
在我的分段控件中,有时标题比其段宽。我怎样才能让它截断? 假设第 1 段的标题是 Text overlaps,第 2 段的名称是 ok。 我希望它看起来如何: [Text ov...| ok
我想创建一个带有重叠单元格的 uitableview,如下图所示。问题是,即使我为单元格的内容 View 设置 clipsToBounds = NO,单元格假标题(例如,将与前一个单元格重叠的西类牙语
有了这个CSS .addProblemClass{ width:300px; height:300px; /*width:25%; height:40%;*/
我有跨窗口移动的图像(2 行),当我离开页面选项卡时,然后返回它,所有图像都相互堆叠。 JS代码(记入jfriend00) function startMoving(img) { va
这是我的一段代码。图像在 23 毫秒后正常可见,但永远不会像第二行所示那样返回隐藏状态。如果我将其从 17 毫秒更改为大于 23 毫秒的值,它就会起作用。反之亦然,如果我将第一行更改为 16 毫秒,它
我正在可汗学院为学校项目编写一款太空入侵者游戏,但我不知道如何在子弹和外星人之间进行碰撞,然后摆脱子弹所碰撞的外星人。这是非常基本的 JS,尽管我尝试过,但我不太明白如何将有关该主题的其他答案放入我的
当我尝试重新加载 tableView 的数据时出现奇怪的重叠,导致单元格的高度发生变化(使用 UITableViewAutomaticDimension),然后内容与上面的单元格重叠,无法弄清楚怎么做
我是一个新手,如果这是一个愚蠢的问题,请原谅我。我想有一个部分与标题分开,但发生了两种情况: (1) 当我把 在 下面,它们相互重叠,如下所示: Section overlapping header
我正在尝试创建两个 那是重叠的。唯一的问题是第二个 在第一个的前面它必须是相反的。我尝试设置第一个 的 z-index至 1但它仍然不起作用。 这是我的代码: #content{ backgrou
是否有重叠 2 个 div 的有效方法。 我有以下内容,但无法让它们重叠。 #top-border{width:100%; height:60px; background:url(image.jpg)
我是一名优秀的程序员,十分优秀!