home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / nspr30-e.zip / nspr30-e / include / prcountr.h < prev    next >
C/C++ Source or Header  |  1998-09-25  |  16KB  |  513 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef prcountr_h___
  20. #define prcountr_h___
  21.  
  22. /*----------------------------------------------------------------------------
  23. ** prcountr.h -- NSPR Instrumentation counters
  24. **
  25. ** The NSPR Counter Feature provides a means to "count
  26. ** something." Counters can be dynamically defined, incremented,
  27. ** decremented, set, and deleted under application program
  28. ** control.
  29. **                                                                                    
  30. ** The Counter Feature is intended to be used as instrumentation,                  
  31. ** not as operational data. If you need a counter for operational                  
  32. ** data, use native integral types.                                                
  33. **                                                                                    
  34. ** Counters are 32bit unsigned intergers. On overflow, a counter                   
  35. ** will wrap. No exception is recognized or reported.                              
  36. **                                                                                 
  37. ** A counter can be dynamically created using a two level naming
  38. ** convention. A "handle" is returned when the counter is
  39. ** created. The counter can subsequently be addressed by its
  40. ** handle. An API is provided to get an existing counter's handle
  41. ** given the names with  which it was originally created. 
  42. ** Similarly, a counter's name can be retrieved given its handle.
  43. ** 
  44. ** The counter naming convention is a two-level hierarchy. The
  45. ** QName is the higher level of the hierarchy; RName is the
  46. ** lower level. RNames can be thought of as existing within a
  47. ** QName. The same RName can exist within multiple QNames. QNames
  48. ** are unique. The NSPR Counter is not a near-zero overhead
  49. ** feature. Application designers should be aware of 
  50. ** serialization issues when using the Counter API. Creating a
  51. ** counter locks a large asset, potentially causing a stall. This
  52. ** suggest that applications should create counters at component
  53. ** initialization, for example, and not create and destroy them
  54. ** willy-nilly. ... You have been warned.
  55. ** 
  56. ** Incrementing and Adding to counters uses atomic operations.
  57. ** The performance of these operations will vary from platform
  58. ** to platform. On platforms where atomic operations are not
  59. ** supported the overhead may be substantial.
  60. ** 
  61. ** When traversing the counter database with FindNext functions,
  62. ** the instantaneous values of any given counter is that at the
  63. ** moment of extraction. The state of the entire counter database
  64. ** may not be viewed as atomic.
  65. ** 
  66. ** The counter interface may be disabled (No-Op'd) at compile
  67. ** time. When DEBUG is defined at compile time, the Counter
  68. ** Feature is compiled into NSPR and applications invoking it.
  69. ** When DEBUG is not defined, the counter macros compile to
  70. ** nothing. To force the Counter Feature to be compiled into an
  71. ** optimized build, define FORCE_NSPR_COUNTERS at compile time
  72. ** for both NSPR and the application intending to use it.
  73. ** 
  74. ** Application designers should use the macro form of the Counter
  75. ** Feature methods to minimize performance impact in optimized
  76. ** builds. The macros normally compile to nothing on optimized
  77. ** builds.
  78. ** 
  79. ** Application designers should be aware of the effects of
  80. ** debug and optimized build differences when using result of the
  81. ** Counter Feature macros in expressions.
  82. ** 
  83. ** The Counter Feature is thread-safe and SMP safe.
  84. ** 
  85. ** /lth. 09-Jun-1998.
  86. */
  87.  
  88. #include "prtypes.h"
  89.  
  90. PR_BEGIN_EXTERN_C
  91.  
  92. /*
  93. ** Opaque counter handle type.
  94. ** ... don't even think of looking in here.
  95. **
  96. */
  97. typedef void *  PRCounterHandle;
  98.  
  99. #define PRCOUNTER_NAME_MAX 31
  100. #define PRCOUNTER_DESC_MAX 255
  101.  
  102.  
  103. #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
  104.  
  105. /* -----------------------------------------------------------------------
  106. ** FUNCTION: PR_DEFINE_COUNTER() -- Define a PRCounterHandle
  107. ** 
  108. ** DESCRIPTION: PR_DEFINE_COUNTER() is used to define a counter
  109. ** handle.
  110. ** 
  111. */
  112. #define PR_DEFINE_COUNTER(name) PRCounterHandle name
  113.  
  114. /* -----------------------------------------------------------------------
  115. ** FUNCTION: PR_INIT_COUNTER_HANDLE() -- Set the value of a PRCounterHandle
  116. ** 
  117. ** DESCRIPTION: 
  118. ** PR_INIT_COUNTER_HANDLE() sets the value of a PRCounterHandle
  119. ** to value.
  120. ** 
  121. */
  122. #define PR_INIT_COUNTER_HANDLE(handle,value)\
  123.     (handle) = (PRCounterHandle)(value)
  124.  
  125. /* -----------------------------------------------------------------------
  126. ** FUNCTION: PR_CreateCounter() -- Create a counter
  127. ** 
  128. ** DESCRIPTION: PR_CreateCounter() creates a counter object and
  129. ** initializes it to zero.
  130. ** 
  131. ** The macro form takes as its first argument the name of the
  132. ** PRCounterHandle to receive the handle returned from
  133. ** PR_CreateCounter().
  134. ** 
  135. ** INPUTS:
  136. **  qName: The QName for the counter object. The maximum length
  137. ** of qName is defined by PRCOUNTER_NAME_MAX
  138. ** 
  139. **  rName: The RName for the counter object. The maximum length
  140. ** of qName is defined by PRCOUNTER_NAME_MAX
  141. ** 
  142. **  descrioption: The description of the counter object. The
  143. ** maximum length of description is defined by
  144. ** PRCOUNTER_DESC_MAX.
  145. ** 
  146. ** OUTPUTS:
  147. ** 
  148. ** RETURNS:
  149. **  PRCounterHandle.
  150. ** 
  151. ** RESTRICTIONS:
  152. ** 
  153. */
  154. #define PR_CREATE_COUNTER(handle,qName,rName,description)\
  155.    (handle) = PR_CreateCounter((qName),(rName),(description))
  156.  
  157. PR_EXTERN(PRCounterHandle) 
  158.     PR_CreateCounter( 
  159.         const char *qName, 
  160.         const char *rName, 
  161.         const char *description 
  162. );
  163.  
  164. /* -----------------------------------------------------------------------
  165. ** FUNCTION: PR_DestroyCounter() -- Destroy a counter object.
  166. ** 
  167. ** DESCRIPTION: PR_DestroyCounter() removes a counter and
  168. ** unregisters its handle from the counter database.
  169. ** 
  170. ** INPUTS:
  171. **  handle: the PRCounterHandle of the counter to be destroyed.
  172. ** 
  173. ** OUTPUTS: 
  174. **  The counter is destroyed.
  175. ** 
  176. ** RETURNS: void
  177. ** 
  178. ** RESTRICTIONS:
  179. ** 
  180. */
  181. #define PR_DESTROY_COUNTER(handle) PR_DestroyCounter((handle))
  182.  
  183. PR_EXTERN(void) 
  184.     PR_DestroyCounter( 
  185.         PRCounterHandle handle 
  186. );
  187.  
  188.  
  189. /* -----------------------------------------------------------------------
  190. ** FUNCTION: PR_GetCounterHandleFromName() -- Retreive a
  191. ** counter's handle give its name.
  192. ** 
  193. ** DESCRIPTION: PR_GetCounterHandleFromName() retreives a
  194. ** counter's handle from the counter database, given the name
  195. ** the counter was originally created with.
  196. ** 
  197. ** INPUTS:
  198. **  qName: Counter's original QName.
  199. **  rName: Counter's original RName.
  200. ** 
  201. ** OUTPUTS:
  202. ** 
  203. ** RETURNS: 
  204. **  PRCounterHandle or PRCounterError.
  205. ** 
  206. ** RESTRICTIONS:
  207. ** 
  208. */
  209. #define PR_GET_COUNTER_HANDLE_FROM_NAME(handle,qName,rName)\
  210.     (handle) = PR_GetCounterHandleFromName((qName),(rName))
  211.  
  212. PR_EXTERN(PRCounterHandle) 
  213.     PR_GetCounterHandleFromName( 
  214.         const char *qName, 
  215.         const char *rName 
  216. );
  217.  
  218. /* -----------------------------------------------------------------------
  219. ** FUNCTION: PR_GetCounterNameFromHandle() -- Retreive a
  220. ** counter's name, given its handle.
  221. ** 
  222. ** DESCRIPTION: PR_GetCounterNameFromHandle() retreives a
  223. ** counter's name given its handle.
  224. ** 
  225. ** INPUTS:
  226. **  qName: Where to store a pointer to qName.
  227. **  rName: Where to store a pointer to rName.
  228. **  description: Where to store a pointer to description.
  229. ** 
  230. ** OUTPUTS: Pointers to the Counter Feature's copies of the names
  231. ** used when the counters were created.
  232. ** 
  233. ** RETURNS: void
  234. ** 
  235. ** RESTRICTIONS:
  236. ** 
  237. */
  238. #define PR_GET_COUNTER_NAME_FROM_HANDLE(handle,qName,rName,description)\
  239.     PR_GetCounterNameFromHandle((handle),(qName),(rName),(description))
  240.  
  241. PR_EXTERN(void) 
  242.     PR_GetCounterNameFromHandle( 
  243.         PRCounterHandle handle,  
  244.         const char **qName, 
  245.         const char **rName, 
  246.         const char **description 
  247. );
  248.  
  249.  
  250. /* -----------------------------------------------------------------------
  251. ** FUNCTION: PR_IncrementCounter() -- Add one to the referenced
  252. ** counter.
  253. ** 
  254. ** DESCRIPTION: Add one to the referenced counter.
  255. ** 
  256. ** INPUTS:
  257. **  handle: The PRCounterHandle of the counter to be incremented
  258. ** 
  259. ** OUTPUTS: The counter is incrementd.
  260. ** 
  261. ** RETURNS: void
  262. ** 
  263. ** RESTRICTIONS:
  264. ** 
  265. */
  266. #define PR_INCREMENT_COUNTER(handle) PR_IncrementCounter(handle)
  267.  
  268. PR_EXTERN(void) 
  269.     PR_IncrementCounter( 
  270.         PRCounterHandle handle
  271. );
  272.  
  273.  
  274. /* -----------------------------------------------------------------------
  275. ** FUNCTION: PR_DecrementCounter() -- Subtract one from the
  276. ** referenced counter
  277. ** 
  278. ** DESCRIPTION: Subtract one from the referenced counter.
  279. ** 
  280. ** INPUTS: 
  281. **  handle: The PRCounterHandle of the coutner to be
  282. ** decremented.
  283. ** 
  284. ** OUTPUTS: the counter is decremented.
  285. ** 
  286. ** RETURNS: void
  287. ** 
  288. ** RESTRICTIONS:
  289. ** 
  290. */
  291. #define PR_DECREMENT_COUNTER(handle) PR_DecrementCounter(handle)
  292.  
  293. PR_EXTERN(void) 
  294.     PR_DecrementCounter( 
  295.         PRCounterHandle handle
  296. );
  297.  
  298. /* -----------------------------------------------------------------------
  299. ** FUNCTION: PR_AddToCounter() -- Add a value to a counter.
  300. ** 
  301. ** DESCRIPTION: Add value to the counter referenced by handle.
  302. ** 
  303. ** INPUTS:
  304. **  handle: the PRCounterHandle of the counter to be added to.
  305. ** 
  306. **  value: the value to be added to the counter.
  307. ** 
  308. ** OUTPUTS: new value for counter.
  309. ** 
  310. ** RETURNS: void
  311. ** 
  312. ** RESTRICTIONS:
  313. ** 
  314. */
  315. #define PR_ADD_TO_COUNTER(handle,value)\
  316.     PR_AddToCounter((handle),(value))
  317.  
  318. PR_EXTERN(void) 
  319.     PR_AddToCounter( 
  320.         PRCounterHandle handle, 
  321.         PRUint32 value 
  322. );
  323.  
  324.  
  325. /* -----------------------------------------------------------------------
  326. ** FUNCTION: PR_SubtractFromCounter() -- A value is subtracted
  327. ** from a counter.
  328. ** 
  329. ** DESCRIPTION:
  330. ** Subtract a value from a counter.
  331. ** 
  332. ** INPUTS:
  333. **  handle: the PRCounterHandle of the counter to be subtracted
  334. ** from.
  335. ** 
  336. **  value: the value to be subtracted from the counter.
  337. ** 
  338. ** OUTPUTS: new value for counter
  339. ** 
  340. ** RETURNS: void
  341. ** 
  342. ** RESTRICTIONS:
  343. ** 
  344. */
  345. #define PR_SUBTRACT_FROM_COUNTER(handle,value)\
  346.     PR_SubtractFromCounter((handle),(value))
  347.  
  348. PR_EXTERN(void) 
  349.     PR_SubtractFromCounter( 
  350.         PRCounterHandle handle, 
  351.         PRUint32 value 
  352. );
  353.  
  354.  
  355. /* -----------------------------------------------------------------------
  356. ** FUNCTION: PR_GetCounter() -- Retreive the value of a counter
  357. ** 
  358. ** DESCRIPTION:
  359. ** Retreive the value of a counter.
  360. ** 
  361. ** INPUTS:
  362. **  handle: the PR_CounterHandle of the counter to be retreived
  363. ** 
  364. ** OUTPUTS:
  365. ** 
  366. ** RETURNS: The value of the referenced counter
  367. ** 
  368. ** RESTRICTIONS:
  369. ** 
  370. */
  371. #define PR_GET_COUNTER(counter,handle)\
  372.     (counter) = PR_GetCounter((handle))
  373.  
  374. PR_EXTERN(PRUint32) 
  375.     PR_GetCounter( 
  376.         PRCounterHandle handle 
  377. );
  378.  
  379. /* -----------------------------------------------------------------------
  380. ** FUNCTION: PR_SetCounter() -- Replace the content of counter
  381. ** with value.
  382. ** 
  383. ** DESCRIPTION: The contents of the referenced counter are
  384. ** replaced by value.
  385. ** 
  386. ** INPUTS:
  387. **  handle: the PRCounterHandle of the counter whose contents
  388. ** are to be replaced.
  389. ** 
  390. **  value: the new value of the counter.
  391. ** 
  392. ** OUTPUTS:
  393. ** 
  394. ** RETURNS: void
  395. ** 
  396. ** RESTRICTIONS:
  397. ** 
  398. */
  399. #define PR_SET_COUNTER(handle,value) PR_SetCounter((handle),(value))
  400.  
  401. PR_EXTERN(void) 
  402.     PR_SetCounter( 
  403.         PRCounterHandle handle, 
  404.         PRUint32 value 
  405. );
  406.  
  407.  
  408. /* -----------------------------------------------------------------------
  409. ** FUNCTION: PR_FindNextCounterQname() -- Retreive the next QName counter
  410. ** handle iterator
  411. ** 
  412. ** DESCRIPTION:
  413. ** PR_FindNextCounterQname() retreives the first or next Qname
  414. ** the counter data base, depending on the value of handle. When
  415. ** handle is NULL, the function attempts to retreive the first
  416. ** QName handle in the database. When handle is a handle previosly
  417. ** retreived QName handle, then the function attempts to retreive
  418. ** the next QName handle.
  419. ** 
  420. ** INPUTS: 
  421. **  handle: PRCounterHandle or NULL.
  422. ** 
  423. ** OUTPUTS: returned
  424. ** 
  425. ** RETURNS: PRCounterHandle or NULL when no more QName counter
  426. ** handles are present.
  427. ** 
  428. ** RESTRICTIONS:
  429. **  A concurrent PR_CreateCounter() or PR_DestroyCounter() may
  430. ** cause unpredictable results.
  431. ** 
  432. ** A PRCounterHandle returned from this function may only be used
  433. ** in another PR_FindNextCounterQname() function call; other
  434. ** operations may cause unpredictable results.
  435. ** 
  436. */
  437. #define PR_FIND_NEXT_COUNTER_QNAME(next,handle)\
  438.     (next) = PR_FindNextCounterQname((handle))
  439.  
  440. PR_EXTERN(PRCounterHandle) 
  441.     PR_FindNextCounterQname( 
  442.         PRCounterHandle handle
  443. );
  444.  
  445. /* -----------------------------------------------------------------------
  446. ** FUNCTION: PR_FindNextCounterRname() -- Retreive the next RName counter
  447. ** handle iterator
  448. ** 
  449. ** DESCRIPTION:
  450. ** PR_FindNextCounterRname() retreives the first or next RNname
  451. ** handle from the counter data base, depending on the
  452. ** value of handle. When handle is NULL, the function attempts to
  453. ** retreive the first RName handle in the database. When handle is
  454. ** a handle previosly retreived RName handle, then the function
  455. ** attempts to retreive the next RName handle.
  456. ** 
  457. ** INPUTS:
  458. **  handle: PRCounterHandle or NULL.
  459. **  qhandle: PRCounterHandle of a previously aquired via
  460. ** PR_FIND_NEXT_QNAME_HANDLE()
  461. ** 
  462. ** OUTPUTS: returned
  463. ** 
  464. ** RETURNS: PRCounterHandle or NULL when no more RName counter
  465. ** handles are present.
  466. ** 
  467. ** RESTRICTIONS:
  468. **  A concurrent PR_CreateCounter() or PR_DestroyCounter() may
  469. ** cause unpredictable results.
  470. ** 
  471. ** A PRCounterHandle returned from this function may only be used
  472. ** in another PR_FindNextCounterRname() function call; other
  473. ** operations may cause unpredictable results.
  474. ** 
  475. */
  476. #define PR_FIND_NEXT_COUNTER_RNAME(next,rhandle,qhandle)\
  477.     (next) = PR_FindNextCounterRname((rhandle),(qhandle))
  478.  
  479. PR_EXTERN(PRCounterHandle) 
  480.     PR_FindNextCounterRname( 
  481.         PRCounterHandle rhandle,
  482.         PRCounterHandle qhandle
  483. );
  484.  
  485.  
  486. #else /* ( !(defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)) */
  487. /*
  488. ** When counters are not compiled in, provide macros that 
  489. ** evaluate to No-Ops.
  490. **
  491. */
  492.  
  493. #define PR_DEFINE_COUNTER(name) PRCounterHandle name
  494. #define PR_INIT_COUNTER_HANDLE(handle,value)
  495. #define PR_CREATE_COUNTER(handle,qName,rName,description)
  496. #define PR_DESTROY_COUNTER(handle)
  497. #define PR_GET_COUNTER_HANDLE_FROM_NAME(handle,qName,rName)
  498. #define PR_GET_COUNTER_NAME_FROM_HANDLE(handle,qName,rName,description )
  499. #define PR_INCREMENT_COUNTER(handle)
  500. #define PR_DECREMENT_COUNTER(handle)
  501. #define PR_ADD_TO_COUNTER(handle,value)
  502. #define PR_SUBTRACT_FROM_COUNTER(handle,value)
  503. #define PR_GET_COUNTER(counter,handle) 0
  504. #define PR_SET_COUNTER(handle,value)
  505. #define PR_FIND_NEXT_COUNTER_QNAME(next,handle) NULL
  506. #define PR_FIND_NEXT_COUNTER_RNAME(next,rhandle,qhandle)
  507.  
  508. #endif /* ( !(defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)) */
  509.  
  510. PR_END_EXTERN_C
  511.  
  512. #endif /* prcountr_h___ */
  513.