michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * error.c michael@0: * michael@0: * This file contains the code implementing the per-thread error michael@0: * stacks upon which most NSS routines report their errors. michael@0: */ michael@0: michael@0: #ifndef BASE_H michael@0: #include "base.h" michael@0: #endif /* BASE_H */ michael@0: #include /* for UINT_MAX */ michael@0: #include /* for memmove */ michael@0: michael@0: #define NSS_MAX_ERROR_STACK_COUNT 16 /* error codes */ michael@0: michael@0: /* michael@0: * The stack itself has a header, and a sequence of integers. michael@0: * The header records the amount of space (as measured in stack michael@0: * slots) already allocated for the stack, and the count of the michael@0: * number of records currently being used. michael@0: */ michael@0: michael@0: struct stack_header_str { michael@0: PRUint16 space; michael@0: PRUint16 count; michael@0: }; michael@0: michael@0: struct error_stack_str { michael@0: struct stack_header_str header; michael@0: PRInt32 stack[1]; michael@0: }; michael@0: typedef struct error_stack_str error_stack; michael@0: michael@0: /* michael@0: * error_stack_index michael@0: * michael@0: * Thread-private data must be indexed. This is that index. michael@0: * See PR_NewThreadPrivateIndex for more information. michael@0: * michael@0: * Thread-private data indexes are in the range [0, 127]. michael@0: */ michael@0: michael@0: #define INVALID_TPD_INDEX UINT_MAX michael@0: static PRUintn error_stack_index = INVALID_TPD_INDEX; michael@0: michael@0: /* michael@0: * call_once michael@0: * michael@0: * The thread-private index must be obtained (once!) at runtime. michael@0: * This block is used for that one-time call. michael@0: */ michael@0: michael@0: static PRCallOnceType error_call_once; michael@0: michael@0: /* michael@0: * error_once_function michael@0: * michael@0: * This is the once-called callback. michael@0: */ michael@0: static PRStatus michael@0: error_once_function ( void) michael@0: { michael@0: return PR_NewThreadPrivateIndex(&error_stack_index, PR_Free); michael@0: } michael@0: michael@0: /* michael@0: * error_get_my_stack michael@0: * michael@0: * This routine returns the calling thread's error stack, creating michael@0: * it if necessary. It may return NULL upon error, which implicitly michael@0: * means that it ran out of memory. michael@0: */ michael@0: michael@0: static error_stack * michael@0: error_get_my_stack ( void) michael@0: { michael@0: PRStatus st; michael@0: error_stack *rv; michael@0: PRUintn new_size; michael@0: PRUint32 new_bytes; michael@0: error_stack *new_stack; michael@0: michael@0: if( INVALID_TPD_INDEX == error_stack_index ) { michael@0: st = PR_CallOnce(&error_call_once, error_once_function); michael@0: if( PR_SUCCESS != st ) { michael@0: return (error_stack *)NULL; michael@0: } michael@0: } michael@0: michael@0: rv = (error_stack *)PR_GetThreadPrivate(error_stack_index); michael@0: if( (error_stack *)NULL == rv ) { michael@0: /* Doesn't exist; create one */ michael@0: new_size = 16; michael@0: } else if( rv->header.count == rv->header.space && michael@0: rv->header.count < NSS_MAX_ERROR_STACK_COUNT ) { michael@0: /* Too small, expand it */ michael@0: new_size = PR_MIN( rv->header.space * 2, NSS_MAX_ERROR_STACK_COUNT); michael@0: } else { michael@0: /* Okay, return it */ michael@0: return rv; michael@0: } michael@0: michael@0: new_bytes = (new_size * sizeof(PRInt32)) + sizeof(error_stack); michael@0: /* Use NSPR's calloc/realloc, not NSS's, to avoid loops! */ michael@0: new_stack = PR_Calloc(1, new_bytes); michael@0: michael@0: if( (error_stack *)NULL != new_stack ) { michael@0: if( (error_stack *)NULL != rv ) { michael@0: (void)nsslibc_memcpy(new_stack,rv,rv->header.space); michael@0: } michael@0: new_stack->header.space = new_size; michael@0: } michael@0: michael@0: /* Set the value, whether or not the allocation worked */ michael@0: PR_SetThreadPrivate(error_stack_index, new_stack); michael@0: return new_stack; michael@0: } michael@0: michael@0: /* michael@0: * The error stack michael@0: * michael@0: * The public methods relating to the error stack are: michael@0: * michael@0: * NSS_GetError michael@0: * NSS_GetErrorStack michael@0: * michael@0: * The nonpublic methods relating to the error stack are: michael@0: * michael@0: * nss_SetError michael@0: * nss_ClearErrorStack michael@0: * michael@0: */ michael@0: michael@0: /* michael@0: * NSS_GetError michael@0: * michael@0: * This routine returns the highest-level (most general) error set michael@0: * by the most recent NSS library routine called by the same thread michael@0: * calling this routine. michael@0: * michael@0: * This routine cannot fail. However, it may return zero, which michael@0: * indicates that the previous NSS library call did not set an error. michael@0: * michael@0: * Return value: michael@0: * 0 if no error has been set michael@0: * A nonzero error number michael@0: */ michael@0: michael@0: NSS_IMPLEMENT PRInt32 michael@0: NSS_GetError ( void) michael@0: { michael@0: error_stack *es = error_get_my_stack(); michael@0: michael@0: if( (error_stack *)NULL == es ) { michael@0: return NSS_ERROR_NO_MEMORY; /* Good guess! */ michael@0: } michael@0: michael@0: if( 0 == es->header.count ) { michael@0: return 0; michael@0: } michael@0: michael@0: return es->stack[ es->header.count-1 ]; michael@0: } michael@0: michael@0: /* michael@0: * NSS_GetErrorStack michael@0: * michael@0: * This routine returns a pointer to an array of integers, containing michael@0: * the entire sequence or "stack" of errors set by the most recent NSS michael@0: * library routine called by the same thread calling this routine. michael@0: * NOTE: the caller DOES NOT OWN the memory pointed to by the return michael@0: * value. The pointer will remain valid until the calling thread michael@0: * calls another NSS routine. The lowest-level (most specific) error michael@0: * is first in the array, and the highest-level is last. The array is michael@0: * zero-terminated. This routine may return NULL upon error; this michael@0: * indicates a low-memory situation. michael@0: * michael@0: * Return value: michael@0: * NULL upon error, which is an implied NSS_ERROR_NO_MEMORY michael@0: * A NON-caller-owned pointer to an array of integers michael@0: */ michael@0: michael@0: NSS_IMPLEMENT PRInt32 * michael@0: NSS_GetErrorStack ( void) michael@0: { michael@0: error_stack *es = error_get_my_stack(); michael@0: michael@0: if( (error_stack *)NULL == es ) { michael@0: return (PRInt32 *)NULL; michael@0: } michael@0: michael@0: /* Make sure it's terminated */ michael@0: es->stack[ es->header.count ] = 0; michael@0: michael@0: return es->stack; michael@0: } michael@0: michael@0: /* michael@0: * nss_SetError michael@0: * michael@0: * This routine places a new error code on the top of the calling michael@0: * thread's error stack. Calling this routine wiht an error code michael@0: * of zero will clear the error stack. michael@0: */ michael@0: michael@0: NSS_IMPLEMENT void michael@0: nss_SetError ( PRUint32 error) michael@0: { michael@0: error_stack *es; michael@0: michael@0: if( 0 == error ) { michael@0: nss_ClearErrorStack(); michael@0: return; michael@0: } michael@0: michael@0: es = error_get_my_stack(); michael@0: if( (error_stack *)NULL == es ) { michael@0: /* Oh, well. */ michael@0: return; michael@0: } michael@0: michael@0: if (es->header.count < es->header.space) { michael@0: es->stack[ es->header.count++ ] = error; michael@0: } else { michael@0: memmove(es->stack, es->stack + 1, michael@0: (es->header.space - 1) * (sizeof es->stack[0])); michael@0: es->stack[ es->header.space - 1 ] = error; michael@0: } michael@0: return; michael@0: } michael@0: michael@0: /* michael@0: * nss_ClearErrorStack michael@0: * michael@0: * This routine clears the calling thread's error stack. michael@0: */ michael@0: michael@0: NSS_IMPLEMENT void michael@0: nss_ClearErrorStack ( void) michael@0: { michael@0: error_stack *es = error_get_my_stack(); michael@0: if( (error_stack *)NULL == es ) { michael@0: /* Oh, well. */ michael@0: return; michael@0: } michael@0: michael@0: es->header.count = 0; michael@0: es->stack[0] = 0; michael@0: return; michael@0: } michael@0: michael@0: /* michael@0: * nss_DestroyErrorStack michael@0: * michael@0: * This routine frees the calling thread's error stack. michael@0: */ michael@0: michael@0: NSS_IMPLEMENT void michael@0: nss_DestroyErrorStack ( void) michael@0: { michael@0: if( INVALID_TPD_INDEX != error_stack_index ) { michael@0: PR_SetThreadPrivate(error_stack_index, NULL); michael@0: } michael@0: return; michael@0: }