Home | History | Annotate | Download | only in src
      1 /* ------------------------------------------------------------------
      2  * Copyright (C) 1998-2009 PacketVideo
      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
     13  * express or implied.
     14  * See the License for the specific language governing permissions
     15  * and limitations under the License.
     16  * -------------------------------------------------------------------
     17  */
     18 // -*- c++ -*-
     19 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
     20 
     21 //           O S C L _ R E F C O U N T E R _ M E M F R A G
     22 
     23 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
     24 
     25 /*! \addtogroup osclbase OSCL Base
     26  *
     27  * @{
     28  */
     29 
     30 
     31 
     32 /**
     33  *  @file oscl_refcounter_memfrag.h
     34  *  @brief This file provides the definition of reference counted
     35  *  memory fragment, which provides access to a buffer and helps manage
     36  *  its manage its lifetime through the refcount.
     37  *
     38  */
     39 
     40 #ifndef OSCL_REFCOUNTER_MEMFRAG_H_INCLUDED
     41 #define OSCL_REFCOUNTER_MEMFRAG_H_INCLUDED
     42 
     43 #ifndef OSCL_BASE_H_INCLUDED
     44 #include "oscl_base.h"
     45 #endif
     46 
     47 #ifndef OSCL_REFCOUNTER_H_INCLUDED
     48 #include "oscl_refcounter.h"
     49 #endif
     50 
     51 
     52 /**
     53  * Class to contain a memory fragment with it's associated
     54  * reference counter.
     55  */
     56 class OsclRefCounterMemFrag
     57 {
     58     public:
     59 
     60         /**
     61          * Constructor.
     62          * A valid memory fragment and reference counter are
     63          * required as input.  The memory fragment structure
     64          * will be copied locally.
     65          *
     66          * @param m      reference to memory fragment
     67          * @param r      pointer to the reference counter associated with the
     68          *               memory fragment.
     69          */
     70         OsclRefCounterMemFrag(OsclMemoryFragment &m, OsclRefCounter *r,
     71                               uint32 in_capacity) :
     72                 memfrag(m), refcnt(r), capacity(in_capacity)
     73                 // no need to increment refcnt--it should already be done.
     74         {}
     75 
     76         /**
     77          * Copy constructor.
     78          */
     79         OsclRefCounterMemFrag(const OsclRefCounterMemFrag &x) :
     80                 memfrag(x.memfrag), refcnt(x.refcnt), capacity(x.capacity)
     81         {
     82             if (refcnt)
     83             {
     84                 refcnt->addRef();
     85             }
     86         }
     87 
     88         /**
     89          * Default constructor.
     90          */
     91         OsclRefCounterMemFrag()
     92         {
     93             memfrag.ptr = 0;
     94             memfrag.len = 0;
     95             refcnt = 0;
     96             capacity = 0;
     97         }
     98 
     99 
    100         /**
    101          * Assignment Operator
    102          */
    103         OsclRefCounterMemFrag& operator= (const OsclRefCounterMemFrag &x)
    104         {
    105             if (this == &x)
    106             {
    107                 // protect against self-assignment
    108                 return *this;
    109             }
    110 
    111             // remove ref for current memfrag
    112             if (refcnt)
    113             {
    114                 refcnt->removeRef();
    115             }
    116 
    117             // copy assigned object
    118             memfrag = x.memfrag;
    119             refcnt = x.refcnt;
    120             capacity = x.capacity;
    121 
    122             // add ref for new memfrag
    123             if (refcnt)
    124             {
    125                 refcnt->addRef();
    126             }
    127 
    128             return *this;
    129         }
    130 
    131         /**
    132          * Destructor.
    133          * Removes this object's reference from the reference counter.
    134          * The reference counter will not be deleted.  The reference
    135          * counter is designed to self-delete when it's reference
    136          * count reaches 0.
    137          */
    138         ~OsclRefCounterMemFrag()
    139         {
    140             if (refcnt)
    141             {
    142                 refcnt->removeRef();
    143             }
    144         }
    145 
    146         /**
    147          * Returns a pointer to the contained reference counter
    148          * object
    149          */
    150         OsclRefCounter* getRefCounter()
    151         {
    152             return refcnt;
    153         }
    154 
    155         /**
    156          * Returns a reference to the contained memory fragment
    157          * structure.
    158          */
    159         OsclMemoryFragment& getMemFrag()
    160         {
    161             return memfrag;
    162         }
    163 
    164         /**
    165          * Returns a pointer to the memory fragment data.
    166          */
    167         OsclAny* getMemFragPtr()
    168         {
    169             return memfrag.ptr;
    170         }
    171 
    172         /**
    173          * Returns the size of the memory fragment data which
    174          * equals its filled size.
    175          *
    176          * @return
    177          */
    178         uint32 getMemFragSize()
    179         {
    180             return memfrag.len;
    181         }
    182 
    183         /**
    184          * Returns the capacity of the memory fragment
    185          *
    186          * @return
    187          */
    188         uint32 getCapacity()
    189         {
    190             return capacity;
    191         }
    192 
    193         /**
    194          * Returns the reference counter's current count.
    195          */
    196         uint32 getCount()
    197         {
    198             return (refcnt) ? refcnt->getCount() : 0;
    199         }
    200 
    201 
    202 
    203     private:
    204 
    205         OsclMemoryFragment memfrag;
    206         OsclRefCounter *refcnt;
    207         uint32 capacity;
    208 };
    209 
    210 
    211 /*! @} */
    212 
    213 
    214 #endif // OSCL_REFCOUNTER_MEMFRAG_H
    215