Back to index

lightning-sunbird  0.9+nobinonly
Functions
cmsarray.c File Reference
#include "cmslocal.h"
#include "secerr.h"

Go to the source code of this file.

Functions

void ** NSS_CMSArray_Alloc (PRArenaPool *poolp, int n)
SECStatus NSS_CMSArray_Add (PRArenaPool *poolp, void ***array, void *obj)
PRBool NSS_CMSArray_IsEmpty (void **array)
int NSS_CMSArray_Count (void **array)
void NSS_CMSArray_Sort (void **primary, int(*compare)(void *, void *), void **secondary, void **tertiary)

Function Documentation

SECStatus NSS_CMSArray_Add ( PRArenaPool poolp,
void ***  array,
void obj 
)

Definition at line 75 of file cmsarray.c.

{
    void **p;
    int n;
    void **dest;

    PORT_Assert(array != NULL);
    if (array == NULL)
       return SECFailure;

    if (*array == NULL) {
       dest = (void **)PORT_ArenaAlloc(poolp, 2 * sizeof(void *));
       n = 0;
    } else {
       n = 0; p = *array;
       while (*p++)
           n++;
       dest = (void **)PORT_ArenaGrow (poolp, 
                           *array,
                           (n + 1) * sizeof(void *),
                           (n + 2) * sizeof(void *));
    }

    if (dest == NULL)
       return SECFailure;

    dest[n] = obj;
    dest[n+1] = NULL;
    *array = dest;
    return SECSuccess;
}

Here is the caller graph for this function:

void** NSS_CMSArray_Alloc ( PRArenaPool poolp,
int  n 
)

Definition at line 64 of file cmsarray.c.

{
    return (void **)PORT_ArenaZAlloc(poolp, n * sizeof(void *));
}
int NSS_CMSArray_Count ( void **  array)

Definition at line 120 of file cmsarray.c.

{
    int n = 0;

    if (array == NULL)
       return 0;

    while (*array++ != NULL)
       n++;

    return n;
}

Here is the caller graph for this function:

Definition at line 111 of file cmsarray.c.

{
    return (array == NULL || array[0] == NULL);
}

Here is the caller graph for this function:

void NSS_CMSArray_Sort ( void **  primary,
int(*)(void *, void *)  compare,
void **  secondary,
void **  tertiary 
)

Definition at line 146 of file cmsarray.c.

{
    int n, i, limit, lastxchg;
    void *tmp;

    n = NSS_CMSArray_Count(primary);

    PORT_Assert(secondary == NULL || NSS_CMSArray_Count(secondary) == n);
    PORT_Assert(tertiary == NULL || NSS_CMSArray_Count(tertiary) == n);
    
    if (n <= 1)      /* ordering is fine */
       return;
    
    /* yes, ladies and gentlemen, it's BUBBLE SORT TIME! */
    limit = n - 1;
    while (1) {
       lastxchg = 0;
       for (i = 0; i < limit; i++) {
           if ((*compare)(primary[i], primary[i+1]) > 0) {
              /* exchange the neighbours */
              tmp = primary[i+1];
              primary[i+1] = primary[i];
              primary[i] = tmp;
              if (secondary) {            /* secondary array? */
                  tmp = secondary[i+1];   /* exchange there as well */
                  secondary[i+1] = secondary[i];
                  secondary[i] = tmp;
              }
              if (tertiary) {                    /* tertiary array? */
                  tmp = tertiary[i+1];    /* exchange there as well */
                  tertiary[i+1] = tertiary[i];
                  tertiary[i] = tmp;
              }
              lastxchg = i+1;      /* index of the last element bubbled up */
           }
       }
       if (lastxchg == 0)   /* no exchanges, so array is sorted */
           break;           /* we're done */
       limit = lastxchg;    /* array is sorted up to [limit] */
    }
}

Here is the caller graph for this function: