File: GSpanExtras.h

package info (click to toggle)
webkit2gtk 2.48.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 429,620 kB
  • sloc: cpp: 3,696,936; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,276; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (147 lines) | stat: -rw-r--r-- 4,643 bytes parent folder | download | duplicates (6)
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
/*
 *  Copyright (C) 2024 Igalia S.L.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#pragma once

#include <wtf/MallocSpan.h>
#include <wtf/StdLibExtras.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/GUniquePtr.h>

extern "C" {
void* g_malloc(size_t);
void* g_malloc0(size_t);
void* g_realloc(void*, size_t);
void* g_try_malloc(size_t);
void* g_try_malloc0(size_t);
void* g_try_realloc(void*, size_t);
void g_strfreev(char**);
}

namespace WTF {

struct GMalloc {
    static void* malloc(size_t size) { return g_malloc(size); }
    static void* tryMalloc(size_t size) { return g_try_malloc(size); }
    static void* zeroedMalloc(size_t size) { return g_malloc0(size); }
    static void* tryZeroedMalloc(size_t size) { return g_try_malloc0(size); }
    static void* realloc(void* ptr, size_t size) { return g_realloc(ptr, size); }
    static void* tryRealloc(void* ptr, size_t size) { return g_try_realloc(ptr, size); }
    static void free(void* ptr) { g_free(ptr); }

    static constexpr ALWAYS_INLINE size_t nextCapacity(size_t capacity)
    {
        return capacity + capacity / 4 + 1;
    }
};

struct GMallocStrv {
    static void free(char** ptr) { g_strfreev(ptr); }
};

template <typename T, typename Malloc = GMalloc>
using GMallocSpan = MallocSpan<T, Malloc>;

template<typename T, typename Malloc = GMalloc>
GMallocSpan<T, Malloc> adoptGMallocSpan(std::span<T> span)
{
    return adoptMallocSpan<T, Malloc>(span);
}

template<typename Malloc = GMalloc>
GMallocSpan<char, Malloc> adoptGMallocString(char* str, size_t length)
{
    return adoptGMallocSpan<char, Malloc>(unsafeMakeSpan(str, length));
}

template<typename Malloc = GMalloc>
GMallocSpan<char, Malloc> adoptGMallocString(char* str)
{
    return adoptGMallocSpan<char, Malloc>(unsafeMakeSpan(str, str ? strlen(str) : 0));
}

WTF_EXPORT_PRIVATE GMallocSpan<char> gFileGetContents(const char* path, GUniqueOutPtr<GError>&);
WTF_EXPORT_PRIVATE GMallocSpan<char*, GMallocStrv> gKeyFileGetKeys(GKeyFile*, const char* groupName, GUniqueOutPtr<GError>&);
WTF_EXPORT_PRIVATE GMallocSpan<GParamSpec*> gObjectClassGetProperties(GObjectClass*);
WTF_EXPORT_PRIVATE GMallocSpan<const char*> gVariantGetStrv(const GRefPtr<GVariant>&);

inline std::span<const uint8_t> span(GBytes* bytes)
{
    size_t size = 0;
    const auto* ptr = static_cast<const uint8_t*>(g_bytes_get_data(bytes, &size));
    return unsafeMakeSpan<const uint8_t>(ptr, size);
}

inline std::span<const uint8_t> span(const GRefPtr<GBytes>& bytes)
{
    return span(bytes.get());
}

inline std::span<const uint8_t> span(GByteArray* array)
{
    return unsafeMakeSpan<const uint8_t>(array->data, array->len);
}

inline std::span<const uint8_t> span(const GRefPtr<GByteArray>& array)
{
    return span(array.get());
}

inline std::span<const uint8_t> span(GVariant* variant)
{
    const auto* ptr = static_cast<const uint8_t*>(g_variant_get_data(variant));
    size_t size = g_variant_get_size(variant);
    return unsafeMakeSpan<const uint8_t>(ptr, size);
}

inline std::span<const uint8_t> span(const GRefPtr<GVariant>& variant)
{
    return span(variant.get());
}

static inline std::span<char*> span(char** strv)
{
    auto size = g_strv_length(strv);
    return unsafeMakeSpan(strv, size);
}

template <typename T = void*, typename = std::enable_if_t<std::is_pointer_v<T>>>
inline std::span<T> span(GPtrArray* array)
{
    if (!array)
        return unsafeMakeSpan<T>(nullptr, 0);

    return unsafeMakeSpan(static_cast<T*>(static_cast<void*>(array->pdata)), array->len);
}

template <typename T = void*, typename = std::enable_if_t<std::is_pointer_v<T>>>
inline std::span<T> span(GRefPtr<GPtrArray>& array)
{
    return span<T>(array.get());
}

} // namespace WTF

using WTF::GMallocSpan;
using WTF::adoptGMallocString;
using WTF::gFileGetContents;
using WTF::gKeyFileGetKeys;
using WTF::gObjectClassGetProperties;
using WTF::gVariantGetStrv;
using WTF::span;