summaryrefslogtreecommitdiffstats
path: root/private/ntos/ndis/pcimac/util.c
blob: 11cf6101b6f0cbc415f89b0e5db0d2c5433177a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * UTIL.C - some utility functions
 */

#include    <ndis.h>
//#include	<ntddk.h>
#include	<mydefs.h>
#include	<mytypes.h>
#include    <util.h>

// Total Memory Allocated
ULONG		TotalMemory = 0;

/* constants */
#define     BUF_SIZE        512

/* get current time, in seconds */
ULONG
ut_time_now(VOID)
{
    LARGE_INTEGER           curr_time;
    LARGE_INTEGER           sec;
    LARGE_INTEGER           rem;
    static LARGE_INTEGER    base = {0, 0};
    static BOOL             first_call = TRUE;

    /* get current system time */
    KeQuerySystemTime(&curr_time);

    /* on first call, store base */
    if ( first_call )
    {
        base = curr_time;
        first_call = FALSE;
    }
    /* make relative to base */
    curr_time.QuadPart -= base.QuadPart;
    
    /* convert to seconds */
    sec.QuadPart = curr_time.QuadPart/10000000L;
    
    /* return as a ULONG */
    return((ULONG)sec.LowPart);
}

/* allocate a buffer */
CHAR*
ut_get_buf(VOID)
{
    CHAR    *ret;
    
    NDIS_PHYSICAL_ADDRESS   pa = NDIS_PHYSICAL_ADDRESS_CONST(-1, -1);

    NdisAllocateMemory((PVOID*)&ret, BUF_SIZE, 0, pa);

    return(ret);
}

/* free buffer */
VOID
ut_free_buf(CHAR *buf)
{
    if ( buf )
        NdisFreeMemory(buf, BUF_SIZE, 0);     
}

/* init */
VOID
sema_init(SEMA *s)
{
	s->signalled = FALSE;
	NdisAllocateSpinLock (&s->lock);
}

VOID
sema_term(SEMA *s)
{
	s->signalled = FALSE;
	NdisFreeSpinLock (&s->lock);
}

/* try to get semaphore */
BOOL
sema_get(SEMA *s)
{
    BOOL    ret;
//	KIRQL	NewIrql, OldIrql;
    
//	NewIrql = HIGH_LEVEL;

//	KeRaiseIrql (NewIrql, &OldIrql);

	NdisAcquireSpinLock (&s->lock);

    if ( !s->signalled )
        ret = s->signalled = TRUE;
    else
        ret = FALSE;
    
	NdisReleaseSpinLock (&s->lock);

//	KeLowerIrql (OldIrql);
    
    return(ret);
}

/* free semaphore */
VOID
sema_free(SEMA *s)
{
	NdisAcquireSpinLock (&s->lock);

    s->signalled = FALSE;

	NdisReleaseSpinLock (&s->lock);
}