📄 ibmstack.c
字号:
/* IbmStack.c
This file is part of the VGB-DOS project
Copyright (C) Marcel de Kogel (m.dekogel@student.utwente.nl), 1996
You may not use this file for commercial purposes
Please notify me if you make any changes to this file */
/* #define TEST */
#include "GB.h"
#include "IbmMsdos.h"
#include <stdlib.h>
#include <dos.h>
#include <go32.h>
#include <dpmi.h>
#include <stdio.h>
#define MIN_STACK_SIZE 1024
#define MAX_STACK_SIZE 65536
#define MIN_STACK_NR 4
#define MAX_STACK_NR 64
static unsigned **StackPtr;
static int StackSize;
static int NumStacks;
static int NumStacksUsed;
static int NumFailed;
static int TotalFailed=0;
static int TotalSuccess=0;
static int MaxStacksUsed=0;
void ExitStacks (void)
{
int i;
if (!StackPtr)
return;
for (i=0;i<NumStacks;++i)
if (StackPtr[i])
free (StackPtr[i]);
else
break;
free (StackPtr);
StackPtr=0;
#ifdef TEST
printf ("\n%d,%d,%d\n",TotalFailed,TotalSuccess,MaxStacksUsed);
#endif
}
int InitStacks (int nr,int size)
{
int i;
if (StackPtr)
return 1; /* allready called */
if (size<MIN_STACK_SIZE)
size=MIN_STACK_SIZE;
if (size>MAX_STACK_SIZE)
size=MAX_STACK_SIZE;
if (nr<MIN_STACK_NR)
nr=MIN_STACK_NR;
if (nr>MAX_STACK_NR)
nr=MAX_STACK_NR;
if (size&3)
size=(size&(~3))+4; /* stack must be dword-aligned */
StackPtr=malloc (nr*4);
if (!StackPtr)
return 0;
StackSize=size;
NumStacks=nr;
for (i=0;i<nr;++i)
{
StackPtr[i]=malloc(size);
if (!StackPtr[i])
{
ExitStacks ();
return 0;
}
}
NumStacksUsed=0;
NumFailed=0;
return 1;
}
/* AllocStack return cf=1 when no stack could be allocated.
FreeStack() must be wether a stack could be allocated or not */
asm ("
.globl _AllocStack
_AllocStack:
popl %esi
movl _NumStacksUsed,%eax
cmpl _NumStacks,%eax
jae AllocStack_Failed
incl %eax
movl %eax,_NumStacksUsed
cmpl _MaxStacksUsed,%eax
jbe Alloc_1
movl %eax,_MaxStacksUsed
Alloc_1:
incl _TotalSuccess
movl _StackPtr,%ebx
decl %eax
leal (%ebx,%eax,4),%ebx
movl (%ebx),%ebx
addl _StackSize,%ebx
subl $64,%ebx
movw %ss,%cx
movl %esp,%edx
movw %ds,%ax
movw %ax,%ss
movl %ebx,%esp
pushl %edx
pushw %cx
clc
jmpl %esi
AllocStack_Failed:
incl _NumFailed
incl _TotalFailed
stc
jmpl %esi
.globl _FreeStack
_FreeStack:
popl %esi
cmpl $0,_NumFailed
jne FreeStack_Failed
popw %ax
popl %edx
movw %ax,%ss
movl %edx,%esp
decl _NumStacksUsed
jmpl %esi
FreeStack_Failed:
decl _NumFailed
jmpl %esi
");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -