Skip to content

Commit da2a71a

Browse files
author
Gé van Geldorp
committed
Remember the last allocated port and try to allocate the next one on the
following call (prevents 1024 from being allocated each time) svn path=/trunk/; revision=20297
1 parent 59946b0 commit da2a71a

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

reactos/drivers/lib/ip/network/ports.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ VOID PortsStartup( PPORT_SET PortSet,
1515
UINT PortsToManage ) {
1616
PortSet->StartingPort = StartingPort;
1717
PortSet->PortsToOversee = PortsToManage;
18+
PortSet->LastAllocatedPort = PortSet->StartingPort +
19+
PortSet->PortsToOversee - 1;
1820
PortSet->ProtoBitBuffer =
1921
PoolAllocateBuffer( (PortSet->PortsToOversee + 7) / 8 );
2022
RtlInitializeBitMap( &PortSet->ProtoBitmap,
@@ -52,12 +54,23 @@ BOOLEAN AllocatePort( PPORT_SET PortSet, ULONG Port ) {
5254

5355
ULONG AllocateAnyPort( PPORT_SET PortSet ) {
5456
ULONG AllocatedPort;
57+
ULONG Next;
58+
59+
__asm__("int $3\n");
60+
if (PortSet->StartingPort + PortSet->PortsToOversee <=
61+
PortSet->LastAllocatedPort + 1) {
62+
Next = PortSet->StartingPort;
63+
} else {
64+
Next = PortSet->LastAllocatedPort + 1;
65+
}
66+
Next -= PortSet->StartingPort;
5567

5668
ExAcquireFastMutex( &PortSet->Mutex );
5769
AllocatedPort = RtlFindClearBits( &PortSet->ProtoBitmap, 1, 0 );
5870
if( AllocatedPort != (ULONG)-1 ) {
5971
RtlSetBit( &PortSet->ProtoBitmap, AllocatedPort );
6072
AllocatedPort += PortSet->StartingPort;
73+
PortSet->LastAllocatedPort = AllocatedPort;
6174
}
6275
ExReleaseFastMutex( &PortSet->Mutex );
6376

@@ -68,16 +81,28 @@ ULONG AllocateAnyPort( PPORT_SET PortSet ) {
6881

6982
ULONG AllocatePortFromRange( PPORT_SET PortSet, ULONG Lowest, ULONG Highest ) {
7083
ULONG AllocatedPort;
84+
ULONG Next;
7185

86+
if (PortSet->StartingPort + PortSet->PortsToOversee <=
87+
PortSet->LastAllocatedPort + 1) {
88+
Next = PortSet->StartingPort;
89+
} else {
90+
Next = PortSet->LastAllocatedPort + 1;
91+
}
92+
if (Next < Lowest || Highest <= Next) {
93+
Next = Lowest;
94+
}
95+
Next -= PortSet->StartingPort;
7296
Lowest -= PortSet->StartingPort;
7397
Highest -= PortSet->StartingPort;
7498

7599
ExAcquireFastMutex( &PortSet->Mutex );
76-
AllocatedPort = RtlFindClearBits( &PortSet->ProtoBitmap, 1, Lowest );
100+
AllocatedPort = RtlFindClearBits( &PortSet->ProtoBitmap, 1, Next );
77101
if( AllocatedPort != (ULONG)-1 && AllocatedPort >= Lowest &&
78102
AllocatedPort <= Highest) {
79103
RtlSetBit( &PortSet->ProtoBitmap, AllocatedPort );
80104
AllocatedPort += PortSet->StartingPort;
105+
PortSet->LastAllocatedPort = AllocatedPort;
81106
}
82107
ExReleaseFastMutex( &PortSet->Mutex );
83108

reactos/drivers/net/tcpip/include/ports.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ typedef struct _PORT_SET {
1616
PVOID ProtoBitBuffer;
1717
UINT StartingPort;
1818
UINT PortsToOversee;
19+
UINT LastAllocatedPort;
1920
FAST_MUTEX Mutex;
2021
} PORT_SET, *PPORT_SET;
2122

0 commit comments

Comments
 (0)