Actual source code: stack.c
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.
7: */
8: #include <petsc/private/logimpl.h>
10: /*@C
11: PetscIntStackDestroy - This function destroys a stack.
13: Not Collective
15: Input Parameter:
16: . stack - The stack
18: Level: developer
20: .seealso: `PetscIntStackCreate()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackPop()`, `PetscIntStackTop()`
21: @*/
22: PetscErrorCode PetscIntStackDestroy(PetscIntStack stack)
23: {
25: PetscFree(stack->stack);
26: PetscFree(stack);
27: return 0;
28: }
30: /*@C
31: PetscIntStackEmpty - This function determines whether any items have been pushed.
33: Not Collective
35: Input Parameter:
36: . stack - The stack
38: Output Parameter:
39: . empty - `PETSC_TRUE` if the stack is empty
41: Level: developer
43: .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackPush()`, `PetscIntStackPop()`, `PetscIntStackTop()`
44: @*/
45: PetscErrorCode PetscIntStackEmpty(PetscIntStack stack, PetscBool *empty)
46: {
49: *empty = stack->top == -1 ? PETSC_TRUE : PETSC_FALSE;
50: return 0;
51: }
53: /*@C
54: PetscIntStackTop - This function returns the top of the stack.
56: Not Collective
58: Input Parameter:
59: . stack - The stack
61: Output Parameter:
62: . top - The integer on top of the stack
64: Level: developer
66: .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackPop()`
67: @*/
68: PetscErrorCode PetscIntStackTop(PetscIntStack stack, int *top)
69: {
72: *top = stack->stack[stack->top];
73: return 0;
74: }
76: /*@C
77: PetscIntStackPush - This function pushes an integer on the stack.
79: Not Collective
81: Input Parameters:
82: + stack - The stack
83: - item - The integer to push
85: Level: developer
87: .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPop()`, `PetscIntStackTop()`
88: @*/
89: PetscErrorCode PetscIntStackPush(PetscIntStack stack, int item)
90: {
92: if (++stack->top >= stack->max) {
93: stack->max *= 2;
94: PetscRealloc(stack->max * sizeof(*stack->stack), &stack->stack);
95: }
96: stack->stack[stack->top] = item;
97: return 0;
98: }
100: /*@C
101: PetscIntStackPop - This function pops an integer from the stack.
103: Not Collective
105: Input Parameter:
106: . stack - The stack
108: Output Parameter:
109: . item - The integer popped
111: Level: developer
113: .seealso: `PetscIntStackCreate()`, `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackTop()`
114: @*/
115: PetscErrorCode PetscIntStackPop(PetscIntStack stack, int *item)
116: {
119: if (item) {
121: PetscIntStackTop(stack, item);
122: }
123: --stack->top;
124: return 0;
125: }
127: /*@C
128: PetscIntStackCreate - This function creates a stack.
130: Not Collective
132: Output Parameter:
133: . stack - The stack
135: Level: developer
137: .seealso: `PetscIntStackDestroy()`, `PetscIntStackEmpty()`, `PetscIntStackPush()`, `PetscIntStackPop()`, `PetscIntStackTop()`
138: @*/
139: PetscErrorCode PetscIntStackCreate(PetscIntStack *stack)
140: {
142: PetscNew(stack);
144: (*stack)->top = -1;
145: (*stack)->max = 128;
147: PetscCalloc1((*stack)->max, &(*stack)->stack);
148: return 0;
149: }