Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate smalloc functions #1565

Merged
merged 3 commits into from
Apr 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,16 @@
#include "v8.h" // NOLINT(build/include_order)
#include "node_version.h" // NODE_MODULE_VERSION

#define NODE_DEPRECATED(msg, fn) V8_DEPRECATED(msg, fn)
#if defined(__GNUC__)
# define NODE_DEPRECATED(message, declarator) \
__attribute__((deprecated(message))) declarator
#elif defined(_MSC_VER)
# define NODE_DEPRECATED(message, declarator) \
__declspec(deprecated) declarator
#else
# define NODE_DEPRECATED(message, declarator) \
declarator
#endif

// Forward-declare libuv loop
struct uv_loop_s;
Expand Down
2 changes: 1 addition & 1 deletion src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Local<Object> New(Isolate* isolate, Handle<String> string, enum encoding enc) {

size_t length = StringBytes::Size(isolate, string, enc);

Local<Object> buf = New(length);
Local<Object> buf = New(isolate, length);
char* data = Buffer::Data(buf);
StringBytes::Write(isolate, data, length, string, enc);

Expand Down
2 changes: 1 addition & 1 deletion src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4593,7 +4593,7 @@ void RandomBytesCheck(RandomBytesRequest* req, Local<Value> argv[2]) {
size_t size;
req->return_memory(&data, &size);
argv[0] = Null(req->env()->isolate());
argv[1] = Buffer::Use(data, size);
argv[1] = Buffer::Use(req->env()->isolate(), data, size);
}
}

Expand Down
67 changes: 36 additions & 31 deletions src/smalloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,37 @@ void CallbackInfo::WeakCallback(
}


inline size_t InternalExternalArraySize(enum ExternalArrayType type) {
switch (type) {
case v8::kExternalUint8Array:
return sizeof(uint8_t);
case v8::kExternalInt8Array:
return sizeof(int8_t);
case v8::kExternalInt16Array:
return sizeof(int16_t);
case v8::kExternalUint16Array:
return sizeof(uint16_t);
case v8::kExternalInt32Array:
return sizeof(int32_t);
case v8::kExternalUint32Array:
return sizeof(uint32_t);
case v8::kExternalFloat32Array:
return sizeof(float); // NOLINT(runtime/sizeof)
case v8::kExternalFloat64Array:
return sizeof(double); // NOLINT(runtime/sizeof)
case v8::kExternalUint8ClampedArray:
return sizeof(uint8_t);
}
return 0;
}


