@@ -161,19 +161,17 @@ struct StringPtr {
161161 StringPtr (const StringPtr&) = delete ;
162162 StringPtr& operator =(const StringPtr&) = delete ;
163163
164- void SetAllocator (StringPtrAllocator* allocator) { allocator_ = allocator; }
165-
166164 // If str_ does not point to owned storage yet, this function makes it do
167165 // so. This is called at the end of each http_parser_execute() so as not
168166 // to leak references. See issue #2438 and test-http-parser-bad-ref.js.
169- void Save () {
167+ void Save (StringPtrAllocator* allocator ) {
170168 if (str_ == nullptr || on_heap_ ||
171- (allocator_ != nullptr && allocator_ ->Contains (str_))) {
169+ (allocator != nullptr && allocator ->Contains (str_))) {
172170 return ;
173171 }
174172 // Try allocator first, fall back to heap
175- if (allocator_ != nullptr ) {
176- char * ptr = allocator_ ->TryAllocate (size_);
173+ if (allocator != nullptr ) {
174+ char * ptr = allocator ->TryAllocate (size_);
177175 if (ptr != nullptr ) {
178176 memcpy (ptr, str_, size_);
179177 str_ = ptr;
@@ -195,19 +193,19 @@ struct StringPtr {
195193 size_ = 0 ;
196194 }
197195
198- void Update (const char * str, size_t size) {
196+ void Update (const char * str, size_t size, StringPtrAllocator* allocator ) {
199197 if (str_ == nullptr ) {
200198 str_ = str;
201199 } else if (on_heap_ ||
202- (allocator_ != nullptr && allocator_ ->Contains (str_)) ||
200+ (allocator != nullptr && allocator ->Contains (str_)) ||
203201 str_ + size_ != str) {
204202 // Non-consecutive input, make a copy
205203 const size_t new_size = size_ + size;
206204 char * new_str = nullptr ;
207205
208206 // Try allocator first (if not already on heap)
209- if (!on_heap_ && allocator_ != nullptr ) {
210- new_str = allocator_ ->TryAllocate (new_size);
207+ if (!on_heap_ && allocator != nullptr ) {
208+ new_str = allocator ->TryAllocate (new_size);
211209 }
212210
213211 if (new_str != nullptr ) {
@@ -245,7 +243,6 @@ struct StringPtr {
245243 const char * str_ = nullptr ;
246244 bool on_heap_ = false ;
247245 size_t size_ = 0 ;
248- StringPtrAllocator* allocator_ = nullptr ;
249246};
250247
251248struct ParserComparator {
@@ -303,15 +300,7 @@ class Parser : public AsyncWrap, public StreamListener {
303300 : AsyncWrap(binding_data->env (), wrap),
304301 current_buffer_len_(0 ),
305302 current_buffer_data_(nullptr ),
306- binding_data_(binding_data) {
307- // Wire up all StringPtrs to use the shared allocator
308- for (size_t i = 0 ; i < kMaxHeaderFieldsCount ; i++) {
309- fields_[i].SetAllocator (&allocator_);
310- values_[i].SetAllocator (&allocator_);
311- }
312- url_.SetAllocator (&allocator_);
313- status_message_.SetAllocator (&allocator_);
314- }
303+ binding_data_(binding_data) {}
315304
316305 SET_NO_MEMORY_INFO ()
317306 SET_MEMORY_INFO_NAME(Parser)
@@ -360,7 +349,7 @@ class Parser : public AsyncWrap, public StreamListener {
360349 return rv;
361350 }
362351
363- url_.Update (at, length);
352+ url_.Update (at, length, &allocator_ );
364353 return 0 ;
365354 }
366355
@@ -371,7 +360,7 @@ class Parser : public AsyncWrap, public StreamListener {
371360 return rv;
372361 }
373362
374- status_message_.Update (at, length);
363+ status_message_.Update (at, length, &allocator_ );
375364 return 0 ;
376365 }
377366
@@ -397,7 +386,7 @@ class Parser : public AsyncWrap, public StreamListener {
397386 CHECK_LT (num_fields_, kMaxHeaderFieldsCount );
398387 CHECK_EQ (num_fields_, num_values_ + 1 );
399388
400- fields_[num_fields_ - 1 ].Update (at, length);
389+ fields_[num_fields_ - 1 ].Update (at, length, &allocator_ );
401390
402391 return 0 ;
403392 }
@@ -418,7 +407,7 @@ class Parser : public AsyncWrap, public StreamListener {
418407 CHECK_LT (num_values_, arraysize (values_));
419408 CHECK_EQ (num_values_, num_fields_);
420409
421- values_[num_values_ - 1 ].Update (at, length);
410+ values_[num_values_ - 1 ].Update (at, length, &allocator_ );
422411
423412 return 0 ;
424413 }
@@ -646,15 +635,15 @@ class Parser : public AsyncWrap, public StreamListener {
646635 }
647636
648637 void Save () {
649- url_.Save ();
650- status_message_.Save ();
638+ url_.Save (&allocator_ );
639+ status_message_.Save (&allocator_ );
651640
652641 for (size_t i = 0 ; i < num_fields_; i++) {
653- fields_[i].Save ();
642+ fields_[i].Save (&allocator_ );
654643 }
655644
656645 for (size_t i = 0 ; i < num_values_; i++) {
657- values_[i].Save ();
646+ values_[i].Save (&allocator_ );
658647 }
659648 }
660649
0 commit comments