LCOV - code coverage report
Current view: top level - media/libstagefright/system/core/libutils - SharedBuffer.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 19 51 37.3 %
Date: 2017-07-14 16:53:18 Functions: 4 8 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             :  */
      16             : 
      17             : #include <stdlib.h>
      18             : #include <string.h>
      19             : 
      20             : #include <utils/SharedBuffer.h>
      21             : #include <utils/Atomic.h>
      22             : 
      23             : // ---------------------------------------------------------------------------
      24             : 
      25             : namespace stagefright {
      26             : 
      27          30 : SharedBuffer* SharedBuffer::alloc(size_t size)
      28             : {
      29          30 :     SharedBuffer* sb = static_cast<SharedBuffer *>(malloc(sizeof(SharedBuffer) + size));
      30          30 :     if (sb) {
      31          30 :         sb->mRefs = 1;
      32          30 :         sb->mSize = size;
      33             :     }
      34          30 :     return sb;
      35             : }
      36             : 
      37             : 
      38          21 : ssize_t SharedBuffer::dealloc(const SharedBuffer* released)
      39             : {
      40          21 :     if (released->mRefs != 0) return -1; // XXX: invalid operation
      41          21 :     free(const_cast<SharedBuffer*>(released));
      42          21 :     return 0;
      43             : }
      44             : 
      45           0 : SharedBuffer* SharedBuffer::edit() const
      46             : {
      47           0 :     if (onlyOwner()) {
      48           0 :         return const_cast<SharedBuffer*>(this);
      49             :     }
      50           0 :     SharedBuffer* sb = alloc(mSize);
      51           0 :     if (sb) {
      52           0 :         memcpy(sb->data(), data(), size());
      53           0 :         release();
      54             :     }
      55           0 :     return sb;    
      56             : }
      57             : 
      58           0 : SharedBuffer* SharedBuffer::editResize(size_t newSize) const
      59             : {
      60           0 :     if (onlyOwner()) {
      61           0 :         SharedBuffer* buf = const_cast<SharedBuffer*>(this);
      62           0 :         if (buf->mSize == newSize) return buf;
      63           0 :         buf = (SharedBuffer*)realloc(buf, sizeof(SharedBuffer) + newSize);
      64           0 :         if (buf != NULL) {
      65           0 :             buf->mSize = newSize;
      66           0 :             return buf;
      67             :         }
      68             :     }
      69           0 :     SharedBuffer* sb = alloc(newSize);
      70           0 :     if (sb) {
      71           0 :         const size_t mySize = mSize;
      72           0 :         memcpy(sb->data(), data(), newSize < mySize ? newSize : mySize);
      73           0 :         release();
      74             :     }
      75           0 :     return sb;    
      76             : }
      77             : 
      78         360 : SharedBuffer* SharedBuffer::attemptEdit() const
      79             : {
      80         360 :     if (onlyOwner()) {
      81         360 :         return const_cast<SharedBuffer*>(this);
      82             :     }
      83           0 :     return 0;
      84             : }
      85             : 
      86           0 : SharedBuffer* SharedBuffer::reset(size_t new_size) const
      87             : {
      88             :     // cheap-o-reset.
      89           0 :     SharedBuffer* sb = alloc(new_size);
      90           0 :     if (sb) {
      91           0 :         release();
      92             :     }
      93           0 :     return sb;
      94             : }
      95             : 
      96           0 : void SharedBuffer::acquire() const {
      97           0 :     android_atomic_inc(&mRefs);
      98           0 : }
      99             : 
     100          21 : int32_t SharedBuffer::release(uint32_t flags) const
     101             : {
     102          21 :     int32_t prev = 1;
     103          21 :     if (onlyOwner() || ((prev = android_atomic_dec(&mRefs)) == 1)) {
     104          21 :         mRefs = 0;
     105          21 :         if ((flags & eKeepStorage) == 0) {
     106           0 :             free(const_cast<SharedBuffer*>(this));
     107             :         }
     108             :     }
     109          21 :     return prev;
     110             : }
     111             : 
     112             : 
     113             : }; // namespace stagefright

Generated by: LCOV version 1.13