ats

来源:互联网 发布:周生生淘宝旗舰店 编辑:程序博客网 时间:2024/06/10 20:09


head heap 记录信息结构体

可以记录已经heap内使用的块的状态只读块

可以记录刚刚分配的快的状态,因为还没使用所以是可读和可写得。

而HdrHeap 自身又是一个单向链表,当地址空间不足时会自分配一个next块 大小为4096 的来承载接下来的信息。

后面我会介绍每个函数的功能。


class HdrHeap
{
  friend class CoreUtils;


public:
  void init();
  inkcoreapi void destroy();


  // PtrHeap allocation
  HdrHeapObjImpl *allocate_obj(int nbytes, int type);
  void deallocate_obj(HdrHeapObjImpl *obj);


  // StrHeap allocation
  char *allocate_str(int nbytes);
  char *expand_str(const char *old_str, int old_len, int new_len);
  char *duplicate_str(const char *str, int nbytes);
  void free_string(const char *s, int len);


  // Marshalling
  inkcoreapi int marshal_length();
  inkcoreapi int marshal(char *buf, int length);
  int unmarshal(int buf_length, int obj_type, HdrHeapObjImpl **found_obj, RefCountObj *block_ref);
  /// Computes the valid data size of an unmarshalled instance.
  /// Callers should round up to HDR_PTR_SIZE to get the actual footprint.
  int unmarshal_size() const; // TBD - change this name, it's confusing.
  // One option - overload marshal_length to return this value if @a magic is HDR_BUF_MAGIC_MARSHALED.


  void inherit_string_heaps(const HdrHeap *inherit_from);
  int attach_block(IOBufferBlock *b, const char *use_start);
  void set_ronly_str_heap_end(int slot, const char *end);


  // Lock read only str heaps so that can't be moved around
  //  by a heap consolidation.  Does NOT lock for Multi-Threaed
  //  access!
  void
  lock_ronly_str_heap(int i)
  {
    m_ronly_heap[i].m_locked = true;
  };
  void
  unlock_ronly_str_heap(int i)
  {
    m_ronly_heap[i].m_locked = false;
    // INKqa11238
    // Move slot i to the first available slot in m_ronly_heap[].
    // The move is necessary because the rest of the code assumes
    // heaps are always allocated in order.
    for (int j = 0; j < i; j++) {
      if (m_ronly_heap[j].m_heap_start == NULL) {
        // move slot i to slot j
        m_ronly_heap[j].m_ref_count_ptr = m_ronly_heap[i].m_ref_count_ptr;
        m_ronly_heap[j].m_heap_start = m_ronly_heap[i].m_heap_start;
        m_ronly_heap[j].m_heap_len = m_ronly_heap[i].m_heap_len;
        m_ronly_heap[j].m_locked = m_ronly_heap[i].m_locked;
        m_ronly_heap[i].m_ref_count_ptr = NULL;
        m_ronly_heap[i].m_heap_start = NULL;
        m_ronly_heap[i].m_heap_len = 0;
        m_ronly_heap[i].m_locked = false;
      }
    }
  };


  // Sanity Check Functions
  void sanity_check_strs();
  bool check_marshalled(uint32_t buf_length);


  // Debugging functions
  void dump_heap(int len = -1);


  uint32_t m_magic;
  char *m_free_start;
  char *m_data_start;
  uint32_t m_size;


  bool m_writeable;


  // Overflow block ptr
  //   Overflow blocks are necessary because we can
  //     run out of space in the header heap and the
  //     heap is not rellocatable
  //   Overflow blocks have the HdrHeap full structure
  //    header on them, although only first block can
  //    point to string heaps
  HdrHeap *m_next;


  // HdrBuf heap pointers
  uint32_t m_free_size;


  int demote_rw_str_heap();
  void coalesce_str_heaps(int incoming_size = 0);
  void evacuate_from_str_heaps(HdrStrHeap *new_heap);
  size_t required_space_for_evacuation();
  int attach_str_heap(char *h_start, int h_len, RefCountObj *h_ref_obj, int *index);


  /** Struct to prevent garbage collection on heaps.
      This bumps the reference count to the heap containing the pointer
      while the instance of this class exists. When it goes out of scope
      the reference is dropped. This is useful inside a method or block
      to keep the required heap data around until leaving the scope.
  */
  struct HeapGuard {
    /// Construct the protection.
    HeapGuard(HdrHeap *heap, const char *str)
    {
      if (heap->m_read_write_heap && heap->m_read_write_heap->contains(str)) {
        m_ptr = heap->m_read_write_heap;
      } else {
        for (int i = 0; i < HDR_BUF_RONLY_HEAPS; ++i) {
          if (heap->m_ronly_heap[i].contains(str)) {
            m_ptr = heap->m_ronly_heap[i].m_ref_count_ptr;
            break;
          }
        }
      }
    }


    // There's no need to have a destructor here, the default dtor will take care of
    // releaseing the (potentially) locked heap.


    /// The heap we protect (if any)
    Ptr<RefCountObj> m_ptr;
  };


  // String Heap access
  Ptr<HdrStrHeap> m_read_write_heap;
  StrHeapDesc m_ronly_heap[HDR_BUF_RONLY_HEAPS];
  int m_lost_string_space;
};
0 0
原创粉丝点击