Sierra Toolkit Version of the Day
libc_allocator_with_realloc.h
00001 // Copyright (c) 2010, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 // ---
00031 // Author: Guilin Chen
00032 
00033 #ifndef UTIL_GTL_LIBC_ALLOCATOR_WITH_REALLOC_H_
00034 #define UTIL_GTL_LIBC_ALLOCATOR_WITH_REALLOC_H_
00035 
00036 #include <stk_util/util/sparseconfig.h>
00037 
00038 #include <stdlib.h>           // for malloc/realloc/free
00039 #include <stddef.h>           // for ptrdiff_t
00040 
00041 
00042 _START_GOOGLE_NAMESPACE_
00043 
00044 template<class T>
00045 class libc_allocator_with_realloc {
00046  public:
00047   typedef T value_type;
00048   typedef size_t size_type;
00049   typedef ptrdiff_t difference_type;
00050 
00051   typedef T* pointer;
00052   typedef const T* const_pointer;
00053   typedef T& reference;
00054   typedef const T& const_reference;
00055 
00056   libc_allocator_with_realloc() {}
00057   libc_allocator_with_realloc(const libc_allocator_with_realloc&) {}
00058   ~libc_allocator_with_realloc() {}
00059 
00060   pointer address(reference r) const  { return &r; }
00061   const_pointer address(const_reference r) const  { return &r; }
00062 
00063   pointer allocate(size_type n, const_pointer = 0) {
00064     return static_cast<pointer>(malloc(n * sizeof(value_type)));
00065   }
00066   void deallocate(pointer p, size_type) {
00067     free(p);
00068   }
00069   pointer reallocate(pointer p, size_type n) {
00070     return static_cast<pointer>(realloc(p, n * sizeof(value_type)));
00071   }
00072 
00073   size_type max_size() const  {
00074     return static_cast<size_type>(-1) / sizeof(value_type);
00075   }
00076 
00077   void construct(pointer p, const value_type& val) {
00078     new(p) value_type(val);
00079   }
00080   void destroy(pointer p) { p->~value_type(); }
00081 
00082   template <class U>
00083   libc_allocator_with_realloc(const libc_allocator_with_realloc<U>&) {}
00084 
00085   template<class U>
00086   struct rebind {
00087     typedef libc_allocator_with_realloc<U> other;
00088   };
00089 };
00090 
00091 // libc_allocator_with_realloc<void> specialization.
00092 template<>
00093 class libc_allocator_with_realloc<void> {
00094  public:
00095   typedef void value_type;
00096   typedef size_t size_type;
00097   typedef ptrdiff_t difference_type;
00098   typedef void* pointer;
00099   typedef const void* const_pointer;
00100 
00101   template<class U>
00102   struct rebind {
00103     typedef libc_allocator_with_realloc<U> other;
00104   };
00105 };
00106 
00107 template<class T>
00108 inline bool operator==(const libc_allocator_with_realloc<T>&,
00109                        const libc_allocator_with_realloc<T>&) {
00110   return true;
00111 }
00112 
00113 template<class T>
00114 inline bool operator!=(const libc_allocator_with_realloc<T>&,
00115                        const libc_allocator_with_realloc<T>&) {
00116   return false;
00117 }
00118 
00119 _END_GOOGLE_NAMESPACE_
00120 
00121 #endif  // UTIL_GTL_LIBC_ALLOCATOR_WITH_REALLOC_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines