Actual source code: stack.c
petsc-3.10.5 2019-03-28
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: .keywords: log, stack, destroy
21: .seealso: PetscIntStackCreate(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
22: @*/
23: PetscErrorCode PetscIntStackDestroy(PetscIntStack stack)
24: {
28: PetscFree(stack->stack);
29: PetscFree(stack);
30: return(0);
31: }
33: /*@C
34: PetscIntStackEmpty - This function determines whether any items have been pushed.
36: Not Collective
38: Input Parameter:
39: . stack - The stack
41: Output Parameter:
42: . empty - PETSC_TRUE if the stack is empty
44: Level: developer
46: .keywords: log, stack, empty
47: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
48: @*/
49: PetscErrorCode PetscIntStackEmpty(PetscIntStack stack, PetscBool *empty)
50: {
53: if (stack->top == -1) *empty = PETSC_TRUE;
54: else *empty = PETSC_FALSE;
55: return(0);
56: }
58: /*@C
59: PetscIntStackTop - This function returns the top of the stack.
61: Not Collective
63: Input Parameter:
64: . stack - The stack
66: Output Parameter:
67: . top - The integer on top of the stack
69: Level: developer
71: .keywords: log, stack, top
72: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop()
73: @*/
74: PetscErrorCode PetscIntStackTop(PetscIntStack stack, int *top)
75: {
78: *top = stack->stack[stack->top];
79: return(0);
80: }
82: /*@C
83: PetscIntStackPush - This function pushes an integer on the stack.
85: Not Collective
87: Input Parameters:
88: + stack - The stack
89: - item - The integer to push
91: Level: developer
93: .keywords: log, stack, push
94: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPop(), PetscIntStackTop()
95: @*/
96: PetscErrorCode PetscIntStackPush(PetscIntStack stack, int item)
97: {
98: int *array;
102: stack->top++;
103: if (stack->top >= stack->max) {
104: PetscMalloc1(stack->max*2, &array);
105: PetscMemcpy(array, stack->stack, stack->max * sizeof(int));
106: PetscFree(stack->stack);
108: stack->stack = array;
109: stack->max *= 2;
110: }
111: stack->stack[stack->top] = item;
112: return(0);
113: }
115: /*@C
116: PetscIntStackPop - This function pops an integer from the stack.
118: Not Collective
120: Input Parameter:
121: . stack - The stack
123: Output Parameter:
124: . item - The integer popped
126: Level: developer
128: .keywords: log, stack, pop
129: .seealso: PetscIntStackCreate(), PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackTop()
130: @*/
131: PetscErrorCode PetscIntStackPop(PetscIntStack stack, int *item)
132: {
135: if (stack->top == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE, "Stack is empty");
136: *item = stack->stack[stack->top--];
137: return(0);
138: }
140: /*@C
141: PetscIntStackCreate - This function creates a stack.
143: Not Collective
145: Output Parameter:
146: . stack - The stack
148: Level: developer
150: .keywords: log, stack, pop
151: .seealso: PetscIntStackDestroy(), PetscIntStackEmpty(), PetscIntStackPush(), PetscIntStackPop(), PetscIntStackTop()
152: @*/
153: PetscErrorCode PetscIntStackCreate(PetscIntStack *stack)
154: {
155: PetscIntStack s;
160: PetscNew(&s);
162: s->top = -1;
163: s->max = 128;
165: PetscCalloc1(s->max, &s->stack);
166: *stack = s;
167: return(0);
168: }