void CallbackInfo::WeakCallback(Isolate* isolate, Local<Object> object) {
void* array_data = object->GetIndexedPropertiesExternalArrayData();
size_t array_length = object->GetIndexedPropertiesExternalArrayDataLength();
enum ExternalArrayType array_type =
object->GetIndexedPropertiesExternalArrayDataType();
size_t array_size = ExternalArraySize(array_type);
size_t array_size = InternalExternalArraySize(array_type);
CHECK_GT(array_size, 0);
if (array_size > 1 && array_data != NULL) {
CHECK_GT(array_length * array_size, array_length); // Overflow check.
Expand All @@ -152,27 +177,7 @@ void CallbackInfo::WeakCallback(Isolate* isolate, Local<Object> object) {

// return size of external array type, or 0 if unrecognized
size_t ExternalArraySize(enum ExternalArrayType type) {
switch (type) {
case v8::kExternalUint8Array:
return sizeof(uint8_t);
case v8::kExternalInt8Array:
return sizeof(int8_t);
case v8::kExternalInt16Array:
return sizeof(int16_t);
case v8::kExternalUint16Array:
return sizeof(uint16_t);
case v8::kExternalInt32Array:
return sizeof(int32_t);
case v8::kExternalUint32Array:
return sizeof(uint32_t);
case v8::kExternalFloat32Array:
return sizeof(float); // NOLINT(runtime/sizeof)
case v8::kExternalFloat64Array:
return sizeof(double); // NOLINT(runtime/sizeof)
case v8::kExternalUint8ClampedArray:
return sizeof(uint8_t);
}
return 0;
return InternalExternalArraySize(type);
}


Expand Down Expand Up @@ -200,12 +205,12 @@ void CopyOnto(const FunctionCallbackInfo<Value>& args) {
size_t source_length = source->GetIndexedPropertiesExternalArrayDataLength();
enum ExternalArrayType source_type =
source->GetIndexedPropertiesExternalArrayDataType();
size_t source_size = ExternalArraySize(source_type);
size_t source_size = InternalExternalArraySize(source_type);

size_t dest_length = dest->GetIndexedPropertiesExternalArrayDataLength();
enum ExternalArrayType dest_type =
dest->GetIndexedPropertiesExternalArrayDataType();
size_t dest_size = ExternalArraySize(dest_type);
size_t dest_size = InternalExternalArraySize(dest_type);

// optimization for Uint8 arrays (i.e. Buffers)
if (source_size != 1 || dest_size != 1) {
Expand Down Expand Up @@ -261,7 +266,7 @@ void SliceOnto(const FunctionCallbackInfo<Value>& args) {
size_t source_len = source->GetIndexedPropertiesExternalArrayDataLength();
enum ExternalArrayType source_type =
source->GetIndexedPropertiesExternalArrayDataType();
size_t source_size = ExternalArraySize(source_type);
size_t source_size = InternalExternalArraySize(source_type);

CHECK_NE(source_size, 0);

Expand Down Expand Up @@ -304,7 +309,7 @@ void Alloc(const FunctionCallbackInfo<Value>& args) {
array_type = kExternalUint8Array;
} else {
array_type = static_cast<ExternalArrayType>(args[2]->Uint32Value());
size_t type_length = ExternalArraySize(array_type);
size_t type_length = InternalExternalArraySize(array_type);
CHECK_GE(type_length * length, length);
length *= type_length;
}
Expand All @@ -318,7 +323,7 @@ void Alloc(Environment* env,
Handle<Object> obj,
size_t length,
enum ExternalArrayType type) {
size_t type_size = ExternalArraySize(type);
size_t type_size = InternalExternalArraySize(type);

CHECK_LE(length, kMaxLength);
CHECK_GT(type_size, 0);
Expand All @@ -345,7 +350,7 @@ void Alloc(Environment* env,
enum ExternalArrayType type) {
CHECK_EQ(false, obj->HasIndexedPropertiesInExternalArrayData());
env->isolate()->AdjustAmountOfExternalAllocatedMemory(length);
size_t size = length / ExternalArraySize(type);
size_t size = length / InternalExternalArraySize(type);
obj->SetIndexedPropertiesToExternalArrayData(data, type, size);
CallbackInfo::New(env->isolate(),
CallbackInfo::kInternal,
Expand Down Expand Up @@ -378,7 +383,7 @@ void AllocDispose(Environment* env, Handle<Object> obj) {
size_t length = obj->GetIndexedPropertiesExternalArrayDataLength();
enum ExternalArrayType array_type =
obj->GetIndexedPropertiesExternalArrayDataType();
size_t array_size = ExternalArraySize(array_type);
size_t array_size = InternalExternalArraySize(array_type);

CHECK_GT(array_size, 0);
CHECK_GE(length * array_size, length);
Expand Down Expand Up @@ -412,7 +417,7 @@ static void Alloc(Environment* env,
env->set_using_smalloc_alloc_cb(true);
CallbackInfo* info = CallbackInfo::New(isolate, ownership, obj, fn, hint);
obj->SetHiddenValue(env->smalloc_p_string(), External::New(isolate, info));
size_t size = length / ExternalArraySize(type);
size_t size = length / InternalExternalArraySize(type);
obj->SetIndexedPropertiesToExternalArrayData(data, type, size);
}

Expand All @@ -425,7 +430,7 @@ void Alloc(Environment* env,
enum ExternalArrayType type) {
CHECK_LE(length, kMaxLength);

size_t type_size = ExternalArraySize(type);
size_t type_size = InternalExternalArraySize(type);

CHECK_GT(type_size, 0);
CHECK_GE(length * type_size, length);
Expand Down
75 changes: 45 additions & 30 deletions src/smalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ NODE_EXTERN typedef void (*FreeCallback)(char* data, void* hint);
/**
* Return byte size of external array type.
*/
NODE_EXTERN size_t ExternalArraySize(enum v8::ExternalArrayType type);
NODE_DEPRECATED(
"Use typed arrays",
NODE_EXTERN size_t ExternalArraySize(enum v8::ExternalArrayType type));

/**
* Allocate external array data onto obj.
Expand Down Expand Up @@ -55,45 +57,58 @@ NODE_EXTERN size_t ExternalArraySize(enum v8::ExternalArrayType type);
* v8::Integer::NewFromUnsigned(array_length));
* \code
*/
NODE_EXTERN void Alloc(v8::Isolate* isolate,
v8::Handle<v8::Object> obj,
size_t length,
enum v8::ExternalArrayType type =
v8::kExternalUnsignedByteArray);
NODE_EXTERN void Alloc(v8::Isolate* isolate,
v8::Handle<v8::Object> obj,
char* data,
size_t length,
enum v8::ExternalArrayType type =
v8::kExternalUnsignedByteArray);
NODE_EXTERN void Alloc(v8::Isolate* isolate,
v8::Handle<v8::Object> obj,
size_t length,
FreeCallback fn,
void* hint,
enum v8::ExternalArrayType type =
v8::kExternalUnsignedByteArray);
NODE_EXTERN void Alloc(v8::Isolate* isolate,
v8::Handle<v8::Object> obj,
char* data,
size_t length,
FreeCallback fn,
void* hint,
enum v8::ExternalArrayType type =
v8::kExternalUnsignedByteArray);
NODE_DEPRECATED(
"Use typed arrays",
NODE_EXTERN void Alloc(v8::Isolate* isolate,
v8::Handle<v8::Object> obj,
size_t length,
enum v8::ExternalArrayType type =
v8::kExternalUnsignedByteArray));
NODE_DEPRECATED(
"Use typed arrays",
NODE_EXTERN void Alloc(v8::Isolate* isolate,
v8::Handle<v8::Object> obj,
char* data,
size_t length,
enum v8::ExternalArrayType type =
v8::kExternalUnsignedByteArray));
NODE_DEPRECATED(
"Use typed arrays",
NODE_EXTERN void Alloc(v8::Isolate* isolate,
v8::Handle<v8::Object> obj,
size_t length,
FreeCallback fn,
void* hint,
enum v8::ExternalArrayType type =
v8::kExternalUnsignedByteArray));
NODE_DEPRECATED(
"Use typed arrays",
NODE_EXTERN void Alloc(v8::Isolate* isolate,
v8::Handle<v8::Object> obj,
char* data,
size_t length,
FreeCallback fn,
void* hint,
enum v8::ExternalArrayType type =
v8::kExternalUnsignedByteArray));

/**
* Free memory associated with an externally allocated object. If no external
* memory is allocated to the object then nothing will happen.
*/
NODE_EXTERN void AllocDispose(v8::Isolate* isolate, v8::Handle<v8::Object> obj);
NODE_DEPRECATED(
"Use typed arrays",
NODE_EXTERN void AllocDispose(v8::Isolate* isolate,
v8::Handle<v8::Object> obj));


/**
* Check if the Object has externally allocated memory.
*/
NODE_EXTERN bool HasExternalData(v8::Isolate* isolate,
v8::Local<v8::Object> obj);
NODE_DEPRECATED(
"Use typed arrays",
NODE_EXTERN bool HasExternalData(v8::Isolate* isolate,
v8::Local<v8::Object> obj));


// Internal use
Expand Down
6 changes: 3 additions & 3 deletions src/spawn_sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ void SyncProcessStdioPipe::Close() {
}


Local<Object> SyncProcessStdioPipe::GetOutputAsBuffer() const {
Local<Object> SyncProcessStdioPipe::GetOutputAsBuffer(Isolate* isolate) const {
size_t length = OutputLength();
Local<Object> js_buffer = Buffer::New(length);
Local<Object> js_buffer = Buffer::New(isolate, length);
CopyOutput(Buffer::Data(js_buffer));
return js_buffer;
}
Expand Down Expand Up @@ -679,7 +679,7 @@ Local<Array> SyncProcessRunner::BuildOutputArray() {
for (uint32_t i = 0; i < stdio_count_; i++) {
SyncProcessStdioPipe* h = stdio_pipes_[i];
if (h != nullptr && h->writable())
js_output->Set(i, h->GetOutputAsBuffer());
js_output->Set(i, h->GetOutputAsBuffer(env()->isolate()));
else
js_output->Set(i, Null(env()->isolate()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/spawn_sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class SyncProcessStdioPipe {
int Start();
void Close();

Local<Object> GetOutputAsBuffer() const;
Local<Object> GetOutputAsBuffer(Isolate* isolate) const;

inline bool readable() const;
inline bool writable() const;
Expand Down
2 changes: 1 addition & 1 deletion src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Utf8Value::Utf8Value(v8::Isolate* isolate, v8::Handle<v8::Value> value)
return;

// Allocate enough space to include the null terminator
size_t len = StringBytes::StorageSize(string, UTF8) + 1;
size_t len = StringBytes::StorageSize(isolate, string, UTF8) + 1;
if (len > sizeof(str_st_)) {
str_ = static_cast<char*>(malloc(len));
CHECK_NE(str_, nullptr);
Expand Down