?? core.c
字號(hào):
/* Method dispatcher and class-object creator for Objective C. Copyright (C) 1992 Free Software Foundation, Inc.This file is part of GNU CC.GNU CC is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU CC is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU CC; see the file COPYING. If not, write tothe Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. *//* As a special exception, if you link this library with files compiled with GCC to produce an executable, this does not cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License. */#include "tconfig.h"#include "assert.h"#include <ctype.h>#include "gstdarg.h"#include <stdio.h>#include "gstddef.h"#include "hash.h"#include "objc.h"#include "objc-proto.h"#define MODULE_HASH_SIZE 32 /* Initial module hash table size. Value doesn't really matter. */#define CLASS_HASH_SIZE 32 /* Initial number of buckets size of class hash table. Value doesn't really matter. *//* Forward declare some functions. */id objc_object_create (Class_t), objc_object_dispose (id), objc_object_realloc (id, unsigned int), objc_object_copy (id);void objc_error (id object, const char *fmt, va_list ap);static id nil_method (id, SEL, ...);static id return_error_static (id, SEL, ...);static IMP handle_runtime_error (id, SEL);static void initialize_dispatch_tables (void);static SEL record_selector (const char*);static void record_methods_from_class (Class_t);static void record_methods_from_list (MethodList_t);static void initialize_class (const char*);/* * This is a hash table of Class_t structures. * * At initialization the executable is searched for all Class_t structures. * Since these structures are created by the compiler they are therefore * located in the TEXT segment. * * A identical structure is allocated from the free store and initialized from * its TEXT counterpart and placed in the hash table using the TEXT part as * its key. * * Since the free store structure is now writable, additional initialization * takes place such as its "info" variable, method cache allocation, and * linking of all of its method and ivar lists from multiple implementations. */cache_ptr class_hash_table = NULL;/* * This variable is a flag used within the messaging routines. If a * application sets it to !0 then the messager will print messages sent to * objects. */BOOL objc_trace = NO;/* This mutex provides a course lock for method dispatch. */MUTEX runtimeMutex;/* * This hash table is used by the initialization routines. When the * constructor function (__objc_execClass) is called it is passed a pointer * to a module structure. That pointer is stored in this table and its * contents are processed in __objcInit. */cache_ptr module_hash_table = NULL;/* * This hash table is used in the constructor subroutine to hold pointers * to categories that have not been attached to a class. Constructors are * received in a random order. Files may contain category implementation * of objects whose constructor hasn't been executed yet. Therefore, * there is no object to attach the categories. * * This hash table holds pointers to categories that haven't been * attached to objects. As objects are processed the category hash * table is searched for attachments. If a category is found for the * object it is attached to the object and deleted from the hash table. */cache_ptr unclaimed_category_hash_table = NULL;/* * This flag is used by the messager routines to determine if the run-time * has been initialized. If the run-time isn't initialized then a * initialization clean up routine is called. */static int initialized = 0;/* * Records that hold pointers to arrays of records. The terminal records are * method implementations. * * The size of the first record is the number of unique classes in the * executable. The second is the number of selectors. * * The second record conatins methods that are visible to the class -- that is, * methods that are not overriden from the classt to the root object. * * The cache pointers of class and meta class structures point to one of these * records. */static struct record *instance_method_record = NULL;static struct record *factory_method_record = NULL;/* * This structure is used to translate between selectors and their ASCII * representation. A NULL terminated array of char*, * OBJC_SELECTOR_REFERENCES, is passed to the constructor routine: * __objc_execClass. That routine places entries from that array into this * structure. The location within OBJC_SELECTOR_REFERENCES where the string * was taken from is replaced with a small integer, the index into the array * inside selectorTranslateTable. That integer then becomes the selector. * * Selectors begin at 1 to numEntries. A selector outside of that range is * considered an error. The selector integers are used as the first index * into the instance_method_record and factory_method_record arrays. */static struct record *selector_record = NULL;/* * This data structure is used in the special case where usual fatal error * functions are called but have been overridden in a class. The value * returned by that function is returned to the calling object. error_static * holds the returned value until it is retrieved by return_error_static * which returns it to the calling function. */static id error_static;/* Given a class and selector, return the selector's implementation. */static inline IMP get_imp (Class_t class, SEL sel){ IMP imp = NULL; imp = record_get (getClassNumber (class), record_get ((unsigned int)sel, *class->cache)); return imp;}static voiderror (msg, arg1, arg2) char *msg, *arg1, *arg2;{#if 0 /* There is no portable way to get the program name. Too bad. */ fprintf (stderr, "%s: ", programname);#endif fprintf (stderr, msg, arg1, arg2); fprintf (stderr, "\n");}static voidfatal (msg, arg1, arg2) char *msg, *arg1, *arg2;{ error (msg, arg1, arg2); exit (1);}static void *xmalloc (unsigned int size){ void *ptr = (void *) malloc (size); if (ptr == 0) fatal ("virtual memory exceeded"); return ptr;}static void *xcalloc (unsigned int size, unsigned int units){ void *ptr = (void *) calloc (size, units); if (ptr == 0) fatal ("virtual memory exceeded"); return ptr;}void *__objc_xmalloc (unsigned int size){ void *ptr = (void *) malloc (size); if (ptr == 0) fatal ("virtual memory exceeded"); return ptr;}void *__objc_xrealloc (void *optr, unsigned int size){ void *ptr = (void *) realloc (optr, size); if (ptr == 0) fatal ("virtual memory exceeded"); return ptr;}void *__objc_xcalloc (unsigned int size, unsigned int units){ void *ptr = (void *) calloc (size, units); if (ptr == 0) fatal ("virtual memory exceeded"); return ptr;}static inline char *my_strdup (const char *str){ char *new = (char *) xmalloc (strlen (str) + 1); strcpy (new, str); return new;}/* * This function is called by constructor functions generated for each module * compiled. * * The purpose of this function is to gather the module pointers so that they * may be processed by the initialization clean up routine. */void __objc_execClass (Module_t module){ /* Has we processed any constructors previously? Flag used to indicate that some global data structures need to be built. */ static BOOL previous_constructors = 0; Symtab_t symtab = module->symtab; Class_t object_class; node_ptr node; SEL *(*selectors)[] = (SEL *(*)[])symtab->refs; int i; BOOL incomplete = 0; assert (module->size == sizeof (Module)); DEBUG_PRINTF ("received load module: %s\n",module->name); /* Header file data structure hack test. */ assert (sizeof (Class) == sizeof (MetaClass)); /* On the first call of this routine, initialize some data structures. */ if (!previous_constructors) {#if defined (DEBUG) && defined (NeXT) malloc_debug (62);#endif /* Allocate and initialize the mutex. */ MUTEX_ALLOC (&runtimeMutex); MUTEX_INIT (runtimeMutex); /* Allocate the module hash table. */ module_hash_table = hash_new (MODULE_HASH_SIZE, (hash_func_type)hash_ptr, (compare_func_type)compare_ptrs); /* Allocate a table for unclaimed categories. */ unclaimed_category_hash_table = hash_new (16, (hash_func_type)hash_ptr, (compare_func_type)compare_ptrs); /* Allocate a master selector table if it doesn't exist. */ selector_record = record_new (); previous_constructors = 1; } /* Save the module pointer for later processing. */ hash_add (&module_hash_table, module, module); /* Parse the classes in the load module and gather selector information. */ DEBUG_PRINTF ("gathering selectors from module: %s\n",module->name); for (i = 0; i < symtab->cls_def_cnt; ++i) { Class_t class = (Class_t)symtab->defs[i]; /* Make sure we have what we think. */ assert (class->info & CLS_CLASS); DEBUG_PRINTF ("phase 1, processing class: %s\n", class->name); /* Store the class in the class table and assign class numbers. */ addClassToHash (class); /* Store all of the selectors in the class and meta class. */ record_methods_from_class (class); record_methods_from_class ((Class_t)class->class_pointer); /* Initialize the cache pointers. */ class->cache = &instance_method_record; class->class_pointer->cache = &factory_method_record; } /* Replace referenced selectors. */ for (i=0; i < symtab->sel_ref_cnt; ++i) (*selectors)[i] = record_selector ((const char*)(*selectors)[i]); /* Try to build the class hierarchy and initialize the data structures. */ object_class = objc_getClass ("Object"); if (object_class) { /* Make sure we have what we think we have. */ assert (object_class->class_pointer->info & CLS_META); assert (object_class->info & CLS_CLASS); /* Connect the classes together (at least as much as we can). */ for (node = hash_next (class_hash_table, NULL); node; node = hash_next (class_hash_table, node)) { Class_t class1 = node->value; /* Make sure we have what we think we have. */ assert (class1->info & CLS_CLASS); assert (class1->class_pointer->info & CLS_META); /* The class_pointer of all meta classes point to Object's meta class. */ class1->class_pointer->class_pointer = object_class->class_pointer; /* Assign super class pointers */ if (class1->super_class) { Class_t aSuperClass = objc_getClass ((char*)class1->super_class); if (aSuperClass) { DEBUG_PRINTF ("making class connections for: %s\n", class1->name); class1->super_class = aSuperClass; if (class1->class_pointer->super_class) class1->class_pointer->super_class = class1->super_class->class_pointer; /* Mark the class as initialized. */ class1->info |= CLS_RTI; } else /* Couldn't find the class's super class. */ incomplete = 1; } } } else /* Couldn't find class Object. */ incomplete = 1; /* Process category information from the module. */ for (i = 0; i < symtab->cat_def_cnt; ++i) { Category_t category = symtab->defs[i + symtab->cls_def_cnt]; Class_t class = objc_getClass (category->class_name); /* If the class for the category exists then append its methods. */ if (class) { DEBUG_PRINTF ("processing categories from (module,object): %s, %s\n", module->name, class_getClassName (class)); /* Do instance methods. */ if (category->instance_methods) addMethodsToClass (class, category->instance_methods); /* Do class methods. */ if (category->class_methods) addMethodsToClass ((Class_t)class->class_pointer, category->class_methods); } else { /* The object to which the category methods belong can't be found. Save the information. */ hash_add (&unclaimed_category_hash_table, category, category); incomplete = 1; } } /* Scan the unclaimed category hash. Attempt to attach any unclaimed categories to objects. */ for (node = hash_next (unclaimed_category_hash_table, NULL); node; node = hash_next (unclaimed_category_hash_table, node)) { Category_t category = node->value; Class_t class = objc_getClass (category->class_name); if (class) { DEBUG_PRINTF ("attaching stored categories to object: %s\n", class_getClassName (class)); /* Delete this class from the hash table. */ hash_remove (unclaimed_category_hash_table, category); node = NULL; if (category->instance_methods) addMethodsToClass (class, category->instance_methods); if (category->class_methods) addMethodsToClass ((Class_t)class->class_pointer, category->class_methods); } else incomplete = 1; } /* Can we finish the run time initialization? */ if (!incomplete) { initialize_dispatch_tables (); /* Debug run time test. We're initialized! */ initialized = 1; /* Print out class tables if debugging. */ DEBUG_PRINTF ("dump of class tables from objcInit\n"); debug_dump_classes (); }}IMP objc_msgSend (id receiver, SEL sel){ /* * A method is always called by the compiler. If a method wasn't * found then supply a default. */ IMP imp = nil_method; /* The run time must be initialized at this point. Otherwise we get a message sent to a object with a bogus selector. */
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -