diff options
author | Denis Brockus <dbrockus@google.com> | 2023-03-20 14:02:41 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-03-23 19:27:00 +0000 |
commit | 1dcfdc359ab94ad03b9ee4bd6e1cb88f1ef2a009 (patch) | |
tree | 43b153933b397b93e85e134e47bf3708068150a6 | |
parent | d434c1bc882f2627f37327f3805bc3cfbc2df37f (diff) | |
download | libchrome-gestures-1dcfdc359ab94ad03b9ee4bd6e1cb88f1ef2a009.tar.gz |
Removal cleanup of MemoryManagedList
Remove MemoryManager
Remove MemoryManagedList
Remove utils RemoveMissingIdsFromMap that passes removed map
BUG=b:271591258
TEST=USE="coverage" FEATURES="test noclean" emerge-brya chromeos-base/gestures
Change-Id: I30686b6e353327066ddc4c8ef496fd51ef6a36dc
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/gestures/+/4357693
Auto-Submit: Denis Brockus <dbrockus@chromium.org>
Tested-by: Denis Brockus <dbrockus@chromium.org>
Reviewed-by: Harry Cutts <hcutts@chromium.org>
Commit-Queue: Denis Brockus <dbrockus@chromium.org>
Code-Coverage: Henry Barnor <hbarnor@chromium.org>
-rw-r--r-- | include/list.h | 70 | ||||
-rw-r--r-- | include/memory_manager.h | 79 | ||||
-rw-r--r-- | include/util.h | 24 |
3 files changed, 6 insertions, 167 deletions
diff --git a/include/list.h b/include/list.h deleted file mode 100644 index c822e7a..0000000 --- a/include/list.h +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2011 The ChromiumOS Authors -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef GESTURES_LIST_H__ -#define GESTURES_LIST_H__ - -#include <list> -#include "include/logging.h" -#include "include/memory_manager.h" - -namespace gestures { - -template<typename Elt> -class MemoryManagedList : public std::list<Elt*> { - public: - MemoryManagedList() { }; - ~MemoryManagedList() { clear(); } - - void Init(MemoryManager<Elt>* memory_manager) { - memory_manager_ = memory_manager; - } - - Elt* PushNewEltBack() { - AssertWithReturnValue(memory_manager_, NULL); - Elt* elt = NewElt(); - AssertWithReturnValue(elt, NULL); - this->push_back(elt); - return elt; - } - - Elt* PushNewEltFront() { - AssertWithReturnValue(memory_manager_, NULL); - Elt* elt = NewElt(); - AssertWithReturnValue(elt, NULL); - this->push_front(elt); - return elt; - } - - void DeleteFront() { - AssertWithReturn(memory_manager_); - memory_manager_->Free(this->front()); - this->pop_front(); - } - - void DeleteBack() { - AssertWithReturn(memory_manager_); - memory_manager_->Free(this->pop_back()); - } - - void clear() { - while (!this->empty()) - DeleteFront(); - } - - private: - MemoryManager<Elt>* memory_manager_; - - Elt* NewElt() { - Elt* elt = memory_manager_->Allocate(); - AssertWithReturnValue(elt, NULL); - elt->next_ = elt->prev_ = NULL; - return elt; - } -}; - - -} // namespace gestures - -#endif // GESTURES_LIST_H__ diff --git a/include/memory_manager.h b/include/memory_manager.h deleted file mode 100644 index 26e096a..0000000 --- a/include/memory_manager.h +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright 2011 The ChromiumOS Authors -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef GESTURES_MEMORY_MANAGER_H_ -#define GESTURES_MEMORY_MANAGER_H_ - -#include <new> - -#include "include/logging.h" -#include "include/macros.h" - -namespace gestures { - -// A simple memory manager class that pre-allocates a size of -// buffer and garbage collects freed variables. It is not intended -// to be used directly. User classes should inherit the -// MemoryManaged wrapper class and use the provided Allocate/Free -// interface instead (see below). - -template<typename T> -class MemoryManager { - public: - explicit MemoryManager(size_t size) : buf_(new T[size]), - free_slots_(new T *[size]), used_mark_(new bool[size]()), - max_size_(size), head_(size) { - for (size_t i = 0; i < max_size_; i++) - free_slots_[i] = buf_.get() + i; - } - - size_t Size() const { return max_size_ - head_; } - size_t MaxSize() const { return max_size_; } - - T* Allocate() { - if (!head_) { - Err("MemoryManager::Allocate: out of space"); - return NULL; - } - - T* ptr = free_slots_[--head_]; - used_mark_[ptr - buf_.get()] = true; - return ptr; - } - - bool Free(T* ptr) { - // Check for invalid pointer and double-free - if (ptr < buf_.get() || ptr >= buf_.get() + max_size_) { - Err("MemoryManager::Free: pointer out of bounds"); - return false; - } - size_t offset_in_bytes = reinterpret_cast<size_t>(ptr) - - reinterpret_cast<size_t>(buf_.get()); - if (offset_in_bytes % sizeof(T)) { - Err("MemoryManager::Free: unaligned pointer"); - return false; - } - size_t offset = ptr - buf_.get(); - if (!used_mark_[offset]) { - Err("MemoryManager::Free: double-free"); - return false; - } - - free_slots_[head_++] = ptr; - used_mark_[offset] = false; - return true; - } - - private: - std::unique_ptr<T[]> buf_; - std::unique_ptr<T*[]> free_slots_; - std::unique_ptr<bool[]> used_mark_; - size_t max_size_; - size_t head_; - DISALLOW_COPY_AND_ASSIGN(MemoryManager<T>); -}; - -} // namespace gestures - -#endif // MEMORY_MANAGER_H_ diff --git a/include/util.h b/include/util.h index 9feb2d5..0e1d89a 100644 --- a/include/util.h +++ b/include/util.h @@ -63,28 +63,16 @@ bool MapContainsKey(const Map& the_map, const Key& the_key) { } // Removes any ids from the map that are not finger ids in hs. -// This implementation supports returning removed elements for -// further processing. -template<typename Data> -void RemoveMissingIdsFromMap(std::map<short, Data>* the_map, - const HardwareState& hs, - std::map<short, Data>* removed) { - removed->clear(); - for (typename std::map<short, Data>::const_iterator it = - the_map->begin(); it != the_map->end(); ++it) - if (!hs.GetFingerState((*it).first)) - (*removed)[it->first] = it->second; - for (typename std::map<short, Data>::const_iterator it = - removed->begin(); it != removed->end(); ++it) - the_map->erase(it->first); -} - -// Removes any ids from the map that are not finger ids in hs. template<typename Data> void RemoveMissingIdsFromMap(std::map<short, Data>* the_map, const HardwareState& hs) { std::map<short, Data> removed; - RemoveMissingIdsFromMap(the_map, hs, &removed); + for (const auto& [key, value] : *the_map) { + if (!hs.GetFingerState(key)) + removed[key] = value; + } + for (const auto& [key, value] : removed) + the_map->erase(key); } // Removes any ids from the set that are not finger ids in hs. |