c++ - Can Win32 "move" heap-allocated memory? -


i have .net/native c++ application. currently, c++ code allocates memory on default heap persists life of application. basically, functions/commands executed in c++ results in allocation/modification of current persistent memory. investigating approach cancelling 1 of these functions/commands mid-execution. have hundreds of these commands, , many complicated (legacy) code.

the brute-force approach trying avoid modifying each , every command/function check cancellation , appropriate clean-up (freeing heap memory). investigating multi-threaded approach in additional thread receives cancellation request , terminates command-execution thread. want dynamic memory allocated on "private heap" using heapcreate() (win32). way, private heap destroyed thread handling cancellation request. however, if command runs completion, need dynamic memory persist. in case, logical equivalent of "moving" private heap memory default/process heap without incurring cost of actual copy. in way possible? make sense?

alternatively, recognize have new private heap every command/function execution (each new thread). private heap destroyed if command cancelled, or survive if command completes. there problem number of heaps growing indefinitely? know there overhead involved each heap. limitations might run into?

i running on windows 7 64-bit 8gb ram (consider target platform). application working 1 million sloc (half c++, half c#). looking experience/suggestions private heap management, or alternatives solution.

heap sort of big chunk of memory. user-level memory manager. heap created lower-level system memory calls (e.g., sbrk in linux , virtualalloc in windows). in a heap, can request or return small chunk of memory malloc/new/free/delete. default, process has single heap (unlike stack, threads share heap). but, can have many heaps.

  • is possible combine 2 heaps w/o copying? heap data structure maintains list of used , freed memory chunks. so, heap should have sort of bookkeeping data called meta data. of course, meta data per heap. afaik, no heap manager supports merge operation of 2 heaps. had reviewed entire source code of malloc implementation in linux glibc (doug lea's implementation), no such operation. windows heap* functions implemented in similar way. so, impossible move or merge 2 separate heaps.

  • is possible have many heaps? don't think there should big problem have many heaps. said before, heap data structure keeps used/freed memory chunks. so, there should amount of overhead. but, it's not severe. when @ 1 of malloc implementation, there malloc_state, basic data structure each heap. example, can create heap create_mspace (in windows, heapcreate), new malloc state. it's not big. so, if tread-off (some heap overhead vs. implementation easiness) fine, may go on.

if you, i'll try way describe. makes sense me. having lot of heap objects not make big overhead.

also, should noted technically moving memory regions impossible. pointers pointed moved memory region result in dangling pointers.

p.s. problem seems transaction, software transactional memory. typical implementation of stm buffers pending memory writes, , commits real system memory transaction had no conflict.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -