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