diff options
author | Anna Henningsen <anna@addaleax.net> | 2016-09-10 19:19:24 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2016-09-29 10:46:42 +0300 |
commit | ea94086ad2b53268b5cb870f9ba5a1f84741fa41 (patch) | |
tree | 71a385cacb45a15d7d573105911b93ff0f5f2e11 /src/util-inl.h | |
parent | 48ed65440ca33fe6063d92379b3a9b10b65e98e9 (diff) |
src: provide allocation + nullptr check shortcuts
Provide shortcut `node::CheckedMalloc()` and friends that
replace `node::Malloc()` + `CHECK_NE(ยท, nullptr);` combinations
in a few places.
PR-URL: https://github.com/nodejs/node/pull/8482
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
Diffstat (limited to 'src/util-inl.h')
-rw-r--r-- | src/util-inl.h | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/src/util-inl.h b/src/util-inl.h index 3a5c7622c73..a3d446c2a51 100644 --- a/src/util-inl.h +++ b/src/util-inl.h @@ -245,7 +245,7 @@ inline size_t MultiplyWithOverflowCheck(size_t a, size_t b) { // nullptr for zero-sized allocation requests. Normalize by always using // a nullptr. template <typename T> -T* Realloc(T* pointer, size_t n) { +T* UncheckedRealloc(T* pointer, size_t n) { size_t full_size = MultiplyWithOverflowCheck(sizeof(T), n); if (full_size == 0) { @@ -258,18 +258,39 @@ T* Realloc(T* pointer, size_t n) { // As per spec realloc behaves like malloc if passed nullptr. template <typename T> -T* Malloc(size_t n) { +T* UncheckedMalloc(size_t n) { if (n == 0) n = 1; - return Realloc<T>(nullptr, n); + return UncheckedRealloc<T>(nullptr, n); } template <typename T> -T* Calloc(size_t n) { +T* UncheckedCalloc(size_t n) { if (n == 0) n = 1; MultiplyWithOverflowCheck(sizeof(T), n); return static_cast<T*>(calloc(n, sizeof(T))); } +template <typename T> +T* Realloc(T* pointer, size_t n) { + T* ret = UncheckedRealloc(pointer, n); + if (n > 0) CHECK_NE(ret, nullptr); + return ret; +} + +template <typename T> +T* Malloc(size_t n) { + T* ret = UncheckedMalloc<T>(n); + if (n > 0) CHECK_NE(ret, nullptr); + return ret; +} + +template <typename T> +T* Calloc(size_t n) { + T* ret = UncheckedCalloc<T>(n); + if (n > 0) CHECK_NE(ret, nullptr); + return ret; +} + } // namespace node #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |