Leptonica  1.77.0
Image processing and image analysis suite
stack.c
Go to the documentation of this file.
1 /*====================================================================*
2  - Copyright (C) 2001 Leptonica. All rights reserved.
3  -
4  - Redistribution and use in source and binary forms, with or without
5  - modification, are permitted provided that the following conditions
6  - are met:
7  - 1. Redistributions of source code must retain the above copyright
8  - notice, this list of conditions and the following disclaimer.
9  - 2. Redistributions in binary form must reproduce the above
10  - copyright notice, this list of conditions and the following
11  - disclaimer in the documentation and/or other materials
12  - provided with the distribution.
13  -
14  - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16  - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17  - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18  - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *====================================================================*/
26 
27 
60 #include "allheaders.h"
61 
62 static const l_int32 INITIAL_PTR_ARRAYSIZE = 20;
63 
64  /* Static function */
65 static l_int32 lstackExtendArray(L_STACK *lstack);
66 
67 
68 /*---------------------------------------------------------------------*
69  * Create/Destroy *
70  *---------------------------------------------------------------------*/
77 L_STACK *
78 lstackCreate(l_int32 nalloc)
79 {
80 L_STACK *lstack;
81 
82  PROCNAME("lstackCreate");
83 
84  if (nalloc <= 0)
85  nalloc = INITIAL_PTR_ARRAYSIZE;
86 
87  lstack = (L_STACK *)LEPT_CALLOC(1, sizeof(L_STACK));
88  lstack->array = (void **)LEPT_CALLOC(nalloc, sizeof(void *));
89  if (!lstack->array) {
90  lstackDestroy(&lstack, FALSE);
91  return (L_STACK *)ERROR_PTR("lstack array not made", procName, NULL);
92  }
93 
94  lstack->nalloc = nalloc;
95  lstack->n = 0;
96 
97  return lstack;
98 }
99 
100 
120 void
122  l_int32 freeflag)
123 {
124 void *item;
125 L_STACK *lstack;
126 
127  PROCNAME("lstackDestroy");
128 
129  if (plstack == NULL) {
130  L_WARNING("ptr address is NULL\n", procName);
131  return;
132  }
133  if ((lstack = *plstack) == NULL)
134  return;
135 
136  if (freeflag) {
137  while(lstack->n > 0) {
138  item = lstackRemove(lstack);
139  LEPT_FREE(item);
140  }
141  } else if (lstack->n > 0) {
142  L_WARNING("memory leak of %d items in lstack\n", procName, lstack->n);
143  }
144 
145  if (lstack->auxstack)
146  lstackDestroy(&lstack->auxstack, freeflag);
147 
148  if (lstack->array)
149  LEPT_FREE(lstack->array);
150  LEPT_FREE(lstack);
151  *plstack = NULL;
152 }
153 
154 
155 
156 /*---------------------------------------------------------------------*
157  * Accessors *
158  *---------------------------------------------------------------------*/
166 l_ok
168  void *item)
169 {
170  PROCNAME("lstackAdd");
171 
172  if (!lstack)
173  return ERROR_INT("lstack not defined", procName, 1);
174  if (!item)
175  return ERROR_INT("item not defined", procName, 1);
176 
177  /* Do we need to extend the array? */
178  if (lstack->n >= lstack->nalloc)
179  lstackExtendArray(lstack);
180 
181  /* Store the new pointer */
182  lstack->array[lstack->n] = (void *)item;
183  lstack->n++;
184 
185  return 0;
186 }
187 
188 
196 void *
198 {
199 void *item;
200 
201  PROCNAME("lstackRemove");
202 
203  if (!lstack)
204  return ERROR_PTR("lstack not defined", procName, NULL);
205 
206  if (lstack->n == 0)
207  return NULL;
208 
209  lstack->n--;
210  item = lstack->array[lstack->n];
211 
212  return item;
213 }
214 
215 
222 static l_int32
224 {
225  PROCNAME("lstackExtendArray");
226 
227  if (!lstack)
228  return ERROR_INT("lstack not defined", procName, 1);
229 
230  if ((lstack->array = (void **)reallocNew((void **)&lstack->array,
231  sizeof(void *) * lstack->nalloc,
232  2 * sizeof(void *) * lstack->nalloc)) == NULL)
233  return ERROR_INT("new lstack array not defined", procName, 1);
234 
235  lstack->nalloc = 2 * lstack->nalloc;
236  return 0;
237 }
238 
239 
246 l_int32
248 {
249  PROCNAME("lstackGetCount");
250 
251  if (!lstack)
252  return ERROR_INT("lstack not defined", procName, 1);
253 
254  return lstack->n;
255 }
256 
257 
258 
259 /*---------------------------------------------------------------------*
260  * Debug output *
261  *---------------------------------------------------------------------*/
269 l_ok
270 lstackPrint(FILE *fp,
271  L_STACK *lstack)
272 {
273 l_int32 i;
274 
275  PROCNAME("lstackPrint");
276 
277  if (!fp)
278  return ERROR_INT("stream not defined", procName, 1);
279  if (!lstack)
280  return ERROR_INT("lstack not defined", procName, 1);
281 
282  fprintf(fp, "\n Stack: nalloc = %d, n = %d, array = %p\n",
283  lstack->nalloc, lstack->n, lstack->array);
284  for (i = 0; i < lstack->n; i++)
285  fprintf(fp, "array[%d] = %p\n", i, lstack->array[i]);
286 
287  return 0;
288 }
l_int32 n
Definition: stack.h:62
void lstackDestroy(L_STACK **plstack, l_int32 freeflag)
lstackDestroy()
Definition: stack.c:121
l_int32 lstackGetCount(L_STACK *lstack)
lstackGetCount()
Definition: stack.c:247
l_ok lstackPrint(FILE *fp, L_STACK *lstack)
lstackPrint()
Definition: stack.c:270
void * reallocNew(void **pindata, l_int32 oldsize, l_int32 newsize)
reallocNew()
Definition: utils2.c:1161
l_int32 nalloc
Definition: stack.h:61
l_ok lstackAdd(L_STACK *lstack, void *item)
lstackAdd()
Definition: stack.c:167
void ** array
Definition: stack.h:63
void * lstackRemove(L_STACK *lstack)
lstackRemove()
Definition: stack.c:197
L_STACK * lstackCreate(l_int32 nalloc)
lstackCreate()
Definition: stack.c:78
struct L_Stack * auxstack
Definition: stack.h:64
Definition: stack.h:59
static l_int32 lstackExtendArray(L_STACK *lstack)
lstackExtendArray()
Definition: stack.c:223