PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/client-async/sequence/SequenceContainer.cpp

https://gitlab.com/github-cloud-corporation/cynara
C++ | 63 lines | 33 code | 8 blank | 22 comment | 5 complexity | e1a8e0e3f9e3405b97b8fab2595b364f MD5 | raw file
  1. /*
  2. * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License
  15. */
  16. /**
  17. * @file src/client-async/sequence/SequenceContainer.cpp
  18. * @author Marcin Niesluchowski <m.niesluchow@samsung.com>
  19. * @version 1.0
  20. * @brief This file contains definition of SequenceContainer class for
  21. * storing check requests identifiers in libcynara-client-async
  22. */
  23. #include <algorithm>
  24. #include <cinttypes>
  25. #include <climits>
  26. #include <cstring>
  27. #include "SequenceContainer.h"
  28. namespace Cynara {
  29. SequenceContainer::SequenceContainer()
  30. : m_sequenceVector(((static_cast<size_t>(UINT16_MAX) + 1) / CHAR_BIT) / sizeof(int), -1) {
  31. }
  32. bool SequenceContainer::get(ProtocolFrameSequenceNumber &sequenceNumber) {
  33. for (size_t index = 0; index < m_sequenceVector.size(); ++index) {
  34. int pos = ffs(m_sequenceVector[index]);
  35. if (pos != 0) {
  36. sequenceNumber = static_cast<uint16_t>(index * sizeof(int) * CHAR_BIT - 1 + pos);
  37. m_sequenceVector[index] ^= 1 << (pos - 1);
  38. return true;
  39. }
  40. }
  41. return false;
  42. }
  43. bool SequenceContainer::release(ProtocolFrameSequenceNumber sequenceNumber) {
  44. size_t index = static_cast<size_t>(sequenceNumber) / (CHAR_BIT * sizeof(int));
  45. int pos = static_cast<int>(sequenceNumber) % (CHAR_BIT * sizeof(int));
  46. int i = m_sequenceVector[index] | 1 << pos;
  47. if (i == m_sequenceVector[index])
  48. return false;
  49. m_sequenceVector[index] = i;
  50. return true;
  51. }
  52. void SequenceContainer::clear(void) {
  53. memset(m_sequenceVector.data(), -1, m_sequenceVector.size() * sizeof(int));
  54. }
  55. } // namespace Cynara