1 /*------------------------------------------------------------------------- 2 * drawElements Quality Program Reference Renderer 3 * ----------------------------------------------- 4 * 5 * Copyright 2014 The Android Open Source Project 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * 19 *//*! 20 * \file 21 * \brief Vertex packet and Vertex packet allocator 22 *//*--------------------------------------------------------------------*/ 23 24 #include "rrVertexPacket.hpp" 25 26 namespace rr 27 { 28 29 VertexPacket::VertexPacket (void) 30 { 31 } 32 33 VertexPacket::~VertexPacket (void) 34 { 35 } 36 37 VertexPacketAllocator::VertexPacketAllocator (const size_t numberOfVertexOutputs) 38 : m_numberOfVertexOutputs(numberOfVertexOutputs) 39 { 40 } 41 42 VertexPacketAllocator::~VertexPacketAllocator (void) 43 { 44 for (size_t i = 0; i < m_allocations.size(); ++i) 45 delete [] m_allocations[i]; 46 m_allocations.clear(); 47 } 48 49 std::vector<VertexPacket*> VertexPacketAllocator::allocArray (size_t count) 50 { 51 if (!count) 52 return std::vector<VertexPacket*>(); 53 54 const size_t extraVaryings = (m_numberOfVertexOutputs == 0) ? (0) : (m_numberOfVertexOutputs-1); 55 const size_t packetSize = sizeof(VertexPacket) + extraVaryings * sizeof(GenericVec4); 56 57 std::vector<VertexPacket*> retVal; 58 deInt8* ptr = new deInt8[packetSize * count]; // throws bad_alloc => ok 59 60 // *.push_back might throw bad_alloc 61 try 62 { 63 // run ctors 64 for (size_t i = 0; i < count; ++i) 65 retVal.push_back(new (ptr + i*packetSize) VertexPacket()); // throws bad_alloc 66 67 m_allocations.push_back(ptr); // throws bad_alloc 68 } 69 catch (std::bad_alloc& ) 70 { 71 delete [] ptr; 72 throw; 73 } 74 75 return retVal; 76 } 77 78 VertexPacket* VertexPacketAllocator::alloc (void) 79 { 80 const size_t poolSize = 8; 81 82 if (m_singleAllocPool.empty()) 83 m_singleAllocPool = allocArray(poolSize); 84 85 VertexPacket* packet = *--m_singleAllocPool.end(); 86 m_singleAllocPool.pop_back(); 87 return packet; 88 } 89 90 } // rr 91