first commit
commit
813b82ef47
@ -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 <YSObject> {
|
||||
|
||||
}
|
||||
|
||||
-(Class) class;
|
||||
-(Class) superclass;
|
||||
-(id) self;
|
||||
//-(YSString *) description;
|
||||
+(id) copyWithZone: (YSZone *) zone;
|
||||
-(YSZone *) zone;
|
||||
+(YSZone *) zone;
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
@ -0,0 +1,165 @@
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#include <inttypes.h>
|
||||
#include <objc/runtime.h>
|
||||
#include <objc/objc.h>
|
||||
#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
|
||||
@ -0,0 +1,27 @@
|
||||
#ifndef YSSTRING_H
|
||||
#define YSSTRING_H
|
||||
|
||||
#import <YSObject.h>
|
||||
#import <YSRange.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
@implementation YSString : YSObject <YSCopying, YSMutableCopying>
|
||||
+(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
|
||||
@ -0,0 +1,25 @@
|
||||
#ifndef YSZONE_H
|
||||
#define YSZONE_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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
|
||||
@ -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);
|
||||
}
|
||||
Loading…
Reference in New Issue