summaryrefslogtreecommitdiffstats
path: root/source/core/slang-blob.h
blob: 5277ef0cd95a5faeaf202b61512a8fe05b1fb477 (plain)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#ifndef SLANG_CORE_BLOB_H
#define SLANG_CORE_BLOB_H

#include "../core/slang-com-object.h"
#include "slang-com-helper.h"
#include "slang-com-ptr.h"
#include "slang-list.h"
#include "slang-string.h"
#include "slang.h"

#include <stdarg.h>

namespace Slang
{

/** Base class for simple blobs.
 */
class BlobBase : public ComBaseObject, public ISlangBlob, public ICastable
{
public:
    // ISlangUnknown
    SLANG_COM_BASE_IUNKNOWN_ALL

    // ICastable
    virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const SlangUUID& guid) SLANG_OVERRIDE;

protected:
    ISlangUnknown* getInterface(const Guid& guid);
    void* getObject(const Guid& guid);
};

/** A blob that uses a `StringRepresentation` for its storage.

By design the StringBlob owns a unique reference to the StringRepresentation.
This is because StringBlob, implements an interface which should work across threads.
*/
class StringBlob : public BlobBase
{
public:
    SLANG_CLASS_GUID(0xf7e0e93c, 0xde70, 0x4531, {0x9c, 0x9f, 0xdd, 0xa3, 0xf6, 0xc6, 0xc0, 0xdd});

    // ICastable
    virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const SlangUUID& guid) SLANG_OVERRIDE;

    // ISlangBlob
    SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE
    {
        return m_slice.begin();
    }
    SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_slice.getLength(); }

    /// Since in is not being moved will *always* create a new representation, unless the in is
    /// empty
    static ComPtr<ISlangBlob> create(const String& in);
    /// Create from a slice
    static ComPtr<ISlangBlob> create(const UnownedStringSlice& slice);

    /// Moves from in into the created blob.
    /// NOTE! That will only use the representation from in, if it is *unique*
    /// otherwise it will make a new copy.
    static ComPtr<ISlangBlob> moveCreate(String& in);
    static ComPtr<ISlangBlob> moveCreate(String&& in);

    /// Dtor
    ~StringBlob();

protected:
    /// Init with a rep when can't be owned.
    void _setWithCopy(StringRepresentation* rep);
    /// Init with a representation that has been moved.
    void _setWithMove(StringRepresentation* rep);

    /// Create a unique copy of rep.
    /// If nullptr will work (if rep is empty, will return that)
    static StringRepresentation* _createUniqueCopy(StringRepresentation* rep);

    /// Rep can only be nullptr or have a single ref
    void _setUniqueRep(StringRepresentation* rep);

    void* getObject(const Guid& guid);

    UnownedTerminatedStringSlice m_slice; ///< The contents
    StringRepresentation* m_uniqueRep =
        nullptr; ///< Holds actual bytes. Can be nullptr if it's an empty string.
};

class ListBlob : public BlobBase
{
public:
    typedef BlobBase Super;
    typedef ListBlob ThisType;

    // ICastable
    virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const SlangUUID& guid) SLANG_OVERRIDE;
    // ISlangBlob
    SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE
    {
        return m_data.getBuffer();
    }
    SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_data.getCount(); }

    static ComPtr<ISlangBlob> create(const List<uint8_t>& data)
    {
        return ComPtr<ISlangBlob>(new ListBlob(data));
    }

    static ComPtr<ISlangBlob> moveCreate(List<uint8_t>& data)
    {
        return ComPtr<ISlangBlob>(new ListBlob(_Move(data)));
    }

protected:
    explicit ListBlob(const List<uint8_t>& data)
        : m_data(data)
    {
    }
    // Move ctor
    explicit ListBlob(List<uint8_t>&& data)
        : m_data(data)
    {
    }

    void* getObject(const Guid& guid);

    void operator=(const ThisType& rhs) = delete;

    List<uint8_t> m_data;
};

class ScopedAllocation
{
public:
    typedef ScopedAllocation ThisType;
    // Returns the allocation if successful.
    void* allocate(size_t size)
    {
        deallocate();
        if (size > 0)
        {
            m_data = ::malloc(size);
        }
        m_sizeInBytes = size;
        m_capacityInBytes = size;
        return m_data;
    }
    /// Allocate size including a 0 byte at `size`.
    void* allocateTerminated(size_t size)
    {
        SLANG_ASSUME(size != std::numeric_limits<size_t>::max());
        uint8_t* data = (uint8_t*)allocate(size + 1);
        data[size] = 0;
        m_sizeInBytes = size;
        return data;
    }
    /// Deallocates if holds an allocation
    void deallocate()
    {
        if (m_data)
        {
            ::free(m_data);
            m_data = nullptr;
        }
        m_sizeInBytes = 0;
        m_capacityInBytes = 0;
    }
    // Reallocate so the buffer is the specified capacity/size. Contents of buffer up to size remain
    // intact.
    void reallocate(size_t capacity)
    {
        if (capacity != m_capacityInBytes)
        {
            m_data = ::realloc(m_data, capacity);
            m_sizeInBytes = capacity;
            m_capacityInBytes = capacity;
        }
    }
    /// Makes this no longer own the allocation. Returns the allocated data (or nullptr if no
    /// allocation)
    void* detach()
    {
        void* data = m_data;
        m_data = nullptr;
        m_sizeInBytes = 0;
        m_capacityInBytes = 0;
        return data;
    }
    /// Attach some data.
    /// NOTE! data must be a pointer that was returned from malloc, otherwise will incorrectly free.
    void attach(void* data, size_t size)
    {
        deallocate();
        m_data = data;
        m_sizeInBytes = size;
        m_capacityInBytes = size;
    }

