-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.c
More file actions
44 lines (33 loc) · 812 Bytes
/
object.c
File metadata and controls
44 lines (33 loc) · 812 Bytes
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
#include "object.h"
#include <stdint.h>
#include <malloc.h>
void* object_create(size_t type_size)
{
void* object = malloc(type_size + sizeof(struct ref));
struct ref* object_ref = (struct ref*)(object);
object_ref->count = 0;
object_ref->free = 0;
return ((uint8_t*)object) + sizeof(struct ref);
}
void* object_ref(void* object)
{
if (!object)
{
return NULL;
}
struct ref* object_ref = (void*)(((uint8_t*)object) - sizeof(struct ref));
++object_ref->count;
return object;
}
void object_deref(void* object)
{
struct ref* object_ref = (void*)(((uint8_t*)object) - sizeof(struct ref));
if (--object_ref->count == 0)
{
if (object_ref->free)
{
object_ref->free(object_ref);
}
free(object);
}
}