Path: blob/main/contrib/llvm-project/libcxx/src/include/sso_allocator.h
35230 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP_SSO_ALLOCATOR_H10#define _LIBCPP_SSO_ALLOCATOR_H1112#include <__config>13#include <cstddef>14#include <memory>15#include <new>16#include <type_traits>1718#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19# pragma GCC system_header20#endif2122_LIBCPP_BEGIN_NAMESPACE_STD2324template <class _Tp, size_t _Np>25class _LIBCPP_HIDDEN __sso_allocator;2627template <size_t _Np>28class _LIBCPP_HIDDEN __sso_allocator<void, _Np> {29public:30typedef const void* const_pointer;31typedef void value_type;32};3334template <class _Tp, size_t _Np>35class _LIBCPP_HIDDEN __sso_allocator {36alignas(_Tp) std::byte buf_[sizeof(_Tp) * _Np];37bool __allocated_;3839public:40typedef size_t size_type;41typedef _Tp* pointer;42typedef _Tp value_type;4344template <class U>45struct rebind {46using other = __sso_allocator<U, _Np>;47};4849_LIBCPP_HIDE_FROM_ABI __sso_allocator() throw() : __allocated_(false) {}50_LIBCPP_HIDE_FROM_ABI __sso_allocator(const __sso_allocator&) throw() : __allocated_(false) {}51template <class _Up>52_LIBCPP_HIDE_FROM_ABI __sso_allocator(const __sso_allocator<_Up, _Np>&) throw() : __allocated_(false) {}5354private:55__sso_allocator& operator=(const __sso_allocator&);5657public:58_LIBCPP_HIDE_FROM_ABI pointer allocate(size_type __n, typename __sso_allocator<void, _Np>::const_pointer = nullptr) {59if (!__allocated_ && __n <= _Np) {60__allocated_ = true;61return (pointer)&buf_;62}63return allocator<_Tp>().allocate(__n);64}65_LIBCPP_HIDE_FROM_ABI void deallocate(pointer __p, size_type __n) {66if (__p == (pointer)&buf_)67__allocated_ = false;68else69allocator<_Tp>().deallocate(__p, __n);70}71_LIBCPP_HIDE_FROM_ABI size_type max_size() const throw() { return size_type(~0) / sizeof(_Tp); }7273_LIBCPP_HIDE_FROM_ABI bool operator==(const __sso_allocator& __a) const { return &buf_ == &__a.buf_; }74_LIBCPP_HIDE_FROM_ABI bool operator!=(const __sso_allocator& __a) const { return &buf_ != &__a.buf_; }75};7677_LIBCPP_END_NAMESPACE_STD7879#endif // _LIBCPP_SSO_ALLOCATOR_H808182