1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#ifndef VAR_LEN_TAG_HPP
#define VAR_LEN_TAG_HPP

#include <cstdlib>
#include <cstring>

namespace moab
{

/* Remove this preprocessor macro to compile
 * simple implementation w/ no inlined storage
 */
#define VAR_LEN_TAG_ELIDE_DATA

/* Define class data layout depending on macros:
 * VAR_LEN_TAG_ELIDE_DATA and TEMPLATE_SPECIALIZATION
 */
#ifndef VAR_LEN_TAG_ELIDE_DATA

/* The trivial implementation */
public
VarLenTagData
{
  public:
    struct
    {
        struct
        {
            unsigned size;
            unsigned char* array;
        } mPointer;
    } mData;
};

#elif !defined( MOAB_TEMPLATE_SPECIALIZATION )

/* A little more advanced data structure for VarLenTag.
 * If the amount of tag data is less than or equal to the
 * size of the array pointer, store it inline in the pointer
 * value field so no memory needs to be allocated.
 */
class VarLenTagData
{
  public:
    enum
    {
        INLINE_COUNT = sizeof( unsigned char* )
    };

    union
    {
        struct
        {
            unsigned char* array;
            unsigned size;
        } mPointer;
        struct
        {
            unsigned char array[INLINE_COUNT];
            unsigned size;
        } mInline;
    } mData;
};

#else

/* Most complex implementation.  Same as the previous one, except
 * when storing data inline, also utilize any padding in the struct.
 * This implementation requires support for template specialization.
 *
 * - The data must be first in the struct to avoid alignment issues
 *   for double and 64-bit handle values on some platforms.
 * - The size must therefore be at the end of the struct (including
 *   after any padding) because a) it cannot be at the beginning and
 *   b) it must be at the same location in both structs in the union.
 * - For the mPointer variation, the padding must be declared
 *   explicitly in order for the size to be forced to the end.
 * - Template specialization is used to avoid declaring a
 *   zero-length array for pad on 32-bit platforms.
 *   NOTE: GCC allows zero-length arrays, but Sun's compiler
 *   (and most others) do not.
 */
template < unsigned >
class VarLenTagDataTemplate
{
  public:
    inline VarLenTagDataTemplate() {}

    struct MallocData
    {
        unsigned char* array;
        unsigned size;
    };

    enum
    {
        INLINE_COUNT = sizeof( MallocData ) - sizeof( unsigned )
    };

    union
    {
        struct
        {
            unsigned char* array;
            unsigned char pad[INLINE_COUNT - sizeof( unsigned char* )];
            unsigned size;
        } mPointer;
        struct
        {
            unsigned char array[INLINE_COUNT];
            unsigned size;
        } mInline;
    } mData;
};

template <>
class VarLenTagDataTemplate< 0u >
{
  public:
    inline VarLenTagDataTemplate< 0u >() {}

    enum
    {
        INLINE_COUNT = sizeof( unsigned char* )
    };

    union
    {
        struct
        {
            unsigned char* array;
            unsigned size;
        } mPointer;
        struct
        {
            unsigned char array[INLINE_COUNT];
            unsigned size;
        } mInline;
    } mData;
};

typedef VarLenTagDataTemplate< sizeof( unsigned char* ) - sizeof( unsigned ) > VarLenTagData;

#endif

/**\brief Class for storing variable-length tag data
 *
 * Class for managing variable-length tag data.
 *\NOTE This class must behave as if it were initialized to empty
 *      if it is memset to zero w/out invoking any constructor.
 */
class VarLenTag
{
  protected:
    VarLenTagData mData;

  public:
    inline VarLenTag()
    {
        mData.mData.mPointer.size = 0;
    }
    inline VarLenTag( unsigned size );
    inline ~VarLenTag()
    {
        clear();
    }
    inline VarLenTag( const VarLenTag& copy );
    inline VarLenTag( unsigned size, const void* data );

    inline unsigned size() const
    {
        return mData.mData.mPointer.size;
    }

    inline unsigned char* data()
#ifdef VAR_LEN_TAG_ELIDE_DATA
    {
        return size() <= VarLenTagData::INLINE_COUNT ? mData.mData.mInline.array : mData.mData.mPointer.array;
    }
#else
    {
        return mData.mData.mPointer.array;
    }
#endif

    inline unsigned long mem() const
#ifdef VAR_LEN_TAG_ELIDE_DATA
    {
        return size() <= VarLenTagData::INLINE_COUNT ? 0 : size();
    }
#else
    {
        return size();
    }
#endif

    inline const unsigned char* data() const
    {
        return const_cast< VarLenTag* >( this )->data();
    }

    inline unsigned char* resize( unsigned size );

    inline void clear();

    inline void set( const void* dat, unsigned sz )
    {
        memcpy( resize( sz ), dat, sz );
    }

    inline VarLenTag& operator=( const VarLenTag& other )
    {
        set( other.data(), other.size() );
        return *this;
    }
};

inline unsigned char* VarLenTag::resize( unsigned s )
{
#ifdef VAR_LEN_TAG_ELIDE_DATA
    if( s <= VarLenTagData::INLINE_COUNT )
    {
        if( size() > VarLenTagData::INLINE_COUNT )
        {
            unsigned char* tmp_ptr = mData.mData.mPointer.array;
            memcpy( mData.mData.mInline.array, tmp_ptr, s );
            free( tmp_ptr );
        }
        mData.mData.mInline.size = s;
        return mData.mData.mInline.array;
    }
    else if( size() <= VarLenTagData::INLINE_COUNT )
    {
        void* tmp_ptr = malloc( s );
        memcpy( tmp_ptr, mData.mData.mInline.array, size() );
        mData.mData.mPointer.array = reinterpret_cast< unsigned char* >( tmp_ptr );
    }
    else
#endif
        if( size() < s )
    {
        void* tmp_ptr              = size() ? realloc( mData.mData.mPointer.array, s ) : malloc( s );
        mData.mData.mPointer.array = reinterpret_cast< unsigned char* >( tmp_ptr );
    }
    mData.mData.mPointer.size = s;
    return mData.mData.mPointer.array;
}

inline VarLenTag::VarLenTag( unsigned sz )
{
#ifdef VAR_LEN_TAG_ELIDE_DATA
    if( sz > VarLenTagData::INLINE_COUNT )
#endif
        mData.mData.mPointer.array = reinterpret_cast< unsigned char* >( malloc( sz ) );
    mData.mData.mPointer.size = sz;
}

inline void VarLenTag::clear()
{
#ifdef VAR_LEN_TAG_ELIDE_DATA
    if( size() > VarLenTagData::INLINE_COUNT )
#else
    if( size() )
#endif
        free( mData.mData.mPointer.array );
    mData.mData.mPointer.size = 0;
}

inline VarLenTag::VarLenTag( const VarLenTag& copy ) : mData( copy.mData )
{
#ifdef VAR_LEN_TAG_ELIDE_DATA
    if( size() > VarLenTagData::INLINE_COUNT )
#endif
    {
        mData.mData.mPointer.array = reinterpret_cast< unsigned char* >( malloc( size() ) );
        memcpy( mData.mData.mPointer.array, copy.mData.mData.mPointer.array, size() );
    }
}

inline VarLenTag::VarLenTag( unsigned sz, const void* dat )
{
    mData.mData.mPointer.size = 0;
    if( sz ) memcpy( resize( sz ), dat, sz );
}

}  // namespace moab

#endif