2010-12-03 05:34:57 +01: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"
|
2011-10-27 01:57:58 +02:00
|
|
|
#include "arch.h"
|
2010-12-03 05:34:57 +01:00
|
|
|
#include "malloc.h"
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
enum { debug = 0 };
|
2010-12-17 07:42:06 +01:00
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
typedef struct Fin Fin;
|
|
|
|
struct Fin
|
2010-12-17 07:42:06 +01:00
|
|
|
{
|
2011-10-27 01:57:58 +02:00
|
|
|
void (*fn)(void*);
|
|
|
|
const struct __go_func_type *ft;
|
|
|
|
};
|
2010-12-03 05:34:57 +01: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
|
|
|
|
{
|
2011-10-27 01:57:58 +02:00
|
|
|
Lock;
|
|
|
|
void **fkey;
|
|
|
|
Fin *val;
|
2010-12-03 05:34:57 +01: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
|
|
|
|
};
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
#define TABSZ 17
|
|
|
|
#define TAB(p) (&fintab[((uintptr)(p)>>3)%TABSZ])
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
Fintab;
|
|
|
|
uint8 pad[0 /* CacheLineSize - sizeof(Fintab) */];
|
|
|
|
} fintab[TABSZ];
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
static void
|
2011-10-27 01:57:58 +02:00
|
|
|
addfintab(Fintab *t, void *k, void (*fn)(void*), const struct __go_func_type *ft)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
int32 i, j;
|
|
|
|
|
|
|
|
i = (uintptr)k % (uintptr)t->max;
|
|
|
|
for(j=0; j<t->max; j++) {
|
2011-10-27 01:57:58 +02:00
|
|
|
if(t->fkey[i] == nil) {
|
2010-12-03 05:34:57 +01:00
|
|
|
t->nkey++;
|
|
|
|
goto ret;
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
if(t->fkey[i] == (void*)-1) {
|
2010-12-03 05:34:57 +01:00
|
|
|
t->ndead--;
|
|
|
|
goto ret;
|
|
|
|
}
|
|
|
|
if(++i == t->max)
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cannot happen - table is known to be non-full
|
|
|
|
runtime_throw("finalizer table inconsistent");
|
|
|
|
|
|
|
|
ret:
|
2011-10-27 01:57:58 +02:00
|
|
|
t->fkey[i] = k;
|
|
|
|
t->val[i].fn = fn;
|
|
|
|
t->val[i].ft = ft;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
static bool
|
|
|
|
lookfintab(Fintab *t, void *k, bool del, Fin *f)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
int32 i, j;
|
|
|
|
|
|
|
|
if(t->max == 0)
|
2011-10-27 01:57:58 +02:00
|
|
|
return false;
|
2010-12-03 05:34:57 +01:00
|
|
|
i = (uintptr)k % (uintptr)t->max;
|
|
|
|
for(j=0; j<t->max; j++) {
|
2011-10-27 01:57:58 +02:00
|
|
|
if(t->fkey[i] == nil)
|
|
|
|
return false;
|
|
|
|
if(t->fkey[i] == k) {
|
|
|
|
if(f)
|
|
|
|
*f = t->val[i];
|
2010-12-03 05:34:57 +01:00
|
|
|
if(del) {
|
2011-10-27 01:57:58 +02:00
|
|
|
t->fkey[i] = (void*)-1;
|
|
|
|
t->val[i].fn = nil;
|
|
|
|
t->val[i].ft = nil;
|
2010-12-03 05:34:57 +01:00
|
|
|
t->ndead++;
|
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
return true;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
if(++i == t->max)
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cannot happen - table is known to be non-full
|
|
|
|
runtime_throw("finalizer table inconsistent");
|
2011-10-27 01:57:58 +02:00
|
|
|
return false;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
static void
|
|
|
|
resizefintab(Fintab *tab)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
Fintab newtab;
|
2011-10-27 01:57:58 +02:00
|
|
|
void *k;
|
2010-12-03 05:34:57 +01:00
|
|
|
int32 i;
|
2011-10-27 01:57:58 +02:00
|
|
|
|
|
|
|
runtime_memclr((byte*)&newtab, sizeof newtab);
|
|
|
|
newtab.max = tab->max;
|
|
|
|
if(newtab.max == 0)
|
|
|
|
newtab.max = 3*3*3;
|
|
|
|
else if(tab->ndead < tab->nkey/2) {
|
|
|
|
// grow table if not many dead values.
|
|
|
|
// otherwise just rehash into table of same size.
|
|
|
|
newtab.max *= 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
newtab.fkey = runtime_mallocgc(newtab.max*sizeof newtab.fkey[0], FlagNoPointers, 0, 1);
|
|
|
|
newtab.val = runtime_mallocgc(newtab.max*sizeof newtab.val[0], 0, 0, 1);
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
for(i=0; i<tab->max; i++) {
|
|
|
|
k = tab->fkey[i];
|
|
|
|
if(k != nil && k != (void*)-1)
|
|
|
|
addfintab(&newtab, k, tab->val[i].fn, tab->val[i].ft);
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-10-27 01:57:58 +02:00
|
|
|
|
|
|
|
runtime_free(tab->fkey);
|
|
|
|
runtime_free(tab->val);
|
|
|
|
|
|
|
|
tab->fkey = newtab.fkey;
|
|
|
|
tab->val = newtab.val;
|
|
|
|
tab->nkey = newtab.nkey;
|
|
|
|
tab->ndead = newtab.ndead;
|
|
|
|
tab->max = newtab.max;
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
bool
|
|
|
|
runtime_addfinalizer(void *p, void (*f)(void*), const struct __go_func_type *ft)
|
|
|
|
{
|
|
|
|
Fintab *tab;
|
|
|
|
byte *base;
|
|
|
|
|
|
|
|
if(debug) {
|
|
|
|
if(!runtime_mlookup(p, &base, nil, nil) || p != base)
|
|
|
|
runtime_throw("addfinalizer on invalid pointer");
|
|
|
|
}
|
|
|
|
|
|
|
|
tab = TAB(p);
|
|
|
|
runtime_lock(tab);
|
2010-12-03 05:34:57 +01:00
|
|
|
if(f == nil) {
|
2011-10-27 01:57:58 +02:00
|
|
|
if(lookfintab(tab, p, true, nil))
|
|
|
|
runtime_setblockspecial(p, false);
|
2011-11-28 06:45:49 +01:00
|
|
|
runtime_unlock(tab);
|
|
|
|
return true;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
if(lookfintab(tab, p, false, nil)) {
|
2011-11-28 06:45:49 +01:00
|
|
|
runtime_unlock(tab);
|
|
|
|
return false;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
if(tab->nkey >= tab->max/2+tab->max/4) {
|
2010-12-03 05:34:57 +01:00
|
|
|
// keep table at most 3/4 full:
|
|
|
|
// allocate new table and rehash.
|
2011-10-27 01:57:58 +02:00
|
|
|
resizefintab(tab);
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
addfintab(tab, p, f, ft);
|
|
|
|
runtime_setblockspecial(p, true);
|
|
|
|
runtime_unlock(tab);
|
2011-11-28 06:45:49 +01:00
|
|
|
return true;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// get finalizer; if del, delete finalizer.
|
2011-10-27 01:57:58 +02:00
|
|
|
// caller is responsible for updating RefHasFinalizer (special) bit.
|
|
|
|
bool
|
|
|
|
runtime_getfinalizer(void *p, bool del, void (**fn)(void*), const struct __go_func_type **ft)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
2011-10-27 01:57:58 +02:00
|
|
|
Fintab *tab;
|
|
|
|
bool res;
|
|
|
|
Fin f;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
tab = TAB(p);
|
|
|
|
runtime_lock(tab);
|
|
|
|
res = lookfintab(tab, p, del, &f);
|
|
|
|
runtime_unlock(tab);
|
|
|
|
if(res==false)
|
|
|
|
return false;
|
|
|
|
*fn = f.fn;
|
|
|
|
*ft = f.ft;
|
|
|
|
return true;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
runtime_walkfintab(void (*fn)(void*), void (*scan)(byte *, int64))
|
|
|
|
{
|
|
|
|
void **key;
|
|
|
|
void **ekey;
|
2011-10-27 01:57:58 +02:00
|
|
|
int32 i;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
for(i=0; i<TABSZ; i++) {
|
|
|
|
runtime_lock(&fintab[i]);
|
|
|
|
key = fintab[i].fkey;
|
|
|
|
ekey = key + fintab[i].max;
|
|
|
|
for(; key < ekey; key++)
|
|
|
|
if(*key != nil && *key != ((void*)-1))
|
|
|
|
fn(*key);
|
|
|
|
scan((byte*)&fintab[i].fkey, sizeof(void*));
|
|
|
|
scan((byte*)&fintab[i].val, sizeof(void*));
|
|
|
|
runtime_unlock(&fintab[i]);
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|