diff options
Diffstat (limited to 'libstdc++-v3/include/bits/stl_alloc.h')
| -rw-r--r-- | libstdc++-v3/include/bits/stl_alloc.h | 138 |
1 files changed, 78 insertions, 60 deletions
diff --git a/libstdc++-v3/include/bits/stl_alloc.h b/libstdc++-v3/include/bits/stl_alloc.h index fceed498df6..bab39fb7150 100644 --- a/libstdc++-v3/include/bits/stl_alloc.h +++ b/libstdc++-v3/include/bits/stl_alloc.h @@ -139,7 +139,8 @@ namespace std allocate(size_t __n) { void* __result = malloc(__n); - if (0 == __result) __result = _S_oom_malloc(__n); + if (__builtin_expect(__result == 0, 0)) + __result = _S_oom_malloc(__n); return __result; } @@ -152,7 +153,7 @@ namespace std reallocate(void* __p, size_t /* old_sz */, size_t __new_sz) { void* __result = realloc(__p, __new_sz); - if (0 == __result) + if (__builtin_expect(__result == 0, 0)) __result = _S_oom_realloc(__p, __new_sz); return __result; } @@ -181,8 +182,8 @@ namespace std for (;;) { __my_malloc_handler = __malloc_alloc_oom_handler; - if (0 == __my_malloc_handler) - std::__throw_bad_alloc(); + if (__builtin_expect(__my_malloc_handler == 0, 0)) + __throw_bad_alloc(); (*__my_malloc_handler)(); __result = malloc(__n); if (__result) @@ -202,8 +203,8 @@ namespace std for (;;) { __my_malloc_handler = __malloc_alloc_oom_handler; - if (0 == __my_malloc_handler) - std::__throw_bad_alloc(); + if (__builtin_expect(__my_malloc_handler == 0, 0)) + __throw_bad_alloc(); (*__my_malloc_handler)(); __result = realloc(__p, __n); if (__result) @@ -232,7 +233,12 @@ namespace std public: static _Tp* allocate(size_t __n) - { return 0 == __n ? 0 : (_Tp*) _Alloc::allocate(__n * sizeof (_Tp)); } + { + _Tp* __ret = 0; + if (__n) + __ret = static_cast<_Tp*>(_Alloc::allocate(__n * sizeof(_Tp))); + return __ret; + } static _Tp* allocate() @@ -293,9 +299,9 @@ namespace std { char* __real_p = (char*)__p - (int) _S_extra; assert(*(size_t*)__real_p == __old_sz); - char* __result = (char*) - _Alloc::reallocate(__real_p, __old_sz + (int) _S_extra, - __new_sz + (int) _S_extra); + char* __result = (char*) _Alloc::reallocate(__real_p, + __old_sz + (int) _S_extra, + __new_sz + (int) _S_extra); *(size_t*)__result = __new_sz; return __result + (int) _S_extra; } @@ -362,7 +368,7 @@ namespace std static size_t _S_freelist_index(size_t __bytes) - { return (((__bytes) + (size_t)_ALIGN-1)/(size_t)_ALIGN - 1); } + { return (((__bytes) + (size_t)_ALIGN - 1)/(size_t)_ALIGN - 1); } // Returns an object of size __n, and optionally adds to size __n // free list. @@ -402,7 +408,7 @@ namespace std else __atomic_add(&_S_force_new, -1); // Trust but verify... - assert (_S_force_new != 0); + assert(_S_force_new != 0); } if ((__n > (size_t) _MAX_BYTES) || (_S_force_new > 0)) @@ -416,13 +422,15 @@ namespace std // unwinding. _Lock __lock_instance; _Obj* __restrict__ __result = *__my_free_list; - if (__result == 0) + if (__builtin_expect(__result == 0, 0)) __ret = _S_refill(_S_round_up(__n)); else { *__my_free_list = __result -> _M_free_list_link; __ret = __result; - } + } + if (__builtin_expect(__ret == 0, 0)) + __throw_bad_alloc(); } return __ret; } @@ -510,7 +518,7 @@ namespace std *__my_free_list = (_Obj*)_S_start_free; } _S_start_free = (char*) __new_alloc::allocate(__bytes_to_get); - if (0 == _S_start_free) + if (_S_start_free == 0) { size_t __i; _Obj* volatile* __my_free_list; @@ -523,7 +531,7 @@ namespace std { __my_free_list = _S_free_list + _S_freelist_index(__i); __p = *__my_free_list; - if (0 != __p) + if (__p != 0) { *__my_free_list = __p -> _M_free_list_link; _S_start_free = (char*)__p; @@ -569,17 +577,17 @@ namespace std *__my_free_list = __next_obj = (_Obj*)(__chunk + __n); for (__i = 1; ; __i++) { - __current_obj = __next_obj; + __current_obj = __next_obj; __next_obj = (_Obj*)((char*)__next_obj + __n); - if (__nobjs - 1 == __i) - { - __current_obj -> _M_free_list_link = 0; - break; - } - else - __current_obj -> _M_free_list_link = __next_obj; - } - return(__result); + if (__nobjs - 1 == __i) + { + __current_obj -> _M_free_list_link = 0; + break; + } + else + __current_obj -> _M_free_list_link = __next_obj; + } + return __result; } @@ -600,7 +608,7 @@ namespace std __copy_sz = __new_sz > __old_sz? __old_sz : __new_sz; memcpy(__result, __p, __copy_sz); deallocate(__p, __old_sz); - return(__result); + return __result; } #endif @@ -669,13 +677,20 @@ namespace std const_pointer address(const_reference __x) const { return &__x; } - // __n is permitted to be 0. The C++ standard says nothing about what - // the return value is when __n == 0. + // NB: __n is permitted to be 0. The C++ standard says nothing + // about what the return value is when __n == 0. _Tp* allocate(size_type __n, const void* = 0) { - return __n != 0 - ? static_cast<_Tp*>(_Alloc::allocate(__n * sizeof(_Tp))) : 0; + _Tp* __ret = 0; + if (__n) + { + if (__n <= this->max_size()) + __ret = static_cast<_Tp*>(_Alloc::allocate(__n * sizeof(_Tp))); + else + __throw_bad_alloc(); + } + return __ret; } // __p is not permitted to be a null pointer. @@ -719,12 +734,13 @@ namespace std /** * @if maint - * Allocator adaptor to turn an "SGI" style allocator (e.g., __alloc, - * __malloc_alloc_template) into a "standard" conforming allocator. Note - * that this adaptor does *not* assume that all objects of the underlying - * alloc class are identical, nor does it assume that all of the underlying - * alloc's member functions are static member functions. Note, also, that - * __allocator<_Tp, __alloc> is essentially the same thing as allocator<_Tp>. + * Allocator adaptor to turn an "SGI" style allocator (e.g., + * __alloc, __malloc_alloc_template) into a "standard" conforming + * allocator. Note that this adaptor does *not* assume that all + * objects of the underlying alloc class are identical, nor does it + * assume that all of the underlying alloc's member functions are + * static member functions. Note, also, that __allocator<_Tp, + * __alloc> is essentially the same thing as allocator<_Tp>. * @endif * (See @link Allocators allocators info @endlink for more.) */ @@ -732,7 +748,7 @@ namespace std struct __allocator { _Alloc __underlying_alloc; - + typedef size_t size_type; typedef ptrdiff_t difference_type; typedef _Tp* pointer; @@ -761,29 +777,31 @@ namespace std const_pointer address(const_reference __x) const { return &__x; } - // __n is permitted to be 0. - _Tp* - allocate(size_type __n, const void* = 0) - { - return __n != 0 - ? static_cast<_Tp*>(__underlying_alloc.allocate(__n * sizeof(_Tp))) - : 0; - } - - // __p is not permitted to be a null pointer. - void - deallocate(pointer __p, size_type __n) - { __underlying_alloc.deallocate(__p, __n * sizeof(_Tp)); } - - size_type - max_size() const throw() { return size_t(-1) / sizeof(_Tp); } - - void - construct(pointer __p, const _Tp& __val) { new(__p) _Tp(__val); } + // NB: __n is permitted to be 0. The C++ standard says nothing + // about what the return value is when __n == 0. + _Tp* + allocate(size_type __n, const void* = 0) + { + _Tp* __ret = 0; + if (__n) + __ret = static_cast<_Tp*>(_Alloc::allocate(__n * sizeof(_Tp))); + return __ret; + } - void - destroy(pointer __p) { __p->~_Tp(); } - }; + // __p is not permitted to be a null pointer. + void + deallocate(pointer __p, size_type __n) + { __underlying_alloc.deallocate(__p, __n * sizeof(_Tp)); } + + size_type + max_size() const throw() { return size_t(-1) / sizeof(_Tp); } + + void + construct(pointer __p, const _Tp& __val) { new(__p) _Tp(__val); } + + void + destroy(pointer __p) { __p->~_Tp(); } + }; template<typename _Alloc> struct __allocator<void, _Alloc> |

