xorp

trie_payload.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2001-2009 XORP, Inc.
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, Version 2, June
00007 // 1991 as published by the Free Software Foundation. Redistribution
00008 // and/or modification of this program under the terms of any other
00009 // version of the GNU General Public License is not permitted.
00010 // 
00011 // This program is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00014 // see the GNU General Public License, Version 2, a copy of which can be
00015 // found in the XORP LICENSE.gpl file.
00016 // 
00017 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00018 // http://xorp.net
00019 
00020 // $XORP: xorp/bgp/harness/trie_payload.hh,v 1.15 2008/11/08 06:14:45 mjh Exp $
00021 
00022 #ifndef __BGP_HARNESS_TRIE_PAYLOAD_HH__
00023 #define __BGP_HARNESS_TRIE_PAYLOAD_HH__
00024 
00031 class TrieData {
00032 public:
00033     TrieData(const TimeVal& tv, const uint8_t *buf, size_t len,
00034          const BGPPeerData *peerdata,
00035          TrieData* &first, TrieData* &last) : _tv(tv),
00036                           _first(first),
00037                           _last(last) {
00038     _packet = new UpdatePacket(buf, len, peerdata, 0, false);
00039     _refcnt = 1;
00040 
00041     _next = 0;
00042     _prev = _last;      
00043 
00044     if(0 == _first)
00045         _first = this;
00046     if(0 != _last) 
00047         _last->_next = this;
00048     _last = this;
00049     }
00050 
00051     void incr_refcnt() {
00052     _refcnt++;
00053     }
00054 
00055     bool decr_refcnt() {
00056     _refcnt--;
00057 
00058     XLOG_ASSERT(_refcnt >= 0);
00059 
00060     return 0 == _refcnt;
00061     }
00062 
00063     const UpdatePacket *data() const {
00064     XLOG_ASSERT(_refcnt > 0);
00065 
00066     return _packet;
00067     }
00068 
00069     const TimeVal& tv() const {
00070     return _tv;
00071     }
00072     
00073     const TrieData* next() const {
00074     return _next;
00075     }
00076 
00077     ~TrieData() {
00078     XLOG_ASSERT(0 == _refcnt);
00079 
00080     if(this == _first)
00081         _first = _next;
00082     else
00083         _prev->_next = _next;
00084     if(this == _last)
00085         _last = _prev;
00086     if(0 != _next) {
00087         _next->_prev = _prev;
00088     }
00089 //  debug_msg("Deleting: %s\n", _packet->str().c_str());
00090     delete _packet;
00091     }
00092 
00093 private:
00094     TimeVal _tv;
00095     TrieData* &_first;
00096     TrieData* &_last;
00097 
00098     int _refcnt;
00099     UpdatePacket *_packet;
00100     TrieData *_next;
00101     TrieData *_prev;
00102 };
00103 
00107 class TriePayload {
00108 public:
00109     TriePayload() : _data(0) {}
00110 
00111     TriePayload(const TimeVal& tv, const uint8_t *buf, size_t len,
00112         const BGPPeerData *peerdata,
00113         TrieData* &first, TrieData* &last) {
00114     _data = new TrieData(tv, buf, len, peerdata, first, last);
00115     }
00116 
00117     ~TriePayload() {
00118     zap();
00119     }
00120 
00121     TriePayload(const TriePayload& rhs) {
00122     _data = 0;
00123     copy(rhs);
00124     }
00125 
00126     TriePayload& operator=(const TriePayload& rhs) {
00127     if(&rhs == this)
00128         return *this;
00129     zap();
00130     copy(rhs);
00131         
00132     return *this;
00133     }
00134 
00135     void copy(const TriePayload& rhs) {
00136     if(rhs._data) {
00137 //      debug_msg("refcnt: %d %#x\n", rhs._data->_refcnt + 1, rhs._data);
00138         rhs._data->incr_refcnt();
00139         _data = rhs._data;
00140     }
00141     }
00142 
00143     const UpdatePacket *get() const {
00144     if(0 == _data)
00145         return 0;
00146     return _data->data();
00147     }
00148 
00149     const UpdatePacket *get(TimeVal& tv) const {
00150     if(0 == _data)
00151         return 0;
00152     tv = _data->tv();
00153     return _data->data();
00154     }
00155 
00156     void zap() {
00157 //  if(_data)
00158 //      debug_msg("refcnt: %d %#x\n", _data->_refcnt - 1, _data);
00159     if(_data && _data->decr_refcnt()) {
00160         delete _data;
00161     }
00162     _data = 0;
00163     }
00164 
00165 private:
00166     TrieData *_data;
00167 };
00168 
00169 #endif // __BGP_HARNESS_TRIE_PAYLOAD_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations