xref: /haiku/src/libs/mapm/mapmstck.c (revision 59d799dabcba86f92658ddb402f634e262d9aae7)
1 
2 /*
3  *  M_APM  -  mapmstck.c
4  *
5  *  Copyright (C) 1999 - 2007   Michael C. Ring
6  *
7  *  Permission to use, copy, and distribute this software and its
8  *  documentation for any purpose with or without fee is hereby granted,
9  *  provided that the above copyright notice appear in all copies and
10  *  that both that copyright notice and this permission notice appear
11  *  in supporting documentation.
12  *
13  *  Permission to modify the software is granted. Permission to distribute
14  *  the modified code is granted. Modifications are to be distributed by
15  *  using the file 'license.txt' as a template to modify the file header.
16  *  'license.txt' is available in the official MAPM distribution.
17  *
18  *  This software is provided "as is" without express or implied warranty.
19  */
20 
21 /*
22  *      $Id: mapmstck.c,v 1.11 2007/12/03 01:58:05 mike Exp $
23  *
24  *      This file contains the stack implementation for using
25  *	local M_APM variables.
26  *
27  *      $Log: mapmstck.c,v $
28  *      Revision 1.11  2007/12/03 01:58:05  mike
29  *      Update license
30  *
31  *      Revision 1.10  2003/07/21 20:39:38  mike
32  *      Modify error messages to be in a consistent format.
33  *
34  *      Revision 1.9  2003/03/31 21:49:08  mike
35  *      call generic error handling function
36  *
37  *      Revision 1.8  2002/11/03 22:42:05  mike
38  *      Updated function parameters to use the modern style
39  *
40  *      Revision 1.7  2002/05/17 22:05:00  mike
41  *      the stack is now dynamically allocated and will grow
42  *      at run-time if needed
43  *
44  *      Revision 1.6  2001/07/16 19:47:04  mike
45  *      add function M_free_all_stck
46  *
47  *      Revision 1.5  2000/09/23 19:27:52  mike
48  *      increase stack size for new functionality
49  *
50  *      Revision 1.4  1999/07/09 00:04:47  mike
51  *      tweak stack again
52  *
53  *      Revision 1.3  1999/07/09 00:02:24  mike
54  *      increase stack size for new functions
55  *
56  *      Revision 1.2  1999/06/20 21:13:18  mike
57  *      comment out printf debug and set max stack depth
58  *
59  *      Revision 1.1  1999/06/19 20:32:43  mike
60  *      Initial revision
61  */
62 
63 #include "m_apm_lc.h"
64 
65 static	int	M_stack_ptr  = -1;
66 static	int	M_last_init  = -1;
67 static	int	M_stack_size = 0;
68 
69 static  char    *M_stack_err_msg = "\'M_get_stack_var\', Out of memory";
70 
71 static	M_APM	*M_stack_array;
72 
73 /****************************************************************************/
M_free_all_stck()74 void	M_free_all_stck()
75 {
76 int	k;
77 
78 if (M_last_init >= 0)
79   {
80    for (k=0; k <= M_last_init; k++)
81      m_apm_free(M_stack_array[k]);
82 
83    M_stack_ptr  = -1;
84    M_last_init  = -1;
85    M_stack_size = 0;
86 
87    MAPM_FREE(M_stack_array);
88   }
89 }
90 /****************************************************************************/
M_get_stack_var()91 M_APM	M_get_stack_var()
92 {
93 void    *vp;
94 
95 if (++M_stack_ptr > M_last_init)
96   {
97    if (M_stack_size == 0)
98      {
99       M_stack_size = 18;
100       if ((vp = MAPM_MALLOC(M_stack_size * sizeof(M_APM))) == NULL)
101         {
102          /* fatal, this does not return */
103 
104          M_apm_log_error_msg(M_APM_FATAL, M_stack_err_msg);
105         }
106 
107       M_stack_array = (M_APM *)vp;
108      }
109 
110    if ((M_last_init + 4) >= M_stack_size)
111      {
112       M_stack_size += 12;
113       if ((vp = MAPM_REALLOC(M_stack_array,
114       			    (M_stack_size * sizeof(M_APM)))) == NULL)
115         {
116          /* fatal, this does not return */
117 
118          M_apm_log_error_msg(M_APM_FATAL, M_stack_err_msg);
119         }
120 
121       M_stack_array = (M_APM *)vp;
122      }
123 
124    M_stack_array[M_stack_ptr]     = m_apm_init();
125    M_stack_array[M_stack_ptr + 1] = m_apm_init();
126    M_stack_array[M_stack_ptr + 2] = m_apm_init();
127    M_stack_array[M_stack_ptr + 3] = m_apm_init();
128 
129    M_last_init = M_stack_ptr + 3;
130 
131    /* printf("M_last_init = %d \n",M_last_init); */
132   }
133 
134 return(M_stack_array[M_stack_ptr]);
135 }
136 /****************************************************************************/
M_restore_stack(int count)137 void	M_restore_stack(int count)
138 {
139 M_stack_ptr -= count;
140 }
141 /****************************************************************************/
142 
143