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