You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by jp...@apache.org on 2016/05/11 20:31:45 UTC

[trafficserver] 21/33: TS-4425: Switch IOBuffers over to Ptr::get().

This is an automated email from the ASF dual-hosted git repository.

jpeach pushed a commit to branch master
in repository https://git-dual.apache.org/repos/asf/trafficserver.git

commit cf53450b74c54a7c1661d95043de91286382ec1f
Author: James Peach <jp...@apache.org>
AuthorDate: Thu May 5 22:27:14 2016 -0700

    TS-4425: Switch IOBuffers over to Ptr::get().
---
 iocore/eventsystem/IOBuffer.cc  |  26 +++++-----
 iocore/eventsystem/I_IOBuffer.h |  21 +++++---
 iocore/eventsystem/P_IOBuffer.h | 112 ++++++++++++++++++++++++++--------------
 3 files changed, 102 insertions(+), 57 deletions(-)

diff --git a/iocore/eventsystem/IOBuffer.cc b/iocore/eventsystem/IOBuffer.cc
index b73d81e..b2aa507 100644
--- a/iocore/eventsystem/IOBuffer.cc
+++ b/iocore/eventsystem/IOBuffer.cc
@@ -79,7 +79,7 @@ MIOBuffer::remove_append(IOBufferReader *r)
     }
     r->start_offset = 0;
     l += b->read_avail();
-    append_block(b);
+    append_block(b.get());
   }
   r->mbuf->_writer = NULL;
   return l;
@@ -140,7 +140,7 @@ int64_t
 MIOBuffer::write(IOBufferReader *r, int64_t alen, int64_t offset)
 {
   int64_t len = alen;
-  IOBufferBlock *b = r->block;
+  IOBufferBlock *b = r->block.get();
   offset += r->start_offset;
 
   while (b && len > 0) {
@@ -148,22 +148,24 @@ MIOBuffer::write(IOBufferReader *r, int64_t alen, int64_t offset)
     max_bytes -= offset;
     if (max_bytes <= 0) {
       offset = -max_bytes;
-      b = b->next;
+      b = b->next.get();
       continue;
     }
     int64_t bytes;
-    if (len < 0 || len >= max_bytes)
+    if (len < 0 || len >= max_bytes) {
       bytes = max_bytes;
-    else
+    } else {
       bytes = len;
+    }
     IOBufferBlock *bb = b->clone();
     bb->_start += offset;
     bb->_buf_end = bb->_end = bb->_start + bytes;
     append_block(bb);
     offset = 0;
     len -= bytes;
-    b = b->next;
+    b = b->next.get();
   }
+
   return alen - len;
 }
 
@@ -212,7 +214,7 @@ IOBufferReader::read(void *ab, int64_t len)
 int64_t
 IOBufferReader::memchr(char c, int64_t len, int64_t offset)
 {
-  IOBufferBlock *b = block;
+  IOBufferBlock *b = block.get();
   offset += start_offset;
   int64_t o = offset;
 
@@ -221,7 +223,7 @@ IOBufferReader::memchr(char c, int64_t len, int64_t offset)
     max_bytes -= offset;
     if (max_bytes <= 0) {
       offset = -max_bytes;
-      b = b->next;
+      b = b->next.get();
       continue;
     }
     int64_t bytes;
@@ -235,7 +237,7 @@ IOBufferReader::memchr(char c, int64_t len, int64_t offset)
       return (int64_t)(o - start_offset + p - s);
     o += bytes;
     len -= bytes;
-    b = b->next;
+    b = b->next.get();
     offset = 0;
   }
 
@@ -246,7 +248,7 @@ char *
 IOBufferReader::memcpy(const void *ap, int64_t len, int64_t offset)
 {
   char *p = (char *)ap;
-  IOBufferBlock *b = block;
+  IOBufferBlock *b = block.get();
   offset += start_offset;
 
   while (b && len) {
@@ -254,7 +256,7 @@ IOBufferReader::memcpy(const void *ap, int64_t len, int64_t offset)
     max_bytes -= offset;
     if (max_bytes <= 0) {
       offset = -max_bytes;
-      b = b->next;
+      b = b->next.get();
       continue;
     }
     int64_t bytes;
@@ -265,7 +267,7 @@ IOBufferReader::memcpy(const void *ap, int64_t len, int64_t offset)
     ::memcpy(p, b->start() + offset, bytes);
     p += bytes;
     len -= bytes;
-    b = b->next;
+    b = b->next.get();
     offset = 0;
   }
 
diff --git a/iocore/eventsystem/I_IOBuffer.h b/iocore/eventsystem/I_IOBuffer.h
index 5931f90..b50dfb2 100644
--- a/iocore/eventsystem/I_IOBuffer.h
+++ b/iocore/eventsystem/I_IOBuffer.h
@@ -917,18 +917,19 @@ public:
     Returns a pointer to the first writable block on the block chain.
     Returns NULL if there are not currently any writable blocks on the
     block list.
-
   */
   IOBufferBlock *
   first_write_block()
   {
     if (_writer) {
-      if (_writer->next && !_writer->write_avail())
-        return _writer->next;
+      if (_writer->next && !_writer->write_avail()) {
+        return _writer->next.get();
+      }
       ink_assert(!_writer->next || !_writer->next->read_avail());
-      return _writer;
-    } else
-      return NULL;
+      return _writer.get();
+    }
+
+    return NULL;
   }
 
   char *
