我想在Linux的/ C / C任意-大的缓冲++ [英] I want an arbitrarily-large buffer in Linux/C/C++

查看:114
本文介绍了我想在Linux的/ C / C任意-大的缓冲++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想要一个任意大的堆栈。我知道这是不可能的,但我可以抛开我的虚拟地址空间的几个TB呢?我希望能够尽可能我需要从头开始,走了缓冲区,Linux可以在网页带来的物理内存的需要的基础上。是一样的东西,可能吗?难道有同样的表现像刚才的malloc-ING的缓冲?会不会有一种发信号到Linux,你的记忆,一旦你弹出堆栈做了什么?

Basically I want an arbitrarily large stack. I know that's not possible, but could I set aside a few terabytes of my virtual address space for it? I'd like to be able to start at the beginning and walk up the buffer as far as I need, and Linux can bring in pages from physical memory on an as-needed basis. Is something like that possible? Would it have the same performance as just malloc-ing a buffer? Would there be a way to signal to Linux that you're done with the memory once you pop the stack?

编辑:我想这是因为我想优化递归/并行算法,在每次调用分配大量内存。 malloc的是我太慢了,我不希望所有的线程上的malloc的锁对方内线跳闸。所以基本上这将是沿着真正的(每个线程),我自己的运行栈。

I'm wanting this because I want to optimize a recursive/parallel algorithm that allocates lots of memory on each call. Malloc is too slow for me and I don't want all the threads to trip on each other inside of malloc's locks. So basically it would be my own run-time stack alongside the real one (one for each thread).

其实,只要在运行时堆栈足够大,应该是足够了。有没有办法知道/保证堆栈的大小?在64位地址空间有足够的空间供多个线程进行堆栈分配千兆字节的数据。那是可行的?

Actually, as long as the run-time stack is big enough that should be good enough. Is there a way to know/ensure the size of the stack? In a 64-bit address space there's enough room for several threads to be stack-allocating gigabytes of data. Is that doable?

它看起来像pthread_attr_setstacksize可以为新主题工作,但没有太多帮助,如果你可以从任何线程调用...

It looks like pthread_attr_setstacksize could work for new threads, but that doesn't help much if you can be called from any thread ...

推荐答案

您使用可以创建这样一个分配的mmap() MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE 标志。当你想释放回系统,请使用则munmap()

You can create such an allocation using mmap() with the MAP_ANONYMOUS | MAP_NORESERVE | MAP_PRIVATE flags. When you want to free it back to the system, use munmap().

请注意,你会发现,如果没有足够的内存来满足你的实际用途是通过你的进程接收 SIGSEGV 的唯一途径 - 而这是与领土的要求一个巨大的映射超出了可用内存大得多。

Note that the only way you will find out if insufficient memory is available to satisfy your actual use is by your process receiving a SIGSEGV - but this is comes with the territory of asking for a giant mapping much larger than available memory.

这篇关于我想在Linux的/ C / C任意-大的缓冲++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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