@@ -715,6 +715,104 @@ inline IsolateData* Environment::isolate_data() const {
715
715
return isolate_data_;
716
716
}
717
717
718
+ inline char * Environment::AllocateUnchecked (size_t size) {
719
+ return static_cast <char *>(
720
+ isolate_data ()->allocator ()->AllocateUninitialized (size));
721
+ }
722
+
723
+ inline char * Environment::Allocate (size_t size) {
724
+ char * ret = AllocateUnchecked (size);
725
+ CHECK_NE (ret, nullptr );
726
+ return ret;
727
+ }
728
+
729
+ inline void Environment::Free (char * data, size_t size) {
730
+ if (data != nullptr )
731
+ isolate_data ()->allocator ()->Free (data, size);
732
+ }
733
+
734
+ inline AllocatedBuffer Environment::AllocateManaged (size_t size, bool checked) {
735
+ char * data = checked ? Allocate (size) : AllocateUnchecked (size);
736
+ if (data == nullptr ) size = 0 ;
737
+ return AllocatedBuffer (this , uv_buf_init (data, size));
738
+ }
739
+
740
+ inline AllocatedBuffer::AllocatedBuffer (Environment* env, uv_buf_t buf)
741
+ : env_(env), buffer_(buf) {}
742
+
743
+ inline void AllocatedBuffer::Resize (size_t len) {
744
+ char * new_data = env_->Reallocate (buffer_.base , buffer_.len , len);
745
+ CHECK_IMPLIES (len > 0 , new_data != nullptr );
746
+ buffer_ = uv_buf_init (new_data, len);
747
+ }
748
+
749
+ inline uv_buf_t AllocatedBuffer::release () {
750
+ uv_buf_t ret = buffer_;
751
+ buffer_ = uv_buf_init (nullptr , 0 );
752
+ return ret;
753
+ }
754
+
755
+ inline char * AllocatedBuffer::data () {
756
+ return buffer_.base ;
757
+ }
758
+
759
+ inline const char * AllocatedBuffer::data () const {
760
+ return buffer_.base ;
761
+ }
762
+
763
+ inline size_t AllocatedBuffer::size () const {
764
+ return buffer_.len ;
765
+ }
766
+
767
+ inline AllocatedBuffer::AllocatedBuffer (Environment* env)
768
+ : env_(env), buffer_(uv_buf_init(nullptr , 0 )) {}
769
+
770
+ inline AllocatedBuffer::AllocatedBuffer (AllocatedBuffer&& other)
771
+ : AllocatedBuffer() {
772
+ *this = std::move (other);
773
+ }
774
+
775
+ inline AllocatedBuffer& AllocatedBuffer::operator =(AllocatedBuffer&& other) {
776
+ clear ();
777
+ env_ = other.env_ ;
778
+ buffer_ = other.release ();
779
+ return *this ;
780
+ }
781
+
782
+ inline AllocatedBuffer::~AllocatedBuffer () {
783
+ clear ();
784
+ }
785
+
786
+ inline void AllocatedBuffer::clear () {
787
+ uv_buf_t buf = release ();
788
+ env_->Free (buf.base , buf.len );
789
+ }
790
+
791
+ // It's a bit awkward to define this Buffer::New() overload here, but it
792
+ // avoids a circular dependency with node_internals.h.
793
+ namespace Buffer {
794
+ v8::MaybeLocal<v8::Object> New (Environment* env,
795
+ char * data,
796
+ size_t length,
797
+ bool uses_malloc);
798
+ }
799
+
800
+ inline v8::MaybeLocal<v8::Object> AllocatedBuffer::ToBuffer () {
801
+ CHECK_NOT_NULL (env_);
802
+ v8::MaybeLocal<v8::Object> obj = Buffer::New (env_, data (), size (), false );
803
+ if (!obj.IsEmpty ()) release ();
804
+ return obj;
805
+ }
806
+
807
+ inline v8::Local<v8::ArrayBuffer> AllocatedBuffer::ToArrayBuffer () {
808
+ CHECK_NOT_NULL (env_);
809
+ uv_buf_t buf = release ();
810
+ return v8::ArrayBuffer::New (env_->isolate (),
811
+ buf.base ,
812
+ buf.len ,
813
+ v8::ArrayBufferCreationMode::kInternalized );
814
+ }
815
+
718
816
inline void Environment::ThrowError (const char * errmsg) {
719
817
ThrowError (v8::Exception::Error, errmsg);
720
818
}
0 commit comments