@@ -937,16 +938,19 @@ public:
     IOBufferBlock *b = first_write_block();
     return b ? b->buf() : 0;
   }
+
   char *
   buf_end()
   {
     return first_write_block()->buf_end();
   }
+
   char *
   start()
   {
     return first_write_block()->start();
   }
+
   char *
   end()
   {
@@ -1318,6 +1322,11 @@ public:
   {
     return new_IOBufferBlock_internal(loc, d, len, offset);
   }
+  IOBufferBlock *
+  operator()(Ptr<IOBufferData> &d, int64_t len = 0, int64_t offset = 0)
+  {
+    return new_IOBufferBlock_internal(loc, d.get(), len, offset);
+  }
 };
 #endif
 
diff --git a/iocore/eventsystem/P_IOBuffer.h b/iocore/eventsystem/P_IOBuffer.h
index 1298b89..2046ca6 100644
--- a/iocore/eventsystem/P_IOBuffer.h
+++ b/iocore/eventsystem/P_IOBuffer.h
@@ -69,27 +69,32 @@ index_to_buffer_size(int64_t idx)
 }
 
 TS_INLINE IOBufferBlock *
-iobufferblock_clone(IOBufferBlock *b, int64_t offset, int64_t len)
+iobufferblock_clone(IOBufferBlock *src, int64_t offset, int64_t len)
 {
   IOBufferBlock *start_buf = NULL;
   IOBufferBlock *current_buf = NULL;
 
-  while (b && len >= 0) {
-    char *start = b->_start;
-    char *end = b->_end;
+  while (src && len >= 0) {
+    char *start = src->_start;
+    char *end = src->_end;
     int64_t max_bytes = end - start;
+
     max_bytes -= offset;
     if (max_bytes <= 0) {
       offset = -max_bytes;
-      b = b->next;
+      src = src->next.get();
       continue;
     }
+
     int64_t bytes = len;
-    if (bytes >= max_bytes)
+    if (bytes >= max_bytes) {
       bytes = max_bytes;
-    IOBufferBlock *new_buf = b->clone();
+    }
+
+    IOBufferBlock *new_buf = src->clone();
     new_buf->_start += offset;
     new_buf->_buf_end = new_buf->_end = new_buf->_start + bytes;
+
     if (!start_buf) {
       start_buf = new_buf;
       current_buf = start_buf;
@@ -97,10 +102,12 @@ iobufferblock_clone(IOBufferBlock *b, int64_t offset, int64_t len)
       current_buf->next = new_buf;
       current_buf = new_buf;
     }
+
     len -= bytes;
-    b = b->next;
+    src = src->next.get();
     offset = 0;
   }
+
   return start_buf;
 }
 
@@ -109,16 +116,21 @@ iobufferblock_skip(IOBufferBlock *b, int64_t *poffset, int64_t *plen, int64_t wr
 {
   int64_t offset = *poffset;
   int64_t len = write;
+
   while (b && len >= 0) {
     int64_t max_bytes = b->read_avail();
+
+    // If this block ends before the start offset, skip it
+    // and adjust the offset to consume its length.
     max_bytes -= offset;
     if (max_bytes <= 0) {
       offset = -max_bytes;
-      b = b->next;
+      b = b->next.get();
       continue;
     }
+
     if (len >= max_bytes) {
-      b = b->next;
+      b = b->next.get();
       len -= max_bytes;
       offset = 0;
     } else {
@@ -126,14 +138,13 @@ iobufferblock_skip(IOBufferBlock *b, int64_t *poffset, int64_t *plen, int64_t wr
       break;
     }
   }
+
   *poffset = offset;
   *plen -= write;
   return b;
 }
 
 #ifdef TRACK_BUFFER_USER
-extern Resource *res_lookup(const char *path);
-
 TS_INLINE void
 iobuffer_mem_inc(const char *_loc, int64_t _size_index)
 {
@@ -406,19 +417,25 @@ TS_INLINE void
 IOBufferBlock::clear()
 {
   data = NULL;
-  IOBufferBlock *p = next;
+
+  IOBufferBlock *p = next.get();
   while (p) {
-    int r = p->refcount_dec();
-    if (r)
-      break;
-    else {
-      IOBufferBlock *n = p->next.m_ptr;
-      p->next.m_ptr = NULL;
+    // If our block pointer refcount dropped to zero,
+    // recursively free the list.
+    if (p->refcount_dec() == 0) {
+      IOBufferBlock *n = p->next.detach();
       p->free();
       p = n;
+    } else {
+      // We don't hold the last refcount, so we are done.
+      break;
     }
   }
-  next.m_ptr = NULL;
+
+  // Nuke the next pointer without dropping the refcount
+  // because we already manually did that.
+  next.detach();
+
   _buf_end = _end = _start = NULL;
 }
 
@@ -555,14 +572,16 @@ IOBufferReader::current_low_water()
 TS_INLINE IOBufferBlock *
 IOBufferReader::get_current_block()
 {
-  return block;
+  return block.get();
 }
 
 TS_INLINE char *
 IOBufferReader::start()
 {
-  if (block == 0)
+  if (!block) {
     return 0;
+  }
+
   skip_empty_blocks();
   return block->start() + start_offset;
 }
@@ -570,8 +589,10 @@ IOBufferReader::start()
 TS_INLINE char *
 IOBufferReader::end()
 {
-  if (block == 0)
+  if (!block) {
     return 0;
+  }
+
   skip_empty_blocks();
   return block->end();
 }
@@ -579,8 +600,10 @@ IOBufferReader::end()
 TS_INLINE int64_t
 IOBufferReader::block_read_avail()
 {
-  if (block == 0)
+  if (!block) {
     return 0;
+  }
+
   skip_empty_blocks();
   return (int64_t)(block->end() - (block->start() + start_offset));
 }
@@ -589,12 +612,13 @@ TS_INLINE int
 IOBufferReader::block_count()
 {
   int count = 0;
-  IOBufferBlock *b = block;
+  IOBufferBlock *b = block.get();
 
   while (b) {
     count++;
-    b = b->next;
+    b = b->next.get();
   }
+
   return count;
 }
 
@@ -602,15 +626,18 @@ TS_INLINE int64_t
 IOBufferReader::read_avail()
 {
   int64_t t = 0;
-  IOBufferBlock *b = block;
+  IOBufferBlock *b = block.get();
 
   while (b) {
     t += b->read_avail();
-    b = b->next;
+    b = b->next.get();
   }
+
   t -= start_offset;
-  if (size_limit != INT64_MAX && t > size_limit)
+  if (size_limit != INT64_MAX && t > size_limit) {
     t = size_limit;
+  }
+
   return t;
 }
 
@@ -618,13 +645,14 @@ inline bool
 IOBufferReader::is_read_avail_more_than(int64_t size)
 {
   int64_t t = -start_offset;
-  IOBufferBlock *b = block;
+  IOBufferBlock *b = block.get();
+
   while (b) {
     t += b->read_avail();
     if (t > size) {
       return true;
     }
-    b = b->next;
+    b = b->next.get();
   }
   return false;
 }
@@ -633,11 +661,15 @@ TS_INLINE void
 IOBufferReader::consume(int64_t n)
 {
   start_offset += n;
-  if (size_limit != INT64_MAX)
+  if (size_limit != INT64_MAX) {
     size_limit -= n;
+  }
+
   ink_assert(size_limit >= 0);
-  if (block == 0)
+  if (!block) {
     return;
+  }
+
   int64_t r = block->read_avail();
   int64_t s = start_offset;
   while (r <= s && block->next && block->next->read_avail()) {
@@ -646,26 +678,28 @@ IOBufferReader::consume(int64_t n)
     block = block->next;
     r = block->read_avail();
   }
+
   ink_assert(read_avail() >= 0);
 }
 
 TS_INLINE char &IOBufferReader::operator[](int64_t i)
 {
   static char _error = '\0';
+  IOBufferBlock *b = block.get();
 
-  IOBufferBlock *b = block;
   i += start_offset;
   while (b) {
     int64_t bytes = b->read_avail();
     if (bytes > i)
       return b->start()[i];
     i -= bytes;
-    b = b->next;
+    b = b->next.get();
   }
 
   ink_assert(!"out of range");
-  if (unlikely(b))
+  if (unlikely(b)) {
     return *b->start();
+  }
 
   return _error;
 }
@@ -893,7 +927,7 @@ MIOBuffer::append_block_internal(IOBufferBlock *b)
     _writer->next = b;
     while (b->read_avail()) {
       _writer = b;
-      b = b->next;
+      b = b->next.get();
       if (!b)
         break;
     }
@@ -964,10 +998,10 @@ TS_INLINE int64_t
 MIOBuffer::current_write_avail()
 {
   int64_t t = 0;
-  IOBufferBlock *b = _writer;
+  IOBufferBlock *b = _writer.get();
   while (b) {
     t += b->write_avail();
-    b = b->next;
+    b = b->next.get();
   }
   return t;
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@trafficserver.apache.org" <co...@trafficserver.apache.org>.