-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfinalizer.c
45 lines (32 loc) · 1.25 KB
/
finalizer.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
/*
* Copyright (c) 2010, the Short-term Memory Project Authors.
* All rights reserved. Please see the AUTHORS file for details.
* Use of this source code is governed by a BSD license that
* can be found in the LICENSE file.
*/
#include "finalizer.h"
//finalizer table contains function pointers;
static int (*finalizer_table[SCM_FINALIZER_TABLE_SIZE])(void*);
//bump pointer on the finalizer table
static int finalizer_index = 0;
int scm_register_finalizer(int(*scm_finalizer)(void*)) {
int index = atomic_int_exchange_and_add(&finalizer_index, 1);
if (index >= SCM_FINALIZER_TABLE_SIZE) return -1; //error, table full
finalizer_table[index] = scm_finalizer;
return index;
}
void scm_set_finalizer(void *ptr, int scm_finalizer_id) {
//set function index
object_header_t *o = OBJECT_HEADER(ptr);
o->finalizer_index = scm_finalizer_id;
}
int run_finalizer(object_header_t *o) {
//INVARIANT: object o is already expired
if (o->finalizer_index == -1) return 0; //object has no finalizer
void *ptr = PAYLOAD_OFFSET(o);
int (*finalizer)(void*);
//get function pointer to objects finalizer
finalizer = finalizer_table[o->finalizer_index];
//run finalizer and return the result of it
return (*finalizer)(ptr);
}