xref: /petsc/src/sys/logging/utils/stack.c (revision f97672e55eacc8688507b9471cd7ec2664d7f203)
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   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 {
47   PetscFunctionBegin;
48   PetscValidBoolPointer(empty,2);
49   if (stack->top == -1) *empty = PETSC_TRUE;
50   else *empty = PETSC_FALSE;
51   PetscFunctionReturn(0);
52 }
53 
54 /*@C
55   PetscIntStackTop - This function returns the top of the stack.
56 
57   Not Collective
58 
59   Input Parameter:
60 . stack - The stack
61 
62   Output Parameter:
63 . top - The integer on top of the stack
64 
65   Level: developer
66 
67 .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackPop()`
68 @*/
69 PetscErrorCode PetscIntStackTop(PetscIntStack stack, int *top)
70 {
71   PetscFunctionBegin;
72   PetscValidIntPointer(top,2);
73   *top = stack->stack[stack->top];
74   PetscFunctionReturn(0);
75 }
76 
77 /*@C
78   PetscIntStackPush - This function pushes an integer on the stack.
79 
80   Not Collective
81 
82   Input Parameters:
83 + stack - The stack
84 - item  - The integer to push
85 
86   Level: developer
87 
88 .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPop()`, `PetscIntStackTop()`
89 @*/
90 PetscErrorCode PetscIntStackPush(PetscIntStack stack, int item)
91 {
92   int            *array;
93 
94   PetscFunctionBegin;
95   stack->top++;
96   if (stack->top >= stack->max) {
97     PetscCall(PetscMalloc1(stack->max*2, &array));
98     PetscCall(PetscArraycpy(array, stack->stack, stack->max));
99     PetscCall(PetscFree(stack->stack));
100 
101     stack->stack = array;
102     stack->max  *= 2;
103   }
104   stack->stack[stack->top] = item;
105   PetscFunctionReturn(0);
106 }
107 
108 /*@C
109   PetscIntStackPop - This function pops an integer from the stack.
110 
111   Not Collective
112 
113   Input Parameter:
114 . stack - The stack
115 
116   Output Parameter:
117 . item  - The integer popped
118 
119   Level: developer
120 
121 .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackTop()`
122 @*/
123 PetscErrorCode PetscIntStackPop(PetscIntStack stack, int *item)
124 {
125   PetscFunctionBegin;
126   PetscValidPointer(item,2);
127   PetscCheck(stack->top != -1,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Stack is empty");
128   *item = stack->stack[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   PetscIntStack  s;
147 
148   PetscFunctionBegin;
149   PetscValidPointer(stack,1);
150   PetscCall(PetscNew(&s));
151 
152   s->top = -1;
153   s->max = 128;
154 
155   PetscCall(PetscCalloc1(s->max, &s->stack));
156   *stack = s;
157   PetscFunctionReturn(0);
158 }
159