1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 * The contents of this file are subject to the Mozilla Public
4 * License Version 1.1 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of
6 * the License at http://www.mozilla.org/MPL/
8 * Software distributed under the License is distributed on an "AS
9 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10 * implied. See the License for the specific language governing
11 * rights and limitations under the License.
13 * The Original Code is the Netscape Portable Runtime (NSPR).
15 * The Initial Developer of the Original Code is Netscape
16 * Communications Corporation. Portions created by Netscape are
17 * Copyright (C) 1998-2000 Netscape Communications Corporation. All
22 * Alternatively, the contents of this file may be used under the
23 * terms of the GNU General Public License Version 2 or later (the
24 * "GPL"), in which case the provisions of the GPL are applicable
25 * instead of those above. If you wish to allow use of your
26 * version of this file only under the terms of the GPL and not to
27 * allow others to use your version of this file under the MPL,
28 * indicate your decision by deleting the provisions above and
29 * replace them with the notice and other provisions required by
30 * the GPL. If you do not delete the provisions above, a recipient
31 * may use your version of this file under either the MPL or the
38 /*----------------------------------------------------------------------------
39 ** prcountr.h -- NSPR Instrumentation counters
41 ** The NSPR Counter Feature provides a means to "count
42 ** something." Counters can be dynamically defined, incremented,
43 ** decremented, set, and deleted under application program
46 ** The Counter Feature is intended to be used as instrumentation,
47 ** not as operational data. If you need a counter for operational
48 ** data, use native integral types.
50 ** Counters are 32bit unsigned intergers. On overflow, a counter
51 ** will wrap. No exception is recognized or reported.
53 ** A counter can be dynamically created using a two level naming
54 ** convention. A "handle" is returned when the counter is
55 ** created. The counter can subsequently be addressed by its
56 ** handle. An API is provided to get an existing counter's handle
57 ** given the names with which it was originally created.
58 ** Similarly, a counter's name can be retrieved given its handle.
60 ** The counter naming convention is a two-level hierarchy. The
61 ** QName is the higher level of the hierarchy; RName is the
62 ** lower level. RNames can be thought of as existing within a
63 ** QName. The same RName can exist within multiple QNames. QNames
64 ** are unique. The NSPR Counter is not a near-zero overhead
65 ** feature. Application designers should be aware of
66 ** serialization issues when using the Counter API. Creating a
67 ** counter locks a large asset, potentially causing a stall. This
68 ** suggest that applications should create counters at component
69 ** initialization, for example, and not create and destroy them
70 ** willy-nilly. ... You have been warned.
72 ** Incrementing and Adding to counters uses atomic operations.
73 ** The performance of these operations will vary from platform
74 ** to platform. On platforms where atomic operations are not
75 ** supported the overhead may be substantial.
77 ** When traversing the counter database with FindNext functions,
78 ** the instantaneous values of any given counter is that at the
79 ** moment of extraction. The state of the entire counter database
80 ** may not be viewed as atomic.
82 ** The counter interface may be disabled (No-Op'd) at compile
83 ** time. When DEBUG is defined at compile time, the Counter
84 ** Feature is compiled into NSPR and applications invoking it.
85 ** When DEBUG is not defined, the counter macros compile to
86 ** nothing. To force the Counter Feature to be compiled into an
87 ** optimized build, define FORCE_NSPR_COUNTERS at compile time
88 ** for both NSPR and the application intending to use it.
90 ** Application designers should use the macro form of the Counter
91 ** Feature methods to minimize performance impact in optimized
92 ** builds. The macros normally compile to nothing on optimized
95 ** Application designers should be aware of the effects of
96 ** debug and optimized build differences when using result of the
97 ** Counter Feature macros in expressions.
99 ** The Counter Feature is thread-safe and SMP safe.
101 ** /lth. 09-Jun-1998.
109 ** Opaque counter handle type.
110 ** ... don't even think of looking in here.
113 typedef void * PRCounterHandle
;
115 #define PRCOUNTER_NAME_MAX 31
116 #define PRCOUNTER_DESC_MAX 255
120 /* -----------------------------------------------------------------------
121 ** FUNCTION: PR_DEFINE_COUNTER() -- Define a PRCounterHandle
123 ** DESCRIPTION: PR_DEFINE_COUNTER() is used to define a counter
127 #define PR_DEFINE_COUNTER(name) PRCounterHandle name
129 /* -----------------------------------------------------------------------
130 ** FUNCTION: PR_INIT_COUNTER_HANDLE() -- Set the value of a PRCounterHandle
133 ** PR_INIT_COUNTER_HANDLE() sets the value of a PRCounterHandle
137 #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
138 #define PR_INIT_COUNTER_HANDLE(handle,value)\
139 (handle) = (PRCounterHandle)(value)
141 #define PR_INIT_COUNTER_HANDLE(handle,value)
144 /* -----------------------------------------------------------------------
145 ** FUNCTION: PR_CreateCounter() -- Create a counter
147 ** DESCRIPTION: PR_CreateCounter() creates a counter object and
148 ** initializes it to zero.
150 ** The macro form takes as its first argument the name of the
151 ** PRCounterHandle to receive the handle returned from
152 ** PR_CreateCounter().
155 ** qName: The QName for the counter object. The maximum length
156 ** of qName is defined by PRCOUNTER_NAME_MAX
158 ** rName: The RName for the counter object. The maximum length
159 ** of qName is defined by PRCOUNTER_NAME_MAX
161 ** descrioption: The description of the counter object. The
162 ** maximum length of description is defined by
163 ** PRCOUNTER_DESC_MAX.
173 #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
174 #define PR_CREATE_COUNTER(handle,qName,rName,description)\
175 (handle) = PR_CreateCounter((qName),(rName),(description))
177 #define PR_CREATE_COUNTER(handle,qName,rName,description)
180 NSPR_API(PRCounterHandle
)
184 const char *description
187 /* -----------------------------------------------------------------------
188 ** FUNCTION: PR_DestroyCounter() -- Destroy a counter object.
190 ** DESCRIPTION: PR_DestroyCounter() removes a counter and
191 ** unregisters its handle from the counter database.
194 ** handle: the PRCounterHandle of the counter to be destroyed.
197 ** The counter is destroyed.
204 #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
205 #define PR_DESTROY_COUNTER(handle) PR_DestroyCounter((handle))
207 #define PR_DESTROY_COUNTER(handle)
212 PRCounterHandle handle
216 /* -----------------------------------------------------------------------
217 ** FUNCTION: PR_GetCounterHandleFromName() -- Retreive a
218 ** counter's handle give its name.
220 ** DESCRIPTION: PR_GetCounterHandleFromName() retreives a
221 ** counter's handle from the counter database, given the name
222 ** the counter was originally created with.
225 ** qName: Counter's original QName.
226 ** rName: Counter's original RName.
231 ** PRCounterHandle or PRCounterError.
236 #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
237 #define PR_GET_COUNTER_HANDLE_FROM_NAME(handle,qName,rName)\
238 (handle) = PR_GetCounterHandleFromName((qName),(rName))
240 #define PR_GET_COUNTER_HANDLE_FROM_NAME(handle,qName,rName)
243 NSPR_API(PRCounterHandle
)
244 PR_GetCounterHandleFromName(
249 /* -----------------------------------------------------------------------
250 ** FUNCTION: PR_GetCounterNameFromHandle() -- Retreive a
251 ** counter's name, given its handle.
253 ** DESCRIPTION: PR_GetCounterNameFromHandle() retreives a
254 ** counter's name given its handle.
257 ** qName: Where to store a pointer to qName.
258 ** rName: Where to store a pointer to rName.
259 ** description: Where to store a pointer to description.
261 ** OUTPUTS: Pointers to the Counter Feature's copies of the names
262 ** used when the counters were created.
269 #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
270 #define PR_GET_COUNTER_NAME_FROM_HANDLE(handle,qName,rName,description)\
271 PR_GetCounterNameFromHandle((handle),(qName),(rName),(description))
273 #define PR_GET_COUNTER_NAME_FROM_HANDLE(handle,qName,rName,description )
277 PR_GetCounterNameFromHandle(
278 PRCounterHandle handle
,
281 const char **description
285 /* -----------------------------------------------------------------------
286 ** FUNCTION: PR_IncrementCounter() -- Add one to the referenced
289 ** DESCRIPTION: Add one to the referenced counter.
292 ** handle: The PRCounterHandle of the counter to be incremented
294 ** OUTPUTS: The counter is incrementd.
301 #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
302 #define PR_INCREMENT_COUNTER(handle) PR_IncrementCounter(handle)
304 #define PR_INCREMENT_COUNTER(handle)
309 PRCounterHandle handle
313 /* -----------------------------------------------------------------------
314 ** FUNCTION: PR_DecrementCounter() -- Subtract one from the
315 ** referenced counter
317 ** DESCRIPTION: Subtract one from the referenced counter.
320 ** handle: The PRCounterHandle of the coutner to be
323 ** OUTPUTS: the counter is decremented.
330 #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
331 #define PR_DECREMENT_COUNTER(handle) PR_DecrementCounter(handle)
333 #define PR_DECREMENT_COUNTER(handle)
338 PRCounterHandle handle
341 /* -----------------------------------------------------------------------
342 ** FUNCTION: PR_AddToCounter() -- Add a value to a counter.
344 ** DESCRIPTION: Add value to the counter referenced by handle.
347 ** handle: the PRCounterHandle of the counter to be added to.
349 ** value: the value to be added to the counter.
351 ** OUTPUTS: new value for counter.
358 #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
359 #define PR_ADD_TO_COUNTER(handle,value)\
360 PR_AddToCounter((handle),(value))
362 #define PR_ADD_TO_COUNTER(handle,value)
367 PRCounterHandle handle
,
372 /* -----------------------------------------------------------------------
373 ** FUNCTION: PR_SubtractFromCounter() -- A value is subtracted
377 ** Subtract a value from a counter.
380 ** handle: the PRCounterHandle of the counter to be subtracted
383 ** value: the value to be subtracted from the counter.
385 ** OUTPUTS: new value for counter
392 #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
393 #define PR_SUBTRACT_FROM_COUNTER(handle,value)\
394 PR_SubtractFromCounter((handle),(value))
396 #define PR_SUBTRACT_FROM_COUNTER(handle,value)
400 PR_SubtractFromCounter(
401 PRCounterHandle handle
,
406 /* -----------------------------------------------------------------------
407 ** FUNCTION: PR_GetCounter() -- Retreive the value of a counter
410 ** Retreive the value of a counter.
413 ** handle: the PR_CounterHandle of the counter to be retreived
417 ** RETURNS: The value of the referenced counter
422 #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
423 #define PR_GET_COUNTER(counter,handle)\
424 (counter) = PR_GetCounter((handle))
426 #define PR_GET_COUNTER(counter,handle) 0
431 PRCounterHandle handle
434 /* -----------------------------------------------------------------------
435 ** FUNCTION: PR_SetCounter() -- Replace the content of counter
438 ** DESCRIPTION: The contents of the referenced counter are
439 ** replaced by value.
442 ** handle: the PRCounterHandle of the counter whose contents
443 ** are to be replaced.
445 ** value: the new value of the counter.
454 #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
455 #define PR_SET_COUNTER(handle,value) PR_SetCounter((handle),(value))
457 #define PR_SET_COUNTER(handle,value)
462 PRCounterHandle handle
,
467 /* -----------------------------------------------------------------------
468 ** FUNCTION: PR_FindNextCounterQname() -- Retreive the next QName counter
472 ** PR_FindNextCounterQname() retreives the first or next Qname
473 ** the counter data base, depending on the value of handle. When
474 ** handle is NULL, the function attempts to retreive the first
475 ** QName handle in the database. When handle is a handle previosly
476 ** retreived QName handle, then the function attempts to retreive
477 ** the next QName handle.
480 ** handle: PRCounterHandle or NULL.
484 ** RETURNS: PRCounterHandle or NULL when no more QName counter
485 ** handles are present.
488 ** A concurrent PR_CreateCounter() or PR_DestroyCounter() may
489 ** cause unpredictable results.
491 ** A PRCounterHandle returned from this function may only be used
492 ** in another PR_FindNextCounterQname() function call; other
493 ** operations may cause unpredictable results.
496 #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
497 #define PR_FIND_NEXT_COUNTER_QNAME(next,handle)\
498 (next) = PR_FindNextCounterQname((handle))
500 #define PR_FIND_NEXT_COUNTER_QNAME(next,handle) NULL
503 NSPR_API(PRCounterHandle
)
504 PR_FindNextCounterQname(
505 PRCounterHandle handle
508 /* -----------------------------------------------------------------------
509 ** FUNCTION: PR_FindNextCounterRname() -- Retreive the next RName counter
513 ** PR_FindNextCounterRname() retreives the first or next RNname
514 ** handle from the counter data base, depending on the
515 ** value of handle. When handle is NULL, the function attempts to
516 ** retreive the first RName handle in the database. When handle is
517 ** a handle previosly retreived RName handle, then the function
518 ** attempts to retreive the next RName handle.
521 ** handle: PRCounterHandle or NULL.
522 ** qhandle: PRCounterHandle of a previously aquired via
523 ** PR_FIND_NEXT_QNAME_HANDLE()
527 ** RETURNS: PRCounterHandle or NULL when no more RName counter
528 ** handles are present.
531 ** A concurrent PR_CreateCounter() or PR_DestroyCounter() may
532 ** cause unpredictable results.
534 ** A PRCounterHandle returned from this function may only be used
535 ** in another PR_FindNextCounterRname() function call; other
536 ** operations may cause unpredictable results.
539 #if defined(DEBUG) || defined(FORCE_NSPR_COUNTERS)
540 #define PR_FIND_NEXT_COUNTER_RNAME(next,rhandle,qhandle)\
541 (next) = PR_FindNextCounterRname((rhandle),(qhandle))
543 #define PR_FIND_NEXT_COUNTER_RNAME(next,rhandle,qhandle)
546 NSPR_API(PRCounterHandle
)
547 PR_FindNextCounterRname(
548 PRCounterHandle rhandle
,
549 PRCounterHandle qhandle
554 #endif /* prcountr_h___ */