📄 osport.c
字号:
int trapsize = 0; /* trap on alloc of block this size */
struct memman {
char status[2]; /* Marker flag, free flag */
int length; /* size of alloced block */
char * data; /* pointer to alloced data */
};
/* memory call counters */
struct BrMemStats {
long brallocs; /* alloc & free call coutners */
long brfrees;
long brallocb; /* byte counters */
long brfreeb;
long brmaxmem; /* max. memory alloced at one time */
long brmaxsize; /* largest single alloc */
} brmem;
#define MAXHIT 30
struct memhit { /* track how much of each size */
int size; /* zero until entry is used */
int blocks; /* total alloc calls for this size */
int curr; /* current bytes alloced for this size */
}hitct[MAXHIT];
/* FUNCTION: blocklist()
*
* PARAM1: void * pio
*
* RETURNS:
*/
int
blocklist(void * pio)
{
int i; /* hitct index */
ns_printf(pio, "win32 heap stats:\n");
ns_printf(pio, "allocs: %d, frees: %d, allocbytes: %d freebytes: %d\n",
brmem.brallocs, brmem.brfrees, brmem.brallocb, brmem.brfreeb );
ns_printf(pio, "current: %d maxmem: %d, maxblock: %d\n",
brmem.brallocb - brmem.brfreeb, brmem.brmaxmem, brmem.brmaxsize);
ns_printf(pio, "Block sizes, in size[blocks] format:");
for (i = 0; i < MAXHIT; i++)
{
if (hitct[i].size == 0) break;
if (i%5 == 0)
ns_printf(pio, "\n");
ns_printf(pio, "%4d[%4d] ", hitct[i].size, hitct[i].blocks);
}
ns_printf(pio, "\n");
return 0;
}
/* npalloc() - alloc & return zeroed out buffer */
/* FUNCTION: npalloc()
*
* PARAM1: unsigned size
*
* RETURNS:
*/
char *
npalloc(unsigned size)
{
struct memman * manp;
char * cp;
int csize;
int i;
int free;
/* should we trap on alloc of block this size? */
if (size == (unsigned)trapsize)
{
dtrap();
}
csize = (size + sizeof(struct memman) + ALIGN_TYPE);
if (csize == (int)trapsize)
{
dtrap();
}
cp = (char*)calloc(1, csize );
if (!cp)
{
dtrap();
return NULL;
}
manp = (struct memman *)cp;
cp += sizeof(struct memman);
manp->status[0] = 'M'; /* add memory marker */
manp->status[1] = 'M'; /* mark as NOT free */
manp->data = cp;
manp->length = size;
*(cp + size) = 'M'; /* end memory marker */
free = -1; /* use -1 to indicate no free hitct[] entry */
for (i = 0; i < MAXHIT; i++)
{
if (hitct[i].size == (int)size) /* found entry for this size */
{
hitct[i].curr += size;
hitct[i].blocks++;
break;
}
if (hitct[i].size == 0) /* size not in array */
{
free = i; /* remember first free slot */
break; /* make new entry */
}
}
if (free != -1) /* see if we should start an entry for this size */
{
hitct[i].size = hitct[i].curr = size;
hitct[i].blocks = 1;
}
brmem.brallocs++;
brmem.brallocb += size;
if (brmem.brmaxsize < (int)size) /* new record for a single alloc? */
brmem.brmaxsize = size;
if (brmem.brmaxmem < (brmem.brallocb - brmem.brfreeb)) /* new record for allocation? */
brmem.brmaxmem = brmem.brallocb - brmem.brfreeb;
return (cp);
}
/* FUNCTION: npfree()
*
* PARAM1: void * ptr
*
* RETURNS:
*/
void
npfree(void * ptr)
{
struct memman * manp;
char * cp;
int i;
/* back up from pointer to get memory manager struct */
manp = (struct memman *)ptr;
manp--;
cp = manp->data;
if (manp->status[0] != 'M') /* Make sure marker is present */
{
dtrap(); /* bad pointer or corrupt memory */
return; /* don't confuse system free() */
}
if (manp->status[1] == 'F') /* Double free ? */
{
dtrap(); /* bad pointer or corrupt memory */
return; /* don't confuse system free() */
}
manp->status[1] = 'F'; /* mark as free */
if (*(cp + manp->length) != 'M')
{
dtrap(); /* should be panic */
return;
}
brmem.brfrees++;
brmem.brfreeb += manp->length;
for (i = 0; i < MAXHIT; i++)
{
if (hitct[i].size == manp->length)
{
hitct[i].curr -= manp->length;
break;
}
}
free((char*)manp); /* memory came through bralloc(), free it */
}
u_long totalmemcpy = 0;
u_long totalmemset = 0;
int memtrapsize = 0;
/* FUNCTION: MEMCPY()
*
* PARAM1: void * dest
* PARAM2: void * src
* PARAM3: unsigned length
*
* RETURNS:
*/
void *
MEMCPY(void * dest, void * src, unsigned length)
{
totalmemcpy += length;
return memcpy(dest, src, length);
}
/* FUNCTION: MEMSET()
*
* PARAM1: void * dest
* PARAM2: unsigned char setc
* PARAM3: unsigned length
*
* RETURNS:
*/
void *
MEMSET(void * dest, unsigned char setc, unsigned length)
{
totalmemset += length;
return memset(dest, setc, length);
}
/* FUNCTION: MEMMOVE()
*
* PARAM1: void * dest
* PARAM2: void * src
* PARAM3: unsigned length
*
* RETURNS:
*/
void *
MEMMOVE(void * dest, void * src, unsigned length)
{
totalmemcpy += length;
return memmove(dest, src, length);
}
/* FUNCTION: lswap()
*
* PARAM1: u_long lval
*
* RETURNS:
*/
u_long
lswap(u_long lval)
{
_asm { mov eax, lval } /* load value to swap */
_asm { mov edx, eax } /* copy for high halfword */
_asm { xchg al, ah } /* swap low word */
_asm { and eax, 0000FFFFh } /* clear high bits of low word */
_asm { shl eax, 16 } /* move low halfword to high half of eax */
_asm { shr edx, 16 } /* move high word to low half of edx */
_asm { xchg dl, dh } /* swap high word */
_asm { or eax, edx } /* put both halfwords back together */
_asm { mov lval, eax } /* load swapped value for return */
return lval;
}
#include "time.h"
static clock_t lasttime = 0;
#ifdef INICHE_TASKS
task * tk_wintime;
extern int kbhit(void); /* from Microsnot library */
extern task * to_keyboard; /* in netmain.c */
/* FUNCTION: wintime_proc()
*
* PARAM1: int unused
*
* RETURNS:
*/
static tick_scale = 0;
int
wintime_proc(int unused)
{
clock_t thistime;
for (;;)
{
tk_next();
thistime = clock();
if (thistime != lasttime)
{
lasttime = thistime;
if(++tick_scale > os_scale)
{
cticks++;
tick_scale = 0;
}
}
/* give the poor keyboard task a boost: */
if (kbhit()) /* is there a key waiting? */
{
tk_wake(to_keyboard); /* ...then wake it up now */
}
}
USE_ARG(unused);
}
/* FUNCTION: clock_init()
*
* PARAM1:
*
* RETURNS:
*/
void
clock_init()
{
if (!tk_wintime)
tk_wintime = tk_new(tk_cur, wintime_proc, 8192, "Win32_time", 0);
if (!tk_wintime)
panic("win32 clock");
}
/* FUNCTION: clock_c()
*
* PARAM1:
*
* RETURNS:
*/
void
clock_c()
{
tk_kill(tk_wintime); /* end clocking task */
}
/* FUNCTION: TK_NETRX_BLOCK()
*
* PARAM1:
*
* RETURNS:
*/
void
TK_NETRX_BLOCK()
{
tk_block();
}
/* w32_tk_return() - called when a task tries to return, usually
* during a menu initiated shutdown.
*/
/* FUNCTION: w32_tk_return()
*
* PARAM1: int err
*
* RETURNS:
*/
void
w32_tk_return(int err)
{
if (err)
dprintf("task error-exit: %s (%d)\n", tk_cur->tk_name, err);
tk_kill(tk_cur); /* mark this task to die */
for (;;)
tk_yield(); /* wait until it does.. */
}
#else /* SUPERLOOP */
#ifdef IN_MENUS
void kbdio(void);
#endif
/* FUNCTION: clock_init()
*
* PARAM1:
*
* RETURNS:
*/
void
clock_init()
{
clock_t thistime;
thistime = clock();
if (thistime != lasttime)
{
lasttime = thistime;
cticks++;
}
#ifdef NOTDEF
/* give the poor keyboard task a boost: */
if (kbhit()) /* is there a key waiting? */
{
#ifdef IN_MENUS
kbdio(); /* process user keys */
#endif
}
#endif
}
/* FUNCTION: clock_c()
*
* PARAM1:
*
* RETURNS:
*/
void
clock_c()
{
}
#endif /* INICHE_TASKS */
#ifdef USE_PROFILER
/* get the windows fast tick. First, define away PPP's GetTickCount()
* Macro which has the same name as MSCs:
*/
#ifdef GetTickCount
#undef GetTickCount
#endif
/* Next, set the defines to get rid of unwanted Microsoft includes.
* It seems MS didn't bother to make their includes compile at warniing
* level 4, which we use by default:
*/
#define _INC_CTYPE
#define __RPCASYNC_H__
/* Now bring in what's left of the windows includes */
#include "windows.h"
/* finally, the tick routine:" */
/* FUNCTION: get_ptick()
*
* PARAM1: void
*
* RETURNS:
*/
u_long
get_ptick(void)
{
return (u_long)(GetTickCount());
// return (u_long)(clock());
}
#endif /* USE_PROFILER */
#ifdef SUPERLOOP
/* tk_yield() - this is called whenever the Station is looping
* waiting for user (or network) input. It handles the various
* backround work needed such as polling alarm conditions and
* demuxing incoming packets. Has the same name as the MIT tasking
* system's tk_yield(). If we move to the MIT package as part of an
* embedded system port, this will be replaced with that.
*/
extern void inet_timer(void);
extern void packet_check(void);
extern void http_check(void);
#ifdef SMTP_ALERTS
extern void smtpalert_task(void);
#endif /* SMTP_ALERTS */
#ifdef FTP_CLIENT
extern void fc_check(void);
#endif
#ifdef FTP_SERVER
extern void ftps_check(void);
#endif
#ifdef TCP_ECHOTEST
extern int tcp_echo_poll(void);
#endif
#ifdef TELNET_SVR
extern void tel_check(void);
#endif
#ifdef SNMP_SOCKETS
extern void snmp_check();
#endif
/* FUNCTION: tk_yield()
*
* PARAM1:
*
* RETURNS:
*/
void
tk_yield()
{
#ifndef INICHE_TASKS
/* packet_chk() receives packets from network and puts them in
* rcvdq. packet_check() checks the rcvdq and hands the packets
* in it to the stack */
packet_chk() ; /* receive packets (if any) from network */
#endif
#ifdef IN_MENUS
kbdio(); /* process user keys */
#endif
packet_check(); /* see if there's a newly received network packet */
inet_timer(); /* standard polls and timeouts */
/* give cycles to optional features */
#ifdef WEBPORT
http_check(); /* spin http server */
#endif
#ifdef SMTP_ALERTS /* not web server, but email alerter... */
smtpalert_task();
#endif
#ifdef FTP_SERVER
ftps_check(); /* spin FTP server */
#endif
#ifdef FTP_CLIENT
fc_check(); /* spin FTP client */
#endif
#ifdef TCP_ECHOTEST
tcp_echo_poll();
#endif
#ifdef SNMP_STATION
station_check();
#endif
#ifdef SNMP_SOCKETS
snmp_check();
#endif
#ifdef TELNET_SVR
tel_check();
#endif
clock_init();
}
void
ENTER_CRIT_SECTION(void *p)
{
EnterCriticalSection((LPCRITICAL_SECTION)&WIN_RESID);
USE_ARG(p);
}
void
EXIT_CRIT_SECTION(void *p)
{
LeaveCriticalSection((LPCRITICAL_SECTION)&WIN_RESID);
USE_ARG(p);
}
#endif /* SUPERLOOP */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -