Line data Source code
1 : /* 2 : * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include <boost/uuid/uuid_io.hpp> 6 : #include <cmn/agent_cmn.h> 7 : #include <init/agent_param.h> 8 : #include <resource_manager/resource_manager.h> 9 : #include <resource_manager/resource_table.h> 10 : #include <resource_manager/index_resource.h> 11 : 12 124 : IndexResourceKey::IndexResourceKey(ResourceManager *rm, 13 124 : Resource::Type resource_key_type) : 14 124 : ResourceKey(rm, resource_key_type) { 15 124 : } 16 : 17 124 : IndexResourceKey::~IndexResourceKey() { 18 124 : } 19 : 20 124 : IndexResourceData::IndexResourceData(ResourceManager *rm, uint32_t index) : 21 124 : ResourceData(rm), index_(index) { 22 124 : } 23 : 24 248 : IndexResourceData::~IndexResourceData() { 25 : 26 248 : } 27 : 28 248 : uint32_t IndexResourceData::index() const { 29 248 : return index_; 30 : } 31 : 32 18 : IndexResourceTable::IndexResourceTable(ResourceManager *rm) : 33 18 : ResourceTable(rm) { 34 18 : } 35 : 36 36 : IndexResourceTable::~IndexResourceTable() { 37 36 : } 38 : 39 48 : void IndexResourceTable::ReserveIndex(uint32_t index) { 40 48 : ResourceKeyPtr dummy_key_entry; 41 48 : index_vector_.InsertAtIndex(index, dummy_key_entry); 42 48 : } 43 : 44 172 : void IndexResourceTable::ReleaseIndex(uint32_t index) { 45 172 : index_vector_.FreeIndex(index); 46 172 : } 47 : 48 124 : void IndexResourceTable::Release(uint32_t index) { 49 124 : ResourceKeyPtr key = index_vector_.FindIndex(index); 50 124 : if (key.get()) { 51 124 : resource_manager()->Release(key); 52 : } 53 124 : } 54 : // Allocate the Index resource data. 55 124 : ResourceTable::DataPtr IndexResourceTable::AllocateData(ResourceKeyPtr key) { 56 124 : uint32_t index = AllocateIndex(key); 57 : ResourceTable::DataPtr data(static_cast<ResourceData *> 58 124 : (new IndexResourceData(key->rm(), index))); 59 124 : return data; 60 : } 61 : 62 : // Restore resource Index. 63 0 : void IndexResourceTable::RestoreIndex(uint32_t index, ResourceKeyPtr key) { 64 0 : index_vector_.InsertAtIndex(index, key); 65 0 : } 66 : 67 124 : uint32_t IndexResourceTable::AllocateIndex(ResourceKeyPtr key) { 68 : //Get the index, populate resource data 69 124 : uint32_t index = index_vector_.AllocIndex(key); 70 124 : return index; 71 : } 72 : // Restore the Key and Index 73 0 : void IndexResourceTable::RestoreKey(KeyPtr key, DataPtr data) { 74 0 : IndexResourceData *index_data = static_cast<IndexResourceData *>(data.get()); 75 0 : RestoreIndex(index_data->index(), key); 76 0 : InsertKey(key, data); 77 0 : } 78 : // Release the Key. 79 124 : void IndexResourceTable::ReleaseKey(KeyPtr key, DataPtr data) { 80 124 : IndexResourceData *index_data = static_cast<IndexResourceData *>(data.get()); 81 124 : ReleaseIndex(index_data->index()); 82 124 : DeleteKey(key); 83 124 : }