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