TaskHeap

Files CCore/inc/TaskHeap.h CCore/src/TaskHeap.cpp

TaskHeap provides per-task heap feature. This feature is based on PerTask.


class TaskHeap : TaskObjectBuild<TaskHeapControl>
 {
  public:
  
   explicit TaskHeap(ulen min_page_alloc_len=0);
   
   ~TaskHeap();
   
   static void * TryMemAlloc(ulen len) noexcept;
   
   static void * MemAlloc(ulen len);
   
   static bool MemExtend(void *mem,ulen len);

   static bool MemShrink(void *mem,ulen len);
    
   static void MemFree(void *mem);
 };

This class is a control class. It assigns per-task object builder. The constructor argument for the HCore TaskHeap specifies the minimum page allocation length value.

You may use static methods of this class to access the per-task heap service, once a builder is active. Semantic of methods is similar to semantic of the global heap functions.

MemAlloc() allocates a memory block of required length and returns its address. The block is always MaxAligned, the actual length can be a slightly greater than the required and always non-null. If the operation is failed, then an exception is thrown, using the GuardNoMem() function.

TryMemAlloc() is similar, but in case of failure it just returns null.

In the remaining functions the argument mem must be either an address of the allocated memory block or null. Otherwise, the memory heap protection abort is called (with high probability, the detection is not 100%).

MemFree() releases the allocated memory block, even if the block was allocated by another task.

MemExtend() tries to extend the allocated memory block. If the block was allocated by another task, the method returns false. Otherwise, if successful, it returns true and the new length of the block is at least len. If the mem is null, the function does nothing and returns false.

MemShrink() shrinks the allocated memory block. If the block was allocated by another task, the method returns false. Otherwise, if successful, it returns true and the new length of the block is at least len. If the mem is null, the function does nothing and returns false.

TaskHeapScope can be used to control the life-time of the per-task object:


using TaskHeapScope = PerTaskObjectScope<TaskHeapControl> ;

XCore variant is slightly different. The constructor argument is the per-task heap space length, defaulted to 1 MByte.


class TaskHeap : TaskObjectBuild<TaskHeapControl>
 {
  public:
  
   explicit TaskHeap(ulen mem_len=1_MByte);
   
   ~TaskHeap();
   
   static void * TryMemAlloc(ulen len) noexcept;
   
   static void * MemAlloc(ulen len);
   
   static bool MemExtend(void *mem,ulen len);

   static bool MemShrink(void *mem,ulen len);
    
   static void MemFree(void *mem);
 };