Skip to content

Commit 4715a41

Browse files
addaleaxcodebytere
authored andcommitted
src: simplify alignment-handling code
Use a common function to handle alignment computations in multiple places. PR-URL: #33884 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: Tobias Nießen <[email protected]>
1 parent 894ec7d commit 4715a41

File tree

5 files changed

+14
-15
lines changed

5 files changed

+14
-15
lines changed

src/node_http2.cc

+1-3
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,7 @@ Origins::Origins(
373373
origin_string_len);
374374

375375
// Make sure the start address is aligned appropriately for an nghttp2_nv*.
376-
char* start = reinterpret_cast<char*>(
377-
RoundUp(reinterpret_cast<uintptr_t>(buf_.data()),
378-
alignof(nghttp2_origin_entry)));
376+
char* start = AlignUp(buf_.data(), alignof(nghttp2_origin_entry));
379377
char* origin_contents = start + (count_ * sizeof(nghttp2_origin_entry));
380378
nghttp2_origin_entry* const nva =
381379
reinterpret_cast<nghttp2_origin_entry*>(start);

src/node_http_common-inl.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ NgHeaders<T>::NgHeaders(Environment* env, v8::Local<v8::Array> headers) {
3131
count_ * sizeof(nv_t) +
3232
header_string_len);
3333

34-
char* start = reinterpret_cast<char*>(
35-
RoundUp(reinterpret_cast<uintptr_t>(*buf_), alignof(nv_t)));
34+
char* start = AlignUp(buf_.out(), alignof(nv_t));
3635
char* header_contents = start + (count_ * sizeof(nv_t));
3736
nv_t* const nva = reinterpret_cast<nv_t*>(start);
3837

src/string_bytes.cc

+2-4
Original file line numberDiff line numberDiff line change
@@ -273,16 +273,14 @@ size_t StringBytes::WriteUCS2(Isolate* isolate,
273273
return 0;
274274
}
275275

276+
uint16_t* const aligned_dst = AlignUp(dst, sizeof(*dst));
276277
size_t nchars;
277-
size_t alignment = reinterpret_cast<uintptr_t>(dst) % sizeof(*dst);
278-
if (alignment == 0) {
278+
if (aligned_dst == dst) {
279279
nchars = str->Write(isolate, dst, 0, max_chars, flags);
280280
*chars_written = nchars;
281281
return nchars * sizeof(*dst);
282282
}
283283

284-
uint16_t* aligned_dst =
285-
reinterpret_cast<uint16_t*>(buf + sizeof(*dst) - alignment);
286284
CHECK_EQ(reinterpret_cast<uintptr_t>(aligned_dst) % sizeof(*dst), 0);
287285

288286
// Write all but the last char

src/util-inl.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,7 @@ void SwapBytes16(char* data, size_t nbytes) {
208208
CHECK_EQ(nbytes % 2, 0);
209209

210210
#if defined(_MSC_VER)
211-
int align = reinterpret_cast<uintptr_t>(data) % sizeof(uint16_t);
212-
if (align == 0) {
211+
if (AlignUp(data, sizeof(uint16_t)) == data) {
213212
// MSVC has no strict aliasing, and is able to highly optimize this case.
214213
uint16_t* data16 = reinterpret_cast<uint16_t*>(data);
215214
size_t len16 = nbytes / sizeof(*data16);
@@ -232,9 +231,8 @@ void SwapBytes32(char* data, size_t nbytes) {
232231
CHECK_EQ(nbytes % 4, 0);
233232

234233
#if defined(_MSC_VER)
235-
int align = reinterpret_cast<uintptr_t>(data) % sizeof(uint32_t);
236234
// MSVC has no strict aliasing, and is able to highly optimize this case.
237-
if (align == 0) {
235+
if (AlignUp(data, sizeof(uint32_t)) == data) {
238236
uint32_t* data32 = reinterpret_cast<uint32_t*>(data);
239237
size_t len32 = nbytes / sizeof(*data32);
240238
for (size_t i = 0; i < len32; i++) {
@@ -256,8 +254,7 @@ void SwapBytes64(char* data, size_t nbytes) {
256254
CHECK_EQ(nbytes % 8, 0);
257255

258256
#if defined(_MSC_VER)
259-
int align = reinterpret_cast<uintptr_t>(data) % sizeof(uint64_t);
260-
if (align == 0) {
257+
if (AlignUp(data, sizeof(uint64_t)) == data) {
261258
// MSVC has no strict aliasing, and is able to highly optimize this case.
262259
uint64_t* data64 = reinterpret_cast<uint64_t*>(data);
263260
size_t len64 = nbytes / sizeof(*data64);

src/util.h

+7
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,13 @@ constexpr T RoundUp(T a, T b) {
719719
return a % b != 0 ? a + b - (a % b) : a;
720720
}
721721

722+
// Align ptr to an `alignment`-bytes boundary.
723+
template <typename T, typename U>
724+
constexpr T* AlignUp(T* ptr, U alignment) {
725+
return reinterpret_cast<T*>(
726+
RoundUp(reinterpret_cast<uintptr_t>(ptr), alignment));
727+
}
728+
722729
class SlicedArguments : public MaybeStackBuffer<v8::Local<v8::Value>> {
723730
public:
724731
inline explicit SlicedArguments(

0 commit comments

Comments
 (0)