This repository was archived by the owner on May 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathProducers.cpp
More file actions
96 lines (73 loc) · 2.68 KB
/
Producers.cpp
File metadata and controls
96 lines (73 loc) · 2.68 KB
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
#include "Producers.h"
#include <iostream>
// Allocation
bool Allocation::operator<(const Allocation &that) const {
return this->toFree_ < that.toFree_;
}
bool Allocation::operator>(const Allocation &that) const {
return !(*this < that);
}
bool Allocation::isEmpty() const { return this->toFree_.size() == 0; }
std::chrono::high_resolution_clock::time_point Allocation::freeAfter() const {
return this->freeAfter_;
}
Allocation::Allocation(std::vector<void *> toFree,
std::chrono::high_resolution_clock::time_point freeAfter)
: toFree_(toFree), freeAfter_(freeAfter) {}
Allocation::~Allocation() {
for (auto it = begin(this->toFree_); it != end(this->toFree_); ++it) {
free(*it);
}
}
// Simple Producer
SimpleProducer::SimpleProducer(int allocSize, int numAllocs)
: allocSize_(allocSize), numAllocs_(numAllocs) {}
Allocation SimpleProducer::run() const {
for (int i = 0; i < this->numAllocs_; i++) {
char *ptr = (char *)calloc(this->allocSize_, sizeof(char));
if (ptr == NULL) {
std::cout << "allocation failed" << std::endl;
}
free(ptr);
}
return std::move(Allocation());
}
void swap(Allocation &a1, Allocation &a2) {
a1.toFree_.swap(a2.toFree_);
std::swap(a1.freeAfter_, a2.freeAfter_);
}
// Vector Producer
VectorProducer::VectorProducer(size_t vectorSize,
std::chrono::duration<double> lifetime,
size_t initialSize)
: vectorSize_(vectorSize), lifetime_(lifetime), initialSize_(initialSize) {}
std::chrono::high_resolution_clock::time_point addToNow(std::chrono::duration<double> d) {
using namespace std::chrono;
high_resolution_clock::time_point t = high_resolution_clock::now();
high_resolution_clock::duration dHighResolution =
duration_cast<high_resolution_clock::duration>(d);
t += dHighResolution;;
return t;
}
Allocation VectorProducer::run() const {
void *ptr = malloc(this->initialSize_);
size_t currSize = this->initialSize_;
while (currSize < this->vectorSize_) {
free(ptr);
currSize *= 2;
ptr = malloc(currSize);
}
return std::move(Allocation(std::vector<void *>({ptr}), addToNow(this->lifetime_)));
}
// LinkedList Producer
Allocation LinkedListProducer::run() const {
std::vector<void *> toFree;
toFree.reserve(this->numNodes_);
for (int i = 0; i < this->numNodes_; i++) {
toFree.push_back(malloc(this->nodeSize_));
}
return std::move(Allocation(toFree, addToNow(this->lifetime_)));
}
// allocate [numNodes] blocks of size [nodeSize] with lifetime [lifetime]
LinkedListProducer::LinkedListProducer(size_t nodeSize, int numNodes, std::chrono::duration<double> lifetime) :
nodeSize_(nodeSize), numNodes_(numNodes), lifetime_(lifetime) {}