• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef PV_THREADSAFE_MEMPOOL_H_INCLUDED
19 #define PV_THREADSAFE_MEMPOOL_H_INCLUDED
20 
21 
22 #ifndef OSCL_MEM_MEMPOOL_H_INCLUDED
23 #include "oscl_mem_mempool.h"
24 #endif
25 
26 #ifndef OSCL_MUTEX_H_INCLUDED
27 #include "oscl_mutex.h"
28 #endif
29 
30 class ThreadSafeMemPoolFixedChunkAllocator: public OsclMemPoolFixedChunkAllocator
31 {
32     public:
33         /**
34           * If numchunk and chunksize parameters are not set,
35           * memory pool of 1 chunk will be created in the first call to allocate.
36           * The chunk size will be set to the n passed in for allocate().
37           * If numchunk parameter is set to 0, the memory pool will use 1 for numchunk.
38           *
39           * @return void
40           *
41           */
42         OSCL_IMPORT_REF static ThreadSafeMemPoolFixedChunkAllocator *Create(const uint32 numchunk = 1, const uint32 chunksize = 0, Oscl_DefAlloc* gen_alloc = NULL);
43         OSCL_IMPORT_REF void Delete();
44 
45         OSCL_IMPORT_REF virtual ~ThreadSafeMemPoolFixedChunkAllocator();
46 
47 
48         /** This API throws an exception when n is greater than the fixed chunk size or there are no free chunk available in the pool.
49           * If the memory pool hasn't been created yet, the pool will be created with chunk size equal to n so n must be greater than 0.
50           * Exception will be thrown if memory allocation for the memory pool fails.
51           *
52           * @return pointer to available chunk from memory pool
53           *
54           */
55         OSCL_IMPORT_REF virtual OsclAny* allocate(const uint32 n);
56 
57         /** This API throws an exception when the pointer p passed in is not part of the memory pool.
58            * Exception will be thrown if the memory pool is not set up yet.
59            *
60            * @return void
61            *
62            */
63         OSCL_IMPORT_REF virtual void deallocate(OsclAny* p);
64 
65         /** This API will set the flag to send a callback via specified observer object when the
66           * next memory chunk is deallocated by deallocate() call.
67           * The observer must be implemented in a THREADSAFE manner
68           * Note that if the mempool to be destroyed (due to refcount going to 0)
69           * there will be NO callback to the observer
70           * @return void
71           *
72           */
73         OSCL_IMPORT_REF virtual void notifyfreechunkavailable(OsclMemPoolFixedChunkAllocatorObserver& obs, OsclAny* aContextData = NULL);
74 
75         /** This API will cancel any past callback requests..
76           *
77           * @return void
78           *
79           */
80         OSCL_IMPORT_REF virtual void CancelFreeChunkAvailableCallback();
81 
82         /** Increments the reference count for this memory pool allocator
83           *
84           * @return void
85           *
86           */
87         OSCL_IMPORT_REF void addRef();
88 
89         /** Decrements the reference count for this memory pool allocator
90           * When the reference count goes to 0, this instance of the memory pool object is deleted
91           *
92           * @return void
93           *
94           */
95         OSCL_IMPORT_REF void removeRef();
96 
97     protected:
98         OSCL_IMPORT_REF ThreadSafeMemPoolFixedChunkAllocator(const uint32 numchunk = 1, const uint32 chunksize = 0, Oscl_DefAlloc* gen_alloc = NULL);
99 
100         /* copy constructor - declared protected to prevent usage of the default copy constructor */
101         /* NOTE: This copy constructor should never be used */
ThreadSafeMemPoolFixedChunkAllocator(const ThreadSafeMemPoolFixedChunkAllocator & alloc)102         ThreadSafeMemPoolFixedChunkAllocator(const ThreadSafeMemPoolFixedChunkAllocator &alloc): OsclMemPoolFixedChunkAllocator(alloc) {};
103 
104 
105     protected:
106         OSCL_IMPORT_REF virtual void createmempool();
107         OSCL_IMPORT_REF virtual void destroymempool();
108 
109         OSCL_IMPORT_REF void addRef_internal();
110         OSCL_IMPORT_REF void removeRef_internal();
111 
112         uint32 iNumChunk;
113         uint32 iChunkSize;
114         uint32 iChunkSizeMemAligned;
115         Oscl_DefAlloc* iMemPoolAllocator;
116         OsclAny* iMemPool;
117 
118         Oscl_Vector<OsclAny*, OsclMemAllocator> iFreeMemChunkList;
119 
120         // For thread-safe applications, e.g. when one thread does allocate() and the other deallocate()
121         // the mempool needs to create a mutex object
122         //  - Each sensitive shared data access is then protected by this lock to ensure thread-safety
123         OsclMutex iMemPoolMutex;
124 
125         bool iCheckNextAvailableFreeChunk;
126         OsclMemPoolFixedChunkAllocatorObserver* iObserver;
127         OsclAny* iNextAvailableContextData;
128 
129         int32 iRefCount;
130 
131 
132 };
133 
134 #endif
135