xref: /petsc/src/sys/logging/utils/stack.c (revision 862e4a309d45a165aaa4da0d704ba733429d833a)
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 /*@C
11   PetscIntStackDestroy - This function destroys a stack.
12 
13   Not Collective
14 
15   Input Parameter:
16 . stack - The stack
17 
18   Level: developer
19 
20 .seealso: `PetscIntStackCreate()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackPop()`, `PetscIntStackTop()`
21 @*/
22 PetscErrorCode PetscIntStackDestroy(PetscIntStack stack)
23 {
24   PetscFunctionBegin;
25   PetscValidPointer(stack, 1);
26   PetscCall(PetscFree(stack->stack));
27   PetscCall(PetscFree(stack));
28   PetscFunctionReturn(0);
29 }
30 
31 /*@C
32   PetscIntStackEmpty - This function determines whether any items have been pushed.
33 
34   Not Collective
35 
36   Input Parameter:
37 . stack - The stack
38 
39   Output Parameter:
40 . empty - `PETSC_TRUE` if the stack is empty
41 
42   Level: developer
43 
44 .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackPush()`, `PetscIntStackPop()`, `PetscIntStackTop()`
45 @*/
46 PetscErrorCode PetscIntStackEmpty(PetscIntStack stack, PetscBool *empty)
47 {
48   PetscFunctionBegin;
49   PetscValidPointer(stack, 1);
50   PetscValidBoolPointer(empty, 2);
51   *empty = stack->top == -1 ? PETSC_TRUE : PETSC_FALSE;
52   PetscFunctionReturn(0);
53 }
54 
55 /*@C
56   PetscIntStackTop - This function returns the top of the stack.
57 
58   Not Collective
59 
60   Input Parameter:
61 . stack - The stack
62 
63   Output Parameter:
64 . top - The integer on top of the stack
65 
66   Level: developer
67 
68 .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackPop()`
69 @*/
70 PetscErrorCode PetscIntStackTop(PetscIntStack stack, int *top)
71 {
72   PetscFunctionBegin;
73   PetscValidPointer(stack, 1);
74   PetscValidIntPointer(top, 2);
75   *top = stack->stack[stack->top];
76   PetscFunctionReturn(0);
77 }
78 
79 /*@C
80   PetscIntStackPush - This function pushes an integer on the stack.
81 
82   Not Collective
83 
84   Input Parameters:
85 + stack - The stack
86 - item  - The integer to push
87 
88   Level: developer
89 
90 .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPop()`, `PetscIntStackTop()`
91 @*/
92 PetscErrorCode PetscIntStackPush(PetscIntStack stack, int item)
93 {
94   PetscFunctionBegin;
95   PetscValidPointer(stack, 1);
96   if (++stack->top >= stack->max) {
97     stack->max *= 2;
98     PetscCall(PetscRealloc(stack->max * sizeof(*stack->stack), &stack->stack));
99   }
100   stack->stack[stack->top] = item;
101   PetscFunctionReturn(0);
102 }
103 
104 /*@C
105   PetscIntStackPop - This function pops an integer from the stack.
106 
107   Not Collective
108 
109   Input Parameter:
110 . stack - The stack
111 
112   Output Parameter:
113 . item  - The integer popped
114 
115   Level: developer
116 
117 .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackTop()`
118 @*/
119 PetscErrorCode PetscIntStackPop(PetscIntStack stack, int *item)
120 {
121   PetscFunctionBegin;
122   PetscValidPointer(stack, 1);
123   PetscCheck(stack->top != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Stack is empty");
124   if (item) {
125     PetscValidIntPointer(item, 2);
126     PetscCall(PetscIntStackTop(stack, item));
127   }
128   --stack->top;
129   PetscFunctionReturn(0);
130 }
131 
132 /*@C
133   PetscIntStackCreate - This function creates a stack.
134 
135   Not Collective
136 
137   Output Parameter:
138 . stack - The stack
139 
140   Level: developer
141 
142 .seealso: `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackPop()`, `PetscIntStackTop()`
143 @*/
144 PetscErrorCode PetscIntStackCreate(PetscIntStack *stack)
145 {
146   PetscFunctionBegin;
147   PetscValidPointer(stack, 1);
148   PetscCall(PetscNew(stack));
149 
150   (*stack)->top = -1;
151   (*stack)->max = 128;
152 
153   PetscCall(PetscCalloc1((*stack)->max, &(*stack)->stack));
154   PetscFunctionReturn(0);
155 }
156