//allocator 主要函数就这两个,一个分配一个回收内存
_NODISCARD_RAW_PTR_ALLOC _CONSTEXPR20 __declspec(allocator) _Ty*allocate(_CRT_GUARDOVERFLOW const size_t _Count){static_assert(sizeof(value_type)>0,"value_type must be complete before calling allocate.");returnstatic_cast<_Ty*>(_Allocate<_New_alignof<_Ty>>(_Get_size_of_n<sizeof(_Ty)>(_Count)));}//_CONSTEXPR20 宏定义的关键字,在c++20 表示 constexpr, c++20之前 表示inline//constexpr 常量表达式,表明编译时就需要计算这个函数的值,如果需要的话//分配内存是调用的是 _Deallocate 常规的库函数,默认使用_Default_allocate_traits 来进行内存分配,如下
_Traits::_Allocate(_Bytes);//_Traits为_Default_allocate_traits 其中_Allocate中,这个只是小内存分配,大内存分配其实也一样,只是多做了写记录,也是调用这个玩意//_Default_allocate_traits 类中void*_Allocate(const size_t _Bytes){return::operatornew(_Bytes);//所以内存还是直接new出来的}//再来看释放内存
_CONSTEXPR20 voiddeallocate(_Ty*const _Ptr,const size_t _Count){_STL_ASSERT(_Ptr !=nullptr|| _Count ==0,"null pointer cannot point to a block of non-zero size");// no overflow check on the following multiply; we assume _Allocate did that check_Deallocate<_New_alignof<_Ty>>(_Ptr,sizeof(_Ty)* _Count);}//_New_alignof<_Ty> 取最大的对齐方式,还是啥的,单个块的大小吧,最小为8//主要调用_Deallocate进行内存释放,其函数内部主要调用 ::operator delete(_Ptr, _Bytes);进行内存释放