आपको एक कस्टम आवंटन पास करने की आवश्यकता है। आप std::allocator
काफी आसानी से खत्म हो गया एक बना सकते हैं:
template <typename T, size_t TALIGN=16, size_t TBLOCK=8>
class aligned_allocator : public std::allocator<T>
{
public:
aligned_allocator() {}
aligned_allocator& operator=(const aligned_allocator &rhs){
std::allocator<T>::operator=(rhs);
return *this;
}
pointer allocate(size_type n, const void *hint){
pointer p = NULL;
size_t count = sizeof(T) * n;
size_t count_left = count % TBLOCK;
if(count_left != 0)
{
count += TBLOCK - count_left;
}
if (!hint)
{
p = reinterpret_cast<pointer>(aligned_malloc(count,TALIGN));
}else{
p = reinterpret_cast<pointer>(aligned_realloc((void*)hint,count,TALIGN));
}
return p;
}
void deallocate(pointer p, size_type n){
aligned_free(p);
}
void construct(pointer p, const T &val){
new(p) T(val);
}
void destroy(pointer p){
p->~T();
}
};
केवल एक चीज यहां कमी aligned_malloc
, aligned_realloc
और aligned_free
है। आपको या तो उन्हें स्वयं लागू करने की आवश्यकता है (यह कठिन नहीं होना चाहिए), या इंटरनेट पर उन लोगों के संस्करण ढूंढें (मैंने OGRE इंजन में कम से कम एक देखा है)।
स्रोत
2010-01-09 07:08:08
@ एनीकॉर्न, क्या आपने कभी इसे कार्यान्वित किया है? ऐसा लगता है कि कॉर्नेल किसिलेविज़ के उदाहरण को पूरा करना बहुत अच्छा होगा क्योंकि मुझे वास्तव में कहीं और ऑनलाइन इसका अच्छा उदाहरण नहीं दिख रहा है। –