-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathCompleter.cpp
116 lines (110 loc) · 2.48 KB
/
Completer.cpp
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
//
// Completer.cpp
// GenericUSBXHCI
//
// Created by Zenith432 on October 18th 2013.
// Copyright (c) 2013 Zenith432. All rights reserved.
//
//
#include "GenericUSBXHCI.h"
#include "Completer.h"
#define MAX_FREE_RETENTION 16
__attribute__((visibility("hidden")))
bool Completer::AddItem(IOUSBCompletion const* pCompletion, IOReturn status, uint32_t actualByteCount, bool allowImmediate)
{
CompleterItem* pItem;
if (!pCompletion)
return true;
if (freeHead) {
pItem = freeHead;
if (freeHead == freeTail) {
freeHead = 0;
freeTail = 0;
} else
freeHead = pItem->next;
--freeCount;
} else {
pItem = static_cast<CompleterItem*>(IOMalloc(sizeof *pItem));
if (!pItem) {
if (allowImmediate) {
if (owner)
owner->Complete(*pCompletion, status, actualByteCount);
return true;
}
return false;
}
}
pItem->completion = *pCompletion;
pItem->status = status;
pItem->actualByteCount = actualByteCount;
if (activeTail)
activeTail->next = pItem;
else
activeHead = pItem;
activeTail = pItem;
/*
* Note: If flushing, we're already executing inside
* InternalFlush, so no need to reschedule.
*/
if (!flushing && owner)
owner->ScheduleEventSource();
return true;
}
__attribute__((visibility("hidden")))
void Completer::InternalFlush(void)
{
CompleterItem* pItem;
flushing = true;
pItem = activeHead;
do {
if (pItem == activeTail) {
activeHead = 0;
activeTail = 0;
} else
activeHead = pItem->next;
if (freeCount < MAX_FREE_RETENTION) {
if (freeTail)
freeTail->next = pItem;
else
freeHead = pItem;
freeTail = pItem;
++freeCount;
/*
* Note: pItem is on free list and may be reused inside Complete,
* but its content is loaded on stack before the call.
*/
if (owner)
owner->Complete(pItem->completion, pItem->status, pItem->actualByteCount);
} else {
if (owner)
owner->Complete(pItem->completion, pItem->status, pItem->actualByteCount);
IOFree(pItem, sizeof *pItem);
}
} while ((pItem = activeHead));
flushing = false;
}
__attribute__((visibility("hidden")))
void Completer::Finalize(void)
{
CompleterItem* pNext;
while (activeHead) {
if (activeHead == activeTail)
pNext = 0;
else
pNext = activeHead->next;
IOFree(activeHead, sizeof *pNext);
activeHead = pNext;
}
activeTail = 0;
while (freeHead) {
if (freeHead == freeTail)
pNext = 0;
else
pNext = freeHead->next;
IOFree(freeHead, sizeof *pNext);
freeHead = pNext;
}
freeTail = 0;
freeCount = 0;
flushing = false;
}