Leptonica  1.83.1
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 
59 #ifdef HAVE_CONFIG_H
60 #include <config_auto.h>
61 #endif /* HAVE_CONFIG_H */
62 
63 #include "allheaders.h"
64 
65  /* Bounds on initial array size */
66 static const l_uint32 MaxPtrArraySize = 100000;
67 static const l_int32 InitialPtrArraySize = 20;
69  /* Static function */
70 static l_int32 lstackExtendArray(L_STACK *lstack);
71 
72 /*---------------------------------------------------------------------*
73  * Create/Destroy *
74  *---------------------------------------------------------------------*/
81 L_STACK *
82 lstackCreate(l_int32 n)
83 {
84 L_STACK *lstack;
85 
86  if (n <= 0 || n > MaxPtrArraySize)
88 
89  lstack = (L_STACK *)LEPT_CALLOC(1, sizeof(L_STACK));
90  lstack->array = (void **)LEPT_CALLOC(n, sizeof(void *));
91  if (!lstack->array) {
92  lstackDestroy(&lstack, FALSE);
93  return (L_STACK *)ERROR_PTR("lstack array not made", __func__, NULL);
94  }
95 
96  lstack->nalloc = n;
97  lstack->n = 0;
98  return lstack;
99 }
100 
101 
121 void
123  l_int32 freeflag)
124 {
125 void *item;
126 L_STACK *lstack;
127 
128  if (plstack == NULL) {
129  L_WARNING("ptr address is NULL\n", __func__);
130  return;
131  }
132  if ((lstack = *plstack) == NULL)
133  return;
134 
135  if (freeflag) {
136  while(lstack->n > 0) {
137  item = lstackRemove(lstack);
138  LEPT_FREE(item);
139  }
140  } else if (lstack->n > 0) {
141  L_WARNING("memory leak of %d items in lstack\n", __func__, lstack->n);
142  }
143 
144  if (lstack->auxstack)
145  lstackDestroy(&lstack->auxstack, freeflag);
146 
147  if (lstack->array)
148  LEPT_FREE(lstack->array);
149  LEPT_FREE(lstack);
150  *plstack = NULL;
151 }
152 
153 
154 
155 /*---------------------------------------------------------------------*
156  * Accessors *
157  *---------------------------------------------------------------------*/
165 l_ok
167  void *item)
168 {
169  if (!lstack)
170  return ERROR_INT("lstack not defined", __func__, 1);
171  if (!item)
172  return ERROR_INT("item not defined", __func__, 1);
173 
174  /* Do we need to extend the array? */
175  if (lstack->n >= lstack->nalloc) {
176  if (lstackExtendArray(lstack))
177  return ERROR_INT("extension failed", __func__, 1);
178  }
179 
180  /* Store the new pointer */
181  lstack->array[lstack->n] = (void *)item;
182  lstack->n++;
183 
184  return 0;
185 }
186 
187 
195 void *
197 {
198 void *item;
199 
200  if (!lstack)
201  return ERROR_PTR("lstack not defined", __func__, NULL);
202 
203  if (lstack->n == 0)
204  return NULL;
205 
206  lstack->n--;
207  item = lstack->array[lstack->n];
208 
209  return item;
210 }
211 
212 
219 static l_int32
221 {
222  if (!lstack)
223  return ERROR_INT("lstack not defined", __func__, 1);
224 
225  if ((lstack->array = (void **)reallocNew((void **)&lstack->array,
226  sizeof(void *) * lstack->nalloc,
227  2 * sizeof(void *) * lstack->nalloc)) == NULL)
228  return ERROR_INT("new lstack array not defined", __func__, 1);
229 
230  lstack->nalloc = 2 * lstack->nalloc;
231  return 0;
232 }
233 
234 
241 l_int32
243 {
244  if (!lstack)
245  return ERROR_INT("lstack not defined", __func__, 1);
246 
247  return lstack->n;
248 }
249 
250 
251 
252 /*---------------------------------------------------------------------*
253  * Debug output *
254  *---------------------------------------------------------------------*/
262 l_ok
263 lstackPrint(FILE *fp,
264  L_STACK *lstack)
265 {
266 l_int32 i;
267 
268  if (!fp)
269  return ERROR_INT("stream not defined", __func__, 1);
270  if (!lstack)
271  return ERROR_INT("lstack not defined", __func__, 1);
272 
273  fprintf(fp, "\n Stack: nalloc = %d, n = %d, array = %p\n",
274  lstack->nalloc, lstack->n, lstack->array);
275  for (i = 0; i < lstack->n; i++)
276  fprintf(fp, "array[%d] = %p\n", i, lstack->array[i]);
277 
278  return 0;
279 }
l_int32 lstackGetCount(L_STACK *lstack)
lstackGetCount()
Definition: stack.c:242
static const l_int32 InitialPtrArraySize
Definition: stack.c:67
static l_int32 lstackExtendArray(L_STACK *lstack)
lstackExtendArray()
Definition: stack.c:220
void lstackDestroy(L_STACK **plstack, l_int32 freeflag)
lstackDestroy()
Definition: stack.c:122
l_ok lstackAdd(L_STACK *lstack, void *item)
lstackAdd()
Definition: stack.c:166
void * lstackRemove(L_STACK *lstack)
lstackRemove()
Definition: stack.c:196
L_STACK * lstackCreate(l_int32 n)
lstackCreate()
Definition: stack.c:82
l_ok lstackPrint(FILE *fp, L_STACK *lstack)
lstackPrint()
Definition: stack.c:263
Definition: stack.h:60
void ** array
Definition: stack.h:63
struct L_Stack * auxstack
Definition: stack.h:64
l_int32 nalloc
Definition: stack.h:61
l_int32 n
Definition: stack.h:62
void * reallocNew(void **pindata, size_t oldsize, size_t newsize)
reallocNew()
Definition: utils2.c:1262