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