]> granicus.if.org Git - transmission/commitdiff
(trunk libT) yet even *more* assertions for Biiaru and persept :)
authorCharles Kerr <charles@transmissionbt.com>
Thu, 29 Jan 2009 16:56:43 +0000 (16:56 +0000)
committerCharles Kerr <charles@transmissionbt.com>
Thu, 29 Jan 2009 16:56:43 +0000 (16:56 +0000)
libtransmission/session.c
libtransmission/session.h
libtransmission/torrent.c
libtransmission/trevent.c

index ca92f42ab2261a8320edae2958a87fa57b1ff732..ba5ecb5e17210b561d0bb9f74fd5e0cccbe75f9a 100644 (file)
@@ -379,6 +379,8 @@ tr_sessionInit( const char  * tag,
     session->bandwidth = tr_bandwidthNew( session, NULL );
     session->lock = tr_lockNew( );
     session->tag = tr_strdup( tag );
+    session->magicNumber = SESSION_MAGIC_NUMBER;
+
     dbgmsg( "tr_sessionInit: the session's top-level bandwidth object is %p", session->bandwidth );
 
     tr_bencInitDict( &settings, 0 );
@@ -540,6 +542,8 @@ static void
 tr_sessionInitImpl( void * vsession )
 {
     tr_session * session = vsession;
+
+    assert( tr_isSession( session ) );
  
     /* first %s is the application name
        second %s is the version number */
@@ -558,6 +562,8 @@ tr_sessionInitImpl( void * vsession )
 void
 tr_sessionSetDownloadDir( tr_session * session, const char * dir )
 {
+    assert( tr_isSession( session ) );
+
     if( session->downloadDir != dir )
     {
         tr_free( session->downloadDir );
@@ -568,6 +574,8 @@ tr_sessionSetDownloadDir( tr_session * session, const char * dir )
 const char *
 tr_sessionGetDownloadDir( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return session->downloadDir;
 }
 
@@ -578,19 +586,23 @@ tr_sessionGetDownloadDir( const tr_session * session )
 void
 tr_globalLock( tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     tr_lockLock( session->lock );
 }
 
 void
 tr_globalUnlock( tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     tr_lockUnlock( session->lock );
 }
 
 tr_bool
 tr_globalIsLocked( const tr_session * session )
 {
-    return session && tr_lockHave( session->lock );
+    return tr_isSession( session ) && tr_lockHave( session->lock );
 }
 
 /***********************************************************************
@@ -621,7 +633,11 @@ tr_setBindPortImpl( void * vdata )
 static void
 setPortImpl( tr_session * session, tr_port port )
 {
-    struct bind_port_data * data = tr_new( struct bind_port_data, 1 );
+    struct bind_port_data * data;
+
+    assert( tr_isSession( session ) );
+
+    data = tr_new( struct bind_port_data, 1 );
     data->session = session;
     data->port = port;
     tr_runInEventThread( session, tr_setBindPortImpl, data );
@@ -631,6 +647,8 @@ void
 tr_sessionSetPeerPort( tr_session * session,
                        tr_port      port )
 {
+    assert( tr_isSession( session ) );
+
     session->isPortRandom = FALSE;
     session->peerPort = port;
     setPortImpl( session, session->peerPort );
@@ -639,6 +657,8 @@ tr_sessionSetPeerPort( tr_session * session,
 tr_port
 tr_sessionSetPeerPortRandom( tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     session->isPortRandom = TRUE;
     session->peerPort = getRandomPort( session );
     setPortImpl( session, session->peerPort );
@@ -648,7 +668,7 @@ tr_sessionSetPeerPortRandom( tr_session * session )
 tr_port
 tr_sessionGetPeerPort( const tr_session * session )
 {
-    assert( session );
+    assert( tr_isSession( session ) );
 
     return session->peerPort;
 }
@@ -656,6 +676,8 @@ tr_sessionGetPeerPort( const tr_session * session )
 tr_port_forwarding
 tr_sessionGetPortForwarding( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return tr_sharedTraversalStatus( session->shared );
 }
 
@@ -666,7 +688,11 @@ tr_sessionGetPortForwarding( const tr_session * session )
 static void
 updateBandwidth( tr_session * session, tr_direction dir )
 {
-    const tr_bool zeroCase = session->speedLimit[dir] < 1 && session->isSpeedLimited[dir];
+    tr_bool zeroCase;
+
+    assert( tr_isSession( session ) );
+
+    zeroCase = session->speedLimit[dir] < 1 && session->isSpeedLimited[dir];
 
     tr_bandwidthSetLimited( session->bandwidth, dir, session->isSpeedLimited[dir] && !zeroCase );
 
@@ -678,7 +704,7 @@ tr_sessionSetSpeedLimitEnabled( tr_session      * session,
                                 tr_direction      dir,
                                 tr_bool           isLimited )
 {
-    assert( session );
+    assert( tr_isSession( session ) );
     assert( tr_isDirection( dir ) );
 
     session->isSpeedLimited[dir] = isLimited;
@@ -690,7 +716,7 @@ tr_sessionSetSpeedLimit( tr_session    * session,
                          tr_direction    dir,
                          int             desiredSpeed )
 {
-    assert( session );
+    assert( tr_isSession( session ) );
     assert( tr_isDirection( dir ) );
 
     session->speedLimit[dir] = desiredSpeed;
@@ -701,7 +727,7 @@ tr_bool
 tr_sessionIsSpeedLimitEnabled( const tr_session  * session,
                                tr_direction        dir )
 {
-    assert( session );
+    assert( tr_isSession( session ) );
     assert( tr_isDirection( dir ) );
 
     return session->isSpeedLimited[dir];
@@ -711,7 +737,7 @@ int
 tr_sessionGetSpeedLimit( const tr_session  * session,
                          tr_direction        dir )
 {
-    assert( session );
+    assert( tr_isSession( session ) );
     assert( tr_isDirection( dir ) );
 
     return session->speedLimit[dir];
@@ -722,27 +748,35 @@ tr_sessionGetSpeedLimit( const tr_session  * session,
 ***/
 
 void
-tr_sessionSetPeerLimit( tr_session * session UNUSED,
+tr_sessionSetPeerLimit( tr_session * session,
                         uint16_t     maxGlobalPeers )
 {
+    assert( tr_isSession( session ) );
+
     tr_fdSetPeerLimit( maxGlobalPeers );
 }
 
 uint16_t
-tr_sessionGetPeerLimit( const tr_session * session UNUSED )
+tr_sessionGetPeerLimit( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return tr_fdGetPeerLimit( );
 }
 
 void
 tr_sessionSetPeerLimitPerTorrent( tr_session  * session, uint16_t n )
 {
+    assert( tr_isSession( session ) );
+
     session->peerLimitPerTorrent = n;
 }
 
 uint16_t
 tr_sessionGetPeerLimitPerTorrent( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return session->peerLimitPerTorrent;
 }
 
@@ -753,19 +787,19 @@ tr_sessionGetPeerLimitPerTorrent( const tr_session * session )
 double
 tr_sessionGetPieceSpeed( const tr_session * session, tr_direction dir )
 {
-    return session ? tr_bandwidthGetPieceSpeed( session->bandwidth, 0, dir ) : 0.0;
+    return tr_isSession( session ) ? tr_bandwidthGetPieceSpeed( session->bandwidth, 0, dir ) : 0.0;
 }
 
 double
 tr_sessionGetRawSpeed( const tr_session * session, tr_direction dir )
 {
-    return session ? tr_bandwidthGetPieceSpeed( session->bandwidth, 0, dir ) : 0.0;
+    return tr_isSession( session ) ? tr_bandwidthGetPieceSpeed( session->bandwidth, 0, dir ) : 0.0;
 }
 
 int
 tr_sessionCountTorrents( const tr_session * session )
 {
-    return session->torrentCount;
+    return tr_isSession( session ) ? session->torrentCount : 0;
 }
 
 static int
@@ -790,6 +824,8 @@ tr_closeAllConnections( void * vsession )
     int           i, n;
     tr_torrent ** torrents;
 
+    assert( tr_isSession( session ) );
+
     tr_statsClose( session );
     tr_sharedShuttingDown( session->shared );
     tr_rpcClose( &session->rpcServer );
@@ -832,6 +868,8 @@ tr_sessionClose( tr_session * session )
     const int      maxwait_msec = SHUTDOWN_MAX_SECONDS * 1000;
     const uint64_t deadline = tr_date( ) + maxwait_msec;
 
+    assert( tr_isSession( session ) );
+
     dbgmsg( "shutting down transmission session %p", session );
 
     /* close the session */
@@ -894,6 +932,8 @@ tr_sessionLoadTorrents( tr_session * session,
     tr_torrent ** torrents;
     tr_list *     l = NULL, *list = NULL;
 
+    assert( tr_isSession( session ) );
+
     tr_ctorSetSave( ctor, FALSE ); /* since we already have them */
 
     if( !stat( dirname, &sb )
@@ -943,12 +983,16 @@ void
 tr_sessionSetPexEnabled( tr_session * session,
                          tr_bool      enabled )
 {
+    assert( tr_isSession( session ) );
+
     session->isPexEnabled = enabled != 0;
 }
 
 tr_bool
 tr_sessionIsPexEnabled( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return session->isPexEnabled;
 }
 
@@ -960,12 +1004,16 @@ void
 tr_sessionSetLazyBitfieldEnabled( tr_session * session,
                                   tr_bool      enabled )
 {
+    assert( tr_isSession( session ) );
+
     session->useLazyBitfield = enabled != 0;
 }
 
 tr_bool
 tr_sessionIsLazyBitfieldEnabled( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return session->useLazyBitfield;
 }
 
@@ -977,6 +1025,8 @@ void
 tr_sessionSetPortForwardingEnabled( tr_session  * session,
                                     tr_bool       enabled )
 {
+    assert( tr_isSession( session ) );
+
     tr_globalLock( session );
     tr_sharedTraversalEnable( session->shared, enabled );
     tr_globalUnlock( session );
@@ -985,6 +1035,8 @@ tr_sessionSetPortForwardingEnabled( tr_session  * session,
 tr_bool
 tr_sessionIsPortForwardingEnabled( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return tr_sharedTraversalIsEnabled( session->shared );
 }
 
@@ -998,6 +1050,8 @@ tr_blocklistGetRuleCount( const tr_session * session )
     int       n = 0;
     tr_list * l;
 
+    assert( tr_isSession( session ) );
+
     for( l = session->blocklists; l; l = l->next )
         n += _tr_blocklistGetRuleCount( l->data );
     return n;
@@ -1006,6 +1060,8 @@ tr_blocklistGetRuleCount( const tr_session * session )
 tr_bool
 tr_blocklistIsEnabled( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return session->isBlocklistEnabled;
 }
 
@@ -1015,6 +1071,8 @@ tr_blocklistSetEnabled( tr_session * session,
 {
     tr_list * l;
 
+    assert( tr_isSession( session ) );
+
     session->isBlocklistEnabled = isEnabled != 0;
 
     for( l=session->blocklists; l!=NULL; l=l->next )
@@ -1024,6 +1082,8 @@ tr_blocklistSetEnabled( tr_session * session,
 tr_bool
 tr_blocklistExists( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return session->blocklists != NULL;
 }
 
@@ -1035,6 +1095,8 @@ tr_blocklistSetContent( tr_session * session,
     tr_blocklist * b;
     const char *   defaultName = "level1.bin";
 
+    assert( tr_isSession( session ) );
+
     for( b = NULL, l = session->blocklists; !b && l; l = l->next )
         if( tr_stringEndsWith( _tr_blocklistGetFilename( l->data ),
                                defaultName ) )
@@ -1057,6 +1119,8 @@ tr_sessionIsAddressBlocked( const tr_session * session,
 {
     tr_list * l;
 
+    assert( tr_isSession( session ) );
+
     for( l = session->blocklists; l; l = l->next )
         if( _tr_blocklistHasAddress( l->data, addr ) )
             return TRUE;
@@ -1068,8 +1132,7 @@ tr_sessionIsAddressBlocked( const tr_session * session,
 ***/
 
 static int
-compareLookupEntries( const void * va,
-                      const void * vb )
+compareLookupEntries( const void * va, const void * vb )
 {
     const struct tr_metainfo_lookup * a = va;
     const struct tr_metainfo_lookup * b = vb;
@@ -1080,6 +1143,8 @@ compareLookupEntries( const void * va,
 static void
 metainfoLookupResort( tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     qsort( session->metainfoLookup,
            session->metainfoLookupCount,
            sizeof( struct tr_metainfo_lookup ),
@@ -1087,8 +1152,7 @@ metainfoLookupResort( tr_session * session )
 }
 
 static int
-compareHashStringToLookupEntry( const void * va,
-                                const void * vb )
+compareHashStringToLookupEntry( const void * va, const void * vb )
 {
     const char *                      a = va;
     const struct tr_metainfo_lookup * b = vb;
@@ -1120,6 +1184,8 @@ metainfoLookupRescan( tr_session * session )
     tr_ctor *    ctor = NULL;
     tr_list *    list = NULL;
 
+    assert( tr_isSession( session ) );
+
     /* walk through the directory and find the mappings */
     ctor = tr_ctorNew( session );
     tr_ctorSetSave( ctor, FALSE ); /* since we already have them */
@@ -1202,6 +1268,8 @@ tr_torrent*
 tr_torrentNext( tr_session * session,
                 tr_torrent * tor )
 {
+    assert( tr_isSession( session ) );
+
     return tor ? tor->next : session->torrentList;
 }
 
@@ -1213,12 +1281,16 @@ void
 tr_sessionSetRPCEnabled( tr_session * session,
                          tr_bool      isEnabled )
 {
+    assert( tr_isSession( session ) );
+
     tr_rpcSetEnabled( session->rpcServer, isEnabled );
 }
 
 tr_bool
 tr_sessionIsRPCEnabled( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return tr_rpcIsEnabled( session->rpcServer );
 }
 
@@ -1226,12 +1298,16 @@ void
 tr_sessionSetRPCPort( tr_session * session,
                       tr_port      port )
 {
+    assert( tr_isSession( session ) );
+
     tr_rpcSetPort( session->rpcServer, port );
 }
 
 tr_port 
 tr_sessionGetRPCPort( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return tr_rpcGetPort( session->rpcServer );
 }
 
@@ -1240,6 +1316,8 @@ tr_sessionSetRPCCallback( tr_session * session,
                           tr_rpc_func  func,
                           void *       user_data )
 {
+    assert( tr_isSession( session ) );
+
     session->rpc_func = func;
     session->rpc_func_user_data = user_data;
 }
@@ -1248,12 +1326,16 @@ void
 tr_sessionSetRPCWhitelist( tr_session * session,
                            const char * whitelist )
 {
+    assert( tr_isSession( session ) );
+
     tr_rpcSetWhitelist( session->rpcServer, whitelist );
 }
 
 char*
 tr_sessionGetRPCWhitelist( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return tr_rpcGetWhitelist( session->rpcServer );
 }
 
@@ -1261,12 +1343,16 @@ void
 tr_sessionSetRPCWhitelistEnabled( tr_session * session,
                                   tr_bool      isEnabled )
 {
+    assert( tr_isSession( session ) );
+
     tr_rpcSetWhitelistEnabled( session->rpcServer, isEnabled );
 }
 
 tr_bool
 tr_sessionGetRPCWhitelistEnabled( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return tr_rpcGetWhitelistEnabled( session->rpcServer );
 }
 
@@ -1275,12 +1361,16 @@ void
 tr_sessionSetRPCPassword( tr_session * session,
                           const char * password )
 {
+    assert( tr_isSession( session ) );
+
     tr_rpcSetPassword( session->rpcServer, password );
 }
 
 char*
 tr_sessionGetRPCPassword( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return tr_rpcGetPassword( session->rpcServer );
 }
 
@@ -1288,12 +1378,16 @@ void
 tr_sessionSetRPCUsername( tr_session * session,
                           const char * username )
 {
+    assert( tr_isSession( session ) );
+
     tr_rpcSetUsername( session->rpcServer, username );
 }
 
 char*
 tr_sessionGetRPCUsername( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return tr_rpcGetUsername( session->rpcServer );
 }
 
@@ -1301,12 +1395,16 @@ void
 tr_sessionSetRPCPasswordEnabled( tr_session * session,
                                  tr_bool      isEnabled )
 {
+    assert( tr_isSession( session ) );
+
     tr_rpcSetPasswordEnabled( session->rpcServer, isEnabled );
 }
 
 tr_bool
 tr_sessionIsRPCPasswordEnabled( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return tr_rpcIsPasswordEnabled( session->rpcServer );
 }
 
@@ -1317,6 +1415,8 @@ tr_sessionIsRPCPasswordEnabled( const tr_session * session )
 tr_bool
 tr_sessionIsProxyEnabled( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return session->isProxyEnabled;
 }
 
@@ -1324,12 +1424,16 @@ void
 tr_sessionSetProxyEnabled( tr_session * session,
                            tr_bool      isEnabled )
 {
+    assert( tr_isSession( session ) );
+
     session->isProxyEnabled = isEnabled != 0;
 }
 
 tr_proxy_type
 tr_sessionGetProxyType( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return session->proxyType;
 }
 
@@ -1337,18 +1441,24 @@ void
 tr_sessionSetProxyType( tr_session *  session,
                         tr_proxy_type type )
 {
+    assert( tr_isSession( session ) );
+
     session->proxyType = type;
 }
 
 const char*
 tr_sessionGetProxy( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return session->proxy;
 }
 
 tr_port
 tr_sessionGetProxyPort( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return session->proxyPort;
 }
 
@@ -1356,6 +1466,8 @@ void
 tr_sessionSetProxy( tr_session * session,
                     const char * proxy )
 {
+    assert( tr_isSession( session ) );
+
     if( proxy != session->proxy )
     {
         tr_free( session->proxy );
@@ -1367,12 +1479,16 @@ void
 tr_sessionSetProxyPort( tr_session * session,
                         tr_port      port )
 {
+    assert( tr_isSession( session ) );
+
     session->proxyPort = port;
 }
 
 tr_bool
 tr_sessionIsProxyAuthEnabled( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return session->isProxyAuthEnabled;
 }
 
@@ -1380,12 +1496,16 @@ void
 tr_sessionSetProxyAuthEnabled( tr_session * session,
                                tr_bool      isEnabled )
 {
+    assert( tr_isSession( session ) );
+
     session->isProxyAuthEnabled = isEnabled != 0;
 }
 
 const char*
 tr_sessionGetProxyUsername( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return session->proxyUsername;
 }
 
@@ -1393,6 +1513,8 @@ void
 tr_sessionSetProxyUsername( tr_session * session,
                             const char * username )
 {
+    assert( tr_isSession( session ) );
+
     if( username != session->proxyUsername )
     {
         tr_free( session->proxyUsername );
@@ -1403,6 +1525,8 @@ tr_sessionSetProxyUsername( tr_session * session,
 const char*
 tr_sessionGetProxyPassword( const tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return session->proxyPassword;
 }
 
@@ -1410,6 +1534,8 @@ void
 tr_sessionSetProxyPassword( tr_session * session,
                             const char * password )
 {
+    assert( tr_isSession( session ) );
+
     if( password != session->proxyPassword )
     {
         tr_free( session->proxyPassword );
@@ -1423,6 +1549,8 @@ tr_sessionGetActiveTorrentCount( tr_session * session )
     int ret = 0;
     tr_torrent * tor = NULL;
 
+    assert( tr_isSession( session ) );
+
     while(( tor = tr_torrentNext( session, tor )))
         if( tr_torrentGetActivity( tor ) != TR_STATUS_STOPPED )
             ++ret;
index c3e577d5d7921aedfa1bc0a0014567232f56635f..70456f7e498db76279e798eed92d3fba0bf4f857 100644 (file)
@@ -69,6 +69,7 @@ struct tr_session
 
     tr_bool                      isSpeedLimited[2];
     int                          speedLimit[2];
+    int                          magicNumber;
 
     tr_encryption_mode           encryptionMode;
 
@@ -147,4 +148,14 @@ void         tr_globalUnlock( tr_session * );
 
 tr_bool      tr_globalIsLocked( const tr_session * );
 
+enum
+{
+    SESSION_MAGIC_NUMBER = 3845
+};
+
+static inline tr_bool tr_isSession( const tr_session * session )
+{
+    return ( session != NULL ) && ( session->magicNumber == SESSION_MAGIC_NUMBER );
+}
+
 #endif
index ec1e340a727ded14238e0fe0d5644aa290073fce..1747cf8807a2937c55173bc33c15811623b264a3 100644 (file)
@@ -1064,6 +1064,9 @@ checkAndStartImpl( void * vtor )
 static void
 checkAndStartCB( tr_torrent * tor )
 {
+    assert( tor );
+    assert( tr_isSession( tor->session ) );
+
     tr_runInEventThread( tor->session, checkAndStartImpl, tor );
 }
 
index 3821c34455ef1fe6bded3af358b64e85c9a81482..4435f37ab9929b593db7fc46e9536ef1271e0cb9 100644 (file)
@@ -279,6 +279,8 @@ tr_eventInit( tr_session * session )
 void
 tr_eventClose( tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     session->events->die = TRUE;
     tr_deepLog( __FILE__, __LINE__, NULL, "closing trevent pipe" );
     EVUTIL_CLOSESOCKET( session->events->fds[1] );
@@ -291,7 +293,7 @@ tr_eventClose( tr_session * session )
 tr_bool
 tr_amInEventThread( tr_session * session )
 {
-    assert( session );
+    assert( tr_isSession( session ) );
     assert( session->events );
 
     return tr_amInThread( session->events->thread );
@@ -346,7 +348,7 @@ tr_timerNew( tr_session * session,
 {
     tr_timer * timer;
 
-    assert( session != NULL );
+    assert( tr_isSession( session ) );
     assert( session->events != NULL );
 
     timer = tr_new0( tr_timer, 1 );
@@ -379,7 +381,7 @@ void
 tr_runInEventThread( tr_session * session,
                      void func( void* ), void * user_data )
 {
-    assert( session != NULL );
+    assert( tr_isSession( session ) );
     assert( session->events != NULL );
 
     if( tr_amInThread( session->events->thread ) )
@@ -405,5 +407,7 @@ tr_runInEventThread( tr_session * session,
 struct event_base *
 tr_eventGetBase( tr_session * session )
 {
+    assert( tr_isSession( session ) );
+
     return session->events->base;
 }