(file) Return to ArrayRep.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 karl  1.14 //%2006////////////////////////////////////////////////////////////////////////
  2 kumpf 1.1  //
  3 karl  1.8  // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  4            // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  5            // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  6 karl  1.6  // IBM Corp.; EMC Corporation, The Open Group.
  7 karl  1.8  // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8            // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9 karl  1.9  // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10            // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 karl  1.14 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12            // EMC Corporation; Symantec Corporation; The Open Group.
 13 kumpf 1.1  //
 14            // Permission is hereby granted, free of charge, to any person obtaining a copy
 15 kumpf 1.2  // of this software and associated documentation files (the "Software"), to
 16            // deal in the Software without restriction, including without limitation the
 17            // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 18 kumpf 1.1  // sell copies of the Software, and to permit persons to whom the Software is
 19            // furnished to do so, subject to the following conditions:
 20            // 
 21 kumpf 1.2  // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 22 kumpf 1.1  // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 23            // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 24 kumpf 1.2  // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 25            // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 26            // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 27 kumpf 1.1  // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 28            // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 29            //
 30            //==============================================================================
 31            //
 32            //%/////////////////////////////////////////////////////////////////////////////
 33            
 34            #ifndef Pegasus_ArrayRep_h
 35            #define Pegasus_ArrayRep_h
 36            
 37            #include <new>
 38            #include <Pegasus/Common/Config.h>
 39            #include <Pegasus/Common/Memory.h>
 40 mike  1.11 #include <Pegasus/Common/AtomicInt.h>
 41            #include <Pegasus/Common/Linkage.h>
 42            
 43            #define Array_rep (static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep))
 44            #define Array_size (Array_rep)->size
 45            #define Array_data (Array_rep)->data()
 46            #define Array_capacity (Array_rep)->capacity
 47            #define Array_refs (Array_rep)->refs
 48 kumpf 1.1  
 49            PEGASUS_NAMESPACE_BEGIN
 50            
 51 mike  1.11 struct PEGASUS_COMMON_LINKAGE ArrayRepBase
 52            {
 53                // We put this first to avoid gaps in this structure. Some compilers may
 54                // align it on a large boundary.
 55                AtomicInt refs;
 56            
 57                Uint32 size;
 58            
 59                union
 60                {
 61 kumpf 1.17         Uint32 capacity;
 62                    Uint64 alignment;
 63 mike  1.11     };
 64            
 65                // Called only only _empty_rep object. We set the reference count to
 66                // two and it will remain two for the lifetime of the process. Anything
 67                // other than one will do. If the _empty_rep.refs were one, an Array
 68                // object may think it is the sole owner and attempt to modify it.
 69            
 70                ArrayRepBase() : refs(2), size(0), capacity(0) { }
 71            
 72                static ArrayRepBase _empty_rep;
 73            };
 74            
 75 kumpf 1.1  /*  ArrayRep<T>
 76                The ArrayRep object represents the array size, capacity,
 77                and elements in one contiguous chunk of memory. The elements
 78                follow immediately after the end of the ArrayRep structure in memory.
 79                The union is used to force 64 bit alignment of these elements. This is
 80                a private class and should not be accessed directly by the user.
 81            */
 82            template<class T>
 83 mike  1.11 struct ArrayRep : public ArrayRepBase
 84 kumpf 1.1  {
 85                // Obtains a pointer to the first element in the array.
 86                T* data() { return (T*)(void*)(this + 1); }
 87            
 88                // Same as method above but returns a constant pointer.
 89                const T* data() const { return (const T*)(void*)(this + 1); }
 90            
 91 mike  1.11     /* Make a new copy */
 92                static ArrayRep<T>* copy_on_write(ArrayRep<T>* rep);
 93 kumpf 1.1  
 94                /* Create and initialize a ArrayRep instance. Note that the
 95                    memory for the elements is unitialized and must be initialized by
 96                    the caller.
 97                */
 98 marek 1.16     static ArrayRep<T>* alloc(Uint32 size);
 99 mike  1.11 
100                static void ref(const ArrayRep<T>* rep);
101 kumpf 1.1  
102 mike  1.11     static void unref(const ArrayRep<T>* rep);
103 kumpf 1.1  };
104            
105            template<class T>
106 marek 1.16 ArrayRep<T>* ArrayRep<T>::alloc(Uint32 size)
107 kumpf 1.1  {
108 mike  1.11     // ATTN-MEB: throw out raising to next power of two (put this
109                // logic in reserveCapacity().
110            
111                if (!size)
112 kumpf 1.17         return (ArrayRep<T>*)&ArrayRepBase::_empty_rep;
113 kumpf 1.1  
114                // Calculate capacity (size rounded to the next power of two).
115            
116 keith.petley 1.4      Uint32 initialCapacity = 8;
117 kumpf        1.1  
118 kumpf        1.5      while ((initialCapacity != 0) && (initialCapacity < size))
119                       {
120 keith.petley 1.4          initialCapacity <<= 1;
121 kumpf        1.5      }
122                   
123                       // Test for Uint32 overflow in the capacity
124                       if (initialCapacity == 0)
125                       {
126                           initialCapacity = size;
127                       }
128                   
129                       // Test for Uint32 overflow in the memory allocation size
130 dave.sudlik  1.15     // throw a bad_alloc exception if overflow would occur.
131 kumpf        1.5      if (initialCapacity > (Uint32(0xffffffff)-sizeof(ArrayRep<T>))/sizeof(T))
132                       {
133 dave.sudlik  1.15         throw PEGASUS_STD(bad_alloc)();
134 kumpf        1.5      }
135 kumpf        1.1  
136                       // Create object:
137                   
138 kumpf        1.5      ArrayRep<T>* rep = (ArrayRep<T>*)operator new(
139                           sizeof(ArrayRep<T>) + sizeof(T) * initialCapacity);
140 david        1.10 
141 kumpf        1.1      rep->size = size;
142 keith.petley 1.4      rep->capacity = initialCapacity;
143 mike         1.11     new(&rep->refs) AtomicInt(1);
144 kumpf        1.1  
145                       return rep;
146                   }
147                   
148                   template<class T>
149 mike         1.11 inline void ArrayRep<T>::ref(const ArrayRep<T>* rep)
150 kumpf        1.1  {
151 mike         1.12     if ((void*)rep != (void*)&ArrayRepBase::_empty_rep)
152 kumpf        1.17         ((ArrayRep<T>*)rep)->refs.inc();
153 mike         1.11 }
154                   
155                   template<class T>
156                   inline void ArrayRep<T>::unref(const ArrayRep<T>* rep_)
157                   {
158                       ArrayRep<T>* rep = (ArrayRep<T>*)rep_;
159                   
160                       if (rep != &ArrayRepBase::_empty_rep && rep->refs.decAndTestIfZero())
161 kumpf        1.1      {
162                           Destroy(rep->data(), rep->size);
163 kumpf        1.17         rep->refs.~AtomicInt();
164                           ::operator delete(rep);
165 mike         1.11     }
166                   }
167 david        1.10 
168 mike         1.11 template<class T>
169                   ArrayRep<T>* ArrayRep<T>::copy_on_write(ArrayRep<T>* rep)
170                   {
171                       ArrayRep<T>* new_rep = ArrayRep<T>::alloc(rep->size);
172                       new_rep->size = rep->size;
173                       CopyToRaw(new_rep->data(), rep->data(), rep->size);
174                       unref(rep);
175                       return new_rep;
176 kumpf        1.1  }
177                   
178                   PEGASUS_NAMESPACE_END
179                   
180                   #endif /* Pegasus_ArrayRep_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2