Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: content/common/worker_use_counter.cc

Issue 2586863002: Worker: Enable UseCounter for SharedWorkerGlobalScope (Closed)
Patch Set: ready to review Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/common/worker_use_counter.h"
6
7 #include "base/memory/ptr_util.h"
8
9 namespace content {
10
11 // Used for reserving a buffer of a bit vector. If a feature count is larger
12 // than this buffer size, it's expanded on Count().
13 // TODO(nhiroki): How do we synchronize this with UseCounter::Feature?
14 const size_t kNumberOfFeatures = 2048;
15
16 const size_t kBitsPerByte = 8;
17 constexpr size_t kNumberOfBytes = kNumberOfFeatures / kBitsPerByte;
kinuko 2017/01/24 13:56:31 Do we need to use vector<char> here? Maybe we cou
nhiroki 2017/01/24 15:07:30 I think std::bitset is a good option. Before movin
kinuko 2017/01/25 05:12:52 Usually sending IPC up to 2KB doesn't make a lot d
nhiroki 2017/01/25 10:16:11 I think it's not so big for now because most of fe
18
19 WorkerUseCounter::WorkerUseCounter()
20 : counter_(std::vector<char>(kNumberOfBytes)) {
21 }
22
23 WorkerUseCounter::WorkerUseCounter(std::vector<char> counter)
24 : counter_(counter) {
25 }
26
27 WorkerUseCounter::~WorkerUseCounter() = default;
28
29 void WorkerUseCounter::Count(uint32_t feature) {
30 size_t byte_pos = feature / kBitsPerByte;
31 size_t bit_pos = feature % kBitsPerByte;
32 char bit_mask = 0x1 << bit_pos;
33 if (counter_.size() < byte_pos)
34 counter_.resize(byte_pos + 1);
35 counter_[byte_pos] |= bit_mask;
36 }
37
38 bool WorkerUseCounter::IsCounted(uint32_t feature) {
39 size_t byte_pos = feature / kBitsPerByte;
40 size_t bit_pos = feature % kBitsPerByte;
41 if (counter_.size() < byte_pos)
42 return false;
43 char bit_mask = 0x1 << bit_pos;
44 return (counter_[byte_pos] & bit_mask) != 0;
45 }
46
47 std::vector<char> WorkerUseCounter::Dump() {
48 return counter_;
49 }
50
51 size_t WorkerUseCounter::GetNumberOfBits() const {
52 size_t number_of_bytes = counter_.size();
53 return number_of_bytes * kBitsPerByte;
54 }
55
56 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698