void* crnlib_calloc(size_t count, size_t size, size_t* pActual_size) { size_t total = count * size; void *p = crnlib_malloc(total, pActual_size); if (p) memset(p, 0, total); return p; }
Area_List * Area_List_dup_new(Area_List *Plist, int x_ofs, int y_ofs) { int i; Area_List *Pnew_list = (Area_List*)crnlib_calloc(1, sizeof(Area_List)); Pnew_list->total_areas = Plist->total_areas; Pnew_list->Phead = (Area *)crnlib_malloc(sizeof(Area) * Plist->total_areas); Pnew_list->Ptail = Pnew_list->Phead + 1; Pnew_list->Pfree = (Plist->Pfree) ? ((Plist->Pfree - Plist->Phead) + Pnew_list->Phead) : NULL; Pnew_list->next_free = Plist->next_free; memcpy(Pnew_list->Phead, Plist->Phead, sizeof(Area) * Plist->total_areas); for (i = 0; i < Plist->total_areas; i++) { Pnew_list->Phead[i].Pnext = (Plist->Phead[i].Pnext == NULL) ? NULL : (Plist->Phead[i].Pnext - Plist->Phead) + Pnew_list->Phead; Pnew_list->Phead[i].Pprev = (Plist->Phead[i].Pprev == NULL) ? NULL : (Plist->Phead[i].Pprev - Plist->Phead) + Pnew_list->Phead; Pnew_list->Phead[i].x1 += x_ofs; Pnew_list->Phead[i].y1 += y_ofs; Pnew_list->Phead[i].x2 += x_ofs; Pnew_list->Phead[i].y2 += y_ofs; } return (Pnew_list); }
bool elemental_vector::increase_capacity(uint min_new_capacity, bool grow_hint, uint element_size, object_mover pMover, bool nofail) { CRNLIB_ASSERT(m_size <= m_capacity); #ifdef CRNLIB_PLATFORM_PC_X64 CRNLIB_ASSERT(min_new_capacity < (0x400000000ULL / element_size)); #else CRNLIB_ASSERT(min_new_capacity < (0x7FFF0000U / element_size)); #endif if (m_capacity >= min_new_capacity) return true; size_t new_capacity = min_new_capacity; if ((grow_hint) && (!math::is_power_of_2(new_capacity))) new_capacity = math::next_pow2(new_capacity); CRNLIB_ASSERT(new_capacity && (new_capacity > m_capacity)); const size_t desired_size = element_size * new_capacity; size_t actual_size; if (!pMover) { void* new_p = crnlib_realloc(m_p, desired_size, &actual_size, true); if (!new_p) { if (nofail) return false; char buf[256]; #ifdef _MSC_VER sprintf_s(buf, sizeof(buf), "vector: crnlib_realloc() failed allocating %u bytes", (uint)desired_size); #else sprintf(buf, "vector: crnlib_realloc() failed allocating %u bytes", (uint)desired_size); #endif CRNLIB_FAIL(buf); } m_p = new_p; } else { void* new_p = crnlib_malloc(desired_size, &actual_size); if (!new_p) { if (nofail) return false; char buf[256]; #ifdef _MSC_VER sprintf_s(buf, sizeof(buf), "vector: crnlib_malloc() failed allocating %u bytes", (uint)desired_size); #else sprintf(buf, "vector: crnlib_malloc() failed allocating %u bytes", (uint)desired_size); #endif CRNLIB_FAIL(buf); } (*pMover)(new_p, m_p, m_size); if (m_p) crnlib_free(m_p); m_p = new_p; } if (actual_size > desired_size) m_capacity = static_cast<uint>(actual_size / element_size); else m_capacity = static_cast<uint>(new_capacity); return true; }