Showing with 46 additions and 0 deletions.
  1. +6 −0 doc/api/smalloc.markdown
  2. +1 −0 lib/smalloc.js
  3. +14 −0 src/smalloc.cc
  4. +6 −0 src/smalloc.h
  5. +19 −0 test/simple/test-smalloc.js
@@ -107,6 +107,12 @@ careful. Cryptic errors may arise in applications that are difficult to trace.

`dispose()` does not support Buffers, and will throw if passed.

## smalloc.hasExternalData(obj)

* `obj` {Object}

Returns `true` if the `obj` has externally allocated memory.

## smalloc.kMaxLength

Size of maximum allocation. This is also applicable to Buffer creation.
@@ -26,6 +26,7 @@ var util = require('util');
exports.alloc = alloc;
exports.copyOnto = smalloc.copyOnto;
exports.dispose = dispose;
exports.hasExternalData = smalloc.hasExternalData;

// don't allow kMaxLength to accidentally be overwritten. it's a lot less
// apparent when a primitive is accidentally changed.
@@ -23,6 +23,7 @@

#include "env.h"
#include "env-inl.h"
#include "node.h"
#include "node_internals.h"
#include "v8-profiler.h"
#include "v8.h"
@@ -402,6 +403,17 @@ void TargetFreeCallback(Isolate* isolate,
}


void HasExternalData(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(args[0]->IsObject() &&
HasExternalData(args[0].As<Object>()));
}


bool HasExternalData(Local<Object> obj) {
return obj->HasIndexedPropertiesInExternalArrayData();
}


class RetainedAllocInfo: public RetainedObjectInfo {
public:
explicit RetainedAllocInfo(Handle<Value> wrapper);
@@ -471,6 +483,8 @@ void Initialize(Handle<Object> exports,
NODE_SET_METHOD(exports, "alloc", Alloc);
NODE_SET_METHOD(exports, "dispose", AllocDispose);

NODE_SET_METHOD(exports, "hasExternalData", HasExternalData);

exports->Set(FIXED_ONE_BYTE_STRING(node_isolate, "kMaxLength"),
Uint32::NewFromUnsigned(kMaxLength, env->isolate()));

@@ -101,6 +101,12 @@ NODE_EXTERN void Alloc(v8::Handle<v8::Object> obj,
*/
NODE_EXTERN void AllocDispose(v8::Handle<v8::Object> obj);


/**
* Check if the Object has externally allocated memory.
*/
NODE_EXTERN bool HasExternalData(v8::Local<v8::Object> obj);

} // namespace smalloc
} // namespace node

@@ -156,6 +156,25 @@ copyOnto(c, 0, b, 0, 2);
assert.equal(b[0], 0.1);


// verify checking external if has external memory

// check objects
var b = {};
assert.ok(!smalloc.hasExternalData(b));
alloc(1, b);
assert.ok(smalloc.hasExternalData(b));
var f = function() { };
alloc(1, f);
assert.ok(smalloc.hasExternalData(f));

// and non-objects
assert.ok(!smalloc.hasExternalData(true));
assert.ok(!smalloc.hasExternalData(1));
assert.ok(!smalloc.hasExternalData('string'));
assert.ok(!smalloc.hasExternalData(null));
assert.ok(!smalloc.hasExternalData());


// verify alloc throws properly

// arrays are not supported