diff options
Diffstat (limited to 'src/util/dyn')
| -rw-r--r-- | src/util/dyn/ChangeLog | 13 | ||||
| -rw-r--r-- | src/util/dyn/Imakefile | 26 | ||||
| -rw-r--r-- | src/util/dyn/Makefile.in | 65 | ||||
| -rw-r--r-- | src/util/dyn/README | 32 | ||||
| -rw-r--r-- | src/util/dyn/TODO | 3 | ||||
| -rw-r--r-- | src/util/dyn/configure.in | 14 | ||||
| -rw-r--r-- | src/util/dyn/dyn.3m | 198 | ||||
| -rw-r--r-- | src/util/dyn/dyn_append.c | 26 | ||||
| -rw-r--r-- | src/util/dyn/dyn_initzero.c | 26 | ||||
| -rw-r--r-- | src/util/dyn/dyn_insert.c | 70 | ||||
| -rw-r--r-- | src/util/dyn/dyn_paranoid.c | 26 | ||||
| -rw-r--r-- | src/util/dyn/dyn_realloc.c | 88 |
12 files changed, 587 insertions, 0 deletions
diff --git a/src/util/dyn/ChangeLog b/src/util/dyn/ChangeLog new file mode 100644 index 0000000000..9fac926656 --- /dev/null +++ b/src/util/dyn/ChangeLog @@ -0,0 +1,13 @@ +Mon Jul 22 04:20:48 1996 Marc Horowitz <marc@mit.edu> + + * dyn_insert.c (DynInsert): what used to be #ifdef POSIX, should + be #ifdef HAVE_MEMMOVE + +Tue Jul 9 19:30:40 1996 Marc Horowitz <marc@mit.edu> + + * configure.in (DEPLIBS): AC_SUBST() it, to hack around an + incorrect assumption in aclocal.m4 + * Makefile.in (DONE): add rules and macros to support shared + libraries + + diff --git a/src/util/dyn/Imakefile b/src/util/dyn/Imakefile new file mode 100644 index 0000000000..471cf5bc81 --- /dev/null +++ b/src/util/dyn/Imakefile @@ -0,0 +1,26 @@ +# This file is part of libdyn.a, the C Dynamic Object library. It +# contains the Imakefile. +# +# There are no restrictions on this code; however, if you make any +# changes, I request that you document them so that I do not get +# credit or blame for your modifications. +# +# Written by Barr3y Jaspan, Student Information Processing Board (SIPB) +# and MIT-Project Athena, 1989. + +SRCS = dyn_create.c dyn_put.c dyn_debug.c dyn_delete.c dyn_size.c \ + dyn_append.c dyn_realloc.c dyn_paranoid.c dyn_insert.c \ + dyn_initzero.c +OBJS = dyn_create.o dyn_put.o dyn_debug.o dyn_delete.o dyn_size.o \ + dyn_append.o dyn_realloc.o dyn_paranoid.o dyn_insert.o \ + dyn_initzero.o +HDRS = dyn.h dynP.h + +DEST = libdyn.a + +StageLibrary($(DEST), $(OBJS)) +StageIncludes(dyn.h,) + +Program(test, test.o, $(DEST), $(DEST)) + +Depend(,, $(SRCS) $(HDRS)) diff --git a/src/util/dyn/Makefile.in b/src/util/dyn/Makefile.in new file mode 100644 index 0000000000..e2c4255bd8 --- /dev/null +++ b/src/util/dyn/Makefile.in @@ -0,0 +1,65 @@ +CFLAGS = $(CCOPTS) $(DEFS) + +##DOSBUILDTOP = ..\.. +##DOSLIBNAME=libdyn.lib + +.c.o: + $(CC) $(CFLAGS) -c $(srcdir)/$*.c +@SHARED_RULE@ + +SRCS = $(srcdir)/dyn_create.c \ + $(srcdir)/dyn_put.c \ + $(srcdir)/dyn_debug.c \ + $(srcdir)/dyn_delete.c \ + $(srcdir)/dyn_size.c \ + $(srcdir)/dyn_append.c \ + $(srcdir)/dyn_realloc.c \ + $(srcdir)/dyn_paranoid.c \ + $(srcdir)/dyn_insert.c \ + $(srcdir)/dyn_initzero.c + +OBJS = dyn_create.$(OBJEXT) \ + dyn_put.$(OBJEXT) \ + dyn_debug.$(OBJEXT) \ + dyn_delete.$(OBJEXT) \ + dyn_size.$(OBJEXT) \ + dyn_append.$(OBJEXT) \ + dyn_realloc.$(OBJEXT) \ + dyn_paranoid.$(OBJEXT) \ + dyn_insert.$(OBJEXT) \ + dyn_initzero.$(OBJEXT) + +LIB_SUBDIRS= . +LIBDONE= DONE + +all-unix:: shared includes $(OBJS) +all-mac:: $(OBJS) +all-windows:: $(OBJS) + +shared: + mkdir shared + +check-windows:: + +clean-unix:: + $(RM) shared/* + +clean-mac:: +clean-windows:: + +DONE:: $(OBJS) + $(RM) DONE + echo $(OBJS) >DONE + +libdyn.$(STEXT): $(OBJS) + $(RM) $@ + $(ARADD) $@ $(OBJS) + $(RANLIB) $@ + +install:: libdyn.a + $(INSTALL_DATA) libdyn.a $(DESTDIR)$(KRB5_LIBDIR)/libdyn.a + $(RANLIB) $(DESTDIR)$(KRB5_LIBDIR)/libdyn.a + +clean:: + $(RM) libdyn.$(LIBEXT) libdyn.bak DONE + diff --git a/src/util/dyn/README b/src/util/dyn/README new file mode 100644 index 0000000000..0c08ac5c62 --- /dev/null +++ b/src/util/dyn/README @@ -0,0 +1,32 @@ +libdyn.a -- Release 1.0 + +A C Dynamic Object is an array that takes care of resizing itself as +elements are added and deleted from it. It can be of any type for +which sizeof is defined and for which an address of a variable of that +type can be passed to a function. + +To build libdyn.a, simply type "make depend all" (if you don't have +the program makedepend, of course, leave out the "depend" part). If +your system's bcopy() cannot handle overlapping regions, you'll need +to write one that can. (Left as an excercise for the reader..) + +The library should compile and work without modification on a vast +number of systems. It only uses 5 external functions: malloc, +realloc, free, bcopy, and fprintf (to stderr). Of these, only bcopy +should need to be changed for other systems (such as MS-DOS) and it +could probably be done with a -D flag to the compiler. + +The test/demo program is built by "make all". This program produces +the library's debugging output (to stderr) as well as some of its own +output (to stdout). + +The library has been tested (with test.c) on a VAX VSII, VAXstation +3100, DECstation 3100, and IBM RT all running BSD4.3 (except for the +DECstation, which was running Ultrix V2.1). + +An earlier version of this library was posted to alt.sources. This +version contains one new function (DynInsert) and slightly cleaner +code, but no bugfixes (no bugs were found). + +Author: Barr3y Jaspan, Student Information Processing Board (SIPB) and +MIT-Project Athena, bjaspan@athena.mit.edu, 1990 diff --git a/src/util/dyn/TODO b/src/util/dyn/TODO new file mode 100644 index 0000000000..d5a242b981 --- /dev/null +++ b/src/util/dyn/TODO @@ -0,0 +1,3 @@ +o be able to get obj->size +o be able to get array without DynAdd (so you can just use DynPut) +o be able to specify bzero on realloc diff --git a/src/util/dyn/configure.in b/src/util/dyn/configure.in new file mode 100644 index 0000000000..6ba8e6d24a --- /dev/null +++ b/src/util/dyn/configure.in @@ -0,0 +1,14 @@ +AC_INIT(dyn.h) +CONFIG_RULES +AC_PROG_ARCHIVE +AC_PROG_ARCHIVE_ADD +AC_PROG_RANLIB +AC_PROG_INSTALL +AC_CHECK_FUNCS(memmove) +V5_SHARED_LIB_OBJS +V5_MAKE_SHARED_LIB(libdyn,1.0,../../lib, ../util/dyn) +dnl DEPLIBS is normally set by KRB5_LIBRARIES, but that makes assumptions +dnl which fail for libdyn. +AC_SUBST(DEPLIBS) +CopySrcHeader(dyn.h,[$](BUILDTOP)/include) +V5_AC_OUTPUT_MAKEFILE diff --git a/src/util/dyn/dyn.3m b/src/util/dyn/dyn.3m new file mode 100644 index 0000000000..21ae70bb2f --- /dev/null +++ b/src/util/dyn/dyn.3m @@ -0,0 +1,198 @@ + + + +15 March 1990 DYN(3M) + + + +NAME + dyn - the C Dynamic Object library + + +DESCRIPTION + A C Dynamic Object is an array that takes care of resizing + itself as you add and delete elements from it. It can be of + any type for which sizeof is defined and for which an + address of a variable of that type can be passed to a func- + tion. The library containing the functions described below + is called _l_i_b_d_y_n._a, and the necessary declarations to use + them are in <_d_y_n._h>. + + A DynObject is actually a structure that contains an array + and a couple of integers to maintain necessary state infor- + mation. When a Dyn function is said to operate on "the + object" or "the array", it is operating on the array stored + in the structure while at the same time updating internal + state information. + + +LIST OF FUNCTIONS + DynObject DynCreate(size, increment) + int size, increment; + + _R_e_q_u_i_r_e_s: _s_i_z_e and _i_n_c_r_e_m_e_n_t are greater than zero. + + _E_f_f_e_c_t_s: Creates a new DynObject that will store elements of + size _s_i_z_e and will allocate memory in blocks large enough to + hold exactly _i_n_c_r_e_m_e_n_t elements. For example, if you are + storing 8-byte double precision numbers and _i_n_c_r_e_m_e_n_t is 5, + each 5th element you add to the object will cause it to + request 40 more bytes (8 * 5) from the operating system. If + _i_n_c_r_e_m_e_n_t is zero, a default value is used (currently 100). + This is the only time the programmer deals with a dynamic + object's memory allocation. + + _R_e_t_u_r_n_s: DynCreate returns the new DynObject, or NULL if + there is insufficient memory. + + int DynDestroy(obj) + DynObject obj; + + _M_o_d_i_f_i_e_s: obj + + _E_f_f_e_c_t_s: Frees all memory associated with _o_b_j. The results + of calling any Dyn function on a destroyed object are unde- + fined (except for DynCreate, which resets the object). + + _R_e_t_u_r_n_s: DynDestroy returns DYN_OK. + + + + + + 1 + + + + + + +DYN(3M) 15 March 1990 + + + + int DynAdd(obj, el) + DynObject obj; + DynPtr el; + + _M_o_d_i_f_i_e_s: obj + + _E_f_f_e_c_t_s: Adds the element pointed to by _e_l to the object + _o_b_j, resizing the object if necessary. The new element + becomes the last element in obj's array. + + _R_e_t_u_r_n_s: DynAdd returns DYN_OK on success or DYN_NOMEM if + there is insufficient memory. + + int DynInsert(obj, index, els, num) + DynObject obj; + DynPtr els; + int index, num; + + _M_o_d_i_f_i_e_s: obj + + _E_f_f_e_c_t_s: Inserts the array of _n_u_m elements, pointed to by + _e_l_s, into the object _o_b_j starting at the array location + _i_n_d_e_x, resizing the object if necessary. Order is + preserved; if you have the array "1 2 3 4 5" and insert "10 + 11 12" at the third position, you will have the array "1 2 + 10 11 12 3 4 5". + + _R_e_t_u_r_n_s: DynInsert returns DYN_BADINDEX if _i_n_d_e_x is not + between 0 and DynSize(obj); DYN_BADVALUE if _n_u_m is less than + 1; DYN_NOMEM if there is insufficient memory. + + int DynGet(obj, index) + DynObject obj; + int index; + + _E_f_f_e_c_t_s: Returns the address of the element _i_n_d_e_x in the + array of _o_b_j. This pointer can be treated as a normal array + of the type specified to DynCreate. The order of elements + in this array is the order in which they were added to the + object. The returned pointer is guaranteed to be valid only + until obj is modified. + + _R_e_t_u_r_n_s: DynGet returns NULL if _i_n_d_e_x is larger than the + number of elements in the array of less than zero. + + int DynDelete(obj, index) + DynObject obj; + int index; + + _M_o_d_i_f_i_e_s: obj + + + + + +2 + + + + + + +15 March 1990 DYN(3M) + + + + _E_f_f_e_c_t_s: The element _i_n_d_e_x is deleted from the object _o_b_j. + Note that the element is actually removed permanently from + the array. If you have the array "1 2 3 4 5" and delete the + third element, you will have the array "1 2 4 5". The order + of elements in not affected. + + _R_e_t_u_r_n_s: DynDelete will return DYN_OK on success or + DYN_BADINDEX if the element _i_n_d_e_x does not exist in the + array or is less than zero. + + int DynSize(obj) + DynObject obj; + + _E_f_f_e_c_t_s: Returns the number of elements in the object _o_b_j. + + int DynHigh(obj) + DynObject obj; + + _E_f_f_e_c_t_s: Returns the index of the highest element in the + object _o_b_j. In this version, DynHigh is macro that expands + to DynSize - 1. + + int DynLow(obj) + DynObject obj; + + _E_f_f_e_c_t_s: Returns the index of the lowest element in the + object _o_b_j. In this version, DynLow is macro that expands + to 0. + + int DynDebug(obj, state) + DynObject obj; + int state; + + _M_o_d_i_f_i_e_s: obj + + _E_f_f_e_c_t_s: Sets the debugging state of _o_b_j to _s_t_a_t_e and prints + a message on stderr saying what state debugging was set to. + Any non-zero value for _s_t_a_t_e turns debugging ``on''. When + debugging is on, all Dyn functions will produce (hopefully + useful) output to stderr describing what is going on. + + _R_e_t_u_r_n_s: DynDebug returns DYN_OK. + +AUTHOR + Barr3y Jaspan, Student Information Processing Board (SIPB) + and MIT-Project Athena, bjaspan@athena.mit.edu + + + + + + + + + + 3 + + + diff --git a/src/util/dyn/dyn_append.c b/src/util/dyn/dyn_append.c new file mode 100644 index 0000000000..81403ece70 --- /dev/null +++ b/src/util/dyn/dyn_append.c @@ -0,0 +1,26 @@ +/* + * This file is part of libdyn.a, the C Dynamic Object library. It + * contains the source code for the function DynAppend(). + * + * There are no restrictions on this code; however, if you make any + * changes, I request that you document them so that I do not get + * credit or blame for your modifications. + * + * Written by Barr3y Jaspan, Student Information Processing Board (SIPB) + * and MIT-Project Athena, 1989. + */ + +#include <stdio.h> + +#include "dynP.h" + +/* + * Made obsolete by DynInsert, now just a convenience function. + */ +int DynAppend(obj, els, num) + DynObjectP obj; + DynPtr els; + int num; +{ + return DynInsert(obj, DynSize(obj), els, num); +} diff --git a/src/util/dyn/dyn_initzero.c b/src/util/dyn/dyn_initzero.c new file mode 100644 index 0000000000..3949f30ec4 --- /dev/null +++ b/src/util/dyn/dyn_initzero.c @@ -0,0 +1,26 @@ +/* + * This file is part of libdyn.a, the C Dynamic Object library. It + * contains the source code for the function DynInitZero(). + * + * There are no restrictions on this code; however, if you make any + * changes, I request that you document them so that I do not get + * credit or blame for your modifications. + * + * Written by Barr3y Jaspan, Student Information Processing Board (SIPB) + * and MIT-Project Athena, 1989. + */ + +#include <stdio.h> + +#include "dynP.h" + +int DynInitzero(obj, state) + DynObjectP obj; + int state; +{ + obj->initzero = state; + + if (obj->debug) + fprintf(stderr, "dyn: initzero: initzero set to %d.\n", state); + return DYN_OK; +} diff --git a/src/util/dyn/dyn_insert.c b/src/util/dyn/dyn_insert.c new file mode 100644 index 0000000000..5654e935d1 --- /dev/null +++ b/src/util/dyn/dyn_insert.c @@ -0,0 +1,70 @@ +/* + * This file is part of libdyn.a, the C Dynamic Object library. It + * contains the source code for the function DynInsert(). + * + * There are no restrictions on this code; however, if you make any + * changes, I request that you document them so that I do not get + * credit or blame for your modifications. + * + * Written by Barr3y Jaspan, Student Information Processing Board (SIPB) + * and MIT-Project Athena, 1989. + */ + +#include <stdio.h> +#include "dynP.h" + +int DynInsert(obj, idx, els_in, num) + DynObjectP obj; + void *els_in; + int idx, num; +{ + DynPtr els = (DynPtr) els_in; + int ret; + + if (idx < 0 || idx > obj->num_el) { + if (obj->debug) + fprintf(stderr, "dyn: insert: index %d is not in [0,%d]\n", + idx, obj->num_el); + return DYN_BADINDEX; + } + + if (num < 1) { + if (obj->debug) + fprintf(stderr, "dyn: insert: cannot insert %d elements\n", + num); + return DYN_BADVALUE; + } + + if (obj->debug) + fprintf(stderr,"dyn: insert: Moving %d bytes from %d + %d to + %d\n", + (obj->num_el-idx)*obj->el_size, obj->array, + obj->el_size*idx, obj->el_size*(idx+num)); + + if ((ret = _DynResize(obj, obj->num_el + num)) != DYN_OK) + return ret; +#ifdef HAVE_MEMMOVE + memmove(obj->array + obj->el_size*(idx + num), + obj->array + obj->el_size*idx, + (obj->num_el-idx)*obj->el_size); +#else + bcopy(obj->array + obj->el_size*idx, + obj->array + obj->el_size*(idx + num), + (obj->num_el-idx)*obj->el_size); +#endif + + if (obj->debug) + fprintf(stderr, "dyn: insert: Copying %d bytes from %d to %d + %d\n", + obj->el_size*num, els, obj->array, obj->el_size*idx); + +#ifdef HAVE_MEMMOVE + memmove(obj->array + obj->el_size*idx, els, obj->el_size*num); +#else + bcopy(els, obj->array + obj->el_size*idx, obj->el_size*num); +#endif + obj->num_el += num; + + if (obj->debug) + fprintf(stderr, "dyn: insert: done.\n"); + + return DYN_OK; +} diff --git a/src/util/dyn/dyn_paranoid.c b/src/util/dyn/dyn_paranoid.c new file mode 100644 index 0000000000..7eb750a21a --- /dev/null +++ b/src/util/dyn/dyn_paranoid.c @@ -0,0 +1,26 @@ +/* + * This file is part of libdyn.a, the C Dynamic Object library. It + * contains the source code for the function DynDebug(). + * + * There are no restrictions on this code; however, if you make any + * changes, I request that you document them so that I do not get + * credit or blame for your modifications. + * + * Written by Barr3y Jaspan, Student Information Processing Board (SIPB) + * and MIT-Project Athena, 1989. + */ + +#include <stdio.h> + +#include "dynP.h" + +int DynParanoid(obj, state) + DynObjectP obj; + int state; +{ + obj->paranoid = state; + + if (obj->debug) + fprintf(stderr, "dyn: paranoid: Paranoia set to %d.\n", state); + return DYN_OK; +} diff --git a/src/util/dyn/dyn_realloc.c b/src/util/dyn/dyn_realloc.c new file mode 100644 index 0000000000..31e3975b57 --- /dev/null +++ b/src/util/dyn/dyn_realloc.c @@ -0,0 +1,88 @@ +/* + * This file is part of libdyn.a, the C Dynamic Object library. It + * contains the source code for the internal function _DynRealloc(). + * + * There are no restrictions on this code; however, if you make any + * changes, I request that you document them so that I do not get + * credit or blame for your modifications. + * + * Written by Barr3y Jaspan, Student Information Processing Board (SIPB) + * and MIT-Project Athena, 1989. + */ + +#include <stdio.h> +#include <stdlib.h> + +#include "dynP.h" + +/* + * Resize the array so that element req exists. + */ +int _DynResize(obj, req) + DynObjectP obj; + int req; +{ + int cnt, size; + + if (obj->size > req) + return DYN_OK; + else if (obj->inc > 0) + return _DynRealloc(obj, (req - obj->size) / obj->inc + 1); + else { + if (obj->size == 0) + size = -obj->inc; + else + size = obj->size; + + while (size <= req) + size <<= 1; + + return _DynRealloc(obj, size); + } +} + +/* + * Resize the array by num_incs units. If obj->inc is positive, this + * means make it obj->inc*num_incs elements larger. If obj->inc is + * negative, this means make the array num_incs elements long. + * + * Ideally, this function should not be called from outside the + * library. However, nothing will break if it is. + */ +int _DynRealloc(obj, num_incs) + DynObjectP obj; + int num_incs; +{ + DynPtr temp; + int new_size_in_bytes; + + if (obj->inc > 0) + new_size_in_bytes = obj->el_size*(obj->size + obj->inc*num_incs); + else + new_size_in_bytes = obj->el_size*num_incs; + + if (obj->debug) + fprintf(stderr, + "dyn: alloc: Increasing object by %d bytes (%d incs).\n", + new_size_in_bytes - obj->el_size*obj->size, + num_incs); + + temp = (DynPtr) realloc(obj->array, new_size_in_bytes); + if (temp == NULL) { + if (obj->debug) + fprintf(stderr, "dyn: alloc: Out of memory.\n"); + return DYN_NOMEM; + } + else { + obj->array = temp; + if (obj->inc > 0) + obj->size += obj->inc*num_incs; + else + obj->size = num_incs; + } + + if (obj->debug) + fprintf(stderr, "dyn: alloc: done.\n"); + + return DYN_OK; +} |
