Array memory management

我与影子孤独终老i 提交于 2019-12-13 00:09:06

问题


I am doing my Computing Science project. I am doing Multiprocessor programming using C. One requirement for us it that, we cannot keep allocating small chunks of memory. Memory can be allocated in big chunks when needed.

So imagine I use structures in my program. And the way that my program works requires dynamic memory allocation. But it is very costly in the hardware that we are using. So the best solution would be to allocate a big pool of memory at the beginning and whenever needed allocate memory from this pool.

The way I envision this to work is that, I will allocate a bit array of these structures and write my own memory management module which allocates and frees memory from this pool. But I want to know the most ideal way to write these modules. Are there any libraries which can assist me in managing memory or is there some way that these can be written?

EDIT: Here is the platform that I use: an AMD opteron system which runs Ubuntu. The opterons has NUMA architecture and I want to make use of this when allocating memory. Hence instead of using malloc, I use numa_alloc_onnode which allocates memory on one specific node. I want to allocate a big chunk of memory using this and then use a memory manager to manage this memory.


回答1:


There are a ton of memory pool managers out there, some commercial and some open source. Have a look at them, and feel free to ask more specific questions here after you have an overview.

Some google results (c memory pool manager open source):

http://256stuff.com/sources/mpool/

http://www.ravenbrook.com/project/mps/

Here's a good article from IBM on the subject:

http://www.ibm.com/developerworks/linux/library/l-memory/

And since you mention a multiprocessor environment (although not directly related to memory management), this is also a worthwhile read:

http://drdobbs.com/go-parallel/article/showArticle.jhtml?articleID=217500206

UPDATE

There are different approaches depending on the desired portability of the library. If portability is important, the library presents a wrapper around malloc (or calloc) to grab chunks of memory and then apply a more efficient implementation (as measured against the particular goals the library wants to achieve). A library that targets a specific OS will typically use native operating system calls, gaining some performance and the ability to leverage specifics of that operating system at the cost of portability.

The specific goals that memory management libraries strive for vary. Here are some goals I have seen over the years:

  • Separate management strategies for large vs. small objects
  • Separate management strategies for things that will live long vs. things that will be deleted rapidly
  • Debugging for memory management issues (e.g. write a specific pattern into allocated memory and allocate some extra padding on the end so that a debug function can see if the padding was overwritten by an out-of-bounds pointer access)
  • Faster memory access given constraints of a particular platform (e.g. some allocations may be expensive, others relatively cheap).
  • Support memory defragmentation (e.g. by really allocating a pointer to a pointer, with special semantics for accessing memory).

UPDATE 2

Based on your update... my fourth bullet point applies. Here's a good read on creating a heap manager for a NUMA architecture (source code at the end of the article):

http://ebookbrowse.com/numa-aware-heap-memory-manager-article-final-pdf-d12526838



来源:https://stackoverflow.com/questions/9439402/array-memory-management

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!