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