xref: /petsc/src/sys/logging/utils/stack.c (revision e6e75211d226c622f451867f53ce5d558649ff4f)
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) *empty = PETSC_TRUE;
58   else *empty = PETSC_FALSE;
59   PetscFunctionReturn(0);
60 }
61 
62 #undef __FUNCT__
63 #define __FUNCT__ "PetscIntStackTop"
64 /*@C
65   PetscIntStackTop - This function returns the top of the stack.
66 
67   Not Collective
68 
69   Input Parameter:
70 . stack - The stack
71 
72   Output Parameter:
73 . top - The integer on top of the stack
74 
75   Level: developer
76 
77 .keywords: log, stack, top
78 .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop()
79 @*/
80 PetscErrorCode PetscIntStackTop(PetscIntStack stack, int *top)
81 {
82   PetscFunctionBegin;
83   PetscValidIntPointer(top,2);
84   *top = stack->stack[stack->top];
85   PetscFunctionReturn(0);
86 }
87 
88 #undef __FUNCT__
89 #define __FUNCT__ "PetscIntStackPush"
90 /*@C
91   PetscIntStackPush - This function pushes an integer on the stack.
92 
93   Not Collective
94 
95   Input Parameters:
96 + stack - The stack
97 - item  - The integer to push
98 
99   Level: developer
100 
101 .keywords: log, stack, push
102 .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPop(), PetscIntStackTop()
103 @*/
104 PetscErrorCode PetscIntStackPush(PetscIntStack stack, int item)
105 {
106   int            *array;
107   PetscErrorCode ierr;
108 
109   PetscFunctionBegin;
110   stack->top++;
111   if (stack->top >= stack->max) {
112     ierr = PetscMalloc1(stack->max*2, &array);CHKERRQ(ierr);
113     ierr = PetscMemcpy(array, stack->stack, stack->max * sizeof(int));CHKERRQ(ierr);
114     ierr = PetscFree(stack->stack);CHKERRQ(ierr);
115 
116     stack->stack = array;
117     stack->max  *= 2;
118   }
119   stack->stack[stack->top] = item;
120   PetscFunctionReturn(0);
121 }
122 
123 #undef __FUNCT__
124 #define __FUNCT__ "PetscIntStackPop"
125 /*@C
126   PetscIntStackPop - This function pops an integer from the stack.
127 
128   Not Collective
129 
130   Input Parameter:
131 . stack - The stack
132 
133   Output Parameter:
134 . item  - The integer popped
135 
136   Level: developer
137 
138 .keywords: log, stack, pop
139 .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackTop()
140 @*/
141 PetscErrorCode PetscIntStackPop(PetscIntStack stack, int *item)
142 {
143   PetscFunctionBegin;
144   PetscValidPointer(item,2);
145   if (stack->top == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Stack is empty");
146   *item = stack->stack[stack->top--];
147   PetscFunctionReturn(0);
148 }
149 
150 #undef __FUNCT__
151 #define __FUNCT__ "PetscIntStackCreate"
152 /*@C
153   PetscIntStackCreate - This function creates a stack.
154 
155   Not Collective
156 
157   Output Parameter:
158 . stack - The stack
159 
160   Level: developer
161 
162 .keywords: log, stack, pop
163 .seealso: PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
164 @*/
165 PetscErrorCode PetscIntStackCreate(PetscIntStack *stack)
166 {
167   PetscIntStack  s;
168   PetscErrorCode ierr;
169 
170   PetscFunctionBegin;
171   PetscValidPointer(stack,1);
172   ierr = PetscNew(&s);CHKERRQ(ierr);
173 
174   s->top = -1;
175   s->max = 128;
176 
177   ierr = PetscCalloc1(s->max, &s->stack);CHKERRQ(ierr);
178   *stack = s;
179   PetscFunctionReturn(0);
180 }
181