Home | History | Annotate | Download | only in hdr
      1 /*
      2  * Copyright (C) 2014 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 #ifndef DMALLOCATED_POINTERS_POOL_INCLUDE
     18 #define DMALLOCATED_POINTERS_POOL_INCLUDE
     19 
     20 #include "dmThreadQueue.h"
     21 #include "dmThreadHelper.h"
     22 #include <malloc.h>
     23 #include <stdio.h>
     24 #include <time.h>
     25 #include <sys/time.h>
     26 
     27 #include "dmstring.h"
     28 #include "dmvector.h"
     29 
     30 #define DM_MAX_TIMERS 10
     31 
     32 #ifdef DEBUG
     33 #include <map>
     34 
     35 class DMAllocatedPointersPool
     36 {
     37 public:
     38   enum { c_nExtraBytes = 16 };
     39 
     40    /**
     41   * Default constructor
     42   */
     43    DMAllocatedPointersPool(){}
     44 
     45    /**
     46   * Destructor
     47   */
     48   ~DMAllocatedPointersPool();
     49 
     50    /**
     51   * Checks if pointer exist in the pool
     52   * \param ptr [in] - pointer
     53   * \return TRUE if pointer is found
     54   */
     55   bool exists(void* ptr);
     56 
     57   /**
     58   * Appends pointer to the pool
     59   * \param ptr [in] - pointer
     60   */
     61   void append(void* ptr);
     62 
     63   /**
     64   * Removes pointer from the pool
     65   * \param ptr [in] - pointer
     66   */
     67   bool remove(void* ptr);
     68 
     69   /**
     70   * Prints unreleased pointers
     71   */
     72   void PrintUnreleased();
     73 
     74 
     75 private:
     76   /** Critical section */
     77   DMCriticalSection  m_csPointerPoolLock;
     78   /** Pool of allocated pointers */
     79   std::map<void*, int> 	m_listOfAllocatedPointers;
     80 };
     81 
     82 
     83 /*====================================================================================================
     84  Inline functions implementation
     85 ==================================================================================================*/
     86 inline bool DMAllocatedPointersPool::exists(void* ptr)
     87 {
     88   DMSingleLock oLock( m_csPointerPoolLock );
     89 
     90   return m_listOfAllocatedPointers.find( ptr ) != m_listOfAllocatedPointers.end();
     91 }
     92 
     93 
     94 inline void DMAllocatedPointersPool::append(void* ptr)
     95 {
     96   DMSingleLock oLock( m_csPointerPoolLock );
     97 
     98   m_listOfAllocatedPointers[ptr] = 0; // add new pointer
     99 }
    100 
    101 inline bool DMAllocatedPointersPool::remove(void* ptr)
    102 {
    103   DMSingleLock oLock( m_csPointerPoolLock );
    104 
    105   std::map<void*, int>::iterator it = m_listOfAllocatedPointers.find(ptr);
    106 
    107   if ( it == m_listOfAllocatedPointers.end() )
    108     return false;
    109 
    110   m_listOfAllocatedPointers.erase( it );
    111   return true;
    112 }
    113 
    114 #endif //DEBUG
    115 
    116 #endif
    117