00001
00011 #include <MemoryMgr.h>
00012 #include <ErrorMgr.h>
00013 #include <FeatureMgr.h>
00014
00015 #include <ui.h>
00016 #include <mem_compat.h>
00017
00019 typedef struct tag_dsobj {
00020 MemHandle sh;
00021 Int32 *ss;
00022 Int32 *sp;
00023 Int32 *se;
00024 Int32 sl;
00025 } dsObj;
00026
00027 #define STACK_IMPL
00028 #include <stack.h>
00029
00033 dsObj *
00034 StackNew(void)
00035 {
00036 dsObj *s = (dsObj *)MemPtrNew(sizeof (dsObj));
00037 if (s == NULL)
00038 return (NULL);
00039 s->sl = 4;
00040 s->sh = MemHandleNew(s->sl * sizeof (Int32));
00041 if (s->sh == NULL) {
00042 MemPtrFree(s);
00043 return (NULL);
00044 }
00045
00046 if ((NULL == (s->ss = (Int32 *)MemHandleLock(s->sh)))) {
00047 MemHandleFree(s->sh);
00048 MemPtrFree(s);
00049 return (NULL);
00050 }
00051 s->se = s->ss + (s->sl - 1);
00052 s->sp = s->ss - 1;
00053 return (s);
00054 }
00055
00056 void
00057 StackDelete(dsObj *sp)
00058 {
00059 if (sp->sh) {
00060 MemHandleUnlock(sp->sh);
00061 MemHandleFree(sp->sh);
00062 }
00063 MemPtrFree(sp);
00064 }
00065
00066 static void
00067 StackResize(dsObj *sp, Int32 newsize)
00068 {
00069 Int32 sd = sp->sp - sp->ss;
00070 Int32 sn;
00071 sp->sl = newsize;
00072 sn = (Int32)(sp->sl * sizeof (Int32));
00073 MemHandleUnlock(sp->sh);
00074 if (errNone != MemHandleResize(sp->sh, (UInt32)sn)) {
00075 ErrFatalDisplayIf(1, "Resize of myStack Chunk Failed");
00076 }
00077 sp->ss = (Int32 *)MemHandleLock(sp->sh);
00078 sp->se = sp->ss + (sp->sl - 1);
00079 sp->sp = sp->ss + sd;
00080 }
00081
00082 Int32
00083 StackPop(dsObj *sp)
00084 {
00085 if (sp->sp >= sp->ss) {
00086 Int32 rv = *sp->sp--;
00087 if (((sp->sl >> 2) > (sp->sp - sp->ss)) && (sp->sl > 4))
00088 StackResize(sp, sp->sl >> 1);
00089 return (rv);
00090 }
00091 ErrFatalDisplayIf(1, "myStack Underflow");
00092 return (-1);
00093 }
00094
00095 void
00096 StackPush(dsObj *sp, Int32 elt)
00097 {
00098 if (sp->sp >= sp->se)
00099 StackResize(sp, sp->sl << 1);
00100 *(++sp->sp) = elt;
00101 }
00102
00103 Int8
00104 StackIsEmpty(dsObj *sp)
00105 {
00106 return ((Int8)(sp->sp < sp->ss));
00107 }
00108
00109 void
00110 StackDoEmpty(dsObj *sp)
00111 {
00112 sp->sp = sp->ss - 1;
00113 }
00114
00115 Int32
00116 StackNElements(dsObj *sp)
00117 {
00118 return ((sp->sp+1) - sp->ss);
00119 }
00120
00121 Int32
00122 ListGet(dsObj *sp, Int32 index)
00123 {
00124 if (index >= (sp->sp - sp->ss))
00125 return (-1);
00126 return (sp->ss[index + 1]);
00127 }
00128
00129 void
00130 ListSet(dsObj *sp, Int32 index, Int32 element)
00131 {
00132 if (index < (sp->sp - sp->ss))
00133 sp->ss[index + 1] = element;
00134 }