假设头部有未使用的对象前提下,在指定位置插入未初始化数据块并转移原有实例的生命周期,允许新的参数描述已使用的对象多少,并不构造新的实例 若有多余的大小,追加到前端
21575 {
21576 const auto from_size=get_size_of_alloc(arg);
21577 const auto grow_size=to_size-from_size;
21578 const auto used_size_after_insert=used_size+insert_size;
21579 const auto unused_size_after_insert=to_size-used_size_after_insert;
21580 const auto unused_size_before_insert=from_size-used_size;
21581
21582 const auto after_insert_size=from_size-insert_pos;
21583 const auto before_insert_size=insert_pos-unused_size_before_insert;
21584 insert_pos-=unused_size_before_insert;
21585 if constexpr(move.
trivial<T>){
21586 realloc(arg,to_size);
21587 const auto new_data_ptr=arg+unused_size_after_insert;
21588 const auto origin_data_ptr=arg+unused_size_before_insert;
21589
21590 ::std::memmove(new_data_ptr+insert_pos+insert_size,origin_data_ptr+insert_pos,after_insert_size*sizeof(T));
21591
21592 ::std::memmove(new_data_ptr,origin_data_ptr,before_insert_size*sizeof(T));
21593 }
21594 else{
21595 T*tmp=alloc<T>(to_size);
21596 const auto new_data_ptr=tmp+unused_size_after_insert;
21597 const auto origin_data_ptr=arg+unused_size_before_insert;
21598 if constexpr(!move.
nothrow<T>){
21599 template_warning(
"the move of T was not noexcept,this may cause memory lack.");
21600 try{
21601 move[after_insert_size](note::from(origin_data_ptr+insert_pos),note::to(new_data_ptr+insert_pos+insert_size));
21602 move[before_insert_size](note::from(origin_data_ptr),note::to(new_data_ptr));
21603 }catch(...){
21604 free(tmp);
21605 throw;
21606 }
21607 }else{
21608 move[after_insert_size](note::from(origin_data_ptr+insert_pos),note::to(new_data_ptr+insert_pos+insert_size));
21609 move[before_insert_size](note::from(origin_data_ptr),note::to(new_data_ptr));
21610 }
21612 arg=tmp;
21613 }
21614 struct result_t{
21615 size_t from_size;
21616 size_t to_size;
21617 size_t unused_size_before_insert;
21618 size_t unused_size_after_insert;
21619 size_t used_size_after_insert;
21620 size_t before_insert_size;
21621 size_t after_insert_size;
21622 size_t used_size;
21623 size_t grow_size;
21624 size_t insert_pos;
21625 size_t insert_size;
21626 T* new_data_ptr(T*arg)const noexcept{return arg+unused_size_after_insert;}
21627 T* origin_data_ptr(T*arg)const noexcept{return arg+unused_size_before_insert;}
21628 size_t before_grow_size()const noexcept{return grow_size+used_size-insert_size;}
21629 T* insert_data_ptr(T*arg)const noexcept{return arg+unused_size_after_insert+insert_pos;}
21630 } result{
21631 from_size,
21632 to_size,
21633 unused_size_before_insert,
21634 unused_size_after_insert,
21635 used_size_after_insert,
21636 before_insert_size,
21637 after_insert_size,
21638 used_size,
21639 grow_size,
21640 insert_pos,
21641 insert_size
21642 };
21643 return result;
21644 }