2 static char *RCSid() { return RCSid("$Id: dynarray.c,v 1.11 2004/07/01 17:10:04 broeker Exp $"); }
6 * Copyright 1999, 2004 Hans-Bernhard Broeker
8 * Permission to use, copy, and distribute this software and its
9 * documentation for any purpose with or without fee is hereby granted,
10 * provided that the above copyright notice appear in all copies and
11 * that both that copyright notice and this permission notice appear
12 * in supporting documentation.
14 * Permission to modify the software is granted, but not the right to
15 * distribute the complete modified source code. Modifications are to
16 * be distributed as patches to the released version. Permission to
17 * distribute binaries produced by compiling modified sources is granted,
19 * 1. distribute the corresponding source modifications from the
20 * released version in the form of a patch file along with the binaries,
21 * 2. add special version identification to distinguish your version
22 * in addition to the base release version number,
23 * 3. provide your name and address as the primary contact for the
24 * support of your modified version, and
25 * 4. retain our contact information in regard to use of the base
27 * Permission to distribute the released version of the source code along
28 * with corresponding source modifications in the form of a patch file is
29 * granted with same provisions 2 through 4 for binary distributions.
31 * This software is provided "as is" without express or implied warranty
32 * to the extent permitted by applicable law.
35 /* This module implements a dynamically growing array of arbitrary
36 * elements parametrized by their sizeof(). There is no 'access
37 * function', i.e. you'll have to access the elements of the
38 * dynarray->v memory block by hand. It's implemented in OO-style,
39 * even though this is C, not C++. In particular, every function
40 * takes a pointer to a data structure type 'dynarray', which mimics
41 * the 'this' pointer of an object. */
46 #include "util.h" /* for graph_error() */
48 /* The 'constructor' of a dynarray object: initializes all the
49 * variables to well-defined startup values */
51 init_dynarray(dynarray *this, size_t entry_size, long size, long increment)
53 this->v = 0; /* preset value, in case gp_alloc fails */
55 this->v = gp_alloc(entry_size*size, "init dynarray");
58 this->increment = increment;
59 this->entry_size = entry_size;
62 /* The 'destructor'; sets all crucial elements of the structure to
63 * well-defined values to avoid problems by use of bad pointers... */
65 free_dynarray(dynarray *this)
67 free(this->v); /* should work, even if gp_alloc failed */
69 this->end = this->size = 0;
72 /* Set the size of the dynamical array to a new, fixed value */
74 resize_dynarray(dynarray *this, long newsize)
77 graph_error("resize_dynarray: dynarray wasn't initialized!");
82 this->v = gp_realloc(this->v, this->entry_size * newsize, "extend dynarray");
87 /* Increase the size of the dynarray by a given amount */
89 extend_dynarray(dynarray *this, long increment)
91 resize_dynarray(this, this->size + increment);
94 /* Get pointer to the element one past the current end of the dynamic
95 * array. Resize it if necessary. Returns a pointer-to-void to that
98 nextfrom_dynarray(dynarray *this)
101 graph_error("nextfrom_dynarray: dynarray wan't initialized!");
103 if (this->end >= this->size)
104 extend_dynarray(this, this->increment);
105 return (void *)((char *)(this->v) + this->entry_size * (this->end++));
108 /* Release the element at the current end of the dynamic array, by
109 * moving the 'end' index one element backwards */
111 droplast_dynarray(dynarray *this)
114 graph_error("droplast_dynarray: dynarray wasn't initialized!");