Initial release of Maemo 5 port of gnuplot
[gnuplot] / src / dynarray.c
1 #ifndef lint
2 static char *RCSid() { return RCSid("$Id: dynarray.c,v 1.11 2004/07/01 17:10:04 broeker 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 graph_error() */
47
48 /* The 'constructor' of a dynarray object: initializes all the
49  * variables to well-defined startup values */
50 void
51 init_dynarray(dynarray *this, size_t entry_size, long size, long increment)
52 {
53     this->v = 0;                /* preset value, in case gp_alloc fails */
54     if (size)
55         this->v = gp_alloc(entry_size*size, "init dynarray");
56     this->size = size;
57     this->end = 0;
58     this->increment = increment;
59     this->entry_size = entry_size;
60 }
61
62 /* The 'destructor'; sets all crucial elements of the structure to
63  * well-defined values to avoid problems by use of bad pointers... */
64 void
65 free_dynarray(dynarray *this)
66 {
67     free(this->v);              /* should work, even if gp_alloc failed */
68     this->v = 0;
69     this->end = this->size = 0;
70 }
71
72 /* Set the size of the dynamical array to a new, fixed value */
73 void
74 resize_dynarray(dynarray *this, long newsize)
75 {
76     if (!this->v)
77         graph_error("resize_dynarray: dynarray wasn't initialized!");
78
79     if (newsize == 0)
80         free_dynarray(this);
81     else {
82         this->v = gp_realloc(this->v, this->entry_size * newsize, "extend dynarray");
83         this->size = newsize;
84     }
85 }
86
87 /* Increase the size of the dynarray by a given amount */
88 void
89 extend_dynarray(dynarray *this, long increment)
90 {
91     resize_dynarray(this, this->size + increment);
92 }
93
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
96  * element. */
97 void GPHUGE *
98 nextfrom_dynarray(dynarray *this)
99 {
100     if (!this->v)
101         graph_error("nextfrom_dynarray: dynarray wan't initialized!");
102
103     if (this->end >= this->size)
104         extend_dynarray(this, this->increment);
105     return (void *)((char *)(this->v) + this->entry_size * (this->end++));
106 }
107
108 /* Release the element at the current end of the dynamic array, by
109  * moving the 'end' index one element backwards */
110 void
111 droplast_dynarray(dynarray *this)
112 {
113     if (!this->v)
114         graph_error("droplast_dynarray: dynarray wasn't initialized!");
115
116     if (this->end)
117         this->end--;
118 }