| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #ifndef MUJOCO_SRC_USER_CACHE_H_ |
| | #define MUJOCO_SRC_USER_CACHE_H_ |
| |
|
| | #include <cstddef> |
| | #include <cstdlib> |
| | #include <functional> |
| | #include <set> |
| | #include <string> |
| | #include <memory> |
| | #include <mutex> |
| | #include <unordered_map> |
| | #include <unordered_set> |
| | #include <utility> |
| |
|
| | #include <mujoco/mjplugin.h> |
| |
|
| | typedef std::function<bool(const void*)> mjCDataFunc; |
| | typedef void (*mjCDeallocFunc)(const void*); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | class mjCAsset { |
| | friend class mjCCache; |
| | public: |
| | mjCAsset(std::string modelname, const mjResource* resource, |
| | std::shared_ptr<const void> data, std::size_t size) : |
| | id_(resource->name), timestamp_(resource->timestamp), |
| | size_(size), data_(std::move(data)) { |
| | AddReference(modelname); |
| | } |
| |
|
| | |
| | mjCAsset(mjCAsset&& other) = default; |
| | mjCAsset& operator=(mjCAsset&& other) = default; |
| | mjCAsset(const mjCAsset& other) = default; |
| | mjCAsset& operator=(const mjCAsset& other) = default; |
| |
|
| | const std::string& Timestamp() const { return timestamp_; } |
| | const std::string& Id() const { return id_; } |
| | std::size_t InsertNum() const { return insert_num_; } |
| | std::size_t AccessCount() const { return access_count_; } |
| |
|
| | |
| | bool PopulateData(mjCDataFunc fn) const { |
| | return fn(data_.get()); |
| | } |
| |
|
| | private: |
| | mjCAsset() = default; |
| |
|
| | |
| | void AddReference(std::string xml_file) { references_.insert(xml_file); } |
| | void RemoveReference(const std::string& xml_file) { |
| | references_.erase(xml_file); |
| | } |
| |
|
| | void ReplaceData(const mjCAsset& other) { |
| | data_ = other.data_; |
| | size_ = other.size_; |
| | } |
| |
|
| | bool HasReferences() const { return !references_.empty(); } |
| |
|
| | void IncrementAccess() { access_count_++; } |
| |
|
| | |
| | static mjCAsset Copy(const mjCAsset& other); |
| |
|
| | |
| | void SetInsertNum(std::size_t num) { insert_num_ = num; } |
| | void SetTimestamp(std::string timestamp) { timestamp_ = timestamp; } |
| |
|
| | |
| | std::size_t BytesCount() const { return size_; } |
| | const void* Data() const { |
| | return data_.get(); |
| | } |
| | const std::set<std::string>& References() const { return references_; } |
| |
|
| | std::string id_; |
| | std::string timestamp_; |
| | std::size_t insert_num_; |
| | std::size_t access_count_ = 0; |
| | std::size_t size_ = 0; |
| | std::shared_ptr<const void> data_; |
| |
|
| | |
| | std::set<std::string> references_; |
| | }; |
| |
|
| | struct mjCAssetCompare { |
| | bool operator()(const mjCAsset* e1, const mjCAsset* e2) const { |
| | if (e1->AccessCount() != e2->AccessCount()) { |
| | return e1->AccessCount() < e2->AccessCount(); |
| | } |
| | return e1->InsertNum() < e2->InsertNum(); |
| | } |
| | }; |
| |
|
| | |
| | class mjCCache { |
| | public: |
| | explicit mjCCache(std::size_t size) : max_size_(size) {} |
| |
|
| | |
| | mjCCache(mjCCache&& other) = delete; |
| | mjCCache& operator=(mjCCache&& other) = delete; |
| | mjCCache(const mjCCache& other) = delete; |
| | mjCCache& operator=(const mjCCache& other) = delete; |
| |
|
| | |
| | |
| | |
| | void SetMaxSize(std::size_t size); |
| |
|
| | |
| | |
| | const std::string* HasAsset(const std::string& id); |
| |
|
| | |
| | |
| | bool Insert(const std::string& modelname, const mjResource *resource, |
| | std::shared_ptr<const void> data, std::size_t size); |
| |
|
| | |
| | bool PopulateData(const mjResource* resource, mjCDataFunc fn); |
| |
|
| | |
| | void DeleteAsset(const std::string& id); |
| |
|
| | |
| | |
| | void RemoveModel(const std::string& filename); |
| |
|
| | |
| | void Reset(const std::string& filename); |
| |
|
| | |
| | void Reset(); |
| |
|
| | |
| | std::size_t MaxSize() const; |
| | std::size_t Size() const; |
| |
|
| | private: |
| | void Delete(mjCAsset* asset); |
| | void Delete(mjCAsset* asset, const std::string& skip); |
| | void Trim(); |
| |
|
| | |
| | |
| | |
| | mutable std::mutex mutex_; |
| | std::size_t insert_num_ = 0; |
| | std::size_t size_ = 0; |
| | std::size_t max_size_ = 0; |
| |
|
| | |
| | std::unordered_map<std::string, mjCAsset> lookup_; |
| |
|
| | |
| | std::set<mjCAsset*, mjCAssetCompare> entries_; |
| |
|
| | |
| | std::unordered_map<std::string, std::unordered_set<mjCAsset*>> models_; |
| | }; |
| |
|
| | #endif |
| |
|