Annotation of OpenXM/src/ox_ntl/oxstack.c, Revision 1.1
1.1 ! iwane 1: /* $OpenXM$ */
! 2:
! 3: #include <stdio.h>
! 4: #include <stdlib.h>
! 5: #include <string.h>
! 6: #include <errno.h>
! 7:
! 8: #include "oxstack.h"
! 9:
! 10:
! 11: #define DPRINTF(x) printf x; fflush(stdout)
! 12:
! 13: #define OXSERV_INIT_STACK_SIZE 2048
! 14: #define OXSERV_EXT_STACK_SIZE 2048
! 15:
! 16: /*===========================================================================*
! 17: * Global Variables.
! 18: *===========================================================================*/
! 19: /* cmo stack */
! 20: static int G_ox_stack_size = 0;
! 21: static int G_ox_stack_pointer = 0;
! 22: static cmo **G_ox_stack = NULL;
! 23:
! 24:
! 25: /*===========================================================================*
! 26: * CMO STACK FUNCTIONs
! 27: *===========================================================================*/
! 28: /*****************************************************************************
! 29: * return the number of cmo in the stack.
! 30: *
! 31: * PARAM : NONE
! 32: * RETURN : the number of cmo in the stack.
! 33: *****************************************************************************/
! 34: int
! 35: oxstack_get_stack_pointer()
! 36: {
! 37: return (G_ox_stack_pointer);
! 38: }
! 39:
! 40:
! 41: /*****************************************************************************
! 42: * initialize stack.
! 43: *
! 44: * PARAM : NONE
! 45: * RETURN : if success return OXSERV_SUCCESS, else OXSERV_FAILURE.
! 46: *****************************************************************************/
! 47: int
! 48: oxstack_init_stack(void)
! 49: {
! 50: free(G_ox_stack);
! 51:
! 52: G_ox_stack_pointer = 0;
! 53: G_ox_stack_size = OXSERV_INIT_STACK_SIZE;
! 54: G_ox_stack = (cmo **)malloc(G_ox_stack_size * sizeof(cmo *));
! 55: if (G_ox_stack == NULL) {
! 56: DPRINTF(("server: %d: %s\n", errno, strerror(errno)));
! 57: return (OXSERV_FAILURE);
! 58: }
! 59:
! 60: return (OXSERV_SUCCESS);
! 61: }
! 62:
! 63: /*****************************************************************************
! 64: *
! 65: * PARAM : NONE
! 66: * RETURN : if success return OXSERV_SUCCESS, else OXSERV_FAILURE.
! 67: *****************************************************************************/
! 68: int
! 69: oxstack_extend_stack(void)
! 70: {
! 71: int size2 = G_ox_stack_size + OXSERV_EXT_STACK_SIZE;
! 72: cmo **stack2 = (cmo **)malloc(size2 * sizeof(cmo *));
! 73: if (stack2 == NULL) {
! 74: DPRINTF(("server: %d: %s\n", errno, strerror(errno)));
! 75: return (OXSERV_FAILURE);
! 76: }
! 77:
! 78: memcpy(stack2, G_ox_stack, G_ox_stack_size * sizeof(cmo *));
! 79: free(G_ox_stack);
! 80:
! 81: G_ox_stack = stack2;
! 82: G_ox_stack_size = size2;
! 83:
! 84: return (OXSERV_SUCCESS);
! 85: }
! 86:
! 87: /*****************************************************************************
! 88: * push a cmo onto the topof the stack.
! 89: *
! 90: * PARAM : m : the cmo to be pushed on the stack.
! 91: * RETURN : if success return OXSERV_SUCCESS, else OXSERV_FAILURE.
! 92: *****************************************************************************/
! 93: int
! 94: oxstack_push(cmo *m)
! 95: {
! 96: int ret;
! 97:
! 98: if (G_ox_stack_pointer >= G_ox_stack_size) {
! 99: ret = oxstack_extend_stack();
! 100: if (ret != OXSERV_SUCCESS)
! 101: return (ret);
! 102: }
! 103:
! 104: G_ox_stack[G_ox_stack_pointer] = m;
! 105: G_ox_stack_pointer++;
! 106:
! 107: return (OXSERV_SUCCESS);
! 108: }
! 109:
! 110: /*****************************************************************************
! 111: * remove thd CMO at the top of this stack and
! 112: * returns that cmo as the value of this function.
! 113: *
! 114: * PARAM : NONE
! 115: * RETURN : CMO at the top of the stack.
! 116: *****************************************************************************/
! 117: cmo *
! 118: oxstack_pop(void)
! 119: {
! 120: cmo *c;
! 121: if (G_ox_stack_pointer > 0) {
! 122: G_ox_stack_pointer--;
! 123: c = G_ox_stack[G_ox_stack_pointer];
! 124: G_ox_stack[G_ox_stack_pointer] = NULL;
! 125: return (c);
! 126: }
! 127: return (NULL);
! 128: }
! 129:
! 130: /*****************************************************************************
! 131: * return the cmo at the specified position in the stack without removing it from the stack.
! 132: *
! 133: * PARAM : i : position in the stack.
! 134: * RETURN: thd cmo at the specified position in the stack.
! 135: *****************************************************************************/
! 136: cmo *
! 137: oxstack_get(int i)
! 138: {
! 139: if (i < G_ox_stack_pointer && i >= 0) {
! 140: return (G_ox_stack[i]);
! 141: }
! 142: return (NULL);
! 143: }
! 144:
! 145:
! 146: /*****************************************************************************
! 147: * return the cmo at the top of the stack without removing it from the stack.
! 148: * PARAM : NONE
! 149: * RETURN: the cmo at the top of the stack.
! 150: *****************************************************************************/
! 151: cmo *
! 152: oxstack_peek(void)
! 153: {
! 154: return (oxstack_get(G_ox_stack_pointer - 1));
! 155: }
! 156:
! 157:
! 158:
! 159: /*****************************************************************************
! 160: * destroy
! 161: *
! 162: * PARAM : NONE
! 163: * RETURN: NONE
! 164: *****************************************************************************/
! 165: void
! 166: oxstack_dest(void)
! 167: {
! 168: free(G_ox_stack);
! 169: }
! 170:
! 171:
! 172:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>