Skip to content

Commit d2470d4

Browse files
addaleaxjasnell
authored andcommitted
src: pass desired return type to allocators
Pass the desired return type directly to the allocation functions, so that the resulting `static_cast` from `void*` becomes unneccessary and the return type can be use as a reasonable default value for the `size` parameter. PR-URL: #8482 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Ilkka Myller <[email protected]>
1 parent de94601 commit d2470d4

11 files changed

+63
-50
lines changed

src/cares_wrap.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ static void ares_poll_close_cb(uv_handle_t* watcher) {
174174

175175
/* Allocates and returns a new node_ares_task */
176176
static node_ares_task* ares_task_create(Environment* env, ares_socket_t sock) {
177-
node_ares_task* task =
178-
static_cast<node_ares_task*>(node::Malloc(sizeof(*task)));
177+
auto task = node::Malloc<node_ares_task>(1);
179178

180179
if (task == nullptr) {
181180
/* Out of memory. */

src/node.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ Local<Value> WinapiErrnoException(Isolate* isolate,
979979

980980
void* ArrayBufferAllocator::Allocate(size_t size) {
981981
if (zero_fill_field_ || zero_fill_all_buffers)
982-
return node::Calloc(size, 1);
982+
return node::Calloc(size);
983983
else
984984
return node::Malloc(size);
985985
}

src/node_buffer.cc

+13-7
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@
4949
THROW_AND_RETURN_IF_OOB(end <= end_max); \
5050
size_t length = end - start;
5151

52-
#define BUFFER_MALLOC(length) \
53-
zero_fill_all_buffers ? node::Calloc(length, 1) : node::Malloc(length)
54-
5552
#if defined(__GNUC__) || defined(__clang__)
5653
#define BSWAP_INTRINSIC_2(x) __builtin_bswap16(x)
5754
#define BSWAP_INTRINSIC_4(x) __builtin_bswap32(x)
@@ -89,6 +86,15 @@ namespace node {
8986
// if true, all Buffer and SlowBuffer instances will automatically zero-fill
9087
bool zero_fill_all_buffers = false;
9188

89+
namespace {
90+
91+
inline void* BufferMalloc(size_t length) {
92+
return zero_fill_all_buffers ? node::Calloc(length) :
93+
node::Malloc(length);
94+
}
95+
96+
} // namespace
97+
9298
namespace Buffer {
9399

94100
using v8::ArrayBuffer;
@@ -266,7 +272,7 @@ MaybeLocal<Object> New(Isolate* isolate,
266272
char* data = nullptr;
267273

268274
if (length > 0) {
269-
data = static_cast<char*>(BUFFER_MALLOC(length));
275+
data = static_cast<char*>(BufferMalloc(length));
270276

271277
if (data == nullptr)
272278
return Local<Object>();
@@ -278,7 +284,7 @@ MaybeLocal<Object> New(Isolate* isolate,
278284
free(data);
279285
data = nullptr;
280286
} else if (actual < length) {
281-
data = static_cast<char*>(node::Realloc(data, actual));
287+
data = node::Realloc(data, actual);
282288
CHECK_NE(data, nullptr);
283289
}
284290
}
@@ -312,7 +318,7 @@ MaybeLocal<Object> New(Environment* env, size_t length) {
312318

313319
void* data;
314320
if (length > 0) {
315-
data = BUFFER_MALLOC(length);
321+
data = BufferMalloc(length);
316322
if (data == nullptr)
317323
return Local<Object>();
318324
} else {
@@ -1080,7 +1086,7 @@ void IndexOfString(const FunctionCallbackInfo<Value>& args) {
10801086
offset,
10811087
is_forward);
10821088
} else if (enc == LATIN1) {
1083-
uint8_t* needle_data = static_cast<uint8_t*>(node::Malloc(needle_length));
1089+
uint8_t* needle_data = node::Malloc<uint8_t>(needle_length);
10841090
if (needle_data == nullptr) {
10851091
return args.GetReturnValue().Set(-1);
10861092
}

src/node_crypto.cc

+10-11
Original file line numberDiff line numberDiff line change
@@ -2279,7 +2279,7 @@ int SSLWrap<Base>::TLSExtStatusCallback(SSL* s, void* arg) {
22792279
size_t len = Buffer::Length(obj);
22802280

22812281
// OpenSSL takes control of the pointer after accepting it
2282-
char* data = reinterpret_cast<char*>(node::Malloc(len));
2282+
char* data = node::Malloc(len);
22832283
CHECK_NE(data, nullptr);
22842284
memcpy(data, resp, len);
22852285

@@ -3330,7 +3330,7 @@ bool CipherBase::GetAuthTag(char** out, unsigned int* out_len) const {
33303330
if (initialised_ || kind_ != kCipher || !auth_tag_)
33313331
return false;
33323332
*out_len = auth_tag_len_;
3333-
*out = static_cast<char*>(node::Malloc(auth_tag_len_));
3333+
*out = node::Malloc(auth_tag_len_);
33343334
CHECK_NE(*out, nullptr);
33353335
memcpy(*out, auth_tag_, auth_tag_len_);
33363336
return true;
@@ -4906,7 +4906,7 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
49064906
// NOTE: field_size is in bits
49074907
int field_size = EC_GROUP_get_degree(ecdh->group_);
49084908
size_t out_len = (field_size + 7) / 8;
4909-
char* out = static_cast<char*>(node::Malloc(out_len));
4909+
char* out = node::Malloc(out_len);
49104910
CHECK_NE(out, nullptr);
49114911

49124912
int r = ECDH_compute_key(out, out_len, pub, ecdh->key_, nullptr);
@@ -4942,7 +4942,7 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
49424942
if (size == 0)
49434943
return env->ThrowError("Failed to get public key length");
49444944

4945-
unsigned char* out = static_cast<unsigned char*>(node::Malloc(size));
4945+
unsigned char* out = node::Malloc<unsigned char>(size);
49464946
CHECK_NE(out, nullptr);
49474947

49484948
int r = EC_POINT_point2oct(ecdh->group_, pub, form, out, size, nullptr);
@@ -4968,7 +4968,7 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
49684968
return env->ThrowError("Failed to get ECDH private key");
49694969

49704970
int size = BN_num_bytes(b);
4971-
unsigned char* out = static_cast<unsigned char*>(node::Malloc(size));
4971+
unsigned char* out = node::Malloc<unsigned char>(size);
49724972
CHECK_NE(out, nullptr);
49734973

49744974
if (size != BN_bn2bin(b, out)) {
@@ -5099,7 +5099,7 @@ class PBKDF2Request : public AsyncWrap {
50995099
saltlen_(saltlen),
51005100
salt_(salt),
51015101
keylen_(keylen),
5102-
key_(static_cast<char*>(node::Malloc(keylen))),
5102+
key_(node::Malloc(keylen)),
51035103
iter_(iter) {
51045104
if (key() == nullptr)
51055105
FatalError("node::PBKDF2Request()", "Out of Memory");
@@ -5262,7 +5262,7 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
52625262

52635263
THROW_AND_RETURN_IF_NOT_BUFFER(args[1], "Salt");
52645264

5265-
pass = static_cast<char*>(node::Malloc(passlen));
5265+
pass = node::Malloc(passlen);
52665266
if (pass == nullptr) {
52675267
FatalError("node::PBKDF2()", "Out of Memory");
52685268
}
@@ -5274,7 +5274,7 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
52745274
goto err;
52755275
}
52765276

5277-
salt = static_cast<char*>(node::Malloc(saltlen));
5277+
salt = node::Malloc(saltlen);
52785278
if (salt == nullptr) {
52795279
FatalError("node::PBKDF2()", "Out of Memory");
52805280
}
@@ -5367,7 +5367,7 @@ class RandomBytesRequest : public AsyncWrap {
53675367
: AsyncWrap(env, object, AsyncWrap::PROVIDER_CRYPTO),
53685368
error_(0),
53695369
size_(size),
5370-
data_(static_cast<char*>(node::Malloc(size))) {
5370+
data_(node::Malloc(size)) {
53715371
if (data() == nullptr)
53725372
FatalError("node::RandomBytesRequest()", "Out of Memory");
53735373
Wrap(object, this);
@@ -5594,8 +5594,7 @@ void GetCurves(const FunctionCallbackInfo<Value>& args) {
55945594
EC_builtin_curve* curves;
55955595

55965596
if (num_curves) {
5597-
curves = static_cast<EC_builtin_curve*>(node::Malloc(sizeof(*curves),
5598-
num_curves));
5597+
curves = node::Malloc<EC_builtin_curve>(num_curves);
55995598

56005599
CHECK_NE(curves, nullptr);
56015600

src/stream_wrap.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ void StreamWrap::OnAlloc(uv_handle_t* handle,
148148

149149

150150
void StreamWrap::OnAllocImpl(size_t size, uv_buf_t* buf, void* ctx) {
151-
buf->base = static_cast<char*>(node::Malloc(size));
151+
buf->base = node::Malloc(size);
152152
buf->len = size;
153153

154154
if (buf->base == nullptr && size > 0) {
@@ -204,7 +204,7 @@ void StreamWrap::OnReadImpl(ssize_t nread,
204204
return;
205205
}
206206

207-
char* base = static_cast<char*>(node::Realloc(buf->base, nread));
207+
char* base = node::Realloc(buf->base, nread);
208208
CHECK_LE(static_cast<size_t>(nread), buf->len);
209209

210210
if (pending == UV_TCP) {

src/string_bytes.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ class ExternString: public ResourceType {
5353
if (length == 0)
5454
return scope.Escape(String::Empty(isolate));
5555

56-
TypeName* new_data =
57-
static_cast<TypeName*>(node::Malloc(length, sizeof(*new_data)));
56+
TypeName* new_data = node::Malloc<TypeName>(length);
5857
if (new_data == nullptr) {
5958
return Local<String>();
6059
}
@@ -624,7 +623,7 @@ Local<Value> StringBytes::Encode(Isolate* isolate,
624623

625624
case ASCII:
626625
if (contains_non_ascii(buf, buflen)) {
627-
char* out = static_cast<char*>(node::Malloc(buflen));
626+
char* out = node::Malloc(buflen);
628627
if (out == nullptr) {
629628
return Local<String>();
630629
}
@@ -659,7 +658,7 @@ Local<Value> StringBytes::Encode(Isolate* isolate,
659658

660659
case BASE64: {
661660
size_t dlen = base64_encoded_size(buflen);
662-
char* dst = static_cast<char*>(node::Malloc(dlen));
661+
char* dst = node::Malloc(dlen);
663662
if (dst == nullptr) {
664663
return Local<String>();
665664
}
@@ -678,7 +677,7 @@ Local<Value> StringBytes::Encode(Isolate* isolate,
678677

679678
case HEX: {
680679
size_t dlen = buflen * 2;
681-
char* dst = static_cast<char*>(node::Malloc(dlen));
680+
char* dst = node::Malloc(dlen);
682681
if (dst == nullptr) {
683682
return Local<String>();
684683
}

src/tls_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ void TLSWrap::OnReadImpl(ssize_t nread,
661661

662662

663663
void TLSWrap::OnAllocSelf(size_t suggested_size, uv_buf_t* buf, void* ctx) {
664-
buf->base = static_cast<char*>(node::Malloc(suggested_size));
664+
buf->base = node::Malloc(suggested_size);
665665
CHECK_NE(buf->base, nullptr);
666666
buf->len = suggested_size;
667667
}

src/udp_wrap.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ void UDPWrap::OnSend(uv_udp_send_t* req, int status) {
374374
void UDPWrap::OnAlloc(uv_handle_t* handle,
375375
size_t suggested_size,
376376
uv_buf_t* buf) {
377-
buf->base = static_cast<char*>(node::Malloc(suggested_size));
377+
buf->base = node::Malloc(suggested_size);
378378
buf->len = suggested_size;
379379

380380
if (buf->base == nullptr && suggested_size > 0) {
@@ -416,7 +416,7 @@ void UDPWrap::OnRecv(uv_udp_t* handle,
416416
return;
417417
}
418418

419-
char* base = static_cast<char*>(node::Realloc(buf->base, nread));
419+
char* base = node::Realloc(buf->base, nread);
420420
argv[2] = Buffer::New(env, base, nread).ToLocalChecked();
421421
argv[3] = AddressToJS(env, addr);
422422
wrap->MakeCallback(env->onmessage_string(), arraysize(argv), argv);

src/util-inl.h

+11-10
Original file line numberDiff line numberDiff line change
@@ -244,29 +244,30 @@ inline size_t MultiplyWithOverflowCheck(size_t a, size_t b) {
244244
// that the standard allows them to either return a unique pointer or a
245245
// nullptr for zero-sized allocation requests. Normalize by always using
246246
// a nullptr.
247-
void* Realloc(void* pointer, size_t n, size_t size) {
248-
size_t full_size = MultiplyWithOverflowCheck(size, n);
247+
template <typename T>
248+
T* Realloc(T* pointer, size_t n) {
249+
size_t full_size = MultiplyWithOverflowCheck(sizeof(T), n);
249250

250251
if (full_size == 0) {
251252
free(pointer);
252253
return nullptr;
253254
}
254255

255-
return realloc(pointer, full_size);
256+
return static_cast<T*>(realloc(pointer, full_size));
256257
}
257258

258259
// As per spec realloc behaves like malloc if passed nullptr.
259-
void* Malloc(size_t n, size_t size) {
260+
template <typename T>
261+
T* Malloc(size_t n) {
260262
if (n == 0) n = 1;
261-
if (size == 0) size = 1;
262-
return Realloc(nullptr, n, size);
263+
return Realloc<T>(nullptr, n);
263264
}
264265

265-
void* Calloc(size_t n, size_t size) {
266+
template <typename T>
267+
T* Calloc(size_t n) {
266268
if (n == 0) n = 1;
267-
if (size == 0) size = 1;
268-
MultiplyWithOverflowCheck(size, n);
269-
return calloc(n, size);
269+
MultiplyWithOverflowCheck(sizeof(T), n);
270+
return static_cast<T*>(calloc(n, sizeof(T)));
270271
}
271272

272273
} // namespace node

src/util.h

+11-4
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,16 @@ namespace node {
3131
// that the standard allows them to either return a unique pointer or a
3232
// nullptr for zero-sized allocation requests. Normalize by always using
3333
// a nullptr.
34-
inline void* Realloc(void* pointer, size_t n, size_t size = 1);
35-
inline void* Malloc(size_t n, size_t size = 1);
36-
inline void* Calloc(size_t n, size_t size = 1);
34+
template <typename T>
35+
inline T* Realloc(T* pointer, size_t n);
36+
template <typename T>
37+
inline T* Malloc(size_t n);
38+
template <typename T>
39+
inline T* Calloc(size_t n);
40+
41+
// Shortcuts for char*.
42+
inline char* Malloc(size_t n) { return Malloc<char>(n); }
43+
inline char* Calloc(size_t n) { return Calloc<char>(n); }
3744

3845
#ifdef __GNUC__
3946
#define NO_RETURN __attribute__((noreturn))
@@ -298,7 +305,7 @@ class MaybeStackBuffer {
298305
if (storage <= kStackStorageSize) {
299306
buf_ = buf_st_;
300307
} else {
301-
buf_ = static_cast<T*>(Malloc(sizeof(T), storage));
308+
buf_ = Malloc<T>(storage);
302309
CHECK_NE(buf_, nullptr);
303310
}
304311

test/cctest/util.cc

+7-5
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,16 @@ TEST(UtilTest, ToLower) {
9292

9393
TEST(UtilTest, Malloc) {
9494
using node::Malloc;
95+
EXPECT_NE(nullptr, Malloc<char>(0));
96+
EXPECT_NE(nullptr, Malloc<char>(1));
9597
EXPECT_NE(nullptr, Malloc(0));
9698
EXPECT_NE(nullptr, Malloc(1));
9799
}
98100

99101
TEST(UtilTest, Calloc) {
100102
using node::Calloc;
101-
EXPECT_NE(nullptr, Calloc(0, 0));
102-
EXPECT_NE(nullptr, Calloc(1, 0));
103-
EXPECT_NE(nullptr, Calloc(0, 1));
104-
EXPECT_NE(nullptr, Calloc(1, 1));
105-
}
103+
EXPECT_NE(nullptr, Calloc<char>(0));
104+
EXPECT_NE(nullptr, Calloc<char>(1));
105+
EXPECT_NE(nullptr, Calloc(0));
106+
EXPECT_NE(nullptr, Calloc(1));
107+
}

0 commit comments

Comments
 (0)