11#pragma once
22
33#include " absl/types/span.h"
4+ #include " toolbelt/hexdump.h"
45#include < functional>
56#include < iostream>
67#include < stdint.h>
78#include < stdlib.h>
89#include < string.h>
910#include < string>
1011#include < string_view>
11- #include " toolbelt/hexdump.h"
1212
1313namespace toolbelt {
1414
@@ -228,7 +228,7 @@ struct PayloadBuffer {
228228 return (p[word] & (1 << bit)) != 0 ;
229229 }
230230
231- static uint32_t DecodeSize (BufferOffset* addr) {
231+ static uint32_t DecodeSize (BufferOffset * addr) {
232232 // Length is 64 bits long but we only need the bottom 32 bits of it.
233233 uint32_t *p = reinterpret_cast <uint32_t *>(addr) - 2 ;
234234 if ((*p & (1U << 31 )) == 0 ) {
@@ -314,12 +314,11 @@ struct PayloadBuffer {
314314 FreeBlockHeader *FreeList () { return ToAddress<FreeBlockHeader>(free_list); }
315315
316316 // Allocate some memory in the buffer. The buffer might move.
317- static void *Allocate (PayloadBuffer **buffer, uint32_t n,
318- bool clear = true , bool enable_small_block = true );
317+ static void *Allocate (PayloadBuffer **buffer, uint32_t n, bool clear = true ,
318+ bool enable_small_block = true );
319319 void Free (void *p);
320320 static void *Realloc (PayloadBuffer **buffer, void *p, uint32_t n,
321- bool clear = true ,
322- bool enable_small_block = true );
321+ bool clear = true , bool enable_small_block = true );
323322 static BufferOffset AllocateBitMapRunVector (PayloadBuffer **self);
324323 static void *AllocateSmallBlock (PayloadBuffer **pb, uint32_t size, int index,
325324 bool clear = true );
@@ -331,8 +330,7 @@ struct PayloadBuffer {
331330 // individually. The addresses of the allocated items are returned in a
332331 // vector.
333332 static std::vector<void *> AllocateMany (PayloadBuffer **buffer, uint32_t size,
334- uint32_t n,
335- bool clear = true );
333+ uint32_t n, bool clear = true );
336334
337335 bool IsValidMagic () const {
338336 uint32_t m = magic & kBitMapMask ;
@@ -353,12 +351,12 @@ struct PayloadBuffer {
353351 // Given the address of a block, return the size of the block. This is
354352 // in the previous 8 bytes but might be encoded if the block is a small
355353 // block.
356- static uint32_t DecodedSize (BufferOffset* addr) {
357- BufferOffset* p = addr - 2 ;
358- if (*p & (1U << 31 )) {
359- return *p & kBitmapRunSizeMask ;
360- }
361- return *p;
354+ static uint32_t DecodedSize (BufferOffset * addr) {
355+ BufferOffset * p = addr - 2 ;
356+ if (*p & (1U << 31 )) {
357+ return *p & kBitmapRunSizeMask ;
358+ }
359+ return *p;
362360 }
363361
364362 template <typename T = void >
@@ -483,21 +481,28 @@ inline void PayloadBuffer::VectorPush(PayloadBuffer **self, VectorHeader *hdr,
483481 // BufferOffset data; - BufferOffset to vector contents
484482 // The vector contents is allocated in the buffer. It is preceded
485483 // by the block size (in bytes).
484+ BufferOffset hdr_offset = (*self)->ToOffset (hdr);
485+
486486 uint32_t total_size = hdr->num_elements * sizeof (T);
487487 if (hdr->data == 0 ) {
488488 // The vector is empty, allocate it with a default size of 2.
489489 void *vecp = Allocate (self, 2 * sizeof (T), true , enable_small_block);
490490 hdr->data = (*self)->ToOffset (vecp);
491+ VectorHeader *new_hdr = (*self)->ToAddress <VectorHeader>(hdr_offset);
492+ new_hdr->data = (*self)->ToOffset (vecp);
493+ hdr = new_hdr;
491494 } else {
492495 // Vector has some values in it. Retrieve the total size from
493496 // the allocated block header (before the start of the memory)
494497 uint32_t *block = (*self)->ToAddress <uint32_t >(hdr->data );
495498 uint32_t current_size = DecodeSize (block);
496499 if (current_size == total_size) {
497500 // Need to double the size of the memory.
498- void *vecp = Realloc (self, block, 2 * hdr->num_elements * sizeof (T),
499- true , enable_small_block);
500- hdr->data = (*self)->ToOffset (vecp);
501+ void *vecp = Realloc (self, block, 2 * hdr->num_elements * sizeof (T), true ,
502+ enable_small_block);
503+ VectorHeader *new_hdr = (*self)->ToAddress <VectorHeader>(hdr_offset);
504+ new_hdr->data = (*self)->ToOffset (vecp);
505+ hdr = new_hdr;
501506 }
502507 }
503508 // Get address of next location in vector.
@@ -512,9 +517,12 @@ template <typename T>
512517inline void PayloadBuffer::VectorReserve (PayloadBuffer **self,
513518 VectorHeader *hdr, size_t n,
514519 bool enable_small_block) {
520+ BufferOffset hdr_offset = (*self)->ToOffset (hdr);
515521 if (hdr->data == 0 ) {
516522 void *vecp = Allocate (self, n * sizeof (T), false , enable_small_block);
517- hdr->data = (*self)->ToOffset (vecp);
523+ VectorHeader* new_hdr = (*self)->ToAddress <VectorHeader>(hdr_offset);
524+ new_hdr->data = (*self)->ToOffset (vecp);
525+ hdr = new_hdr;
518526 } else {
519527 // Vector has some values in it. Retrieve the total size from
520528 // the allocated block header (before the start of the memory)
@@ -524,17 +532,22 @@ inline void PayloadBuffer::VectorReserve(PayloadBuffer **self,
524532 // Need to expand the memory to the size given.
525533 void *vecp =
526534 Realloc (self, block, n * sizeof (T), false , enable_small_block);
527- hdr->data = (*self)->ToOffset (vecp);
535+ VectorHeader* new_hdr = (*self)->ToAddress <VectorHeader>(hdr_offset);
536+ new_hdr->data = (*self)->ToOffset (vecp);
537+ hdr = new_hdr;
528538 }
529539 }
530540}
531541
532542template <typename T>
533543inline void PayloadBuffer::VectorResize (PayloadBuffer **self, VectorHeader *hdr,
534544 size_t n) {
545+ BufferOffset hdr_offset = (*self)->ToOffset (hdr);
535546 if (hdr->data == 0 ) {
536547 void *vecp = Allocate (self, n * sizeof (T));
537- hdr->data = (*self)->ToOffset (vecp);
548+ VectorHeader* new_hdr = (*self)->ToAddress <VectorHeader>(hdr_offset);
549+ new_hdr->data = (*self)->ToOffset (vecp);
550+ hdr = new_hdr;
538551 } else {
539552 // Vector has some values in it. Retrieve the total size from
540553 // the allocated block header (before the start of the memory)
@@ -543,7 +556,9 @@ inline void PayloadBuffer::VectorResize(PayloadBuffer **self, VectorHeader *hdr,
543556 if (current_size < n * sizeof (T)) {
544557 // Need to expand the memory to the size given.
545558 void *vecp = Realloc (self, block, n * sizeof (T), 8 );
546- hdr->data = (*self)->ToOffset (vecp);
559+ VectorHeader* new_hdr = (*self)->ToAddress <VectorHeader>(hdr_offset);
560+ new_hdr->data = (*self)->ToOffset (vecp);
561+ hdr = new_hdr;
547562 }
548563 }
549564 hdr->num_elements = n;
0 commit comments