Back to index

lightning-sunbird  0.9+nobinonly
whatnspr.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #ifdef DEBUG
00038 static const char CVS_ID[] = "@(#) $RCSfile: whatnspr.c,v $ $Revision: 1.4 $ $Date: 2005/01/20 02:25:45 $";
00039 #endif /* DEBUG */
00040 
00041 #ifndef BASE_H
00042 #include "base.h"
00043 #endif /* BASE_H */
00044 
00045 /*
00046  * This file isolates us from differences in NSPR versions.
00047  * We have to detect the library with which we're running at
00048  * runtime, and switch behaviours there.  This lets us do
00049  * stuff like load cryptoki modules in Communicator.
00050  *
00051  * Hey, it's the PORT layer all over again!
00052  */
00053 
00054 static int whatnspr = 0;
00055 
00056 static int
00057 set_whatnspr
00058 (
00059   void
00060 )
00061 {
00062   /*
00063    * The only runtime difference I could find was the
00064    * return value of PR_dtoa.  We can't just look for
00065    * a symbol in NSPR >=2, because it'll always be
00066    * found (because we compile against NSPR >=2).
00067    * Maybe we could look for a symbol merely in NSPR 1?
00068    *
00069    */
00070 
00071   char buffer[64];
00072   int decpt = 0, sign = 0;
00073   char *rve = (char *)0;
00074   /* extern int PR_dtoa(double, int, int, int *, int *, char **, char *, int); */
00075   int r = (int)PR_dtoa((double)1.0, 0, 5, &decpt, &sign, &rve, 
00076                        buffer, sizeof(buffer));
00077 
00078   switch( r ) {
00079   case 0:
00080   case -1:
00081     whatnspr = 2;
00082     /*
00083      * If we needed to, *now* we could look up "libVersionPoint"
00084      * and get more data there.. except all current NSPR's (up
00085      * to NSPR 4.x at time of writing) still say 2 in their
00086      * version structure.
00087      */
00088     break;
00089   default:
00090     whatnspr = 1;
00091     break;
00092   }
00093 
00094   return whatnspr;
00095 }
00096 
00097 #define WHATNSPR (whatnspr ? whatnspr : set_whatnspr())
00098 
00099 NSS_IMPLEMENT PRStatus
00100 nss_NewThreadPrivateIndex
00101 (
00102   PRUintn *ip,
00103   PRThreadPrivateDTOR dtor
00104 )
00105 {
00106   switch( WHATNSPR ) {
00107   case 1:
00108     {
00109       PRLibrary *l = (PRLibrary *)0;
00110       void *f = PR_FindSymbolAndLibrary("PR_NewThreadPrivateID", &l);
00111       typedef PRInt32 (*ntpt)(void);
00112       ntpt ntp = (ntpt) f;
00113 
00114       PR_ASSERT((void *)0 != f);
00115 
00116       *ip = ntp();
00117       return PR_SUCCESS;
00118     }
00119   case 2:
00120   default:
00121     return PR_NewThreadPrivateIndex(ip, dtor);
00122   }
00123 }
00124 
00125 NSS_IMPLEMENT void *
00126 nss_GetThreadPrivate
00127 (
00128   PRUintn i
00129 )
00130 {
00131   switch( WHATNSPR ) {
00132   case 1:
00133     {
00134       PRLibrary *l = (PRLibrary *)0;
00135       void *f = PR_FindSymbolAndLibrary("PR_GetThreadPrivate", &l);
00136       typedef void *(*gtpt)(PRThread *, PRInt32);
00137       gtpt gtp = (gtpt) f;
00138 
00139       PR_ASSERT((void *)0 != f);
00140 
00141       return gtp(PR_CurrentThread(), i);
00142     }
00143   case 2:
00144   default:
00145     return PR_GetThreadPrivate(i);
00146   }
00147 }
00148 
00149 NSS_IMPLEMENT void
00150 nss_SetThreadPrivate
00151 (
00152   PRUintn i,
00153   void *v
00154 )
00155 {
00156   switch( WHATNSPR ) {
00157   case 1:
00158     {
00159       PRLibrary *l = (PRLibrary *)0;
00160       void *f = PR_FindSymbolAndLibrary("PR_SetThreadPrivate", &l);
00161       typedef PRStatus (*stpt)(PRThread *, PRInt32, void *);
00162       stpt stp = (stpt) f;
00163 
00164       PR_ASSERT((void *)0 != f);
00165 
00166       (void)stp(PR_CurrentThread(), i, v);
00167       return;
00168     }
00169   case 2:
00170   default:
00171     (void)PR_SetThreadPrivate(i, v);
00172     return;
00173   }
00174 }