objects.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* GNU Objective C Runtime class related functions
  2. Copyright (C) 1993-2022 Free Software Foundation, Inc.
  3. Contributed by Kresten Krab Thorup
  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/thr.h" /* Required by objc-private/runtime.h. */
  22. #include "objc-private/module-abi-8.h" /* For CLS_ISCLASS and similar. */
  23. #include "objc-private/runtime.h" /* the kitchen sink */
  24. #include <string.h> /* For memcpy() */
  25. #if OBJC_WITH_GC
  26. # include <gc/gc.h>
  27. # include <gc/gc_typed.h>
  28. #endif
  29. /* FIXME: The semantics of extraBytes are not really clear. */
  30. inline
  31. id
  32. class_createInstance (Class class, size_t extraBytes)
  33. {
  34. id new = nil;
  35. #if OBJC_WITH_GC
  36. if (CLS_ISCLASS (class))
  37. new = (id) GC_malloc_explicitly_typed (class->instance_size + extraBytes,
  38. (GC_descr)class->gc_object_type);
  39. #else
  40. if (CLS_ISCLASS (class))
  41. new = (id) objc_calloc (class->instance_size + extraBytes, 1);
  42. #endif
  43. if (new != nil)
  44. {
  45. /* There is no need to zero the memory, since both
  46. GC_malloc_explicitly_typed and objc_calloc return zeroed
  47. memory. */
  48. new->class_pointer = class;
  49. }
  50. /* TODO: Invoke C++ constructors on all appropriate C++ instance
  51. variables of the new object. */
  52. return new;
  53. }
  54. /* Traditional GNU Objective-C Runtime API. */
  55. id
  56. object_copy (id object, size_t extraBytes)
  57. {
  58. if ((object != nil) && CLS_ISCLASS (object->class_pointer))
  59. {
  60. /* TODO: How should it work with C++ constructors ? */
  61. id copy = class_createInstance (object->class_pointer, extraBytes);
  62. memcpy (copy, object, object->class_pointer->instance_size + extraBytes);
  63. return copy;
  64. }
  65. else
  66. return nil;
  67. }
  68. id
  69. object_dispose (id object)
  70. {
  71. if ((object != nil) && CLS_ISCLASS (object->class_pointer))
  72. {
  73. /* TODO: Invoke C++ destructors on all appropriate C++ instance
  74. variables. But what happens with the garbage collector ?
  75. Would object_dispose() be ever called in that case ? */
  76. objc_free (object);
  77. }
  78. return nil;
  79. }
  80. const char *
  81. object_getClassName (id object)
  82. {
  83. if (object != nil)
  84. return object->class_pointer->name;
  85. else
  86. return "Nil";
  87. }
  88. Class
  89. object_setClass (id object, Class class_)
  90. {
  91. if (object == nil)
  92. return Nil;
  93. else
  94. {
  95. Class old_class = object->class_pointer;
  96. object->class_pointer = class_;
  97. return old_class;
  98. }
  99. }