llcorebufferarray.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @file llcorebufferarray.h
  3. * @brief Public-facing declaration for the BufferArray scatter/gather class
  4. *
  5. * $LicenseInfo:firstyear=2012&license=viewerlgpl$
  6. *
  7. * Copyright (C) 2012, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef _LLCORE_BUFFER_ARRAY_H_
  33. #define _LLCORE_BUFFER_ARRAY_H_
  34. #include <cstdlib>
  35. #include <vector>
  36. #include "boost/intrusive_ptr.hpp"
  37. #include "llcorerefcounted.h"
  38. namespace LLCore
  39. {
  40. class BufferArrayStreamBuf;
  41. // A very simple scatter/gather type map for bulk data. The motivation for
  42. // this class is the writedata callback used by libcurl. Response bodies are
  43. // delivered to the caller in a sequence of sequential write operations and
  44. // this class captures them without having to reallocate and move data.
  45. //
  46. // The interface looks a little like a unix file descriptor but only just.
  47. // There is a notion of a current position, starting from 0, which is used as
  48. // the position in the data when performing read and write operations. The
  49. // position also moves after various operations:
  50. // - seek(...)
  51. // - read(...)
  52. // - write(...)
  53. // - append(...)
  54. // - appendBufferAlloc(...)
  55. // The object also keeps a total length value which is updated after write and
  56. // append operations and beyond which the current position cannot be set.
  57. //
  58. // Threading: not thread-safe
  59. //
  60. // Allocation: Refcounted, heap only. Caller of the constructor is given a
  61. // single refcount.
  62. class BufferArray : public LLCoreInt::RefCounted
  63. {
  64. public:
  65. // BufferArrayStreamBuf has intimate knowledge of this implementation to
  66. // implement a buffer-free adapter. Changes here will likely need to be
  67. // reflected there.
  68. friend class BufferArrayStreamBuf;
  69. BufferArray();
  70. BufferArray(const BufferArray&) = delete;
  71. void operator=(const BufferArray&) = delete;
  72. typedef LLCoreInt::IntrusivePtr<BufferArray> ptr_t;
  73. protected:
  74. virtual ~BufferArray(); // Use release()
  75. public:
  76. // Appends the indicated data to the BufferArray modifying current position
  77. // and total size. New position is one beyond the final byte of the buffer.
  78. // Returns the count of bytes copied to BufferArray
  79. size_t append(const void* src, size_t len);
  80. // Similar to append(), this method guarantees a contiguous block of memory
  81. // of requested size placed at the current end of the BufferArray.
  82. // On return, the data in the memory is considered valid whether the caller
  83. // writes to it or not. Returns a pointer to contiguous region at end of
  84. // BufferArray of 'len' size.
  85. void *appendBufferAlloc(size_t len);
  86. // Current count of bytes in BufferArray instance.
  87. LL_INLINE size_t size() const { return mLen; }
  88. // Copies data from the given position in the instance to the caller's
  89. // buffer. Will return a short count of bytes copied if the 'len' extends
  90. // beyond the data.
  91. size_t read(size_t pos, void* dst, size_t len);
  92. // Copies data from the caller's buffer to the instance at the current
  93. // position. May overwrite existing data, append data when current position
  94. // is equal to the size of the instance or do a mix of both.
  95. size_t write(size_t pos, const void* src, size_t len);
  96. protected:
  97. // Returns -1 when there is no corresponding block.
  98. S32 findBlock(size_t pos, size_t& ret_offset) const;
  99. bool getBlockStartEnd(S32 block, const char** start, const char** end);
  100. protected:
  101. class Block;
  102. typedef std::vector<Block*> container_t;
  103. container_t mBlocks;
  104. size_t mLen;
  105. }; // End class BufferArray
  106. } // End namespace LLCore
  107. #endif // _LLCORE_BUFFER_ARRAY_H_