xref: /petsc/src/sys/logging/utils/stack.c (revision f23aa3dd738493dcb3a70a8c0c7f5454aa9150c2)
1 
2 /*
3      This defines part of the private API for logging performance information. It is intended to be used only by the
4    PETSc PetscLog...() interface and not elsewhere, nor by users. Hence the prototypes for these functions are NOT
5    in the public PETSc include files.
6 
7 */
8 #include <petsc-private/logimpl.h> /*I    "petscsys.h"   I*/
9 
10 #undef __FUNCT__
11 #define __FUNCT__ "PetscIntStackDestroy"
12 /*@C
13   PetscIntStackDestroy - This function destroys a stack.
14 
15   Not Collective
16 
17   Input Parameter:
18 . stack - The stack
19 
20   Level: developer
21 
22 .keywords: log, stack, destroy
23 .seealso: PetscIntStackCreate(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
24 @*/
25 PetscErrorCode PetscIntStackDestroy(PetscIntStack stack)
26 {
27   PetscErrorCode ierr;
28 
29   PetscFunctionBegin;
30   ierr = PetscFree(stack->stack);CHKERRQ(ierr);
31   ierr = PetscFree(stack);CHKERRQ(ierr);
32   PetscFunctionReturn(0);
33 }
34 
35 #undef __FUNCT__
36 #define __FUNCT__ "PetscIntStackEmpty"
37 /*@C
38   PetscIntStackEmpty - This function determines whether any items have been pushed.
39 
40   Not Collective
41 
42   Input Parameter:
43 . stack - The stack
44 
45   Output Parameter:
46 . empty - PETSC_TRUE if the stack is empty
47 
48   Level: developer
49 
50 .keywords: log, stack, empty
51 .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
52 @*/
53 PetscErrorCode PetscIntStackEmpty(PetscIntStack stack, PetscBool  *empty)
54 {
55   PetscFunctionBegin;
56   PetscValidIntPointer(empty,2);
57   if (stack->top == -1) {
58     *empty = PETSC_TRUE;
59   } else {
60     *empty = PETSC_FALSE;
61   }
62   PetscFunctionReturn(0);
63 }
64 
65 #undef __FUNCT__
66 #define __FUNCT__ "PetscIntStackTop"
67 /*@C
68   PetscIntStackTop - This function returns the top of the stack.
69 
70   Not Collective
71 
72   Input Parameter:
73 . stack - The stack
74 
75   Output Parameter:
76 . top - The integer on top of the stack
77 
78   Level: developer
79 
80 .keywords: log, stack, top
81 .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop()
82 @*/
83 PetscErrorCode PetscIntStackTop(PetscIntStack stack, int *top)
84 {
85   PetscFunctionBegin;
86   PetscValidIntPointer(top,2);
87   *top = stack->stack[stack->top];
88   PetscFunctionReturn(0);
89 }
90 
91 #undef __FUNCT__
92 #define __FUNCT__ "PetscIntStackPush"
93 /*@C
94   PetscIntStackPush - This function pushes an integer on the stack.
95 
96   Not Collective
97 
98   Input Parameters:
99 + stack - The stack
100 - item  - The integer to push
101 
102   Level: developer
103 
104 .keywords: log, stack, push
105 .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPop(), PetscIntStackTop()
106 @*/
107 PetscErrorCode PetscIntStackPush(PetscIntStack stack, int item)
108 {
109   int            *array;
110   PetscErrorCode ierr;
111 
112   PetscFunctionBegin;
113   stack->top++;
114   if (stack->top >= stack->max) {
115     ierr = PetscMalloc(stack->max*2 * sizeof(int), &array);CHKERRQ(ierr);
116     ierr = PetscMemcpy(array, stack->stack, stack->max * sizeof(int));CHKERRQ(ierr);
117     ierr = PetscFree(stack->stack);CHKERRQ(ierr);
118     stack->stack = array;
119     stack->max  *= 2;
120   }
121   stack->stack[stack->top] = item;
122   PetscFunctionReturn(0);
123 }
124 
125 #undef __FUNCT__
126 #define __FUNCT__ "PetscIntStackPop"
127 /*@C
128   PetscIntStackPop - This function pops an integer from the stack.
129 
130   Not Collective
131 
132   Input Parameter:
133 . stack - The stack
134 
135   Output Parameter:
136 . item  - The integer popped
137 
138   Level: developer
139 
140 .keywords: log, stack, pop
141 .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackTop()
142 @*/
143 PetscErrorCode PetscIntStackPop(PetscIntStack stack, int *item)
144 {
145   PetscFunctionBegin;
146   PetscValidPointer(item,2);
147   if (stack->top == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Stack is empty");
148   *item = stack->stack[stack->top--];
149   PetscFunctionReturn(0);
150 }
151 
152 #undef __FUNCT__
153 #define __FUNCT__ "PetscIntStackCreate"
154 /*@C
155   PetscIntStackCreate - This function creates a stack.
156 
157   Not Collective
158 
159   Output Parameter:
160 . stack - The stack
161 
162   Level: developer
163 
164 .keywords: log, stack, pop
165 .seealso: PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
166 @*/
167 PetscErrorCode PetscIntStackCreate(PetscIntStack *stack)
168 {
169   PetscIntStack       s;
170   PetscErrorCode ierr;
171 
172   PetscFunctionBegin;
173   PetscValidPointer(stack,1);
174   ierr = PetscNew(struct _n_PetscIntStack, &s);CHKERRQ(ierr);
175   s->top = -1;
176   s->max = 128;
177   ierr = PetscMalloc(s->max * sizeof(int), &s->stack);CHKERRQ(ierr);
178   ierr = PetscMemzero(s->stack, s->max * sizeof(int));CHKERRQ(ierr);
179   *stack = s;
180   PetscFunctionReturn(0);
181 }
182