Debian Codegraph @

gcc/d/dmd/common/smallbuffer.d

raw
1 /**
2 * Common string functions including filename manipulation.
3 *
4 * Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
5 * Authors: Walter Bright, https://www.digitalmars.com
6 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
7 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/common/smallbuffer.d, common/_smallbuffer.d)
8 * Documentation: https://dlang.org/phobos/dmd_common_smallbuffer.html
9 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/common/smallbuffer.d
10 */
11 module dmd.common.smallbuffer;
12
13 nothrow:
14
15 /**
16 Defines a temporary array of `Element`s using a fixed-length buffer as back store. If the length
17 of the buffer suffices, it is readily used. Otherwise, `malloc` is used to
18 allocate memory for the array and `free` is used for deallocation in the
19 destructor.
20
21 This type is meant to use exclusively as an automatic variable. It is not
22 default constructible or copyable.
23 */
24 struct SmallBuffer(Element)
25 {
26 import core.stdc.stdlib : malloc, free;
27
28 private Element[] _extent;
29 private bool needsFree;
30
31 nothrow:
32 @nogc:
33
34 @disable this(); // no default ctor
35 @disable this(ref const SmallBuffer!Element); // noncopyable, nonassignable
36
37 /***********
38 * Construct a SmallBuffer
39 * Params:
40 * len = number of elements in array
41 * buffer = slice to use as backing-store, if len will fit in it
42 */
43 scope this(size_t len, return scope Element[] buffer)
44 {
45 if (len <= buffer.length)
46 {
47 _extent = buffer[0 .. len];
48 }
49 else
50 {
51 assert(len < size_t.max / (2 * Element.sizeof));
52 _extent = (cast(typeof(_extent.ptr)) malloc(len * Element.sizeof))[0 .. len];
53 _extent.ptr || assert(0, "Out of memory.");
54 needsFree = true;
55 }
56 assert(this.length == len);
57 }
58
59 ~this()
60 {
61 if (needsFree)
62 free(_extent.ptr);
63 }
64
65 /******
66 * Resize existing SmallBuffer.
67 * Params:
68 * len = number of elements after resize
69 */
70 scope void create(size_t len)
71 {
72 if (len <= _extent.length)
73 {
74 _extent = _extent[0 .. len]; // reuse existing storage
75 }
76 else
77 {
78 __dtor();
79 assert(len < size_t.max / Element.sizeof);
80 _extent = (cast(typeof(_extent.ptr)) malloc(len * Element.sizeof))[0 .. len];
81 _extent.ptr || assert(0, "Out of memory.");
82 needsFree = true;
83 }
84 assert(this.length == len);
85 }
86
87 // Force accesses to extent to be scoped.
88 scope inout extent()
89 {
90 return _extent;
91 }
92
93 alias extent this;
94 }
95
96 /// ditto
97 unittest
98 {
99 char[230] buf = void;
100 auto a = SmallBuffer!char(10, buf);
101 assert(a[] is buf[0 .. 10]);
102 auto b = SmallBuffer!char(1000, buf);
103 assert(b[] !is buf[]);
104 b.create(1000);
105 assert(b.length == 1000);
106 assert(b[] !is buf[]);
107 }
108
109 /**
110 * (Windows only) Converts a narrow string to a wide string using `buffer` as strorage.
111 * Params:
112 * narrow = string to be converted
113 * buffer = where to place the converted string
114 * Returns: a slice of `buffer` containing the converted string. A zero follows the slice.
115 */
116 version(Windows) wchar[] toWStringz(scope const(char)[] narrow, ref SmallBuffer!wchar buffer) nothrow
117 {
118 if (narrow is null)
119 return null;
120
121 size_t charsToWchars(scope const(char)[] narrow, scope wchar[] buffer)
122 {
123 // https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-multibytetowidechar
124 import core.sys.windows.winnls : MultiByteToWideChar, CP_ACP;
125 return MultiByteToWideChar(CP_ACP, 0, narrow.ptr, cast(int) narrow.length, buffer.ptr, cast(int) buffer.length);
126 }
127
128 size_t length = charsToWchars(narrow, buffer[]);
129 if (length >= buffer.length) // not enough room in buffer[]
130 {
131 buffer.create(length + 1); // extend buffer length
132 length = charsToWchars(narrow, buffer[]); // try again
133 assert(length < buffer.length);
134 }
135 buffer[length] = 0;
136 return buffer[0 .. length];
137 }
138
139 /**************************************
140 * Converts a path to one suitable to be passed to Win32 API
141 * functions that can deal with paths longer than 248
142 * characters then calls the supplied function on it.
143 *
144 * Params:
145 * path = The Path to call F on.
146 *
147 * Returns:
148 * The result of calling F on path.
149 *
150 * References:
151 * https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
152 */
153 version(Windows) auto extendedPathThen(alias F)(const(char)[] path)
154 {
155 import core.sys.windows.winbase;
156 import core.sys.windows.winnt;
157
158 if (!path.length)
159 return F((wchar[]).init);
160
161 wchar[1024] buf = void;
162 auto store = SmallBuffer!wchar(buf.length, buf);
163 auto wpath = toWStringz(path, store);
164
165 // GetFullPathNameW expects a sized buffer to store the result in. Since we don't
166 // know how large it has to be, we pass in null and get the needed buffer length
167 // as the return code.
168 const pathLength = GetFullPathNameW(&wpath[0],
169 0 /*length8*/,
170 null /*output buffer*/,
171 null /*filePartBuffer*/);
172 if (pathLength == 0)
173 {
174 return F((wchar[]).init);
175 }
176
177 // wpath is the UTF16 version of path, but to be able to use
178 // extended paths, we need to prefix with `\\?\` and the absolute
179 // path.
180 static immutable prefix = `\\?\`w;
181
182 // prefix only needed for long names and non-UNC names
183 const needsPrefix = pathLength >= MAX_PATH && (wpath[0] != '\\' || wpath[1] != '\\');
184 const prefixLength = needsPrefix ? prefix.length : 0;
185
186 // +1 for the null terminator
187 const bufferLength = pathLength + prefixLength + 1;
188
189 wchar[1024] absBuf = void;
190 auto absPath = SmallBuffer!wchar(bufferLength, absBuf);
191
192 absPath[0 .. prefixLength] = prefix[0 .. prefixLength];
193
194 const absPathRet = GetFullPathNameW(&wpath[0],
195 cast(uint)(absPath.length - prefixLength - 1),
196 &absPath[prefixLength],
197 null /*filePartBuffer*/);
198
199 if (absPathRet == 0 || absPathRet > absPath.length - prefixLength)
200 {
201 return F((wchar[]).init);
202 }
203
204 absPath[$ - 1] = '\0';
205 // Strip null terminator from the slice
206 return F(absPath[0 .. $ - 1]);
207 }