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"
|
2011-12-16 13:33:58 -07:00
|
|
|
#include "arch_GOARCH.h"
|
2010-02-03 17:31:34 -07:00
|
|
|
#include "malloc.h"
|
2013-08-14 12:54:31 -06:00
|
|
|
#include "type.h"
|
2010-02-03 17:31:34 -07:00
|
|
|
|
2011-10-06 09:42:51 -06:00
|
|
|
enum { debug = 0 };
|
|
|
|
|
|
|
|
typedef struct Fin Fin;
|
|
|
|
struct Fin
|
|
|
|
{
|
2013-02-21 15:01:13 -07:00
|
|
|
FuncVal *fn;
|
2012-09-24 12:58:34 -06:00
|
|
|
uintptr nret;
|
2013-08-14 12:54:31 -06:00
|
|
|
Type *fint;
|
|
|
|
PtrType *ot;
|
2011-10-06 09:42:51 -06:00
|
|
|
};
|
2010-03-15 21:29:17 -06:00
|
|
|
|
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
|
|
|
|
{
|
2011-10-06 09:42:51 -06:00
|
|
|
Lock;
|
2010-02-03 17:31:34 -07:00
|
|
|
void **key;
|
2011-10-06 09:42:51 -06:00
|
|
|
Fin *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
|
|
|
|
};
|
|
|
|
|
2011-10-06 09:42:51 -06:00
|
|
|
#define TABSZ 17
|
|
|
|
#define TAB(p) (&fintab[((uintptr)(p)>>3)%TABSZ])
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
Fintab;
|
|
|
|
uint8 pad[CacheLineSize - sizeof(Fintab)];
|
|
|
|
} fintab[TABSZ];
|
|
|
|
|
2010-02-03 17:31:34 -07:00
|
|
|
static void
|
2013-08-14 12:54:31 -06:00
|
|
|
addfintab(Fintab *t, void *k, FuncVal *fn, uintptr nret, Type *fint, PtrType *ot)
|
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
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·throw("finalizer table inconsistent");
|
2010-02-03 17:31:34 -07:00
|
|
|
|
|
|
|
ret:
|
|
|
|
t->key[i] = k;
|
2011-10-06 09:42:51 -06:00
|
|
|
t->val[i].fn = fn;
|
|
|
|
t->val[i].nret = nret;
|
2013-08-14 12:54:31 -06:00
|
|
|
t->val[i].fint = fint;
|
2013-07-29 09:43:08 -06:00
|
|
|
t->val[i].ot = ot;
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
|
|
|
|
2011-10-06 09:42:51 -06:00
|
|
|
static bool
|
|
|
|
lookfintab(Fintab *t, void *k, bool del, Fin *f)
|
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
|
|
|
if(t->max == 0)
|
2011-10-06 09:42:51 -06:00
|
|
|
return false;
|
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)
|
2011-10-06 09:42:51 -06:00
|
|
|
return false;
|
2010-02-03 17:31:34 -07:00
|
|
|
if(t->key[i] == k) {
|
2011-10-06 09:42:51 -06:00
|
|
|
if(f)
|
|
|
|
*f = t->val[i];
|
2010-02-03 17:31:34 -07:00
|
|
|
if(del) {
|
|
|
|
t->key[i] = (void*)-1;
|
2011-10-06 09:42:51 -06:00
|
|
|
t->val[i].fn = nil;
|
|
|
|
t->val[i].nret = 0;
|
2013-07-29 09:43:08 -06:00
|
|
|
t->val[i].ot = nil;
|
2010-02-03 17:31:34 -07:00
|
|
|
t->ndead++;
|
|
|
|
}
|
2011-10-06 09:42:51 -06:00
|
|
|
return true;
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
|
|
|
if(++i == t->max)
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cannot happen - table is known to be non-full
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·throw("finalizer table inconsistent");
|
2011-10-06 09:42:51 -06:00
|
|
|
return false;
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
|
|
|
|
2011-10-06 09:42:51 -06:00
|
|
|
static void
|
|
|
|
resizefintab(Fintab *tab)
|
2010-02-03 17:31:34 -07:00
|
|
|
{
|
|
|
|
Fintab newtab;
|
2011-10-06 09:42:51 -06:00
|
|
|
void *k;
|
2010-02-03 17:31:34 -07:00
|
|
|
int32 i;
|
2011-10-06 09:42:51 -06: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;
|
|
|
|
}
|
2010-03-26 15:15:30 -06:00
|
|
|
|
2013-08-23 18:28:47 -06:00
|
|
|
newtab.key = runtime·mallocgc(newtab.max*sizeof newtab.key[0], 0, FlagNoInvokeGC|FlagNoScan);
|
runtime: refactor mallocgc
Make it accept type, combine flags.
Several reasons for the change:
1. mallocgc and settype must be atomic wrt GC
2. settype is called from only one place now
3. it will help performance (eventually settype
functionality must be combined with markallocated)
4. flags are easier to read now (no mallocgc(sz, 0, 1, 0) anymore)
R=golang-dev, iant, nightlyone, rsc, dave, khr, bradfitz, r
CC=golang-dev
https://golang.org/cl/10136043
2013-07-26 11:17:24 -06:00
|
|
|
newtab.val = runtime·mallocgc(newtab.max*sizeof newtab.val[0], 0, FlagNoInvokeGC);
|
2011-10-06 09:42:51 -06:00
|
|
|
|
|
|
|
for(i=0; i<tab->max; i++) {
|
|
|
|
k = tab->key[i];
|
|
|
|
if(k != nil && k != (void*)-1)
|
2013-08-14 12:54:31 -06:00
|
|
|
addfintab(&newtab, k, tab->val[i].fn, tab->val[i].nret, tab->val[i].fint, tab->val[i].ot);
|
2010-03-26 15:15:30 -06:00
|
|
|
}
|
2011-10-06 09:42:51 -06:00
|
|
|
|
|
|
|
runtime·free(tab->key);
|
|
|
|
runtime·free(tab->val);
|
|
|
|
|
|
|
|
tab->key = newtab.key;
|
|
|
|
tab->val = newtab.val;
|
|
|
|
tab->nkey = newtab.nkey;
|
|
|
|
tab->ndead = newtab.ndead;
|
|
|
|
tab->max = newtab.max;
|
|
|
|
}
|
2010-02-10 01:00:12 -07:00
|
|
|
|
2011-10-06 09:42:51 -06:00
|
|
|
bool
|
2013-08-14 12:54:31 -06:00
|
|
|
runtime·addfinalizer(void *p, FuncVal *f, uintptr nret, Type *fint, PtrType *ot)
|
2011-10-06 09:42:51 -06:00
|
|
|
{
|
|
|
|
Fintab *tab;
|
|
|
|
byte *base;
|
|
|
|
|
|
|
|
if(debug) {
|
|
|
|
if(!runtime·mlookup(p, &base, nil, nil) || p != base)
|
|
|
|
runtime·throw("addfinalizer on invalid pointer");
|
2010-03-15 21:29:17 -06:00
|
|
|
}
|
2011-10-06 09:42:51 -06:00
|
|
|
|
|
|
|
tab = TAB(p);
|
|
|
|
runtime·lock(tab);
|
2010-02-10 01:00:12 -07:00
|
|
|
if(f == nil) {
|
2012-05-30 00:04:11 -06:00
|
|
|
lookfintab(tab, p, true, nil);
|
2011-10-06 09:42:51 -06:00
|
|
|
runtime·unlock(tab);
|
|
|
|
return true;
|
2010-02-10 01:00:12 -07:00
|
|
|
}
|
|
|
|
|
2011-10-06 09:42:51 -06:00
|
|
|
if(lookfintab(tab, p, false, nil)) {
|
|
|
|
runtime·unlock(tab);
|
|
|
|
return false;
|
2010-03-15 21:29:17 -06:00
|
|
|
}
|
2010-02-03 17:31:34 -07:00
|
|
|
|
2011-10-06 09:42:51 -06:00
|
|
|
if(tab->nkey >= tab->max/2+tab->max/4) {
|
2010-02-03 17:31:34 -07:00
|
|
|
// keep table at most 3/4 full:
|
|
|
|
// allocate new table and rehash.
|
2011-10-06 09:42:51 -06:00
|
|
|
resizefintab(tab);
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
2010-02-10 01:00:12 -07:00
|
|
|
|
2013-08-14 12:54:31 -06:00
|
|
|
addfintab(tab, p, f, nret, fint, ot);
|
2011-10-06 09:42:51 -06:00
|
|
|
runtime·setblockspecial(p, true);
|
|
|
|
runtime·unlock(tab);
|
|
|
|
return true;
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
|
|
|
|
2010-02-10 01:00:12 -07:00
|
|
|
// get finalizer; if del, delete finalizer.
|
2011-10-06 09:42:51 -06:00
|
|
|
// caller is responsible for updating RefHasFinalizer (special) bit.
|
|
|
|
bool
|
2013-08-14 12:54:31 -06:00
|
|
|
runtime·getfinalizer(void *p, bool del, FuncVal **fn, uintptr *nret, Type **fint, PtrType **ot)
|
2010-02-03 17:31:34 -07:00
|
|
|
{
|
2011-10-06 09:42:51 -06:00
|
|
|
Fintab *tab;
|
|
|
|
bool res;
|
|
|
|
Fin f;
|
2010-03-15 21:29:17 -06:00
|
|
|
|
2011-10-06 09:42:51 -06:00
|
|
|
tab = TAB(p);
|
|
|
|
runtime·lock(tab);
|
|
|
|
res = lookfintab(tab, p, del, &f);
|
|
|
|
runtime·unlock(tab);
|
|
|
|
if(res==false)
|
|
|
|
return false;
|
|
|
|
*fn = f.fn;
|
|
|
|
*nret = f.nret;
|
2013-08-14 12:54:31 -06:00
|
|
|
*fint = f.fint;
|
2013-07-29 09:43:08 -06:00
|
|
|
*ot = f.ot;
|
2011-10-06 09:42:51 -06:00
|
|
|
return true;
|
2010-02-03 17:31:34 -07:00
|
|
|
}
|
2010-03-26 15:15:30 -06:00
|
|
|
|
|
|
|
void
|
runtime: ,s/[a-zA-Z0-9_]+/runtime·&/g, almost
Prefix all external symbols in runtime by runtime·,
to avoid conflicts with possible symbols of the same
name in linked-in C libraries. The obvious conflicts
are printf, malloc, and free, but hide everything to
avoid future pain.
The symbols left alone are:
** known to cgo **
_cgo_free
_cgo_malloc
libcgo_thread_start
initcgo
ncgocall
** known to linker **
_rt0_$GOARCH
_rt0_$GOARCH_$GOOS
text
etext
data
end
pclntab
epclntab
symtab
esymtab
** known to C compiler **
_divv
_modv
_div64by32
etc (arch specific)
Tested on darwin/386, darwin/amd64, linux/386, linux/amd64.
Built (but not tested) for freebsd/386, freebsd/amd64, linux/arm, windows/386.
R=r, PeterGo
CC=golang-dev
https://golang.org/cl/2899041
2010-11-04 12:00:19 -06:00
|
|
|
runtime·walkfintab(void (*fn)(void*))
|
2010-03-26 15:15:30 -06:00
|
|
|
{
|
|
|
|
void **key;
|
|
|
|
void **ekey;
|
2011-10-06 09:42:51 -06:00
|
|
|
int32 i;
|
2010-03-26 15:15:30 -06:00
|
|
|
|
2011-10-06 09:42:51 -06:00
|
|
|
for(i=0; i<TABSZ; i++) {
|
|
|
|
runtime·lock(&fintab[i]);
|
|
|
|
key = fintab[i].key;
|
|
|
|
ekey = key + fintab[i].max;
|
|
|
|
for(; key < ekey; key++)
|
|
|
|
if(*key != nil && *key != ((void*)-1))
|
|
|
|
fn(*key);
|
|
|
|
runtime·unlock(&fintab[i]);
|
|
|
|
}
|
2010-03-26 15:15:30 -06:00
|
|
|
}
|