Skip to content

Commit

Permalink
src: deprecate smalloc public functions
Browse files Browse the repository at this point in the history
Upcoming V8 changes will make it impossible to keep supporting the
smalloc functionality so deprecate the functions in smalloc.h now
and tell people to switch to typed arrays.

This commit shuffles code around in smalloc.cc to avoid generating
the deprecation warnings when building io.js itself.
  • Loading branch information
bnoordhuis committed Apr 29, 2015
1 parent 19e4d98 commit 18b2738
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 61 deletions.
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

0 comments on commit 18b2738

Please sign in to comment.