@ -58,23 +58,12 @@ static inline PktPool *GetThreadPacketPool(void)
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					{ 
 
					 
					 
					 
					{ 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    return  & thread_pkt_pool ; 
 
					 
					 
					 
					    return  & thread_pkt_pool ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					} 
 
					 
					 
					 
					} 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					static  inline  PktPool  * ThreadPacketPoolCreate ( void ) 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					{ 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    /* Nothing to do since __thread statically allocates the memory. */ 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    return  GetThreadPacketPool ( ) ; 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					} 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# else 
 
					 
					 
					 
					# else 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					/* __thread not supported. */ 
 
					 
					 
					 
					/* __thread not supported. */ 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					static  pthread_key_t  pkt_pool_thread_key ; 
 
					 
					 
					 
					static  pthread_key_t  pkt_pool_thread_key ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					static  SCMutex  pkt_pool_thread_key_mutex  =  SCMUTEX_INITIALIZER ; 
 
					 
					 
					 
					static  SCMutex  pkt_pool_thread_key_mutex  =  SCMUTEX_INITIALIZER ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					static  int  pkt_pool_thread_key_initialized  =  0 ; 
 
					 
					 
					 
					static  int  pkt_pool_thread_key_initialized  =  0 ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					static  inline  PktPool  * GetThreadPacketPool ( void ) 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					{ 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    return  ( PktPool * ) pthread_getspecific ( pkt_pool_thread_key ) ; 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					} 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					static  void  PktPoolThreadDestroy ( void  *  buf ) 
 
					 
					 
					 
					static  void  PktPoolThreadDestroy ( void  *  buf ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					{ 
 
					 
					 
					 
					{ 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    free ( buf ) ; 
 
					 
					 
					 
					    free ( buf ) ; 
 
				
			 
			
		
	
	
		
		
			
				
					
						
							
								 
							 
						
						
							
								 
							 
						
						
					 
					 
					@ -102,15 +91,10 @@ static void TmqhPacketpoolInit(void)
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    SCMutexUnlock ( & pkt_pool_thread_key_mutex ) ; 
 
					 
					 
					 
					    SCMutexUnlock ( & pkt_pool_thread_key_mutex ) ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					} 
 
					 
					 
					 
					} 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					static  inline  PktPool  * ThreadPacketPoolCreate ( void ) 
 
					 
					 
					 
					static  PktPool  * ThreadPacketPoolCreate ( void ) 
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					{ 
 
					 
					 
					 
					{ 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    TmqhPacketpoolInit ( ) ; 
 
					 
					 
					 
					    TmqhPacketpoolInit ( ) ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    /* Check that the pool is not already created */ 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    PktPool  * pool  =  GetThreadPacketPool ( ) ; 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    if  ( pool ) 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					        return  pool ; 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    /* Create a new pool for this thread. */ 
 
					 
					 
					 
					    /* Create a new pool for this thread. */ 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    pool  =  ( PktPool * ) SCMallocAligned ( sizeof ( PktPool ) ,  CLS ) ; 
 
					 
					 
					 
					    pool  =  ( PktPool * ) SCMallocAligned ( sizeof ( PktPool ) ,  CLS ) ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    if  ( pool  = =  NULL )  { 
 
					 
					 
					 
					    if  ( pool  = =  NULL )  { 
 
				
			 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
							 
						
					 
					 
					@ -125,6 +109,15 @@ static inline PktPool *ThreadPacketPoolCreate(void)
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    return  pool ; 
 
					 
					 
					 
					    return  pool ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					} 
 
					 
					 
					 
					} 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					static  inline  PktPool  * GetThreadPacketPool ( void ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					{ 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    PktPool *  pool  =  ( PktPool * ) pthread_getspecific ( pkt_pool_thread_key ) ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    if  ( pool  = =  NULL ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					      pool  =  ThreadPacketPoolCreate ( ) ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    return  pool ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					} 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					# endif 
 
					 
					 
					 
					# endif 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					/**
 
 
					 
					 
					 
					/**
 
 
				
			 
			
		
	
	
		
		
			
				
					
						
							
								 
							 
						
						
							
								 
							 
						
						
					 
					 
					@ -168,6 +161,15 @@ static void PacketPoolStorePacket(Packet *p)
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    PacketPoolReturnPacket ( p ) ; 
 
					 
					 
					 
					    PacketPoolReturnPacket ( p ) ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					} 
 
					 
					 
					 
					} 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					static  void  PacketPoolGetReturnedPackets ( PktPool  * pool ) 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					{ 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    SCMutexLock ( & pool - > return_stack . mutex ) ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    /* Move all the packets from the locked return stack to the local stack. */ 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    pool - > head  =  pool - > return_stack . head ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    pool - > return_stack . head  =  NULL ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    SCMutexUnlock ( & pool - > return_stack . mutex ) ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					} 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					/** \brief Get a new packet from the packet pool
 
 
					 
					 
					 
					/** \brief Get a new packet from the packet pool
 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 * 
 
					 
					 
					 
					 * 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 *  Only  allocates  from  the  thread ' s  local  stack ,  or  mallocs  new  packets . 
 
					 
					 
					 
					 *  Only  allocates  from  the  thread ' s  local  stack ,  or  mallocs  new  packets . 
 
				
			 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
							 
						
					 
					 
					@ -189,11 +191,7 @@ Packet *PacketPoolGetPacket(void)
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    /* Local Stack is empty, so check the return stack, which requires
 
 
					 
					 
					 
					    /* Local Stack is empty, so check the return stack, which requires
 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					     *  locking .  */ 
 
					 
					 
					 
					     *  locking .  */ 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    SCMutexLock ( & pool - > return_stack . mutex ) ; 
 
					 
					 
					 
					    PacketPoolGetReturnedPackets ( pool ) ; 
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					    /* Move all the packets from the locked return stack to the local stack. */ 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    pool - > head  =  pool - > return_stack . head ; 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    pool - > return_stack . head  =  NULL ; 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    SCMutexUnlock ( & pool - > return_stack . mutex ) ; 
 
					 
					 
					 
					 
				
			 
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    /* Try to allocate again. Need to check for not empty again, since the
 
 
					 
					 
					 
					    /* Try to allocate again. Need to check for not empty again, since the
 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					     *  return  stack  might  have  been  empty  too . 
 
					 
					 
					 
					     *  return  stack  might  have  been  empty  too . 
 
				
			 
			
		
	
	
		
		
			
				
					
						
							
								 
							 
						
						
							
								 
							 
						
						
					 
					 
					@ -266,7 +264,7 @@ void PacketPoolInit(void)
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					{ 
 
					 
					 
					 
					{ 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    extern  intmax_t  max_pending_packets ; 
 
					 
					 
					 
					    extern  intmax_t  max_pending_packets ; 
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    PktPool  * my_pool  =   ThreadPacketPoolCreate ( ) ; 
 
					 
					 
					 
					    PktPool  * my_pool  =  Get ThreadPacketPool( ) ; 
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					    SCMutexInit ( & my_pool - > return_stack . mutex ,  NULL ) ; 
 
					 
					 
					 
					    SCMutexInit ( & my_pool - > return_stack . mutex ,  NULL ) ;