dataLink.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * This file is part of the PikaPython project.
  3. * http://github.com/pikastech/pikapython
  4. *
  5. * MIT License
  6. *
  7. * Copyright (c) 2021 lyon liang6516@outlook.com
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  24. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. * DEALINGS IN THE SOFTWARE.
  26. */
  27. #include "dataLink.h"
  28. #include "dataArg.h"
  29. #include "dataLinkNode.h"
  30. #include "dataMemory.h"
  31. void _link_deinit_pyload(Link* self, pika_bool is_object);
  32. void link_deinit_ex(Link* self, pika_bool is_object) {
  33. pika_assert(self != NULL);
  34. _link_deinit_pyload(self, is_object);
  35. pikaFree(self, sizeof(Link));
  36. }
  37. void link_deinit(Link* self) {
  38. link_deinit_ex(self, 0);
  39. }
  40. void link_deinit_stack(Link* self) {
  41. _link_deinit_pyload(self, 0);
  42. }
  43. void link_addNode(Link* self, void* content) {
  44. // old first node become new second node
  45. LinkNode* secondNode = self->firstNode;
  46. self->firstNode = content;
  47. // change the first node to new node
  48. arg_setNext((Arg*)content, (Arg*)secondNode);
  49. }
  50. static void _link_removeNode(Link* self,
  51. void* content,
  52. uint8_t is_deinit_node) {
  53. LinkNode* nodeToDelete = NULL;
  54. LinkNode* nodeNow = self->firstNode;
  55. LinkNode* priorNode = NULL;
  56. LinkNode* nextNode;
  57. while (1) {
  58. if (nodeNow == content) {
  59. nodeToDelete = nodeNow;
  60. break;
  61. }
  62. if (nodeNow == NULL) {
  63. // error, node no found
  64. goto __exit;
  65. }
  66. priorNode = nodeNow;
  67. nodeNow = (LinkNode*)arg_getNext((Arg*)nodeNow);
  68. }
  69. nextNode = (LinkNode*)arg_getNext((Arg*)nodeToDelete);
  70. if (nodeToDelete == self->firstNode) {
  71. self->firstNode = (LinkNode*)arg_getNext((Arg*)nodeToDelete);
  72. }
  73. if (NULL == priorNode) {
  74. self->firstNode = nextNode;
  75. goto __exit;
  76. }
  77. arg_setNext((Arg*)priorNode, (Arg*)nextNode);
  78. goto __exit;
  79. // deinit the node
  80. __exit:
  81. if (is_deinit_node) {
  82. pika_assert(NULL != nodeToDelete);
  83. linkNode_deinit(nodeToDelete);
  84. }
  85. return;
  86. }
  87. void link_removeNode(Link* self, void* content) {
  88. _link_removeNode(self, content, 1);
  89. }
  90. void link_removeNode_notDeinitNode(Link* self, void* content) {
  91. _link_removeNode(self, content, 0);
  92. }
  93. int32_t link_getSize(Link* self) {
  94. LinkNode* NowNode;
  95. int32_t size = 0;
  96. NowNode = self->firstNode;
  97. while (NULL != NowNode) {
  98. size++;
  99. NowNode = (LinkNode*)arg_getNext((Arg*)NowNode);
  100. }
  101. return size;
  102. }
  103. void link_init(Link* self, void* args) {
  104. self->firstNode = NULL;
  105. }
  106. Link* New_link(void* args) {
  107. Link* self = pikaMalloc(sizeof(Link));
  108. link_init(self, args);
  109. return self;
  110. }