int gd_timer_processor_alloc(gd_timer_mgr_t mgr, gd_timer_id_t * id) { ptr_int_t newStart; struct gd_timer_processor * newPage; size_t i; if (!cpe_range_mgr_is_empty(&mgr->m_ids)) { *id = (gd_timer_id_t)cpe_range_get_one(&mgr->m_ids); return 0; } if (mgr->m_timer_page_count + 1 >= mgr->m_timer_page_capacity) { size_t newProcessorPageCapacity; struct gd_timer_processor ** newProcessorBuf; newProcessorPageCapacity = mgr->m_timer_page_count + 128; newProcessorBuf = (struct gd_timer_processor **)mem_alloc(mgr->m_alloc, sizeof(struct gd_timer_processor*) * newProcessorPageCapacity); if (newProcessorBuf == NULL) return -1; bzero(newProcessorBuf, sizeof(struct gd_timer_processor *) * newProcessorPageCapacity); memcpy(newProcessorBuf, mgr->m_timer_buf, sizeof(struct gd_timer_processor*) * mgr->m_timer_page_count); if (mgr->m_timer_buf) mem_free(mgr->m_alloc, mgr->m_timer_buf); mgr->m_timer_buf = newProcessorBuf; mgr->m_timer_page_capacity = newProcessorPageCapacity; if (mgr->m_debug) { CPE_INFO(mgr->m_em, "%s: gd_timer_processor_alloc: resize processor buf to " FMT_SIZE_T, gd_timer_mgr_name(mgr), newProcessorPageCapacity); } } newStart = mgr->m_timer_page_count * mgr->m_timer_count_in_page; newPage = (struct gd_timer_processor *)mem_alloc(mgr->m_alloc, sizeof(struct gd_timer_processor) * mgr->m_timer_count_in_page); if (newPage == NULL) { return -1; } bzero(newPage, sizeof(struct gd_timer_processor) * mgr->m_timer_count_in_page); for(i = 0; i < mgr->m_timer_count_in_page; ++i) { newPage[i].m_id = newStart + i; } if (cpe_range_put_range(&mgr->m_ids, newStart, newStart + mgr->m_timer_count_in_page) != 0) { mem_free(mgr->m_alloc, newPage); return -1; } mgr->m_timer_buf[mgr->m_timer_page_count] = newPage; ++mgr->m_timer_page_count; if (mgr->m_debug) { CPE_INFO( mgr->m_em, "alloc a new processor page[%d,%d), page count is " FMT_SIZE_T, (gd_timer_id_t)newStart, (gd_timer_id_t)(newStart + mgr->m_timer_page_count), mgr->m_timer_page_count); } *id = (gd_timer_id_t)cpe_range_get_one(&mgr->m_ids); return 0; }
bool empty(void) const { return cpe_range_mgr_is_empty(const_cast<struct cpe_range_mgr *>(&m_rm)) ? true : false; }