C ++ memory allocator architecture

I need to write a Hoard allocator for C ++ on Linux. Although the algorithm is quite simple, I don’t understand where (and how) to store distributor data (e.g. heaps)

What I see: a distributor is not a process, it is a set of functions that any application can use. Each application has its own heaps.

  • What happens when the application starts?
  • And how does the distributor know that heaps are already created?
  • How does the dispenser create, save, and destroy (when the application closes) heaps?
  • When a function is called, how do I know which thread (or processor) it is running on?
+4
source share
3 answers
  • There is probably not much during the launch of the application, unless the allocator is designed and connected to the application launch code to pre-request some memory from the operating system.
  • Heaps are not really created. The allocation system shuts down and requests the operating system for some memory when it needs it - either for its initial configuration, or later, when it needs to additionally perform the requested allocation. On unix-like systems, a commonly used system call is called sbrk . (Strictly speaking, in linux sbrk is a library wrapper function for the brk system call - this may or may not be an important difference for you.)
  • The sbrk retrieves memory from the operating system using the sbrk call mentioned above. After that, he himself controls this memory. When the application exits, the operating system restores the memory - it knows that it was being distributed using sbrk calls, so it knows what memory it needs to return to.
  • It almost never matters which thread or processor is processing a particular piece of code - if you explain the context of what you are asking for a bit more, I will try to answer.
+2
source

What do you mean by "write Hoard allocator"? Someone already wrote this dispenser. Are you trying to use from C ++? The internal development of the Emery Berger Hoard is described in some detail in the white Hoard: a scalable memory allocator for multi-threaded applications. Everything that does not answer can always be solved by reading the source or by contacting the author. I would be surprised if there is no mailing list.

+1
source

What happens when the application starts?

When the application starts, the dispenser initializes itself. You can do this the first time you call your allocator, if you want.

And how does the distributor know that heaps have already been created?

I am not sure that I am following this question. If you are talking about heaps that are managed by code that uses this same allocator, it discovers that it creates tracking records when it creates heaps. If you are talking about heaps in other processes or creating other allocators, it doesn't matter, since he cannot use these heaps anyway.

How does the dispenser create, save, and destroy (when the application closes) heaps?

Typically, you have a low-level and high-level dispenser. The low-level allocator simply receives raw chunks of memory from the operating system. The exact mechanism is platform specific. The high-level allocator manages the heaps, and it receives memory for storing heap structures from the low-level allocator.

When a function is called, how do I know which thread (or processor) it is running on?

You can find out which thread is associated with the data depending on the thread, or by calling the "get thread ID" function for a specific platform. As for the processor, it is very specific to the platform, and by the time it is received, the information may be outdated. Most platforms have this feature - just remember that you can only use it as an optimization (to reduce lock conflict or improve cache attack speed). Critically, you cannot use it to bypass a lock, because a thread can move from one processor to another at any time.

Honestly, a memory allocation that provides good performance and is portable across all platforms is indeed a case of heavy magic. If you are not an expert in this field, it is unlikely that you will be able to develop a distributor that provides the performance and stability needed for a production application.

+1
source

Source: https://habr.com/ru/post/1401737/


All Articles