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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/omx-plugin/include/froyo/utils/SharedBuffer.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,146 @@
     1.4 +/*
     1.5 + * Copyright (C) 2005 The Android Open Source Project
     1.6 + *
     1.7 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.8 + * you may not use this file except in compliance with the License.
     1.9 + * You may obtain a copy of the License at
    1.10 + *
    1.11 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.12 + *
    1.13 + * Unless required by applicable law or agreed to in writing, software
    1.14 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.16 + * See the License for the specific language governing permissions and
    1.17 + * limitations under the License.
    1.18 + */
    1.19 +
    1.20 +#ifndef ANDROID_SHARED_BUFFER_H
    1.21 +#define ANDROID_SHARED_BUFFER_H
    1.22 +
    1.23 +#include <stdint.h>
    1.24 +#include <sys/types.h>
    1.25 +
    1.26 +// ---------------------------------------------------------------------------
    1.27 +
    1.28 +namespace android {
    1.29 +
    1.30 +class SharedBuffer
    1.31 +{
    1.32 +public:
    1.33 +
    1.34 +    /* flags to use with release() */
    1.35 +    enum {
    1.36 +        eKeepStorage = 0x00000001
    1.37 +    };
    1.38 +
    1.39 +    /*! allocate a buffer of size 'size' and acquire() it.
    1.40 +     *  call release() to free it.
    1.41 +     */
    1.42 +    static          SharedBuffer*           alloc(size_t size);
    1.43 +    
    1.44 +    /*! free the memory associated with the SharedBuffer.
    1.45 +     * Fails if there are any users associated with this SharedBuffer.
    1.46 +     * In other words, the buffer must have been release by all its
    1.47 +     * users.
    1.48 +     */
    1.49 +    static          ssize_t                 dealloc(const SharedBuffer* released);
    1.50 +    
    1.51 +    //! get the SharedBuffer from the data pointer
    1.52 +    static  inline  const SharedBuffer*     sharedBuffer(const void* data);
    1.53 +
    1.54 +    //! access the data for read
    1.55 +    inline          const void*             data() const;
    1.56 +    
    1.57 +    //! access the data for read/write
    1.58 +    inline          void*                   data();
    1.59 +
    1.60 +    //! get size of the buffer
    1.61 +    inline          size_t                  size() const;
    1.62 + 
    1.63 +    //! get back a SharedBuffer object from its data
    1.64 +    static  inline  SharedBuffer*           bufferFromData(void* data);
    1.65 +    
    1.66 +    //! get back a SharedBuffer object from its data
    1.67 +    static  inline  const SharedBuffer*     bufferFromData(const void* data);
    1.68 +
    1.69 +    //! get the size of a SharedBuffer object from its data
    1.70 +    static  inline  size_t                  sizeFromData(const void* data);
    1.71 +    
    1.72 +    //! edit the buffer (get a writtable, or non-const, version of it)
    1.73 +                    SharedBuffer*           edit() const;
    1.74 +
    1.75 +    //! edit the buffer, resizing if needed
    1.76 +                    SharedBuffer*           editResize(size_t size) const;
    1.77 +
    1.78 +    //! like edit() but fails if a copy is required
    1.79 +                    SharedBuffer*           attemptEdit() const;
    1.80 +    
    1.81 +    //! resize and edit the buffer, loose it's content.
    1.82 +                    SharedBuffer*           reset(size_t size) const;
    1.83 +
    1.84 +    //! acquire/release a reference on this buffer
    1.85 +                    void                    acquire() const;
    1.86 +                    
    1.87 +    /*! release a reference on this buffer, with the option of not
    1.88 +     * freeing the memory associated with it if it was the last reference
    1.89 +     * returns the previous reference count
    1.90 +     */     
    1.91 +                    int32_t                 release(uint32_t flags = 0) const;
    1.92 +    
    1.93 +    //! returns wether or not we're the only owner
    1.94 +    inline          bool                    onlyOwner() const;
    1.95 +    
    1.96 +
    1.97 +private:
    1.98 +        inline SharedBuffer() { }
    1.99 +        inline ~SharedBuffer() { }
   1.100 +        inline SharedBuffer(const SharedBuffer&);
   1.101 + 
   1.102 +        // 16 bytes. must be sized to preserve correct alingment.
   1.103 +        mutable int32_t        mRefs;
   1.104 +                size_t         mSize;
   1.105 +                uint32_t       mReserved[2];
   1.106 +};
   1.107 +
   1.108 +// ---------------------------------------------------------------------------
   1.109 +
   1.110 +const SharedBuffer* SharedBuffer::sharedBuffer(const void* data) {
   1.111 +    return data ? reinterpret_cast<const SharedBuffer *>(data)-1 : 0;
   1.112 +}
   1.113 +
   1.114 +const void* SharedBuffer::data() const {
   1.115 +    return this + 1;
   1.116 +}
   1.117 +
   1.118 +void* SharedBuffer::data() {
   1.119 +    return this + 1;
   1.120 +}
   1.121 +
   1.122 +size_t SharedBuffer::size() const {
   1.123 +    return mSize;
   1.124 +}
   1.125 +
   1.126 +SharedBuffer* SharedBuffer::bufferFromData(void* data)
   1.127 +{
   1.128 +    return ((SharedBuffer*)data)-1;
   1.129 +}
   1.130 +    
   1.131 +const SharedBuffer* SharedBuffer::bufferFromData(const void* data)
   1.132 +{
   1.133 +    return ((const SharedBuffer*)data)-1;
   1.134 +}
   1.135 +
   1.136 +size_t SharedBuffer::sizeFromData(const void* data)
   1.137 +{
   1.138 +    return (((const SharedBuffer*)data)-1)->mSize;
   1.139 +}
   1.140 +
   1.141 +bool SharedBuffer::onlyOwner() const {
   1.142 +    return (mRefs == 1);
   1.143 +}
   1.144 +
   1.145 +}; // namespace android
   1.146 +
   1.147 +// ---------------------------------------------------------------------------
   1.148 +
   1.149 +#endif // ANDROID_VECTOR_H

mercurial