media/omx-plugin/include/ics/utils/SharedBuffer.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /*
     2  * Copyright (C) 2005 The Android Open Source Project
     3  *
     4  * Licensed under the Apache License, Version 2.0 (the "License");
     5  * you may not use this file except in compliance with the License.
     6  * You may obtain a copy of the License at
     7  *
     8  *      http://www.apache.org/licenses/LICENSE-2.0
     9  *
    10  * Unless required by applicable law or agreed to in writing, software
    11  * distributed under the License is distributed on an "AS IS" BASIS,
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  * See the License for the specific language governing permissions and
    14  * limitations under the License.
    15  */
    17 #ifndef ANDROID_SHARED_BUFFER_H
    18 #define ANDROID_SHARED_BUFFER_H
    20 #include <stdint.h>
    21 #include <sys/types.h>
    23 // ---------------------------------------------------------------------------
    25 namespace android {
    27 class SharedBuffer
    28 {
    29 public:
    31     /* flags to use with release() */
    32     enum {
    33         eKeepStorage = 0x00000001
    34     };
    36     /*! allocate a buffer of size 'size' and acquire() it.
    37      *  call release() to free it.
    38      */
    39     static          SharedBuffer*           alloc(size_t size);
    41     /*! free the memory associated with the SharedBuffer.
    42      * Fails if there are any users associated with this SharedBuffer.
    43      * In other words, the buffer must have been release by all its
    44      * users.
    45      */
    46     static          ssize_t                 dealloc(const SharedBuffer* released);
    48     //! get the SharedBuffer from the data pointer
    49     static  inline  const SharedBuffer*     sharedBuffer(const void* data);
    51     //! access the data for read
    52     inline          const void*             data() const;
    54     //! access the data for read/write
    55     inline          void*                   data();
    57     //! get size of the buffer
    58     inline          size_t                  size() const;
    60     //! get back a SharedBuffer object from its data
    61     static  inline  SharedBuffer*           bufferFromData(void* data);
    63     //! get back a SharedBuffer object from its data
    64     static  inline  const SharedBuffer*     bufferFromData(const void* data);
    66     //! get the size of a SharedBuffer object from its data
    67     static  inline  size_t                  sizeFromData(const void* data);
    69     //! edit the buffer (get a writtable, or non-const, version of it)
    70                     SharedBuffer*           edit() const;
    72     //! edit the buffer, resizing if needed
    73                     SharedBuffer*           editResize(size_t size) const;
    75     //! like edit() but fails if a copy is required
    76                     SharedBuffer*           attemptEdit() const;
    78     //! resize and edit the buffer, loose it's content.
    79                     SharedBuffer*           reset(size_t size) const;
    81     //! acquire/release a reference on this buffer
    82                     void                    acquire() const;
    84     /*! release a reference on this buffer, with the option of not
    85      * freeing the memory associated with it if it was the last reference
    86      * returns the previous reference count
    87      */     
    88                     int32_t                 release(uint32_t flags = 0) const;
    90     //! returns wether or not we're the only owner
    91     inline          bool                    onlyOwner() const;
    94 private:
    95         inline SharedBuffer() { }
    96         inline ~SharedBuffer() { }
    97         inline SharedBuffer(const SharedBuffer&);
    99         // 16 bytes. must be sized to preserve correct alingment.
   100         mutable int32_t        mRefs;
   101                 size_t         mSize;
   102                 uint32_t       mReserved[2];
   103 };
   105 // ---------------------------------------------------------------------------
   107 const SharedBuffer* SharedBuffer::sharedBuffer(const void* data) {
   108     return data ? reinterpret_cast<const SharedBuffer *>(data)-1 : 0;
   109 }
   111 const void* SharedBuffer::data() const {
   112     return this + 1;
   113 }
   115 void* SharedBuffer::data() {
   116     return this + 1;
   117 }
   119 size_t SharedBuffer::size() const {
   120     return mSize;
   121 }
   123 SharedBuffer* SharedBuffer::bufferFromData(void* data)
   124 {
   125     return ((SharedBuffer*)data)-1;
   126 }
   128 const SharedBuffer* SharedBuffer::bufferFromData(const void* data)
   129 {
   130     return ((const SharedBuffer*)data)-1;
   131 }
   133 size_t SharedBuffer::sizeFromData(const void* data)
   134 {
   135     return (((const SharedBuffer*)data)-1)->mSize;
   136 }
   138 bool SharedBuffer::onlyOwner() const {
   139     return (mRefs == 1);
   140 }
   142 }; // namespace android
   144 // ---------------------------------------------------------------------------
   146 #endif // ANDROID_VECTOR_H

mercurial