-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
design-memory-allocator.cpp
51 lines (47 loc) · 1.32 KB
/
design-memory-allocator.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
// Time: ctor: O(1)
// allocate: O(logn)
// free: O(logn)
// Space: O(n)
// bst
class Allocator {
public:
Allocator(int n) : avails_{{0, n}} {
}
int allocate(int size, int mID) {
for (const auto [l, s] : avails_) {
if (s < size) {
continue;
}
avails_.erase(l);
lookup_[mID].emplace_back(l, size);
if (s - size > 0) {
avails_.emplace(l + size, s - size);
}
return l;
}
return -1;
}
int free(int mID) {
if (!lookup_.count(mID)) {
return 0;
}
int result = 0;
for (const auto& [l, s] : lookup_[mID]) {
auto it = avails_.emplace(l, s).first;
if (next(it) != end(avails_) && it->first + it->second == next(it)->first) {
it->second += next(it)->second;
avails_.erase(next(it));
}
if (it != begin(avails_) && prev(it)->first + prev(it)->second == it->first) {
prev(it)->second += it->second;
avails_.erase(it);
}
result += s;
}
lookup_.erase(mID);
return result;
}
private:
map<int, int> avails_;
unordered_map<int, vector<pair<int, int>>> lookup_;
};