From d9c2690705ee6c1e478c3b1531f352d379668edc Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sun, 27 Jan 2019 21:59:28 +0100 Subject: [PATCH] src: turn ROUND_UP into an inline function PR-URL: https://github.com/nodejs/node/pull/25743 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell Reviewed-By: Ben Noordhuis Reviewed-By: Tiancheng "Timothy" Gu --- src/node_http2.cc | 6 +++--- src/spawn_sync.cc | 4 ++-- src/util.h | 8 +++++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/node_http2.cc b/src/node_http2.cc index 11698d177ad082..7904c680b89369 100644 --- a/src/node_http2.cc +++ b/src/node_http2.cc @@ -382,7 +382,7 @@ Headers::Headers(Isolate* isolate, header_string_len); // Make sure the start address is aligned appropriately for an nghttp2_nv*. char* start = reinterpret_cast( - ROUND_UP(reinterpret_cast(*buf_), alignof(nghttp2_nv))); + RoundUp(reinterpret_cast(*buf_), alignof(nghttp2_nv))); char* header_contents = start + (count_ * sizeof(nghttp2_nv)); nghttp2_nv* const nva = reinterpret_cast(start); @@ -438,8 +438,8 @@ Origins::Origins(Isolate* isolate, // Make sure the start address is aligned appropriately for an nghttp2_nv*. char* start = reinterpret_cast( - ROUND_UP(reinterpret_cast(*buf_), - alignof(nghttp2_origin_entry))); + RoundUp(reinterpret_cast(*buf_), + alignof(nghttp2_origin_entry))); char* origin_contents = start + (count_ * sizeof(nghttp2_origin_entry)); nghttp2_origin_entry* const nva = reinterpret_cast(start); diff --git a/src/spawn_sync.cc b/src/spawn_sync.cc index 962fafa91f30b5..0dbf5f09fc78bf 100644 --- a/src/spawn_sync.cc +++ b/src/spawn_sync.cc @@ -1049,7 +1049,7 @@ Maybe SyncProcessRunner::CopyJsStringArray(Local js_value, Maybe maybe_size = StringBytes::StorageSize(isolate, value, UTF8); if (maybe_size.IsNothing()) return Nothing(); data_size += maybe_size.FromJust() + 1; - data_size = ROUND_UP(data_size, sizeof(void*)); + data_size = RoundUp(data_size, sizeof(void*)); } buffer = new char[list_size + data_size]; @@ -1066,7 +1066,7 @@ Maybe SyncProcessRunner::CopyJsStringArray(Local js_value, value, UTF8); buffer[data_offset++] = '\0'; - data_offset = ROUND_UP(data_offset, sizeof(void*)); + data_offset = RoundUp(data_offset, sizeof(void*)); } list[length] = nullptr; diff --git a/src/util.h b/src/util.h index ea705b800bdf17..f3a174821ab882 100644 --- a/src/util.h +++ b/src/util.h @@ -617,9 +617,11 @@ constexpr size_t arraysize(const T (&)[N]) { return N; } -#ifndef ROUND_UP -#define ROUND_UP(a, b) ((a) % (b) ? ((a) + (b)) - ((a) % (b)) : (a)) -#endif +// Round up a to the next highest multiple of b. +template +constexpr T RoundUp(T a, T b) { + return a % b != 0 ? a + b - (a % b) : a; +} #ifdef __GNUC__ #define MUST_USE_RESULT __attribute__((warn_unused_result))