Commit c72fc2d9 by Tom Wood

Initial revision

From-SVN: r4077
parent b2055d6d
/* Interface for the Object class for Objective-C.
Copyright (C) 1993 Free Software Foundation, Inc.
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
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 of MERCHANTABILITY or
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to
the 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. */
#ifndef __object_INCLUDE_GNU
#define __object_INCLUDE_GNU
#include <objc/objc.h>
/*
* All classes are derived from Object. As such,
* this is the overhead tacked onto those objects.
*/
@interface Object
{
Class_t isa; /* A pointer to the instance's class structure */
}
/* Initializing classes and instances */
+ initialize;
- - init;
/* Creating, freeing, and copying instances */
+ + new;
+ + alloc;
- - free;
- - copy;
- - shallowCopy;
- - deepen;
- - deepCopy;
/* Identifying classes */
- (Class_t)class;
- (Class_t)superClass;
- (MetaClass_t)metaClass;
- (const char *)name;
/* Identifying and comparing objects */
- - self;
- (unsigned int)hash;
- (BOOL)isEqual:anObject;
/* Testing object type */
- (BOOL)isMetaClass;
- (BOOL)isClass;
- (BOOL)isInstance;
/* Testing inheritance relationships */
- (BOOL)isKindOf:(Class_t)aClassObject;
- (BOOL)isMemberOf:(Class_t)aClassObject;
- (BOOL)isKindOfClassNamed:(const char *)aClassName;
- (BOOL)isMemberOfClassNamed:(const char *)aClassName;
/* Testing class functionality */
+ (BOOL)instancesRespondTo:(SEL)aSel;
- (BOOL)respondsTo:(SEL)aSel;
/* Testing protocol conformance */
- (BOOL)conformsTo:(Protocol*)aProtocol;
/* Introspection */
+ (IMP)instanceMethodFor:(SEL)aSel;
- (IMP)methodFor:(SEL)aSel;
+ (struct objc_method_description *)descriptionForInstanceMethod:(SEL)aSel;
- (struct objc_method_description *)descriptionForMethod:(SEL)aSel;
/* Sending messages determined at run time */
- - perform:(SEL)aSel;
- - perform:(SEL)aSel with:anObject;
- - perform:(SEL)aSel with:anObject1 with:anObject2;
/* Forwarding */
- - forward:(SEL)aSel :(arglist_t)argFrame;
- - performv:(SEL)aSel :(arglist_t)argFrame;
/* Posing */
+ + poseAs:(Class_t)aClassObject;
- (Class_t)transmuteClassTo:(Class_t)aClassObject;
/* Enforcing intentions */
- - subclassResponsibility:(SEL)aSel;
- - notImplemented:(SEL)aSel;
/* Error handling */
- - doesNotRecognize:(SEL)aSel;
- - error:(const char *)aString, ...;
/* Archiving */
+ (int)version;
+ + setVersion:(int)aVersion;
- - read: (TypedStream*)aStream;
- - write: (TypedStream*)aStream;
+ + read: (TypedStream*)aStream;
+ + write: (TypedStream*)aStream;
@end
#endif
/* Declare the class Protocol for Objective C programs.
Copyright (C) 1993 Free Software Foundation, Inc.
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the 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 of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to
the 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. */
#ifndef __Protocol_INCLUDE_GNU
#define __Protocol_INCLUDE_GNU
#include <objc/Object.h>
@interface Protocol : Object
{
@private
char *protocol_name;
struct objc_protocol_list *protocol_list;
struct objc_method_description_list *instance_methods, *class_methods;
}
/* Obtaining attributes intrinsic to the protocol */
- (const char *)name;
/* Testing protocol conformance */
- (BOOL) conformsTo: (Protocol *)aProtocolObject;
/* Looking up information specific to a protocol */
- (struct objc_method_description *) descriptionForInstanceMethod:(SEL)aSel;
- (struct objc_method_description *) descriptionForClassMethod:(SEL)aSel;
@end
#endif __Protocol_INCLUDE_GNU
/* This file contains the implementation of class Protocol.
Copyright (C) 1993 Free Software Foundation, Inc.
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the 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 of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to
the 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 <objc/Protocol.h>
#include <objc/objc-api.h>
/* Method description list */
struct objc_method_description_list {
int count;
struct objc_method_description list[1];
};
@implementation Protocol
{
@private
char *protocol_name;
struct objc_protocol_list *protocol_list;
struct objc_method_description_list *instance_methods, *class_methods;
}
/* Obtaining attributes intrinsic to the protocol */
- (const char *)name
{
return protocol_name;
}
/* Testing protocol conformance */
- (BOOL) conformsTo: (Protocol *)aProtocolObject
{
int i;
struct objc_protocol_list* proto_list;
if (!strcmp(aProtocolObject->protocol_name, self->protocol_name))
return YES;
for (proto_list = protocol_list; proto_list; proto_list = proto_list->next)
{
for (i=0; i < proto_list->count; i++)
{
if ([proto_list->list[i] conformsTo: aProtocolObject])
return YES;
}
}
return NO;
}
/* Looking up information specific to a protocol */
- (struct objc_method_description *) descriptionForInstanceMethod:(SEL)aSel
{
int i;
struct objc_protocol_list* proto_list;
const char* name = sel_get_name (aSel);
struct objc_method_description *result;
for (i = 0; i < instance_methods->count; i++)
{
if (!strcmp ((char*)instance_methods->list[i].name, name))
return &(instance_methods->list[i]);
}
for (proto_list = protocol_list; proto_list; proto_list = proto_list->next)
{
for (i=0; i < proto_list->count; i++)
{
if ((result = [proto_list->list[i]
descriptionForInstanceMethod: aSel]))
return result;
}
}
return NULL;
}
- (struct objc_method_description *) descriptionForClassMethod:(SEL)aSel;
{
int i;
struct objc_protocol_list* proto_list;
const char* name = sel_get_name (aSel);
struct objc_method_description *result;
for (i = 0; i < class_methods->count; i++)
{
if (!strcmp ((char*)class_methods->list[i].name, name))
return &(class_methods->list[i]);
}
for (proto_list = protocol_list; proto_list; proto_list = proto_list->next)
{
for (i=0; i < proto_list->count; i++)
{
if ((result = [proto_list->list[i]
descriptionForClassMethod: aSel]))
return result;
}
}
return NULL;
}
@end
/* GNU Objective C Runtime initialization
Copyright (C) 1993 Free Software Foundation, Inc.
Author: Kresten Krab Thorup
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the 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 of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
GNU CC; see the file COPYING. If not, write to the 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 "runtime.h"
/* The version number of this runtime. This must match the number
defined in gcc (objc-act.c) */
#define OBJC_VERSION 5
#define PROTOCOL_VERSION 2
/* This list contains all modules currently loaded into the runtime */
static struct objc_list* __objc_module_list = 0;
/* This list contains all proto_list's not yet assigned class links */
static struct objc_list* unclaimed_proto_list = 0;
/* Check compiler vs runtime version */
static void init_check_module_version(Module_t);
/* Assign isa links to protos */
static void __objc_init_protocols (struct objc_protocol_list* protos);
/* Add protocol to class */
static void __objc_class_add_protocols (Class_t, struct objc_protocol_list*);
/* Is all categories/classes resolved? */
BOOL __objc_dangling_categories = NO;
/* This function is called by constructor functions generated for each
module compiled. (_GLOBAL_$I$...) The purpose of this function is to
gather the module pointers so that they may be processed by the
initialization routines as soon as possible */
void
__objc_exec_class (Module_t module)
{
/* Has we processed any constructors previously? This flag used to
indicate that some global data structures need to be built. */
static BOOL previous_constructors = 0;
static struct objc_list* unclaimed_categories = 0;
/* The symbol table (defined in objc.h) generated by gcc */
Symtab_t symtab = module->symtab;
/* Pointer to the class Object class object */
Class_t object_class;
/* Entry used to traverse hash lists */
struct objc_list** cell;
/* The table of selector references for this module */
SEL *selectors = symtab->refs;
/* dummy counter */
int i;
DEBUG_PRINTF ("received module: %s\n", module->name);
/* check gcc version */
init_check_module_version(module);
/* On the first call of this routine, initialize some data structures. */
if (!previous_constructors)
{
__objc_init_selector_tables();
__objc_init_class_tables();
__objc_init_dispatch_tables();
previous_constructors = 1;
}
/* Save the module pointer for later processing. (not currently used) */
__objc_module_list = list_cons(module, __objc_module_list);
/* 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 (CLS_ISCLASS(class));
assert (CLS_ISMETA(class->class_pointer));
DEBUG_PRINTF ("phase 1, processing class: %s\n", class->name);
/* Store the class in the class table and assign class numbers. */
__objc_add_class_to_hash (class);
/* Register all of the selectors in the class and meta class. */
__objc_register_selectors_from_class (class);
__objc_register_selectors_from_class ((Class_t) class->class_pointer);
/* Install the fake dispatch tables */
__objc_install_premature_dtable(class);
__objc_install_premature_dtable(class->class_pointer);
if (class->protocols)
__objc_init_protocols (class->protocols);
}
/* Replace referenced selectors from names to SEL's. */
for (i = 0; selectors[i]; ++i)
selectors[i] = sel_register_name ((const char *) selectors[i]);
/* 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_lookup_class (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->name);
/* Do instance methods. */
if (category->instance_methods)
class_add_method_list (class, category->instance_methods);
/* Do class methods. */
if (category->class_methods)
class_add_method_list ((Class_t) class->class_pointer,
category->class_methods);
if (category->protocols)
{
__objc_init_protocols (category->protocols);
__objc_class_add_protocols (class, category->protocols);
}
}
else
{
/* The object to which the category methods belong can't be found.
Save the information. */
unclaimed_categories = list_cons(category, unclaimed_categories);
}
}
/* Scan the unclaimed category hash. Attempt to attach any unclaimed
categories to objects. */
for (cell = &unclaimed_categories;
*cell;
*cell && (cell = &(*cell)->tail))
{
Category_t category = (*cell)->head;
Class_t class = objc_lookup_class (category->class_name);
if (class)
{
DEBUG_PRINTF ("attaching stored categories to object: %s\n",
class->name);
list_remove_head (cell);
if (category->instance_methods)
class_add_method_list (class, category->instance_methods);
if (category->class_methods)
class_add_method_list ((Class_t) class->class_pointer,
category->class_methods);
if (category->protocols)
{
__objc_init_protocols (category->protocols);
__objc_class_add_protocols (class, category->protocols);
}
}
}
if (unclaimed_proto_list && objc_lookup_class ("Protocol"))
{
list_mapcar (unclaimed_proto_list,(void(*)(void*))__objc_init_protocols);
list_free (unclaimed_proto_list);
unclaimed_proto_list = 0;
}
}
/* Sanity check the version of gcc used to compile `module'*/
static void init_check_module_version(Module_t module)
{
if ((module->version != OBJC_VERSION) || (module->size != sizeof (Module)))
{
fprintf (stderr, "Module %s version %d doesn't match runtime %d\n",
module->name, module->version, OBJC_VERSION);
if(module->version > OBJC_VERSION)
fprintf (stderr, "Runtime (libobjc.a) is out of date\n");
else if (module->version < OBJC_VERSION)
fprintf (stderr, "Compiler (gcc) is out of date\n");
else
fprintf (stderr, "Objective C internal error -- bad Module size\n");
abort ();
}
}
static void
__objc_init_protocols (struct objc_protocol_list* protos)
{
int i;
Class_t proto_class;
if (! protos)
return;
proto_class = objc_lookup_class("Protocol");
if (proto_class == 0 && ! list_find (&unclaimed_proto_list, protos))
{
unclaimed_proto_list = list_cons (protos, unclaimed_proto_list);
return;
}
assert (protos->next == 0); /* only single ones allowed */
for(i = 0; i < protos->count; i++)
{
if (((int)((id)protos->list[i])->class_pointer) == PROTOCOL_VERSION)
((id)protos->list[i])->class_pointer = proto_class;
else
{
fprintf (stderr,
"Version %d doesn't protocol version %d\n",
((int)((id)protos->list[i])->class_pointer),
PROTOCOL_VERSION);
abort ();
}
}
}
static void __objc_class_add_protocols (Class_t class,
struct objc_protocol_list* protos)
{
#ifndef NeXT_OBJC /* force class Protocol to be linked in */
extern char* __objc_class_name_Protocol;
char* x = __objc_class_name_Protocol;
#endif
/* Well... */
if (! protos)
return;
/* Add it... */
protos->next = class->protocols;
class->protocols = protos;
}
/* GNU Objective C Runtime Miscellanious
Copyright (C) 1993 Free Software Foundation, Inc.
Author: Kresten Krab Thorup
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the 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 of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
GNU CC; see the file COPYING. If not, write to the 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 "runtime.h"
void* malloc(size_t);
void* realloc(void*, size_t);
void objc_error(id object, const char* fmt, va_list);
void (*_objc_error)(id, const char*, va_list) = objc_error;
/* id (*_objc_object_alloc)(Class_t) = 0; */
/* id (*_objc_object_dispose)(id) = 0; */
/* id (*_objc_object_copy)(id) = 0; */
void
objc_error(id object, const char* fmt, va_list ap)
{
vfprintf (stderr, fmt, ap);
abort ();
}
volatile void
objc_fatal(const char* msg)
{
write(2, msg, strlen(msg));
abort();
}
void*
__objc_xmalloc(size_t size)
{
void* res = malloc(size);
if(!res)
objc_fatal("Virtual memory exhausted\n");
return res;
}
void*
__objc_xrealloc(void* mem, size_t size)
{
void* res = realloc(mem, size);
if(!res)
objc_fatal("Virtual memory exhausted\n");
return res;
}
void*
__objc_xcalloc(size_t nelem, size_t size)
{
void* res = (void*)calloc(nelem, size);
if(!res)
objc_fatal("Virtual memory exhausted\n");
return res;
}
/* GNU Objective C Runtime class related functions
Copyright (C) 1993 Free Software Foundation, Inc.
Author: Kresten Krab Thorup
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the 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 of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
GNU CC; see the file COPYING. If not, write to the 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 "runtime.h" /* the kitchen sink */
id __objc_object_alloc(Class_t);
id __objc_object_dispose(id);
id __objc_object_copy(id);
id (*_objc_object_alloc)(Class_t) = __objc_object_alloc;
id (*_objc_object_dispose)(id) = __objc_object_dispose;
id (*_objc_object_copy)(id) = __objc_object_copy;
id
class_create_instance(Class_t class)
{
id res = (*_objc_object_alloc)(class);
res->class_pointer = class;
return res;
}
id
object_copy(id object)
{
return (*_objc_object_copy)(object);
}
id
object_dispose(id object)
{
return (*_objc_object_dispose)(object);
}
id __objc_object_alloc(Class_t class)
{
return (id)__objc_xmalloc(class->instance_size);
}
id __objc_object_dispose(id object)
{
free(object);
return 0;
}
id __objc_object_copy(id object)
{
id copy = class_create_instance(object->class_pointer);
bcopy(object, copy, object->class_pointer->instance_size);
return copy;
}
/* GNU Objective C Runtime internal declarations
Copyright (C) 1993 Free Software Foundation, Inc.
Author: Kresten Krab Thorup
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the 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 of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
GNU CC; see the file COPYING. If not, write to the 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. */
#ifndef __objc_runtime_INCLUDE_GNU
#define __objc_runtime_INCLUDE_GNU
#include <objc/objc.h> /* core data types */
#include <objc/objc-api.h> /* runtime api functions */
#include <objc/hash.h> /* hash structures */
#include <objc/list.h> /* linear lists */
#include <stdio.h> /* argh! I hate this */
#include <stdarg.h> /* for varargs and va_list's */
#include <stdlib.h>
#include <assert.h>
extern void __objc_add_class_to_hash(Class_t); /* (objc-class.c) */
extern void __objc_init_selector_tables(); /* (objc-sel.c) */
extern void __objc_init_class_tables(); /* (objc-class.c) */
extern void __objc_init_dispatch_tables(); /* (objc-dispatch.c) */
extern void __objc_install_premature_dtable(Class_t); /* (objc-dispatch.c) */
extern void __objc_resolve_class_links(); /* (objc-class.c) */
extern void __objc_register_selectors_from_class(Class_t); /* (objc-sel.c) */
extern void __objc_update_dispatch_table_for_class (Class_t);/* (objc-msg.c) */
/* True when class links has been resolved */
extern BOOL __objc_class_links_resolved;
/* Number of selectors stored in each of the selector tables */
extern int __objc_selector_max_index;
#ifdef DEBUG
#define DEBUG_PRINTF printf
#else
#define DEBUG_PRINTF
#endif
/* standard functions */
int bcopy(void*, void*, size_t);
int vprintf(const char*, va_list);
#endif /* not __objc_runtime_INCLUDE_GNU */
/* Sparse Arrays for Objective C dispatch tables
Copyright (C) 1993 Free Software Foundation, Inc.
Author: Kresten Krab Thorup
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the 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 of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to
the 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. */
#ifndef __sarray_INCLUDE_GNU
#define __sarray_INCLUDE_GNU
#define OBJC_SPARSE3 /* 2-level sparse array */
/* #define OBJC_SPARSE2 */ /* 3-level sparse array */
#ifdef OBJC_SPARSE2
extern const char* __objc_sparse2_id;
#endif
#ifdef OBJC_SPARSE3
extern const char* __objc_sparse3_id;
#endif
#include <stddef.h>
extern int nbuckets; /* for stats */
extern int nindices;
extern int narrays;
extern int idxsize;
#include <assert.h>
#if defined(sparc) || defined(OBJC_SPARSE2)
#define PRECOMPUTE_SELECTORS
#endif
#ifdef OBJC_SPARSE3
/* Buckets are 8 words each */
#define BUCKET_BITS 3
#define BUCKET_SIZE (1<<BUCKET_BITS)
#define BUCKET_MASK (BUCKET_SIZE-1)
/* Indices are 16 words each */
#define INDEX_BITS 4
#define INDEX_SIZE (1<<INDEX_BITS)
#define INDEX_MASK (INDEX_SIZE-1)
#define INDEX_CAPACITY (BUCKET_SIZE*INDEX_SIZE)
#else /* OBJC_SPARSE2 */
/* Buckets are 32 words each */
#define BUCKET_BITS 5
#define BUCKET_SIZE (1<<BUCKET_BITS)
#define BUCKET_MASK (BUCKET_SIZE-1)
#endif /* OBJC_SPARSE2 */
typedef unsigned int sidx;
#ifdef PRECOMPUTE_SELECTORS
struct soffset {
#ifdef OBJC_SPARSE3
unsigned char unused;
unsigned char eoffset;
unsigned char boffset;
unsigned char ioffset;
#else /* OBJC_SPARSE2 */
#ifdef sparc
unsigned int boffset : 30 - BUCKET_BITS;
unsigned int eoffset : BUCKET_BITS;
unsigned int unused : 2;
#else
unsigned short boffset;
unsigned short eoffset;
#endif
#endif /* OBJC_SPARSE2 */
};
union sofftype {
struct soffset off;
sidx idx;
};
#endif /* not PRECOMPUTE_SELECTORS */
void * __objc_xrealloc (void *optr, size_t size);
void * __objc_xmalloc (size_t size);
struct sbucket {
void* elems[BUCKET_SIZE]; /* elements stored in array */
short version; /* used for copy-on-write */
};
#ifdef OBJC_SPARSE3
struct sindex {
struct sbucket* buckets[INDEX_SIZE];
short version;
};
#endif /* OBJC_SPARSE3 */
struct sarray {
#ifdef OBJC_SPARSE3
struct sindex** indices;
struct sindex* empty_index;
#else /* OBJC_SPARSE2 */
struct sbucket** buckets;
#endif /* OBJC_SPARSE2 */
struct sbucket* empty_bucket;
short version;
short ref_count;
struct sarray* is_copy_of;
int capacity;
};
struct sarray* sarray_new(int, void* default_element);
void sarray_free(struct sarray*);
struct sarray* sarray_lazy_copy(struct sarray*);
struct sarray* sarray_hard_copy(struct sarray*); /* ... like the name? */
void sarray_realloc(struct sarray*, int new_size);
void sarray_at_put(struct sarray*, sidx index, void* elem);
void sarray_at_put_safe(struct sarray*, sidx index, void* elem);
#ifdef PRECOMPUTE_SELECTORS
/* Transform soffset values to ints and vica verca */
static inline unsigned int
soffset_decode(sidx index)
{
union sofftype x;
x.idx = index;
#ifdef OBJC_SPARSE3
return x.off.eoffset
+ (x.off.boffset*BUCKET_SIZE)
+ (x.off.ioffset*INDEX_CAPACITY);
#else /* OBJC_SPARSE2 */
return x.off.eoffset + (x.off.boffset*BUCKET_SIZE);
#endif /* OBJC_SPARSE2 */
}
static inline sidx
soffset_encode(unsigned int offset)
{
union sofftype x;
x.off.eoffset = offset%BUCKET_SIZE;
#ifdef OBJC_SPARSE3
x.off.boffset = (offset/BUCKET_SIZE)%INDEX_SIZE;
x.off.ioffset = offset/INDEX_CAPACITY;
#else /* OBJC_SPARSE2 */
x.off.boffset = offset/BUCKET_SIZE;
#endif
return (sidx)x.idx;
}
#else /* not PRECOMPUTE_SELECTORS */
static inline unsigned int
soffset_decode(sidx index)
{
return index;
}
static inline sidx
soffset_encode(unsigned int offset)
{
return offset;
}
#endif /* not PRECOMPUTE_SELECTORS */
/* Get element from the Sparse array `array' at offset `index' */
static inline void* sarray_get(struct sarray* array, sidx index)
{
#ifdef PRECOMPUTE_SELECTORS
union sofftype x;
x.idx = index;
#ifdef OBJC_SPARSE3
return
array->
indices[x.off.ioffset]->
buckets[x.off.boffset]->
elems[x.off.eoffset];
#else /* OBJC_SPARSE2 */
return array->buckets[x.off.boffset]->elems[x.off.eoffset];
#endif /* OBJC_SPARSE2 */
#else /* not PRECOMPUTE_SELECTORS */
return array->
indices[index/INDEX_CAPACITY]->
buckets[(index/BUCKET_SIZE)%INDEX_SIZE]->
elems[index%BUCKET_SIZE];
#endif /* not PRECOMPUTE_SELECTORS */
}
static inline void* sarray_get_safe(struct sarray* array, sidx index)
{
if(soffset_decode(index) < array->capacity)
return sarray_get(array, index);
else
return (array->empty_bucket->elems[0]);
}
#endif /* __sarray_INCLUDE_GNU */
/* GNU Objective C Runtime selector related functions
Copyright (C) 1993 Free Software Foundation, Inc.
Author: Kresten Krab Thorup
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the 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 of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along with
GNU CC; see the file COPYING. If not, write to the 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 "runtime.h"
#include <objc/sarray.h>
/* Initial selector hash table size. Value doesnt matter much */
#define SELECTOR_HASH_SIZE 128
/* Tables mapping selector names to uid and opposite */
static struct sarray* __objc_selector_array = 0; /* uid -> name */
static cache_ptr __objc_selector_hash = 0; /* name -> uid */
static void register_selectors_from_list(MethodList_t);
/* Number of selectors stored in each of the above tables */
int __objc_selector_max_index = 0;
void __objc_init_selector_tables()
{
__objc_selector_array = sarray_new (SELECTOR_HASH_SIZE, 0);
__objc_selector_hash
= hash_new (SELECTOR_HASH_SIZE,
(hash_func_type) hash_string,
(compare_func_type) compare_strings);
}
/* This routine is given a class and records all of the methods in its class
structure in the record table. */
void
__objc_register_selectors_from_class (Class_t class)
{
MethodList_t method_list;
method_list = class->methods;
while (method_list)
{
register_selectors_from_list (method_list);
method_list = method_list->method_next;
}
}
/* This routine is given a list of methods and records each of the methods in
the record table. This is the routine that does the actual recording
work.
This one is only called for Class objects. For categories,
class_add_method_list is called.
*/
static void
register_selectors_from_list (MethodList_t method_list)
{
int i = 0;
while (i < method_list->method_count)
{
Method_t method = &method_list->method_list[i];
method->method_name = sel_register_name ((char*)method->method_name);
i += 1;
}
}
/* return selector representing name */
SEL
sel_get_uid (const char *name)
{
return (SEL) hash_value_for_key (__objc_selector_hash, name);
}
/* Get name of selector. If selector is unknown, the empty string ""
is returned */
const char*
sel_get_name (SEL selector)
{
if ((soffset_decode((unsigned)selector) > 0)
&& (soffset_decode((unsigned)selector) <= __objc_selector_max_index))
return sarray_get (__objc_selector_array, (sidx) selector);
else
return NULL;
}
BOOL
sel_is_mapped (SEL selector)
{
unsigned int idx = soffset_decode ((sidx)selector);
return ((idx > 0) && (idx <= __objc_selector_max_index));
}
#ifdef OBJC_SPARSE_LOOKUP
/* The uninstalled dispatch table */
extern struct sarray* __objc_uninstalled_dtable;
#endif
/* Store the passed selector name in the selector record and return its
selector value (value returned by sel_get_uid). */
SEL
sel_register_name (const char *sel)
{
SEL j;
sidx i;
if ((j = sel_get_uid ((const char *) sel)))
return j;
/* Save the selector name. */
__objc_selector_max_index += 1;
i = soffset_encode(__objc_selector_max_index);
DEBUG_PRINTF ("Record selector %s as: %#x\n", sel, i);
sarray_at_put_safe (__objc_selector_array, i, (void *) sel);
hash_add (&__objc_selector_hash, (void *) sel, (void *) i);
#ifdef OBJC_SPARSE_LOOKUP
sarray_realloc(__objc_uninstalled_dtable, __objc_selector_max_index+1);
#endif
return (SEL) i;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment