zone_table.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <util/memory_segment.h>
  15. #include <dns/name.h>
  16. #include <datasrc/memory/zone_data.h>
  17. #include <datasrc/memory/zone_table.h>
  18. #include <datasrc/memory/domaintree.h>
  19. #include <cassert>
  20. using namespace std;
  21. using namespace isc::dns;
  22. namespace isc {
  23. namespace datasrc {
  24. namespace memory {
  25. namespace {
  26. // A simple holder to create and use some objects in this implementation
  27. // in an exception safe manner. It works like std::auto_ptr but much
  28. // more simplified.
  29. template <typename T>
  30. class Holder {
  31. public:
  32. Holder(util::MemorySegment& mem_sgmt, T* obj) :
  33. mem_sgmt_(mem_sgmt), obj_(obj)
  34. {}
  35. ~Holder() {
  36. if (obj_ != NULL) {
  37. T::destroy(mem_sgmt_, obj_);
  38. }
  39. }
  40. T* get() { return (obj_); }
  41. T* release() {
  42. T* ret = obj_;
  43. obj_ = NULL;
  44. return (ret);
  45. }
  46. private:
  47. util::MemorySegment& mem_sgmt_;
  48. T* obj_;
  49. };
  50. }
  51. void
  52. ZoneTable::ZoneDataDeleter::operator()(util::MemorySegment& mem_sgmt,
  53. ZoneData* zone_data) const
  54. {
  55. ZoneData::destroy(mem_sgmt, zone_data);
  56. }
  57. ZoneTable*
  58. ZoneTable::create(util::MemorySegment& mem_sgmt) {
  59. Holder<ZoneTableTree> holder(mem_sgmt, ZoneTableTree::create(mem_sgmt));
  60. void* p = mem_sgmt.allocate(sizeof(ZoneTable));
  61. ZoneTable* zone_table = new(p) ZoneTable(holder.get());
  62. holder.release();
  63. return (zone_table);
  64. }
  65. void
  66. ZoneTable::destroy(util::MemorySegment& mem_sgmt, ZoneTable* ztable) {
  67. ZoneTableTree::destroy(mem_sgmt, ztable->zones_.get());
  68. mem_sgmt.deallocate(ztable, sizeof(ZoneTable));
  69. }
  70. ZoneTable::AddResult
  71. ZoneTable::addZone(util::MemorySegment& mem_sgmt, const Name& zone_name) {
  72. // Create a new ZoneData instance first. If the specified name already
  73. // exists in the table, the new data will soon be destroyed, but we want
  74. // to make sure if this allocation fails the tree won't be changed to
  75. // provide as strong guarantee as possible. In practice, we generally
  76. // expect the caller tries to add a zone only when it's a new one, so
  77. // this should be a minor concern.
  78. Holder<ZoneData> holder(mem_sgmt, ZoneData::create(mem_sgmt));
  79. // Get the node where we put the zone
  80. ZoneTableNode* node(NULL);
  81. switch (zones_->insert(mem_sgmt, zone_name, &node)) {
  82. case ZoneTableTree::SUCCESS:
  83. case ZoneTableTree::ALREADYEXISTS:
  84. // These are OK
  85. break;
  86. default:
  87. // Can Not Happen
  88. assert(false);
  89. }
  90. // Can Not Happen
  91. assert(node != NULL);
  92. // Is it empty? We either just created it or it might be nonterminal
  93. if (node->isEmpty()) {
  94. node->setData(mem_sgmt, holder.get());
  95. return (AddResult(result::SUCCESS, holder.release()));
  96. } else { // There's something there already
  97. return (AddResult(result::EXIST, node->getData()));
  98. }
  99. }
  100. ZoneTable::FindResult
  101. ZoneTable::findZone(const Name& name) const {
  102. ZoneTableNode* node(NULL);
  103. result::Result my_result;
  104. // Translate the return codes
  105. switch (zones_->find(name, &node)) {
  106. case ZoneTableTree::EXACTMATCH:
  107. my_result = result::SUCCESS;
  108. break;
  109. case ZoneTableTree::PARTIALMATCH:
  110. my_result = result::PARTIALMATCH;
  111. break;
  112. case ZoneTableTree::NOTFOUND:
  113. // We have no data there, so translate the pointer to NULL as well
  114. return (FindResult(result::NOTFOUND, NULL));
  115. default:
  116. // Can Not Happen
  117. assert(0);
  118. // Because of warning
  119. return (FindResult(result::NOTFOUND, NULL));
  120. }
  121. // Can Not Happen (remember, NOTFOUND is handled)
  122. assert(node != NULL);
  123. return (FindResult(my_result, node->getData()));
  124. }
  125. } // end of namespace memory
  126. } // end of namespace datasrc
  127. } // end of namespace isc