methods.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /* GNU Objective C Runtime method related functions.
  2. Copyright (C) 2010-2015 Free Software Foundation, Inc.
  3. Contributed by Nicola Pero
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation; either version 3, or (at your option) any later version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  11. details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "objc-private/common.h"
  20. #include "objc/runtime.h"
  21. #include "objc-private/module-abi-8.h" /* For runtime structures. */
  22. #include "objc/thr.h"
  23. #include "objc-private/runtime.h" /* For __objc_runtime_mutex. */
  24. #include <stdlib.h> /* For malloc. */
  25. SEL
  26. method_getName (struct objc_method * method)
  27. {
  28. if (method == NULL)
  29. return NULL;
  30. return method->method_name;
  31. }
  32. const char *
  33. method_getTypeEncoding (struct objc_method * method)
  34. {
  35. if (method == NULL)
  36. return NULL;
  37. return method->method_types;
  38. }
  39. IMP
  40. method_getImplementation (struct objc_method * method)
  41. {
  42. if (method == NULL)
  43. return NULL;
  44. return method->method_imp;
  45. }
  46. struct objc_method_description *
  47. method_getDescription (struct objc_method * method)
  48. {
  49. /* Note that the following returns NULL if method is NULL, which is
  50. fine. */
  51. return (struct objc_method_description *)method;
  52. }
  53. struct objc_method **
  54. class_copyMethodList (Class class_, unsigned int *numberOfReturnedMethods)
  55. {
  56. unsigned int count = 0;
  57. struct objc_method **returnValue = NULL;
  58. struct objc_method_list* method_list;
  59. if (class_ == Nil)
  60. {
  61. if (numberOfReturnedMethods)
  62. *numberOfReturnedMethods = 0;
  63. return NULL;
  64. }
  65. /* Lock the runtime mutex because the class methods may be
  66. concurrently modified. */
  67. objc_mutex_lock (__objc_runtime_mutex);
  68. /* Count how many methods we have. */
  69. method_list = class_->methods;
  70. while (method_list)
  71. {
  72. count = count + method_list->method_count;
  73. method_list = method_list->method_next;
  74. }
  75. if (count != 0)
  76. {
  77. unsigned int i = 0;
  78. /* Allocate enough memory to hold them. */
  79. returnValue
  80. = (struct objc_method **)(malloc (sizeof (struct objc_method *)
  81. * (count + 1)));
  82. /* Copy the methods. */
  83. method_list = class_->methods;
  84. while (method_list)
  85. {
  86. int j;
  87. for (j = 0; j < method_list->method_count; j++)
  88. {
  89. returnValue[i] = &(method_list->method_list[j]);
  90. i++;
  91. }
  92. method_list = method_list->method_next;
  93. }
  94. returnValue[i] = NULL;
  95. }
  96. objc_mutex_unlock (__objc_runtime_mutex);
  97. if (numberOfReturnedMethods)
  98. *numberOfReturnedMethods = count;
  99. return returnValue;
  100. }
  101. IMP
  102. method_setImplementation (struct objc_method * method, IMP implementation)
  103. {
  104. IMP old_implementation;
  105. if (method == NULL || implementation == NULL)
  106. return NULL;
  107. /* We lock the runtime mutex so that concurrent calls to change the
  108. same method won't conflict with each other. */
  109. objc_mutex_lock (__objc_runtime_mutex);
  110. old_implementation = method->method_imp;
  111. method->method_imp = implementation;
  112. /* That was easy :-). But now we need to find all classes that use
  113. this method, and update the IMP in the dispatch tables. */
  114. __objc_update_classes_with_methods (method, NULL);
  115. objc_mutex_unlock (__objc_runtime_mutex);
  116. return old_implementation;
  117. }
  118. void
  119. method_exchangeImplementations (struct objc_method * method_a, struct objc_method * method_b)
  120. {
  121. IMP old_implementation_a;
  122. IMP old_implementation_b;
  123. if (method_a == NULL || method_b == NULL)
  124. return;
  125. /* We lock the runtime mutex so that concurrent calls to exchange
  126. similar methods won't conflict with each other. Each of them
  127. should be atomic. */
  128. objc_mutex_lock (__objc_runtime_mutex);
  129. old_implementation_a = method_a->method_imp;
  130. old_implementation_b = method_b->method_imp;
  131. method_a->method_imp = old_implementation_b;
  132. method_b->method_imp = old_implementation_a;
  133. /* That was easy :-). But now we need to find all classes that use
  134. these methods, and update the IMP in the dispatch tables. */
  135. __objc_update_classes_with_methods (method_a, method_b);
  136. objc_mutex_unlock (__objc_runtime_mutex);
  137. }