commit 813b82ef478dc919a3308ccef470c208c7888da1 Author: sandyx86 Date: Tue Jul 23 08:52:00 2024 -0500 first commit diff --git a/YSObject.h b/YSObject.h new file mode 100644 index 0000000..19f3081 --- /dev/null +++ b/YSObject.h @@ -0,0 +1,52 @@ +#ifndef YSOBJECT_H +#define YSOBJECT_H + +#import "YSZone.h" + +typedef unsigned int YSUInteger; + +@protocol YSObject +-(Class) class; +-(Class) superclass; +//-(BOOL) isEqual: (id)anObject; +//-(BOOL) isKindOfClass: (Class)aClass; +//-(BOOL) isMemberOfClass: (Class)aClass; +//-(BOOL) isProxy; +//-(YSUInteger) hash; +-(id) self; + +//-(id) performSelector: (SEL)aSelector; + +//-(id) performSelector: (SEL)aSelector +// withObject: (id)anObject; + +//-(id) performSelector: (SEL)aSelector +// withObject: (id)object1 +// withObject: (id)object2; + +//-(BOOL) respondsToSelector: (SEL)aSelector; +//-(BOOL) conformsToProtocol: (Protocol *)aProtocol; + +//-(id) retain; +//-(oneway void) release; +//-(id) autorelease; +//-(YSUInteger) retainCount; +//-(YSString *) description; +-(YSZone *)zone; +@end + +@interface YSObject { + +} + +-(Class) class; +-(Class) superclass; +-(id) self; +//-(YSString *) description; ++(id) copyWithZone: (YSZone *) zone; +-(YSZone *) zone; ++(YSZone *) zone; + +@end + +#endif \ No newline at end of file diff --git a/YSObject.m b/YSObject.m new file mode 100644 index 0000000..5be872d --- /dev/null +++ b/YSObject.m @@ -0,0 +1,165 @@ +#include +#include +#include +#include +#include +#import "YSObject.h" +#import "YSZone.h" + +typedef struct obj_layout_unpadded { + int32_t retained; +} unp; +#define UNP sizeof(unp) + +#ifndef __BIGGEST_ALIGNMENT__ +#define __BIGGEST_ALIGNMENT__ sizeof(void *) + sizeof(void *) +#endif + +struct obj_layout { + char padding[__BIGGEST_ALIGNMENT__ - ((UNP % __BIGGEST_ALIGNMENT__) + ? (UNP % __BIGGEST_ALIGNMENT__) : __BIGGEST_ALIGNMENT__)]; + int32_t retained; +}; +typedef struct obj_layout *obj; + + +inline id YSAllocateObject(Class theClass, YSUInteger extraBytes, YSZone *zone) { + id new; + int size; + + size = class_getInstanceSize(theClass) + extraBytes + sizeof(struct obj_layout); + if (zone == 0) { + zone = YSDefaultMallocZone(); + } + + new = YSZoneMalloc(zone, size); + if (new != nil) { + memset(new, 0, size); + new = (id) &( (obj)new )[1]; + object_setClass(new, theClass); + //AADD + } + + return new; +} + +inline id YSDeallocateObject(id theObject) { + Class theClass = object_getClass(theObject); + + if ( (theObject != nil) && !class_isMetaClass(theClass) ) { + object_dispose(theObject); + } +} + +@implementation YSObject + +/* ++(void) load { + //impl + return; +}*/ + +-(id) init { + return self; +} + +-(id) self { + return self; +} + ++(void) initialize { + //implement later + return; +} + ++(id) allocWithZone: (YSZone *) zone { + return YSAllocateObject(self, 0, zone); +} + ++(id) copyWithZone: (YSZone *) zone { + return self; +} + ++(id) alloc { + return [self allocWithZone: YSDefaultMallocZone()]; +} + ++(id) new { + return [[self alloc] init]; +} + +-(Class) class { + return object_getClass(self); +} + +-(void) dealloc { + YSDeallocateObject(self); +} + +-(id) copy { + return [(id)self copyWithZone: YSDefaultMallocZone()]; +} + ++(Class) superclass { + return class_getSuperclass(self); +} +-(Class) superclass { + return class_getSuperclass(object_getClass(self)); +} + ++(Class) class { + return self; +} + +-(BOOL) isEqual: (id) anObject { + return (self == anObject); +} + ++(BOOL) isKindOfClass: (Class) theClass { + if (theClass == [YSObject class]) { + return YES; + } + + return NO; +} + +-(BOOL) isProxy { + return NO; +} + +-(id) performSelector: (SEL) theSelector { + IMP msg; + + if (theSelector == 0) { + //raise exception + perror("null selector given"); + return; + } + + msg = objc_msg_lookup(self, theSelector); + if (!msg) { + //raise exception + perror("invalid selector"); + return nil; + } + + return (*msg)(self, theSelector); +} + +-(YSZone *) zone { + return YSZoneFromPointer(self); +} + ++(YSZone *) zone { + return YSDefaultMallocZone(); +} + +//dummy methods ++(oneway void) release { + return; +} + ++(id) retain { + return self; +} +@end diff --git a/YSString.h b/YSString.h new file mode 100644 index 0000000..52d64e9 --- /dev/null +++ b/YSString.h @@ -0,0 +1,27 @@ +#ifndef YSSTRING_H +#define YSSTRING_H + +#import +#import + +#include + +@implementation YSString : YSObject ++(instancetype) string; ++(instancetype) stringWithCharacters: (const unichar*) chars + length: (YSUInteger) length; ++(instancetype) stringWithCString: (const char *) byteString + length: (YSUInteger) length; ++(instancetype) stringWithCString: (const char *) byteString; +//+(instancetype) stringWithFormat: (YSString *)format, ... ++(instancetype) stringWithContentsOfFile: (YSString *) path; +-(instancetype) init; +-(YSUInteger) length; +-(unichar) characterAtIndex: (YSUInteger) index; +-(void) getCharacters: (unichar *) buffer; +-(void) getCharacters: (unichar *) buffer + range: (YSRange) aRange; +@end + + +#endif \ No newline at end of file diff --git a/YSZone.h b/YSZone.h new file mode 100644 index 0000000..21c4c1e --- /dev/null +++ b/YSZone.h @@ -0,0 +1,25 @@ +#ifndef YSZONE_H +#define YSZONE_H + +#include +#include + +#define BOOL int +#define true YES +#define false NO + +typedef unsigned int YSUInteger; +typedef struct _YSZone YSZone; + +struct _YSZone { + void *(*malloc)(struct _YSZone *zone, size_t size); + void (*free)(struct _YSZone *zone, void *ptr); + int (*lookup)(struct _YSZone *zone, void *ptr); + YSZone *next; +}; + +YSZone *YSDefaultMallocZone(void); +YSZone *YSZoneFromPointer(void *ptr); +void *YSZoneMalloc(YSZone *zone, YSUInteger size); + +#endif \ No newline at end of file diff --git a/YSZone.m b/YSZone.m new file mode 100644 index 0000000..0a9196f --- /dev/null +++ b/YSZone.m @@ -0,0 +1,56 @@ +#import "YSZone.h" + +static YSZone *zone_list = 0; + +static void *default_malloc(YSZone *zone, size_t size) { + void *mem; + + mem = malloc(size); + if (mem != NULL) { + return mem; + } + + //raise oom exception + + return 0; +} + +static void default_free(YSZone *zone, void *ptr) { + free(ptr); +} + +static YSZone default_zone = { + default_malloc, default_free, +}; + +YSZone *YSDefaultMallocZone(void) { + return &default_zone; +} + +YSZone *YSZoneFromPointer(void *ptr) { + YSZone *zone; + + if (ptr == 0) + return 0; + + if (zone_list == 0) + return &default_zone; + + //mutex lock + + for (zone = zone_list; zone != 0; zone = zone->next) { + if ( (zone->lookup)(zone, ptr) == 1 ) { break; } + } + + //mutex unlock + + return (zone == 0) ? &default_zone : zone; +} + +void *YSZoneMalloc(YSZone *zone, YSUInteger size) { + if (!zone) { + zone = YSDefaultMallocZone(); + } + + return (zone->malloc)(zone, size); +} \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..c208283 --- /dev/null +++ b/makefile @@ -0,0 +1,7 @@ +SHELL=/bin/sh +CC=gcc + +yeslib.a: + $(CC) YSObject.m -c + $(CC) YSZone.m -c + ar rcs libyeslib.a YSObject.o YSZone.o \ No newline at end of file