|
24 | 24 | #include "node_buffer.h"
|
25 | 25 |
|
26 | 26 | #include "v8.h"
|
| 27 | +#include "v8-profiler.h" |
27 | 28 |
|
28 | 29 | #include <assert.h>
|
29 | 30 | #include <stdlib.h> // malloc, free
|
|
33 | 34 | # include <arpa/inet.h> // htons, htonl
|
34 | 35 | #endif
|
35 | 36 |
|
36 |
| - |
37 | 37 | #define MIN(a,b) ((a) < (b) ? (a) : (b))
|
38 | 38 |
|
| 39 | +#define BUFFER_CLASS_ID (0xBABE) |
| 40 | + |
39 | 41 | namespace node {
|
40 | 42 |
|
41 | 43 | using namespace v8;
|
@@ -188,6 +190,7 @@ Buffer::Buffer(Handle<Object> wrapper, size_t length) : ObjectWrap() {
|
188 | 190 |
|
189 | 191 | length_ = 0;
|
190 | 192 | callback_ = NULL;
|
| 193 | + handle_.SetWrapperClassId(BUFFER_CLASS_ID); |
191 | 194 |
|
192 | 195 | Replace(NULL, length, NULL, NULL);
|
193 | 196 | }
|
@@ -728,6 +731,61 @@ bool Buffer::HasInstance(v8::Handle<v8::Value> val) {
|
728 | 731 | }
|
729 | 732 |
|
730 | 733 |
|
| 734 | +class RetainedBufferInfo: public v8::RetainedObjectInfo { |
| 735 | +public: |
| 736 | + RetainedBufferInfo(Buffer* buffer); |
| 737 | + virtual void Dispose(); |
| 738 | + virtual bool IsEquivalent(RetainedObjectInfo* other); |
| 739 | + virtual intptr_t GetHash(); |
| 740 | + virtual const char* GetLabel(); |
| 741 | + virtual intptr_t GetSizeInBytes(); |
| 742 | +private: |
| 743 | + Buffer* buffer_; |
| 744 | + static const char label[]; |
| 745 | +}; |
| 746 | + |
| 747 | +const char RetainedBufferInfo::label[] = "Buffer"; |
| 748 | + |
| 749 | + |
| 750 | +RetainedBufferInfo::RetainedBufferInfo(Buffer* buffer): buffer_(buffer) { |
| 751 | +} |
| 752 | + |
| 753 | + |
| 754 | +void RetainedBufferInfo::Dispose() { |
| 755 | + buffer_ = NULL; |
| 756 | + delete this; |
| 757 | +} |
| 758 | + |
| 759 | + |
| 760 | +bool RetainedBufferInfo::IsEquivalent(RetainedObjectInfo* other) { |
| 761 | + return label == other->GetLabel() && |
| 762 | + buffer_ == static_cast<RetainedBufferInfo*>(other)->buffer_; |
| 763 | +} |
| 764 | + |
| 765 | + |
| 766 | +intptr_t RetainedBufferInfo::GetHash() { |
| 767 | + return reinterpret_cast<intptr_t>(buffer_); |
| 768 | +} |
| 769 | + |
| 770 | + |
| 771 | +const char* RetainedBufferInfo::GetLabel() { |
| 772 | + return label; |
| 773 | +} |
| 774 | + |
| 775 | + |
| 776 | +intptr_t RetainedBufferInfo::GetSizeInBytes() { |
| 777 | + return Buffer::Length(buffer_); |
| 778 | +} |
| 779 | + |
| 780 | + |
| 781 | +RetainedObjectInfo* WrapperInfo(uint16_t class_id, Handle<Value> wrapper) { |
| 782 | + assert(class_id == BUFFER_CLASS_ID); |
| 783 | + assert(Buffer::HasInstance(wrapper)); |
| 784 | + Buffer* buffer = Buffer::Unwrap<Buffer>(wrapper.As<Object>()); |
| 785 | + return new RetainedBufferInfo(buffer); |
| 786 | +} |
| 787 | + |
| 788 | + |
731 | 789 | void Buffer::Initialize(Handle<Object> target) {
|
732 | 790 | HandleScope scope;
|
733 | 791 |
|
@@ -775,6 +833,8 @@ void Buffer::Initialize(Handle<Object> target) {
|
775 | 833 | Buffer::MakeFastBuffer);
|
776 | 834 |
|
777 | 835 | target->Set(String::NewSymbol("SlowBuffer"), constructor_template->GetFunction());
|
| 836 | + |
| 837 | + HeapProfiler::DefineWrapperClass(BUFFER_CLASS_ID, WrapperInfo); |
778 | 838 | }
|
779 | 839 |
|
780 | 840 |
|
|
0 commit comments