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 PVMF_RESIZABLE_SIMPLE_MEDIAMSG_H_INCLUDED 19 #define PVMF_RESIZABLE_SIMPLE_MEDIAMSG_H_INCLUDED 20 21 #ifndef OSCL_BASE_H_INCLUDED 22 #include "oscl_base.h" 23 #endif 24 25 #ifndef OSCL_MEM_H_INCLUDED 26 #include "oscl_mem.h" 27 #endif 28 29 #ifndef OSCL_DEFALLOC_H_INCLUDED 30 #include "oscl_defalloc.h" 31 #endif 32 33 #ifndef OSCL_BASE_ALLOC_H_INCLUDED 34 #include "oscl_base_alloc.h" 35 #endif 36 37 #ifndef OSCL_REFCOUNTER_H_INCLUDED 38 #include "oscl_refcounter.h" 39 #endif 40 41 #ifndef OSCL_MEM_BASIC_FUNCTIONS_H 42 #include "oscl_mem_basic_functions.h" 43 #endif 44 45 #ifndef OSCL_MEM_H_INCLUDED 46 #include "oscl_mem.h" 47 #endif 48 49 #ifndef OSCL_MEM_MEMPOOL_H_INCLUDED 50 #include "oscl_mem_mempool.h" 51 #endif 52 53 #ifndef PVMF_SIMPLE_MEDIA_BUFFER_H_INCLUDED 54 #include "pvmf_simple_media_buffer.h" 55 #endif 56 57 58 class OsclMemPoolResizableAllocatorCleanupDA : public OsclDestructDealloc 59 { 60 public: OsclMemPoolResizableAllocatorCleanupDA(Oscl_DefAlloc * in_gen_alloc)61 OsclMemPoolResizableAllocatorCleanupDA(Oscl_DefAlloc* in_gen_alloc) : 62 gen_alloc(in_gen_alloc) {}; 63 ~OsclMemPoolResizableAllocatorCleanupDA()64 virtual ~OsclMemPoolResizableAllocatorCleanupDA() {}; 65 destruct_and_dealloc(OsclAny * ptr)66 virtual void destruct_and_dealloc(OsclAny* ptr) 67 { 68 gen_alloc->deallocate(ptr); 69 } 70 private: 71 Oscl_DefAlloc* gen_alloc; 72 }; 73 74 class PVMFResizableSimpleMediaMsgAlloc 75 { 76 public: PVMFResizableSimpleMediaMsgAlloc(OsclMemPoolResizableAllocator * in_gen_alloc)77 PVMFResizableSimpleMediaMsgAlloc(OsclMemPoolResizableAllocator* in_gen_alloc) 78 { 79 if (in_gen_alloc) 80 { 81 gen_alloc = in_gen_alloc; 82 gen_alloc->enablenullpointerreturn(); 83 iBufferOverhead = 0; 84 uint aligned_class_size = 85 oscl_mem_aligned_size(sizeof(PVMFSimpleMediaBuffer)); 86 uint aligned_cleanup_size = 87 oscl_mem_aligned_size(sizeof(OsclMemPoolResizableAllocatorCleanupDA)); 88 uint aligned_refcnt_size = 89 oscl_mem_aligned_size(sizeof(OsclRefCounterDA)); 90 iBufferOverhead = (aligned_refcnt_size + 91 aligned_cleanup_size + 92 aligned_class_size); 93 94 } 95 else 96 { 97 OSCL_LEAVE(OsclErrArgument); 98 } 99 }; 100 ~PVMFResizableSimpleMediaMsgAlloc()101 virtual ~PVMFResizableSimpleMediaMsgAlloc() 102 { 103 }; 104 allocate(uint32 size)105 OsclSharedPtr<PVMFMediaDataImpl> allocate(uint32 size) 106 { 107 OsclSharedPtr<PVMFMediaDataImpl> shared_media_data; 108 109 if (size == 0) 110 { 111 OSCL_ASSERT(false); 112 } 113 114 uint aligned_in_size = oscl_mem_aligned_size(size); 115 116 uint8* my_ptr; 117 OsclRefCounter* my_refcnt; 118 119 uint aligned_class_size = 120 oscl_mem_aligned_size(sizeof(PVMFSimpleMediaBuffer)); 121 122 uint aligned_cleanup_size = 123 oscl_mem_aligned_size(sizeof(OsclMemPoolResizableAllocatorCleanupDA)); 124 125 uint aligned_refcnt_size = 126 oscl_mem_aligned_size(sizeof(OsclRefCounterDA)); 127 128 my_ptr = (uint8*) gen_alloc->allocate(aligned_refcnt_size + 129 aligned_cleanup_size + 130 aligned_class_size + 131 aligned_in_size); 132 133 if (my_ptr == NULL) 134 { 135 return shared_media_data; 136 } 137 138 OsclMemPoolResizableAllocatorCleanupDA *my_cleanup = 139 OSCL_PLACEMENT_NEW(my_ptr + aligned_refcnt_size, OsclMemPoolResizableAllocatorCleanupDA(gen_alloc)); 140 141 my_refcnt = 142 OSCL_PLACEMENT_NEW(my_ptr, OsclRefCounterDA(my_ptr, my_cleanup)); 143 144 my_ptr += aligned_refcnt_size + aligned_cleanup_size; 145 146 void* ptr; 147 ptr = my_ptr + aligned_class_size; 148 149 PVMFMediaDataImpl* media_data_ptr = 150 new(my_ptr) PVMFSimpleMediaBuffer(ptr, 151 aligned_in_size, 152 my_refcnt); 153 154 shared_media_data.Bind(media_data_ptr, my_refcnt); 155 return shared_media_data; 156 } 157 ResizeMemoryFragment(OsclSharedPtr<PVMFMediaDataImpl> & aSharedBuffer)158 void ResizeMemoryFragment(OsclSharedPtr<PVMFMediaDataImpl>& aSharedBuffer) 159 { 160 OsclRefCounterMemFrag memFrag; 161 aSharedBuffer->getMediaFragment(0, memFrag); 162 uint32 currCapacity = memFrag.getCapacity(); 163 uint32 bytesUsed = memFrag.getMemFragSize(); 164 165 //uint32 alignedBytesUsed = bytesUsed; 166 uint32 alignedBytesUsed = oscl_mem_aligned_size(bytesUsed); 167 168 if (alignedBytesUsed < currCapacity) 169 { 170 uint32 bytesToReclaim = (currCapacity - alignedBytesUsed); 171 OsclMemPoolResizableAllocator* dataAllocator = 172 reinterpret_cast<OsclMemPoolResizableAllocator*>(gen_alloc); 173 /* Account for the overhead */ 174 uint8* memFragPtr = (uint8*)(memFrag.getMemFragPtr()); 175 uint8* ptr = (memFragPtr - iBufferOverhead); 176 dataAllocator->trim((OsclAny*)ptr, bytesToReclaim); 177 aSharedBuffer->setCapacity(alignedBytesUsed); 178 } 179 } 180 GetMediaMsgAllocationOverheadBytes()181 uint32 GetMediaMsgAllocationOverheadBytes() 182 { 183 return iBufferOverhead; 184 } 185 186 private: 187 uint iBufferOverhead; 188 OsclMemPoolResizableAllocator* gen_alloc; 189 }; 190 191 class PVMFResizableSimpleMediaMsgAllocCleanupSA : public OsclDestructDealloc 192 { 193 public: ~PVMFResizableSimpleMediaMsgAllocCleanupSA()194 virtual ~PVMFResizableSimpleMediaMsgAllocCleanupSA() {}; 195 destruct_and_dealloc(OsclAny * ptr)196 virtual void destruct_and_dealloc(OsclAny* ptr) 197 { 198 uint8* tmp_ptr = (uint8*) ptr; 199 200 uint aligned_refcnt_size = 201 oscl_mem_aligned_size(sizeof(OsclRefCounterSA<PVMFResizableSimpleMediaMsgAllocCleanupSA>)); 202 203 tmp_ptr += aligned_refcnt_size; 204 PVMFResizableSimpleMediaMsgAlloc* dataBufferAlloc = 205 reinterpret_cast<PVMFResizableSimpleMediaMsgAlloc*>(tmp_ptr); 206 dataBufferAlloc->~PVMFResizableSimpleMediaMsgAlloc(); 207 OsclMemAllocator alloc; 208 alloc.deallocate(ptr); 209 } 210 }; 211 212 #endif //PVMF_RESIZABLE_SIMPLE_MEDIAMSG_H_INCLUDED 213 214 215