Leptonica  1.82.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 
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  PROCNAME("lstackCreate");
87 
88  if (n <= 0 || n > MaxPtrArraySize)
90 
91  lstack = (L_STACK *)LEPT_CALLOC(1, sizeof(L_STACK));
92  lstack->array = (void **)LEPT_CALLOC(n, sizeof(void *));
93  if (!lstack->array) {
94  lstackDestroy(&lstack, FALSE);
95  return (L_STACK *)ERROR_PTR("lstack array not made", procName, NULL);
96  }
97 
98  lstack->nalloc = n;
99  lstack->n = 0;
100  return lstack;
101 }
102 
103 
123 void
125  l_int32 freeflag)
126 {
127 void *item;
128 L_STACK *lstack;
129 
130  PROCNAME("lstackDestroy");
131 
132  if (plstack == NULL) {
133  L_WARNING("ptr address is NULL\n", procName);
134  return;
135  }
136  if ((lstack = *plstack) == NULL)
137  return;
138 
139  if (freeflag) {
140  while(lstack->n > 0) {
141  item = lstackRemove(lstack);
142  LEPT_FREE(item);
143  }
144  } else if (lstack->n > 0) {
145  L_WARNING("memory leak of %d items in lstack\n", procName, lstack->n);
146  }
147 
148  if (lstack->auxstack)
149  lstackDestroy(&lstack->auxstack, freeflag);
150 
151  if (lstack->array)
152  LEPT_FREE(lstack->array);
153  LEPT_FREE(lstack);
154  *plstack = NULL;
155 }
156 
157 
158 
159 /*---------------------------------------------------------------------*
160  * Accessors *
161  *---------------------------------------------------------------------*/
169 l_ok
171  void *item)
172 {
173  PROCNAME("lstackAdd");
174 
175  if (!lstack)
176  return ERROR_INT("lstack not defined", procName, 1);
177  if (!item)
178  return ERROR_INT("item not defined", procName, 1);
179 
180  /* Do we need to extend the array? */
181  if (lstack->n >= lstack->nalloc) {
182  if (lstackExtendArray(lstack))
183  return ERROR_INT("extension failed", procName, 1);
184  }
185 
186  /* Store the new pointer */
187  lstack->array[lstack->n] = (void *)item;
188  lstack->n++;
189 
190  return 0;
191 }
192 
193 
201 void *
203 {
204 void *item;
205 
206  PROCNAME("lstackRemove");
207 
208  if (!lstack)
209  return ERROR_PTR("lstack not defined", procName, NULL);
210 
211  if (lstack->n == 0)
212  return NULL;
213 
214  lstack->n--;
215  item = lstack->array[lstack->n];
216 
217  return item;
218 }
219 
220 
227 static l_int32
229 {
230  PROCNAME("lstackExtendArray");
231 
232  if (!lstack)
233  return ERROR_INT("lstack not defined", procName, 1);
234 
235  if ((lstack->array = (void **)reallocNew((void **)&lstack->array,
236  sizeof(void *) * lstack->nalloc,
237  2 * sizeof(void *) * lstack->nalloc)) == NULL)
238  return ERROR_INT("new lstack array not defined", procName, 1);
239 
240  lstack->nalloc = 2 * lstack->nalloc;
241  return 0;
242 }
243 
244 
251 l_int32
253 {
254  PROCNAME("lstackGetCount");
255 
256  if (!lstack)
257  return ERROR_INT("lstack not defined", procName, 1);
258 
259  return lstack->n;
260 }
261 
262 
263 
264 /*---------------------------------------------------------------------*
265  * Debug output *
266  *---------------------------------------------------------------------*/
274 l_ok
275 lstackPrint(FILE *fp,
276  L_STACK *lstack)
277 {
278 l_int32 i;
279 
280  PROCNAME("lstackPrint");
281 
282  if (!fp)
283  return ERROR_INT("stream not defined", procName, 1);
284  if (!lstack)
285  return ERROR_INT("lstack not defined", procName, 1);
286 
287  fprintf(fp, "\n Stack: nalloc = %d, n = %d, array = %p\n",
288  lstack->nalloc, lstack->n, lstack->array);
289  for (i = 0; i < lstack->n; i++)
290  fprintf(fp, "array[%d] = %p\n", i, lstack->array[i]);
291 
292  return 0;
293 }
l_int32 lstackGetCount(L_STACK *lstack)
lstackGetCount()
Definition: stack.c:252
static const l_int32 InitialPtrArraySize
Definition: stack.c:67
static l_int32 lstackExtendArray(L_STACK *lstack)
lstackExtendArray()
Definition: stack.c:228
void lstackDestroy(L_STACK **plstack, l_int32 freeflag)
lstackDestroy()
Definition: stack.c:124
l_ok lstackAdd(L_STACK *lstack, void *item)
lstackAdd()
Definition: stack.c:170
void * lstackRemove(L_STACK *lstack)
lstackRemove()
Definition: stack.c:202
L_STACK * lstackCreate(l_int32 n)
lstackCreate()
Definition: stack.c:82
l_ok lstackPrint(FILE *fp, L_STACK *lstack)
lstackPrint()
Definition: stack.c:275
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:1302