home *** CD-ROM | disk | FTP | other *** search
- /*
- jarray.c 4/30/86
-
- % jarray routines
-
- 'smart' array functions (replaces x,l, and iarrays)
-
- OWL 1.1
- Copyright (c) 1986-1989 by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- 4/30/86 jmd/sng created.
- 5/05/86 sng added errno handling, renamed xa_put1 to _xa_put
- 5/22/86 sng now clears out extra allocated space to NULLs.
- 6/14/86 sng fixed boundary case bug in _xa_put, added non-define
- xa_get (untested!).
- 7/08/86 sng fixed noninitialization of xa_make.
- 6/01/88 jmd changed growth strategy
- 6/01/88 jmd changed some names
- 6/24/88 jmd moved to oaklib
- 8/17/88 jmd removed eNOMEM
- 11/8/88 ted fixed bug in _xa_put: if ('elt' < XA_GROW) {
- also changed : size = (eltno < UINT_MAX - (XA_GROW + 1)) ?
- to: size = (eltno + XA_GROW < UINT_MAX) ?
-
- 4/14/89 jmd combined into one new super thingy
-
- */
-
- #include "oakhead.h"
- #include "jadecl.h"
- /* -------------------------------------------------------------------------- */
-
- jarray ja_Open(size, eltsize)
- unsigned int size;
- SIZE_T eltsize;
- /*
- Create a jarray with elements of size eltsize.
- size is the starting size of the array.
- if eltsize == 0, then it's an array of pointers.
- */
- {
- jarray j;
-
- if ((j = (jarray) omalloc(OA_XARR, sizeof(struct jarray_struct))) == NULL) {
- return(NULL);
- }
-
- if ((j->array = ocalloc(OA_XARRA, size, (eltsize == 0) ? sizeof(VOID *) : eltsize)) == NULL) {
- return(NULL);
- }
-
- j->size = size;
- j->eltsize = eltsize;
- return(j);
- }
- /* -------------------------------------------------------------------------- */
-
- void ja_Close(ja)
- jarray ja;
- {
- ofree(OA_XARRA, ja->array);
- ofree(OA_XARR, (VOID *) ja);
- }
-
- int ja_Put(ja, eltno, elt)
- jarray ja;
- unsigned int eltno;
- VOID *elt;
- /*
- Puts an element into a jarray.
-
- If necessary, it increases the size of the jarray to hold the new element.
- The array doubles in size until JA_GROW then adds JA_GROW at a time.
- */
- {
- SIZE_T eltsize;
-
- eltsize = (ja->eltsize == 0) ? sizeof(VOID *) : ja->eltsize;
-
- if (eltno >= ja->size) {
- if (!ja_Resize(ja, eltno)) {
- return(FALSE);
- }
- }
-
- /* put the new elt into the array (test if elts are pointers) */
- if (ja->eltsize == 0) {
- ((VOID **)ja->array)[(eltno)] = elt;
- }
- else {
- memmove((VOID *) ((char *) ja->array + (eltno * eltsize)), elt, eltsize);
- }
-
- return(TRUE);
- }
-
- boolean ja_Resize(ja, newsize)
- jarray ja;
- unsigned int newsize;
- /*
- Increase the size of the jarray's array.
- */
- {
- unsigned int size;
- SIZE_T eltsize;
-
- eltsize = (ja->eltsize == 0) ? sizeof(VOID *) : ja->eltsize;
-
- if (newsize < JA_GROW) {
- size = newsize * 2;
- }
- else {
- size = (newsize + JA_GROW < UINT_MAX) ? newsize + JA_GROW : UINT_MAX;
- }
-
- if ((ja->array = orealloc(OA_XARRA, ja->array, size * eltsize)) == NULL) {
- return(FALSE);
- }
-
- /* clear out the extra area */
- memset((VOID *) ((char *) ja->array + (ja->size * eltsize)), 0,
- (size - ja->size) * eltsize);
-
- ja->size = size;
-
- return(TRUE);
- }
-
- /* -------------------------------------------------------------------------- */
-
- VOID *ja_Get(ja, eltno, out)
- jarray ja;
- unsigned int eltno;
- VOID *out;
- /*
- Gets an element from a jarray.
-
- Copies it into out returns a pointer to the array slot.
-
- If eltno > size, clears out and returns NULL.
- */
- {
- SIZE_T eltsize;
- VOID *ret;
-
- eltsize = (ja->eltsize == 0) ? sizeof(VOID *) : ja->eltsize;
-
- if (eltno >= ja->size) {
- /* clear out 'out' */
- memset(out, 0, eltsize);
- return(NULL);
- }
- else {
- /* copy the array elt into 'out' */
- memmove(out, ret = (VOID *) ((char *) ja->array + (eltno * eltsize)), eltsize);
- }
-
- return(ret);
- }
-
- /* ------------------------------------------------------------------------- */
-
-