Ejemplo n.º 1
0
 inline void* allocate(std::size_t sz) throw(std::bad_alloc) {
   sz = ALIGN_SIZE(sz, std::size_t, sizeof(aligner));
   if (sz <= T_depth) {
     heap* h = m_head;
     while (h) {
       void* const ptr = h->allocate(sz);
       if (ptr) { 
         if (h != m_head) {
           dlist_pop(this, h);
           dlist_push_head(this, h);
           DBG_PRINTF(("(%p/%d/%d) Dynamic Region Heap Adjust\n", 
             (void*)h, m_depth, m_max_depth));
         }
         return ptr;
       }
       h = h->m_next;
     }
     void* const ptr = create_heap()->allocate(sz);
     if (ptr) {
       return ptr;
     }
   }
   void* const ptr = std::malloc(sz);
   if(! ptr) {
     throw std::bad_alloc();
   }
   return ptr;
 }
Ejemplo n.º 2
0
bool dlist_insert (DList* list, int32_t index, void* data) {
    assert(NULL != list);
    assert(0 <= index);
    assert(index <= list->count);

    if (0 == index) {
        return dlist_push_head(list, data);
    } else if (index == list->count) {
        return dlist_push_tail(list, data);
    } else if (index > list->count) {
        return false;
    }

    DListItem* item     = NULL;
    DListItem* new_item = (DListItem*) malloc(sizeof(DListItem));

    if (NULL == new_item) {
        return false;
    }

    __DLIST_GET(item, list, index);

    new_item->data       = data;
    new_item->next       = item;
    new_item->prev       = item->prev;
    new_item->next->prev = new_item;
    new_item->prev->next = new_item;
    list->count++;

    return true;
}
Ejemplo n.º 3
0
bool dlist_push_head_ts (DList* list, void* data) {
    assert(NULL != list);
    assert(NULL != list->mutex);

    pthread_mutex_lock(list->mutex);

    bool ret = dlist_push_head(list, data);

    pthread_mutex_unlock(list->mutex);

    return ret;
}
Ejemplo n.º 4
0
 heap* create_heap() {
   heap* const h = reinterpret_cast<heap*>(std::malloc(sizeof(*h)));
   if (! h) { 
     throw std::bad_alloc();
   }
   h->ctor();
   dlist_push_head(this, h);
   ++m_depth;
   DBG_PRINTF(("(%p/%d/%d) Dynamic Region  Expand\n", 
     (void*)h, m_depth, m_max_depth));
   return h;
 }