-
Notifications
You must be signed in to change notification settings - Fork 1
/
object.c
70 lines (57 loc) · 1.3 KB
/
object.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <string.h>
#include <stdlib.h>
#include "object.h"
hash_t hash(void *obj, int size) {
hash_t hash = 0;
int c;
for(c = 0; c < size; c++)
hash = *((char *) obj + c) + (hash << 6) + (hash << 16) - hash;
return hash;
}
void retain(Object *obj) {
obj->refcount++;
}
void release(Object *obj) {
obj->refcount--;
if(obj->refcount == 0) {
obj->class->free(obj);
}
}
bool ostring_equal(Object *self, Object *other) {
if ((self->class == other->class) && (strcmp(((OString*)self)->str, ((OString*)other)->str) == 0))
return true;
else
return false;
}
void ostring_free(Object *obj) {
OString *s = (OString*)obj;
//free(s->str);
free(s);
}
ObjectType ostring_type = {ostring_equal, ostring_free};
OString *new_ostring(char *str) {
OString *ostr = malloc(sizeof(OString));
ostr->hash = hash(str, strlen(str));
ostr->class = &ostring_type;
ostr->str = str;
ostr->refcount = 1;
return ostr;
}
bool oint_equal(Object *self, Object *other) {
if(self->class == other->class && ((OInt*)self)->n == ((OInt*)other)->n)
return true;
else
return false;
}
void oint_free(Object *obj) {
free(obj);
}
ObjectType oint_type = {oint_equal, oint_free};
OInt *new_oint(int n) {
OInt *on = malloc(sizeof(OInt));
on->hash = (hash_t)n;
on->class = &oint_type;
on->n = n;
on->refcount = 1;
return on;
}