1/********************************************************************
2 * Copyright (c) 2013 - 2014, Pivotal Inc.
3 * All rights reserved.
4 *
5 * Author: Zhanwei Wang
6 ********************************************************************/
7/********************************************************************
8 * 2014 -
9 * open source under Apache License Version 2.0
10 ********************************************************************/
11/**
12 * Licensed to the Apache Software Foundation (ASF) under one
13 * or more contributor license agreements. See the NOTICE file
14 * distributed with this work for additional information
15 * regarding copyright ownership. The ASF licenses this file
16 * to you under the Apache License, Version 2.0 (the
17 * "License"); you may not use this file except in compliance
18 * with the License. You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS,
24 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
27 */
28#include "Logger.h"
29#include "Packet.h"
30#include "PacketPool.h"
31
32namespace Hdfs {
33namespace Internal {
34
35PacketPool::PacketPool(int size) :
36 maxSize(size) {
37}
38
39shared_ptr<Packet> PacketPool::getPacket(int pktSize, int chunksPerPkt,
40 int64_t offsetInBlock, int64_t seqno, int checksumSize) {
41 if (packets.empty()) {
42 return shared_ptr<Packet>(
43 new Packet(pktSize, chunksPerPkt, offsetInBlock, seqno,
44 checksumSize));
45 } else {
46 shared_ptr<Packet> retval = packets.front();
47 packets.pop_front();
48 retval->reset(pktSize, chunksPerPkt, offsetInBlock, seqno,
49 checksumSize);
50 return retval;
51 }
52}
53
54void PacketPool::relesePacket(shared_ptr<Packet> packet) {
55 if (static_cast<int>(packets.size()) >= maxSize) {
56 return;
57 }
58
59 packets.push_back(packet);
60}
61
62}
63}
64