12.07.2015 Views

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}else {p = new Packet;p->bits_ = new u_char[hdrsize_];if (p == 0 || p->bits_ == 0)abort();}return (p);/* alloc<strong>at</strong>e a packet with an n byte d<strong>at</strong>a buffer */inline Packet* Packet::alloc(int n){Packet* p = alloc();if (n > 0)p->allocd<strong>at</strong>a(n);return (p);}/* alloc<strong>at</strong>e an n byte d<strong>at</strong>a buffer to an existing packet */inline void Packet::allocd<strong>at</strong>a(int n){d<strong>at</strong>alen_ = n;d<strong>at</strong>a_ = new u_char[n];if (d<strong>at</strong>a_ == 0)abort();}inline void Packet::free(Packet* p){p->next_ = free_;free_ = p;if (p->d<strong>at</strong>alen_) {delete p->d<strong>at</strong>a_;p->d<strong>at</strong>alen_ = 0;}}inline Packet* Packet::copy() co<strong>ns</strong>t{Packet* p = alloc();memcpy(p->bits(), bits_, hdrlen_);if (d<strong>at</strong>alen_) {p->d<strong>at</strong>alen_ = d<strong>at</strong>alen_;p->d<strong>at</strong>a_ = new u_char[d<strong>at</strong>alen_];memcpy(p->d<strong>at</strong>a_, d<strong>at</strong>a_, d<strong>at</strong>alen_);}return (p);}<strong>The</strong> alloc() method is a support function commonly used to cre<strong>at</strong>e new packets. It is called by Agent::allocpkt()method on behalf of agents <strong>and</strong> is thus not normally invoked directly by most objects. It first <strong>at</strong>tempts to loc<strong>at</strong>e an old packeton the free list <strong>and</strong> if this fails alloc<strong>at</strong>es a new one using the C++ new oper<strong>at</strong>or. Note th<strong>at</strong> Packet class objects <strong>and</strong> BOBs arealloc<strong>at</strong>ed separ<strong>at</strong>ely. <strong>The</strong> free() method frees a packet by returning it to the free list. Note th<strong>at</strong> packets are never returned to121

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!