llpluginsharedmemory.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @file llpluginsharedmemory.h
  3. * @brief LLPluginSharedMemory manages a shared memory segment for use by the
  4. * LLPlugin API.
  5. *
  6. * $LicenseInfo:firstyear=2008&license=viewergpl$
  7. *
  8. * Copyright (c) 2008-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LL_LLPLUGINSHAREDMEMORY_H
  34. #define LL_LLPLUGINSHAREDMEMORY_H
  35. #include "llpreprocessor.h"
  36. class LLPluginSharedMemoryPlatformImpl;
  37. class LLPluginSharedMemory
  38. {
  39. protected:
  40. LOG_CLASS(LLPluginSharedMemory);
  41. public:
  42. LLPluginSharedMemory();
  43. ~LLPluginSharedMemory();
  44. // Parent will use create/destroy, child will use attach/detach.
  45. // Message transactions will ensure child attaches after parent creates
  46. // and detaches before parent destroys.
  47. /**
  48. * Creates a shared memory segment, with a name which is guaranteed to be
  49. * unique on the host at the current time. Used by parent.
  50. * Message transactions will (*TODO: DOC - should ? must ?) ensure child
  51. * attaches after parent creates and detaches before parent destroys.
  52. *
  53. * @param[in] size Shared memory size in TODO:DOC units = bytes ?
  54. *
  55. * @return False for failure, true for success.
  56. */
  57. bool create(size_t size);
  58. /**
  59. * Destroys a shared memory segment. Used by parent.
  60. * Message transactions will (? TODO:DOC - should? must?) ensure child
  61. * attaches after parent creates and detaches before parent destroys.
  62. *
  63. * @return True. *TODO: DOC - always returns true. Is this the intended
  64. * behavior ?
  65. */
  66. bool destroy();
  67. /**
  68. * Creates and attaches a name to a shared memory segment. *TODO: DOC
  69. * what is the difference between attach() and create() ?
  70. *
  71. * @param[in] name Name to attach to memory segment
  72. * @param[in] size Size of memory segment *TODO: DOC in bytes ?
  73. *
  74. * @return False on failure, true otherwise.
  75. */
  76. bool attach(const std::string& name, size_t size);
  77. /**
  78. * Detaches shared memory segment.
  79. *
  80. * @return False on failure, true otherwise.
  81. */
  82. bool detach();
  83. /**
  84. * Checks if shared memory is mapped to a non-null address.
  85. *
  86. * @return True if memory address is non-null, false otherwise.
  87. */
  88. LL_INLINE bool isMapped() const { return mMappedAddress != NULL; }
  89. /**
  90. * Get pointer to shared memory.
  91. *
  92. * @return Pointer to shared memory.
  93. */
  94. LL_INLINE void* getMappedAddress() const { return mMappedAddress; }
  95. /**
  96. * Get size of shared memory.
  97. *
  98. * @return Size of shared memory in bytes. TODO:DOC are bytes the correct
  99. * unit ?
  100. */
  101. LL_INLINE size_t getSize() const { return mSize; }
  102. /**
  103. * Get name of shared memory.
  104. *
  105. * @return Name of shared memory.
  106. */
  107. LL_INLINE std::string getName() const { return mName; }
  108. private:
  109. bool map();
  110. bool unmap();
  111. bool close();
  112. bool unlink();
  113. static std::string createName();
  114. private:
  115. LLPluginSharedMemoryPlatformImpl* mImpl;
  116. void* mMappedAddress;
  117. std::string mName;
  118. size_t mSize;
  119. bool mNeedsDestroy;
  120. static int sSegmentNumber;
  121. };
  122. #endif // LL_LLPLUGINSHAREDMEMORY_H