2010-02-03 17:31:34 -07:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "runtime.h"
|
|
|
|
#include "malloc.h"
|
|
|
|
|
2010-03-15 21:29:17 -06:00
|
|
|
Lock finlock;
|
|
|
|
|
2010-02-03 17:31:34 -07:00
|
|
|
// Finalizer hash table. Direct hash, linear scan, at most 3/4 full.
|
|
|
|
// Table size is power of 3 so that hash can be key % max.
|
|
|
|
// Key[i] == (void*)-1 denotes free but formerly occupied entry
|
|
|
|
// (doesn't stop the linear scan).
|
|
|
|
// Key and val are separate tables because the garbage collector
|
|
|
|
// must be instructed to ignore the pointers in key but follow the
|
|
|
|
// pointers in val.
|
|
|
|
typedef struct Fintab Fintab;
|
|
|
|
struct Fintab
|
|
|
|
{
|
|
|
|
void **key;
|
2010-03-26 15:15:30 -06:00
|
|
|
Finalizer **val;
|
2010-02-03 17:31:34 -07:00
|
|
|
int32 nkey; // number of non-nil entries in key
|
|
|
|
int32 ndead; // number of dead (-1) entries in key
|
|
|
|
int32 max; // size of key, val allocations
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2010-03-26 15:15:30 -06:00
|
|
|
addfintab(Fintab *t, void *k, Finalizer *v)
|
2010-02-03 17:31:34 -07:00
|
|
|
{
|
|
|
|
int32 i, j;
|
2010-02-10 01:00:12 -07:00
|
|
|
|
2010-02-03 17:31:34 -07:00
|
|
|
i = (uintptr)k % (uintptr)t->max;
|
|
|
|
for(j=0; j<t->max; j++) {
|
|
|
|
if(t->key[i] == nil) {
|
|
|
|
t->nkey++;
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
if(t->key[i] == (void*)-1) {
|
|
|
|
t->ndead--;
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
if(++i == t->max)
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cannot happen - table is known to be non-full
|
|
|
|
throw("finalizer table inconsistent");
|
|
|
|
|
|
|
|
ret:
|
|
|
|
t->key[i] = k;
|
2010-03-26 15:15:30 -06:00
|
|
|
t->val[i] = v;
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
|
|
|
|
2010-03-26 15:15:30 -06:00
|
|
|
static Finalizer*
|
|
|
|
lookfintab(Fintab *t, void *k, bool del)
|
2010-02-03 17:31:34 -07:00
|
|
|
{
|
|
|
|
int32 i, j;
|
2010-03-26 15:15:30 -06:00
|
|
|
Finalizer *v;
|
2010-02-10 01:00:12 -07:00
|
|
|
|
2010-02-03 17:31:34 -07:00
|
|
|
if(t->max == 0)
|
|
|
|
return nil;
|
|
|
|
i = (uintptr)k % (uintptr)t->max;
|
|
|
|
for(j=0; j<t->max; j++) {
|
|
|
|
if(t->key[i] == nil)
|
|
|
|
return nil;
|
|
|
|
if(t->key[i] == k) {
|
2010-03-26 15:15:30 -06:00
|
|
|
v = t->val[i];
|
2010-02-03 17:31:34 -07:00
|
|
|
if(del) {
|
|
|
|
t->key[i] = (void*)-1;
|
2010-03-26 15:15:30 -06:00
|
|
|
t->val[i] = nil;
|
2010-02-03 17:31:34 -07:00
|
|
|
t->ndead++;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
if(++i == t->max)
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cannot happen - table is known to be non-full
|
|
|
|
throw("finalizer table inconsistent");
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Fintab fintab;
|
|
|
|
|
|
|
|
// add finalizer; caller is responsible for making sure not already in table
|
|
|
|
void
|
2010-02-08 22:41:54 -07:00
|
|
|
addfinalizer(void *p, void (*f)(void*), int32 nret)
|
2010-02-03 17:31:34 -07:00
|
|
|
{
|
|
|
|
Fintab newtab;
|
|
|
|
int32 i;
|
2010-02-10 01:00:12 -07:00
|
|
|
uint32 *ref;
|
|
|
|
byte *base;
|
2010-03-26 15:15:30 -06:00
|
|
|
Finalizer *e;
|
|
|
|
|
|
|
|
e = nil;
|
|
|
|
if(f != nil) {
|
|
|
|
e = mal(sizeof *e);
|
|
|
|
e->fn = f;
|
|
|
|
e->nret = nret;
|
|
|
|
}
|
2010-02-10 01:00:12 -07:00
|
|
|
|
2010-03-15 21:29:17 -06:00
|
|
|
lock(&finlock);
|
|
|
|
if(!mlookup(p, &base, nil, nil, &ref) || p != base) {
|
|
|
|
unlock(&finlock);
|
2010-02-10 01:00:12 -07:00
|
|
|
throw("addfinalizer on invalid pointer");
|
2010-03-15 21:29:17 -06:00
|
|
|
}
|
2010-02-10 01:00:12 -07:00
|
|
|
if(f == nil) {
|
|
|
|
if(*ref & RefHasFinalizer) {
|
2010-03-26 15:15:30 -06:00
|
|
|
lookfintab(&fintab, p, 1);
|
2010-02-10 01:00:12 -07:00
|
|
|
*ref &= ~RefHasFinalizer;
|
|
|
|
}
|
2010-03-15 21:29:17 -06:00
|
|
|
unlock(&finlock);
|
2010-02-10 01:00:12 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-15 21:29:17 -06:00
|
|
|
if(*ref & RefHasFinalizer) {
|
|
|
|
unlock(&finlock);
|
2010-02-10 01:00:12 -07:00
|
|
|
throw("double finalizer");
|
2010-03-15 21:29:17 -06:00
|
|
|
}
|
2010-02-10 01:00:12 -07:00
|
|
|
*ref |= RefHasFinalizer;
|
2010-02-03 17:31:34 -07:00
|
|
|
|
|
|
|
if(fintab.nkey >= fintab.max/2+fintab.max/4) {
|
|
|
|
// keep table at most 3/4 full:
|
|
|
|
// allocate new table and rehash.
|
2010-02-10 01:00:12 -07:00
|
|
|
|
2010-02-03 17:31:34 -07:00
|
|
|
runtime_memclr((byte*)&newtab, sizeof newtab);
|
|
|
|
newtab.max = fintab.max;
|
|
|
|
if(newtab.max == 0)
|
|
|
|
newtab.max = 3*3*3;
|
|
|
|
else if(fintab.ndead < fintab.nkey/2) {
|
|
|
|
// grow table if not many dead values.
|
|
|
|
// otherwise just rehash into table of same size.
|
|
|
|
newtab.max *= 3;
|
|
|
|
}
|
2010-02-10 01:00:12 -07:00
|
|
|
|
2010-04-09 16:30:40 -06:00
|
|
|
newtab.key = mallocgc(newtab.max*sizeof newtab.key[0], RefNoPointers, 0, 1);
|
|
|
|
newtab.val = mallocgc(newtab.max*sizeof newtab.val[0], 0, 0, 1);
|
2010-02-10 01:00:12 -07:00
|
|
|
|
2010-02-03 17:31:34 -07:00
|
|
|
for(i=0; i<fintab.max; i++) {
|
|
|
|
void *k;
|
2010-02-10 01:00:12 -07:00
|
|
|
|
2010-02-03 17:31:34 -07:00
|
|
|
k = fintab.key[i];
|
|
|
|
if(k != nil && k != (void*)-1)
|
2010-03-26 15:15:30 -06:00
|
|
|
addfintab(&newtab, k, fintab.val[i]);
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
|
|
|
free(fintab.key);
|
|
|
|
free(fintab.val);
|
|
|
|
fintab = newtab;
|
|
|
|
}
|
2010-02-10 01:00:12 -07:00
|
|
|
|
2010-03-26 15:15:30 -06:00
|
|
|
addfintab(&fintab, p, e);
|
2010-03-15 21:29:17 -06:00
|
|
|
unlock(&finlock);
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
|
|
|
|
2010-02-10 01:00:12 -07:00
|
|
|
// get finalizer; if del, delete finalizer.
|
|
|
|
// caller is responsible for updating RefHasFinalizer bit.
|
2010-03-26 15:15:30 -06:00
|
|
|
Finalizer*
|
|
|
|
getfinalizer(void *p, bool del)
|
2010-02-03 17:31:34 -07:00
|
|
|
{
|
2010-03-26 15:15:30 -06:00
|
|
|
Finalizer *f;
|
2010-03-15 21:29:17 -06:00
|
|
|
|
|
|
|
lock(&finlock);
|
2010-03-26 15:15:30 -06:00
|
|
|
f = lookfintab(&fintab, p, del);
|
2010-03-15 21:29:17 -06:00
|
|
|
unlock(&finlock);
|
|
|
|
return f;
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
2010-03-26 15:15:30 -06:00
|
|
|
|
|
|
|
void
|
|
|
|
walkfintab(void (*fn)(void*))
|
|
|
|
{
|
|
|
|
void **key;
|
|
|
|
void **ekey;
|
|
|
|
|
|
|
|
lock(&finlock);
|
|
|
|
key = fintab.key;
|
|
|
|
ekey = key + fintab.max;
|
|
|
|
for(; key < ekey; key++)
|
|
|
|
if(*key != nil && *key != ((void*)-1))
|
|
|
|
fn(*key);
|
|
|
|
unlock(&finlock);
|
|
|
|
}
|