1 #ifndef lint
RCSid()2 static char *RCSid() { return RCSid("$Id: dynarray.c,v 1.12 2015/04/23 20:10:02 sfeam Exp $"); }
3 #endif
4
5 /*[
6 * Copyright 1999, 2004 Hans-Bernhard Broeker
7 *
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.
13 *
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,
18 * provided you
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
26 * software.
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.
30 *
31 * This software is provided "as is" without express or implied warranty
32 * to the extent permitted by applicable law.
33 ]*/
34
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. */
42
43 #include "dynarray.h"
44
45 #include "alloc.h"
46 #include "util.h" /* for int_error() */
47
48 static char *init_failure_msg = "dynarray wasn't initialized";
49
50
51 /* The 'constructor' of a dynarray object: initializes all the
52 * variables to well-defined startup values */
53 void
init_dynarray(dynarray * this,size_t entry_size,long size,long increment)54 init_dynarray(dynarray *this, size_t entry_size, long size, long increment)
55 {
56 this->v = 0; /* preset value, in case gp_alloc fails */
57 if (size)
58 this->v = gp_alloc(entry_size*size, "init dynarray");
59 this->size = size;
60 this->end = 0;
61 this->increment = increment;
62 this->entry_size = entry_size;
63 }
64
65 /* The 'destructor'; sets all crucial elements of the structure to
66 * well-defined values to avoid problems by use of bad pointers... */
67 void
free_dynarray(dynarray * this)68 free_dynarray(dynarray *this)
69 {
70 free(this->v); /* should work, even if gp_alloc failed */
71 this->v = 0;
72 this->end = this->size = 0;
73 }
74
75 /* Set the size of the dynamical array to a new, fixed value */
76 void
resize_dynarray(dynarray * this,long newsize)77 resize_dynarray(dynarray *this, long newsize)
78 {
79 if (!this->v)
80 int_error(NO_CARET, init_failure_msg);
81
82 if (newsize == 0)
83 free_dynarray(this);
84 else {
85 this->v = gp_realloc(this->v, this->entry_size * newsize, "extend dynarray");
86 this->size = newsize;
87 }
88 }
89
90 /* Increase the size of the dynarray by a given amount */
91 void
extend_dynarray(dynarray * this,long increment)92 extend_dynarray(dynarray *this, long increment)
93 {
94 resize_dynarray(this, this->size + increment);
95 }
96
97 /* Get pointer to the element one past the current end of the dynamic
98 * array. Resize it if necessary. Returns a pointer-to-void to that
99 * element. */
100 void GPHUGE *
nextfrom_dynarray(dynarray * this)101 nextfrom_dynarray(dynarray *this)
102 {
103 if (!this->v)
104 int_error(NO_CARET, init_failure_msg);
105
106 if (this->end >= this->size)
107 extend_dynarray(this, this->increment);
108 return (void *)((char *)(this->v) + this->entry_size * (this->end++));
109 }
110
111 /* Release the element at the current end of the dynamic array, by
112 * moving the 'end' index one element backwards */
113 void
droplast_dynarray(dynarray * this)114 droplast_dynarray(dynarray *this)
115 {
116 if (!this->v)
117 int_error(NO_CARET, init_failure_msg);
118
119 if (this->end)
120 this->end--;
121 }
122