    void* set(const void* data, size_t size)
    {
        void* dst = allocate(size);
        if (dst)
        {
            memcpy(dst, data, size);
        }
        return dst;
    }

    /// Get the allocated data. Returns nullptr if there is no allocated data
    void* getData() const { return m_data; }
    /// Get the size of the allocated data.
    size_t getSizeInBytes() const { return m_sizeInBytes; }
    /// Get the capacity in bytes
    size_t getCapacityInBytes() const { return m_capacityInBytes; }

    void setSizeInBytes(size_t size)
    {
        SLANG_ASSERT(size <= m_capacityInBytes);
        m_sizeInBytes = size;
    }

    void swap(ThisType& rhs)
    {
        Swap(m_data, rhs.m_data);
        Swap(m_sizeInBytes, rhs.m_sizeInBytes);
        Swap(m_capacityInBytes, rhs.m_capacityInBytes);
    }

    /// True if has zero termination, at the byte at m_sizeInBytes
    bool isTerminated() const
    {
        return m_capacityInBytes > m_sizeInBytes && ((const char*)m_data)[m_sizeInBytes] == 0;
    }

    ScopedAllocation()
        : m_data(nullptr), m_sizeInBytes(0), m_capacityInBytes(0)
    {
    }

    ~ScopedAllocation() { deallocate(); }

private:
    // disable
    ScopedAllocation(const ThisType& rhs) = delete;
    void operator=(const ThisType& rhs) = delete;

    void* m_data;
    size_t m_sizeInBytes;
    size_t m_capacityInBytes;
};

/** A blob that manages some raw data that it owns.
 */
class RawBlob : public BlobBase
{
public:
    // ICastable
    virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const SlangUUID& guid) SLANG_OVERRIDE;
    // ISlangBlob
    SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE
    {
        return m_data.getData();
    }
    SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE
    {
        return m_data.getSizeInBytes();
    }

    static ComPtr<ISlangBlob> moveCreate(ScopedAllocation& alloc)
    {
        RawBlob* blob = new RawBlob;
        blob->m_data.swap(alloc);
        return ComPtr<ISlangBlob>(blob);
    }

    /// Create a blob that will retain (a copy of) raw data.
    static inline ComPtr<ISlangBlob> create(void const* inData, size_t size)
    {
        return ComPtr<ISlangBlob>(new RawBlob(inData, size));
    }

protected:
    // Ctor
    // NOTE! Takes a copy of the input data
    RawBlob(const void* data, size_t size) { memcpy(m_data.allocateTerminated(size), data, size); }

    void* getObject(const Guid& guid);

    RawBlob() = default;

    ScopedAllocation m_data;
};

// A blob that does not own it's contained data.
class UnownedRawBlob : public BlobBase
{
public:
    // ISlangBlob
    SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE { return m_data; }
    SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_dataSizeInBytes; }

    static inline ComPtr<ISlangBlob> create(void const* inData, size_t size)
    {
        return ComPtr<ISlangBlob>(new UnownedRawBlob(inData, size));
    }

protected:
    // Ctor
    UnownedRawBlob(const void* data, size_t size)
        : m_data(data), m_dataSizeInBytes(size)
    {
    }

    UnownedRawBlob() = default;

    const void* m_data;
    size_t m_dataSizeInBytes;
};

/** A Blob that has no ref counting and exists typically for entire execution.
The memory it references is *not* owned by the blob.
This is useful when a Blob is useful to represent some global immutable chunk of memory.
*/
class StaticBlob : public ISlangBlob, public ICastable
{
public:
    // ISlangUnknown
    SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface(SlangUUID const& uuid, void** outObject)
        SLANG_OVERRIDE;
    SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE { return 1; }
    SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE { return 1; }

    // ICastable
    virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const SlangUUID& guid) SLANG_OVERRIDE;

    // ISlangBlob
    SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE { return m_data; }
    SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE { return m_dataCount; }

    StaticBlob(const void* data, size_t dataCount)
        : m_data(data), m_dataCount(dataCount)
    {
    }

protected:
    ISlangUnknown* getInterface(const Guid& guid);
    void* getObject(const Guid& guid);

    const void* m_data;
    size_t m_dataCount;
};

class ScopeBlob : public BlobBase
{
public:
    // ICastable
    virtual SLANG_NO_THROW void* SLANG_MCALL castAs(const SlangUUID& guid) SLANG_OVERRIDE;

    // ISlangBlob
    SLANG_NO_THROW void const* SLANG_MCALL getBufferPointer() SLANG_OVERRIDE
    {
        return m_blob->getBufferPointer();
    }
    SLANG_NO_THROW size_t SLANG_MCALL getBufferSize() SLANG_OVERRIDE
    {
        return m_blob->getBufferSize();
    }

    static inline ComPtr<ISlangBlob> create(ISlangBlob* blob, ISlangUnknown* scope)
    {
        return ComPtr<ISlangBlob>(new ScopeBlob(blob, scope));
    }

protected:
    // Ctor
    ScopeBlob(ISlangBlob* blob, ISlangUnknown* scope)
        : m_blob(blob), m_scope(scope)
    {
        // Cache the ICastable interface if there is one.
        blob->queryInterface(SLANG_IID_PPV_ARGS(m_castable.writeRef()));
    }

    ComPtr<ISlangUnknown> m_scope;
    ComPtr<ISlangBlob> m_blob;
    ComPtr<ICastable>
        m_castable; ///< Set if the blob has this interface. Set to nullptr if does not.
};

} // namespace Slang

#endif // SLANG_CORE_BLOB_H