2008-11-13 11:35:44 -07:00
|
|
|
// Copyright 2009 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 "hashmap.h"
|
2009-09-08 14:46:54 -06:00
|
|
|
#include "type.h"
|
2008-11-13 11:35:44 -07:00
|
|
|
|
|
|
|
/* Return a pointer to the struct/union of type "type"
|
|
|
|
whose "field" field is addressed by pointer "p". */
|
|
|
|
|
2011-01-31 04:27:28 -07:00
|
|
|
struct Hmap { /* a hash table; initialize with hash_init() */
|
2008-11-13 11:35:44 -07:00
|
|
|
uint32 count; /* elements in table - must be first */
|
|
|
|
|
|
|
|
uint8 datasize; /* amount of data to store in entry */
|
|
|
|
uint8 max_power; /* max power of 2 to create sub-tables */
|
|
|
|
uint8 max_probes; /* max entries to probe before rehashing */
|
2010-05-19 13:57:47 -06:00
|
|
|
uint8 indirectval; /* storing pointers to values */
|
2008-11-13 11:35:44 -07:00
|
|
|
int32 changes; /* inc'ed whenever a subtable is created/grown */
|
|
|
|
hash_hash_t (*data_hash) (uint32, void *a); /* return hash of *a */
|
|
|
|
uint32 (*data_eq) (uint32, void *a, void *b); /* return whether *a == *b */
|
|
|
|
void (*data_del) (uint32, void *arg, void *data); /* invoked on deletion */
|
|
|
|
struct hash_subtable *st; /* first-level table */
|
|
|
|
|
|
|
|
uint32 keysize;
|
|
|
|
uint32 valsize;
|
2009-01-26 16:36:39 -07:00
|
|
|
uint32 datavo;
|
2009-06-30 21:02:07 -06:00
|
|
|
|
|
|
|
// three sets of offsets: the digit counts how many
|
|
|
|
// of key, value are passed as inputs:
|
|
|
|
// 0 = func() (key, value)
|
|
|
|
// 1 = func(key) (value)
|
|
|
|
// 2 = func(key, value)
|
|
|
|
uint32 ko0;
|
|
|
|
uint32 vo0;
|
|
|
|
uint32 ko1;
|
|
|
|
uint32 vo1;
|
|
|
|
uint32 po1;
|
|
|
|
uint32 ko2;
|
|
|
|
uint32 vo2;
|
|
|
|
uint32 po2;
|
2008-11-13 11:35:44 -07:00
|
|
|
Alg* keyalg;
|
|
|
|
Alg* valalg;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct hash_entry {
|
|
|
|
hash_hash_t hash; /* hash value of data */
|
|
|
|
byte data[1]; /* user data has "datasize" bytes */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct hash_subtable {
|
|
|
|
uint8 power; /* bits used to index this table */
|
|
|
|
uint8 used; /* bits in hash used before reaching this table */
|
|
|
|
uint8 datasize; /* bytes of client data in an entry */
|
|
|
|
uint8 max_probes; /* max number of probes when searching */
|
|
|
|
int16 limit_bytes; /* max_probes * (datasize+sizeof (hash_hash_t)) */
|
2011-10-01 11:00:53 -06:00
|
|
|
struct hash_entry *last; /* points to last element of entry[] */
|
2008-11-13 11:35:44 -07:00
|
|
|
struct hash_entry entry[1]; /* 2**power+max_probes-1 elements of elemsize bytes */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define HASH_DATA_EQ(h,x,y) ((*h->data_eq) (h->keysize, (x), (y)))
|
|
|
|
|
|
|
|
#define HASH_REHASH 0x2 /* an internal flag */
|
|
|
|
/* the number of bits used is stored in the flags word too */
|
|
|
|
#define HASH_USED(x) ((x) >> 2)
|
|
|
|
#define HASH_MAKE_USED(x) ((x) << 2)
|
|
|
|
|
|
|
|
#define HASH_LOW 6
|
|
|
|
#define HASH_ONE (((hash_hash_t)1) << HASH_LOW)
|
|
|
|
#define HASH_MASK (HASH_ONE - 1)
|
|
|
|
#define HASH_ADJUST(x) (((x) < HASH_ONE) << HASH_LOW)
|
|
|
|
|
|
|
|
#define HASH_BITS (sizeof (hash_hash_t) * 8)
|
|
|
|
|
|
|
|
#define HASH_SUBHASH HASH_MASK
|
|
|
|
#define HASH_NIL 0
|
|
|
|
#define HASH_NIL_MEMSET 0
|
|
|
|
|
|
|
|
#define HASH_OFFSET(base, byte_offset) \
|
|
|
|
((struct hash_entry *) (((byte *) (base)) + (byte_offset)))
|
|
|
|
|
|
|
|
|
|
|
|
/* return a hash layer with 2**power empty entries */
|
|
|
|
static struct hash_subtable *
|
2011-01-31 04:27:28 -07:00
|
|
|
hash_subtable_new (Hmap *h, int32 power, int32 used)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
int32 elemsize = h->datasize + offsetof (struct hash_entry, data[0]);
|
|
|
|
int32 bytes = elemsize << power;
|
|
|
|
struct hash_subtable *st;
|
|
|
|
int32 limit_bytes = h->max_probes * elemsize;
|
|
|
|
int32 max_probes = h->max_probes;
|
|
|
|
|
|
|
|
if (bytes < limit_bytes) {
|
|
|
|
limit_bytes = bytes;
|
|
|
|
max_probes = 1 << power;
|
|
|
|
}
|
|
|
|
bytes += limit_bytes - elemsize;
|
|
|
|
st = malloc (offsetof (struct hash_subtable, entry[0]) + bytes);
|
|
|
|
st->power = power;
|
|
|
|
st->used = used;
|
|
|
|
st->datasize = h->datasize;
|
|
|
|
st->max_probes = max_probes;
|
|
|
|
st->limit_bytes = limit_bytes;
|
2011-10-01 11:00:53 -06:00
|
|
|
st->last = HASH_OFFSET (st->entry, bytes) - 1;
|
2008-11-13 11:35:44 -07:00
|
|
|
memset (st->entry, HASH_NIL_MEMSET, bytes);
|
|
|
|
return (st);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_sizes (int64 hint, int32 *init_power, int32 *max_power)
|
|
|
|
{
|
|
|
|
int32 log = 0;
|
|
|
|
int32 i;
|
|
|
|
|
|
|
|
for (i = 32; i != 0; i >>= 1) {
|
|
|
|
if ((hint >> (log + i)) != 0) {
|
|
|
|
log += i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log += 1 + (((hint << 3) >> log) >= 11); /* round up for utilization */
|
|
|
|
if (log <= 14) {
|
|
|
|
*init_power = log;
|
|
|
|
} else {
|
|
|
|
*init_power = 12;
|
|
|
|
}
|
|
|
|
*max_power = 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-01-31 04:27:28 -07:00
|
|
|
hash_init (Hmap *h,
|
2008-11-13 11:35:44 -07:00
|
|
|
int32 datasize,
|
|
|
|
hash_hash_t (*data_hash) (uint32, void *),
|
|
|
|
uint32 (*data_eq) (uint32, void *, void *),
|
|
|
|
void (*data_del) (uint32, void *, void *),
|
|
|
|
int64 hint)
|
|
|
|
{
|
|
|
|
int32 init_power;
|
|
|
|
int32 max_power;
|
|
|
|
|
|
|
|
if(datasize < sizeof (void *))
|
|
|
|
datasize = sizeof (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
|
|
|
datasize = runtime·rnd(datasize, sizeof (void *));
|
2008-11-13 11:35:44 -07:00
|
|
|
init_sizes (hint, &init_power, &max_power);
|
|
|
|
h->datasize = datasize;
|
|
|
|
h->max_power = max_power;
|
|
|
|
h->max_probes = 15;
|
|
|
|
assert (h->datasize == datasize);
|
|
|
|
assert (h->max_power == max_power);
|
|
|
|
assert (sizeof (void *) <= h->datasize || h->max_power == 255);
|
|
|
|
h->count = 0;
|
|
|
|
h->changes = 0;
|
|
|
|
h->data_hash = data_hash;
|
|
|
|
h->data_eq = data_eq;
|
|
|
|
h->data_del = data_del;
|
|
|
|
h->st = hash_subtable_new (h, init_power, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hash_remove_n (struct hash_subtable *st, struct hash_entry *dst_e, int32 n)
|
|
|
|
{
|
|
|
|
int32 elemsize = st->datasize + offsetof (struct hash_entry, data[0]);
|
|
|
|
struct hash_entry *src_e = HASH_OFFSET (dst_e, n * elemsize);
|
2011-10-01 11:00:53 -06:00
|
|
|
struct hash_entry *last_e = st->last;
|
2008-11-13 11:35:44 -07:00
|
|
|
int32 shift = HASH_BITS - (st->power + st->used);
|
|
|
|
int32 index_mask = (((hash_hash_t)1) << st->power) - 1;
|
|
|
|
int32 dst_i = (((byte *) dst_e) - ((byte *) st->entry)) / elemsize;
|
|
|
|
int32 src_i = dst_i + n;
|
|
|
|
hash_hash_t hash;
|
|
|
|
int32 skip;
|
|
|
|
int32 bytes;
|
|
|
|
|
|
|
|
while (dst_e != src_e) {
|
2011-10-01 11:00:53 -06:00
|
|
|
if (src_e <= last_e) {
|
2008-11-13 11:35:44 -07:00
|
|
|
struct hash_entry *cp_e = src_e;
|
|
|
|
int32 save_dst_i = dst_i;
|
2011-10-01 11:00:53 -06:00
|
|
|
while (cp_e <= last_e && (hash = cp_e->hash) != HASH_NIL &&
|
2008-11-13 11:35:44 -07:00
|
|
|
((hash >> shift) & index_mask) <= dst_i) {
|
|
|
|
cp_e = HASH_OFFSET (cp_e, elemsize);
|
|
|
|
dst_i++;
|
|
|
|
}
|
|
|
|
bytes = ((byte *) cp_e) - (byte *) src_e;
|
|
|
|
memmove (dst_e, src_e, bytes);
|
|
|
|
dst_e = HASH_OFFSET (dst_e, bytes);
|
|
|
|
src_e = cp_e;
|
|
|
|
src_i += dst_i - save_dst_i;
|
2011-10-01 11:00:53 -06:00
|
|
|
if (src_e <= last_e && (hash = src_e->hash) != HASH_NIL) {
|
2008-11-13 11:35:44 -07:00
|
|
|
skip = ((hash >> shift) & index_mask) - dst_i;
|
|
|
|
} else {
|
|
|
|
skip = src_i - dst_i;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
skip = src_i - dst_i;
|
|
|
|
}
|
|
|
|
bytes = skip * elemsize;
|
|
|
|
memset (dst_e, HASH_NIL_MEMSET, bytes);
|
|
|
|
dst_e = HASH_OFFSET (dst_e, bytes);
|
|
|
|
dst_i += skip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32
|
|
|
|
hash_insert_internal (struct hash_subtable **pst, int32 flags, hash_hash_t hash,
|
2011-01-31 04:27:28 -07:00
|
|
|
Hmap *h, void *data, void **pres);
|
2008-11-13 11:35:44 -07:00
|
|
|
|
|
|
|
static void
|
2011-01-31 04:27:28 -07:00
|
|
|
hash_conv (Hmap *h,
|
2008-11-13 11:35:44 -07:00
|
|
|
struct hash_subtable *st, int32 flags,
|
|
|
|
hash_hash_t hash,
|
|
|
|
struct hash_entry *e)
|
|
|
|
{
|
|
|
|
int32 new_flags = (flags + HASH_MAKE_USED (st->power)) | HASH_REHASH;
|
|
|
|
int32 shift = HASH_BITS - HASH_USED (new_flags);
|
|
|
|
hash_hash_t prefix_mask = (-(hash_hash_t)1) << shift;
|
|
|
|
int32 elemsize = h->datasize + offsetof (struct hash_entry, data[0]);
|
|
|
|
void *dummy_result;
|
|
|
|
struct hash_entry *de;
|
|
|
|
int32 index_mask = (1 << st->power) - 1;
|
|
|
|
hash_hash_t e_hash;
|
|
|
|
struct hash_entry *pe = HASH_OFFSET (e, -elemsize);
|
|
|
|
|
|
|
|
while (e != st->entry && (e_hash = pe->hash) != HASH_NIL && (e_hash & HASH_MASK) != HASH_SUBHASH) {
|
|
|
|
e = pe;
|
|
|
|
pe = HASH_OFFSET (pe, -elemsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
de = e;
|
2011-10-01 11:00:53 -06:00
|
|
|
while (e <= st->last &&
|
2008-11-13 11:35:44 -07:00
|
|
|
(e_hash = e->hash) != HASH_NIL &&
|
|
|
|
(e_hash & HASH_MASK) != HASH_SUBHASH) {
|
|
|
|
struct hash_entry *target_e = HASH_OFFSET (st->entry, ((e_hash >> shift) & index_mask) * elemsize);
|
|
|
|
struct hash_entry *ne = HASH_OFFSET (e, elemsize);
|
|
|
|
hash_hash_t current = e_hash & prefix_mask;
|
|
|
|
if (de < target_e) {
|
|
|
|
memset (de, HASH_NIL_MEMSET, ((byte *) target_e) - (byte *) de);
|
|
|
|
de = target_e;
|
|
|
|
}
|
|
|
|
if ((hash & prefix_mask) == current ||
|
2011-10-01 11:00:53 -06:00
|
|
|
(ne <= st->last && (e_hash = ne->hash) != HASH_NIL &&
|
2008-11-13 11:35:44 -07:00
|
|
|
(e_hash & prefix_mask) == current)) {
|
|
|
|
struct hash_subtable *new_st = hash_subtable_new (h, 1, HASH_USED (new_flags));
|
|
|
|
int32 rc = hash_insert_internal (&new_st, new_flags, e->hash, h, e->data, &dummy_result);
|
|
|
|
assert (rc == 0);
|
|
|
|
memcpy(dummy_result, e->data, h->datasize);
|
|
|
|
e = ne;
|
2011-10-01 11:00:53 -06:00
|
|
|
while (e <= st->last && (e_hash = e->hash) != HASH_NIL && (e_hash & prefix_mask) == current) {
|
2008-11-13 11:35:44 -07:00
|
|
|
assert ((e_hash & HASH_MASK) != HASH_SUBHASH);
|
|
|
|
rc = hash_insert_internal (&new_st, new_flags, e_hash, h, e->data, &dummy_result);
|
|
|
|
assert (rc == 0);
|
|
|
|
memcpy(dummy_result, e->data, h->datasize);
|
|
|
|
e = HASH_OFFSET (e, elemsize);
|
|
|
|
}
|
|
|
|
memset (de->data, HASH_NIL_MEMSET, h->datasize);
|
|
|
|
*(struct hash_subtable **)de->data = new_st;
|
|
|
|
de->hash = current | HASH_SUBHASH;
|
|
|
|
} else {
|
|
|
|
if (e != de) {
|
|
|
|
memcpy (de, e, elemsize);
|
|
|
|
}
|
|
|
|
e = HASH_OFFSET (e, elemsize);
|
|
|
|
}
|
|
|
|
de = HASH_OFFSET (de, elemsize);
|
|
|
|
}
|
|
|
|
if (e != de) {
|
|
|
|
hash_remove_n (st, de, (((byte *) e) - (byte *) de) / elemsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-01-31 04:27:28 -07:00
|
|
|
hash_grow (Hmap *h, struct hash_subtable **pst, int32 flags)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
struct hash_subtable *old_st = *pst;
|
|
|
|
int32 elemsize = h->datasize + offsetof (struct hash_entry, data[0]);
|
|
|
|
*pst = hash_subtable_new (h, old_st->power + 1, HASH_USED (flags));
|
2011-10-01 11:00:53 -06:00
|
|
|
struct hash_entry *last_e = old_st->last;
|
2008-11-13 11:35:44 -07:00
|
|
|
struct hash_entry *e;
|
|
|
|
void *dummy_result;
|
|
|
|
int32 used = 0;
|
|
|
|
|
|
|
|
flags |= HASH_REHASH;
|
2011-10-01 11:00:53 -06:00
|
|
|
for (e = old_st->entry; e <= last_e; e = HASH_OFFSET (e, elemsize)) {
|
2008-11-13 11:35:44 -07:00
|
|
|
hash_hash_t hash = e->hash;
|
|
|
|
if (hash != HASH_NIL) {
|
|
|
|
int32 rc = hash_insert_internal (pst, flags, e->hash, h, e->data, &dummy_result);
|
|
|
|
assert (rc == 0);
|
|
|
|
memcpy(dummy_result, e->data, h->datasize);
|
|
|
|
used++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (old_st);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static int32
|
2011-01-31 04:27:28 -07:00
|
|
|
hash_lookup (Hmap *h, void *data, void **pres)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
int32 elemsize = h->datasize + offsetof (struct hash_entry, data[0]);
|
|
|
|
hash_hash_t hash = (*h->data_hash) (h->keysize, data) & ~HASH_MASK;
|
|
|
|
struct hash_subtable *st = h->st;
|
|
|
|
int32 used = 0;
|
|
|
|
hash_hash_t e_hash;
|
|
|
|
struct hash_entry *e;
|
|
|
|
struct hash_entry *end_e;
|
|
|
|
|
|
|
|
hash += HASH_ADJUST (hash);
|
|
|
|
for (;;) {
|
|
|
|
int32 shift = HASH_BITS - (st->power + used);
|
|
|
|
int32 index_mask = (1 << st->power) - 1;
|
|
|
|
int32 i = (hash >> shift) & index_mask; /* i is the natural position of hash */
|
2008-12-09 17:16:07 -07:00
|
|
|
|
2008-11-13 11:35:44 -07:00
|
|
|
e = HASH_OFFSET (st->entry, i * elemsize); /* e points to element i */
|
|
|
|
e_hash = e->hash;
|
|
|
|
if ((e_hash & HASH_MASK) != HASH_SUBHASH) { /* a subtable */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
used += st->power;
|
|
|
|
st = *(struct hash_subtable **)e->data;
|
|
|
|
}
|
|
|
|
end_e = HASH_OFFSET (e, st->limit_bytes);
|
|
|
|
while (e != end_e && (e_hash = e->hash) != HASH_NIL && e_hash < hash) {
|
|
|
|
e = HASH_OFFSET (e, elemsize);
|
|
|
|
}
|
|
|
|
while (e != end_e && ((e_hash = e->hash) ^ hash) < HASH_SUBHASH) {
|
|
|
|
if (HASH_DATA_EQ (h, data, e->data)) { /* a match */
|
|
|
|
*pres = e->data;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
e = HASH_OFFSET (e, elemsize);
|
|
|
|
}
|
|
|
|
USED(e_hash);
|
|
|
|
*pres = 0;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static int32
|
2011-01-31 04:27:28 -07:00
|
|
|
hash_remove (Hmap *h, void *data, void *arg)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
int32 elemsize = h->datasize + offsetof (struct hash_entry, data[0]);
|
|
|
|
hash_hash_t hash = (*h->data_hash) (h->keysize, data) & ~HASH_MASK;
|
|
|
|
struct hash_subtable *st = h->st;
|
|
|
|
int32 used = 0;
|
|
|
|
hash_hash_t e_hash;
|
|
|
|
struct hash_entry *e;
|
|
|
|
struct hash_entry *end_e;
|
|
|
|
|
|
|
|
hash += HASH_ADJUST (hash);
|
|
|
|
for (;;) {
|
|
|
|
int32 shift = HASH_BITS - (st->power + used);
|
|
|
|
int32 index_mask = (1 << st->power) - 1;
|
|
|
|
int32 i = (hash >> shift) & index_mask; /* i is the natural position of hash */
|
2008-12-09 17:16:07 -07:00
|
|
|
|
2008-11-13 11:35:44 -07:00
|
|
|
e = HASH_OFFSET (st->entry, i * elemsize); /* e points to element i */
|
|
|
|
e_hash = e->hash;
|
|
|
|
if ((e_hash & HASH_MASK) != HASH_SUBHASH) { /* a subtable */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
used += st->power;
|
|
|
|
st = *(struct hash_subtable **)e->data;
|
|
|
|
}
|
|
|
|
end_e = HASH_OFFSET (e, st->limit_bytes);
|
|
|
|
while (e != end_e && (e_hash = e->hash) != HASH_NIL && e_hash < hash) {
|
|
|
|
e = HASH_OFFSET (e, elemsize);
|
|
|
|
}
|
|
|
|
while (e != end_e && ((e_hash = e->hash) ^ hash) < HASH_SUBHASH) {
|
|
|
|
if (HASH_DATA_EQ (h, data, e->data)) { /* a match */
|
2010-05-19 13:57:47 -06:00
|
|
|
(*h->data_del) (h->datavo, arg, e->data);
|
2008-11-13 11:35:44 -07:00
|
|
|
hash_remove_n (st, e, 1);
|
|
|
|
h->count--;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
e = HASH_OFFSET (e, elemsize);
|
|
|
|
}
|
|
|
|
USED(e_hash);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int32
|
|
|
|
hash_insert_internal (struct hash_subtable **pst, int32 flags, hash_hash_t hash,
|
2011-01-31 04:27:28 -07:00
|
|
|
Hmap *h, void *data, void **pres)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
int32 elemsize = h->datasize + offsetof (struct hash_entry, data[0]);
|
|
|
|
|
|
|
|
if ((flags & HASH_REHASH) == 0) {
|
|
|
|
hash += HASH_ADJUST (hash);
|
|
|
|
hash &= ~HASH_MASK;
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
struct hash_subtable *st = *pst;
|
|
|
|
int32 shift = HASH_BITS - (st->power + HASH_USED (flags));
|
|
|
|
int32 index_mask = (1 << st->power) - 1;
|
|
|
|
int32 i = (hash >> shift) & index_mask; /* i is the natural position of hash */
|
|
|
|
struct hash_entry *start_e =
|
|
|
|
HASH_OFFSET (st->entry, i * elemsize); /* start_e is the pointer to element i */
|
|
|
|
struct hash_entry *e = start_e; /* e is going to range over [start_e, end_e) */
|
|
|
|
struct hash_entry *end_e;
|
|
|
|
hash_hash_t e_hash = e->hash;
|
2008-12-09 17:16:07 -07:00
|
|
|
|
2008-11-13 11:35:44 -07:00
|
|
|
if ((e_hash & HASH_MASK) == HASH_SUBHASH) { /* a subtable */
|
|
|
|
pst = (struct hash_subtable **) e->data;
|
|
|
|
flags += HASH_MAKE_USED (st->power);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
end_e = HASH_OFFSET (start_e, st->limit_bytes);
|
|
|
|
while (e != end_e && (e_hash = e->hash) != HASH_NIL && e_hash < hash) {
|
|
|
|
e = HASH_OFFSET (e, elemsize);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (e != end_e && e_hash != HASH_NIL) {
|
|
|
|
/* ins_e ranges over the elements that may match */
|
|
|
|
struct hash_entry *ins_e = e;
|
|
|
|
int32 ins_i = i;
|
|
|
|
hash_hash_t ins_e_hash;
|
|
|
|
while (ins_e != end_e && ((e_hash = ins_e->hash) ^ hash) < HASH_SUBHASH) {
|
|
|
|
if (HASH_DATA_EQ (h, data, ins_e->data)) { /* a match */
|
|
|
|
*pres = ins_e->data;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
assert (e_hash != hash || (flags & HASH_REHASH) == 0);
|
|
|
|
hash += (e_hash == hash); /* adjust hash if it collides */
|
|
|
|
ins_e = HASH_OFFSET (ins_e, elemsize);
|
|
|
|
ins_i++;
|
|
|
|
if (e_hash <= hash) { /* set e to insertion point */
|
|
|
|
e = ins_e;
|
|
|
|
i = ins_i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* set ins_e to the insertion point for the new element */
|
|
|
|
ins_e = e;
|
|
|
|
ins_i = i;
|
|
|
|
ins_e_hash = 0;
|
|
|
|
/* move ins_e to point at the end of the contiguous block, but
|
|
|
|
stop if any element can't be moved by one up */
|
2011-10-01 11:00:53 -06:00
|
|
|
while (ins_e <= st->last && (ins_e_hash = ins_e->hash) != HASH_NIL &&
|
2008-11-13 11:35:44 -07:00
|
|
|
ins_i + 1 - ((ins_e_hash >> shift) & index_mask) < st->max_probes &&
|
|
|
|
(ins_e_hash & HASH_MASK) != HASH_SUBHASH) {
|
|
|
|
ins_e = HASH_OFFSET (ins_e, elemsize);
|
|
|
|
ins_i++;
|
|
|
|
}
|
2011-10-01 11:00:53 -06:00
|
|
|
if (e == end_e || ins_e > st->last || ins_e_hash != HASH_NIL) {
|
2008-11-13 11:35:44 -07:00
|
|
|
e = end_e; /* can't insert; must grow or convert to subtable */
|
|
|
|
} else { /* make space for element */
|
|
|
|
memmove (HASH_OFFSET (e, elemsize), e, ((byte *) ins_e) - (byte *) e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (e != end_e) {
|
|
|
|
e->hash = hash;
|
|
|
|
*pres = e->data;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
h->changes++;
|
|
|
|
if (st->power < h->max_power) {
|
|
|
|
hash_grow (h, pst, flags);
|
|
|
|
} else {
|
|
|
|
hash_conv (h, st, flags, hash, start_e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static int32
|
2011-01-31 04:27:28 -07:00
|
|
|
hash_insert (Hmap *h, void *data, void **pres)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
int32 rc = hash_insert_internal (&h->st, 0, (*h->data_hash) (h->keysize, data), h, data, pres);
|
|
|
|
|
|
|
|
h->count += (rc == 0); /* increment count if element didn't previously exist */
|
|
|
|
return (rc);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static uint32
|
2011-01-31 04:27:28 -07:00
|
|
|
hash_count (Hmap *h)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
return (h->count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
iter_restart (struct hash_iter *it, struct hash_subtable *st, int32 used)
|
|
|
|
{
|
|
|
|
int32 elemsize = it->elemsize;
|
|
|
|
hash_hash_t last_hash = it->last_hash;
|
|
|
|
struct hash_entry *e;
|
|
|
|
hash_hash_t e_hash;
|
|
|
|
struct hash_iter_sub *sub = &it->subtable_state[it->i];
|
2011-10-01 11:00:53 -06:00
|
|
|
struct hash_entry *last;
|
2008-11-13 11:35:44 -07:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int32 shift = HASH_BITS - (st->power + used);
|
|
|
|
int32 index_mask = (1 << st->power) - 1;
|
|
|
|
int32 i = (last_hash >> shift) & index_mask;
|
|
|
|
|
2011-10-01 11:00:53 -06:00
|
|
|
last = st->last;
|
2008-11-13 11:35:44 -07:00
|
|
|
e = HASH_OFFSET (st->entry, i * elemsize);
|
|
|
|
sub->start = st->entry;
|
2011-10-01 11:00:53 -06:00
|
|
|
sub->last = last;
|
2008-11-13 11:35:44 -07:00
|
|
|
|
|
|
|
if ((e->hash & HASH_MASK) != HASH_SUBHASH) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sub->e = HASH_OFFSET (e, elemsize);
|
|
|
|
sub = &it->subtable_state[++(it->i)];
|
|
|
|
used += st->power;
|
|
|
|
st = *(struct hash_subtable **)e->data;
|
|
|
|
}
|
2011-10-01 11:00:53 -06:00
|
|
|
while (e <= last && ((e_hash = e->hash) == HASH_NIL || e_hash <= last_hash)) {
|
2008-11-13 11:35:44 -07:00
|
|
|
e = HASH_OFFSET (e, elemsize);
|
|
|
|
}
|
|
|
|
sub->e = e;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static void *
|
2008-11-13 11:35:44 -07:00
|
|
|
hash_next (struct hash_iter *it)
|
|
|
|
{
|
|
|
|
int32 elemsize = it->elemsize;
|
|
|
|
struct hash_iter_sub *sub = &it->subtable_state[it->i];
|
|
|
|
struct hash_entry *e = sub->e;
|
2011-10-01 11:00:53 -06:00
|
|
|
struct hash_entry *last = sub->last;
|
2008-11-13 11:35:44 -07:00
|
|
|
hash_hash_t e_hash = 0;
|
|
|
|
|
|
|
|
if (it->changes != it->h->changes) { /* hash table's structure changed; recompute */
|
|
|
|
it->changes = it->h->changes;
|
|
|
|
it->i = 0;
|
|
|
|
iter_restart (it, it->h->st, 0);
|
|
|
|
sub = &it->subtable_state[it->i];
|
|
|
|
e = sub->e;
|
2011-10-01 11:00:53 -06:00
|
|
|
last = sub->last;
|
2008-11-13 11:35:44 -07:00
|
|
|
}
|
|
|
|
if (e != sub->start && it->last_hash != HASH_OFFSET (e, -elemsize)->hash) {
|
|
|
|
struct hash_entry *start = HASH_OFFSET (e, -(elemsize * it->h->max_probes));
|
|
|
|
struct hash_entry *pe = HASH_OFFSET (e, -elemsize);
|
|
|
|
hash_hash_t last_hash = it->last_hash;
|
|
|
|
if (start < sub->start) {
|
|
|
|
start = sub->start;
|
|
|
|
}
|
|
|
|
while (e != start && ((e_hash = pe->hash) == HASH_NIL || last_hash < e_hash)) {
|
|
|
|
e = pe;
|
|
|
|
pe = HASH_OFFSET (pe, -elemsize);
|
|
|
|
}
|
2011-10-01 11:00:53 -06:00
|
|
|
while (e <= last && ((e_hash = e->hash) == HASH_NIL || e_hash <= last_hash)) {
|
2008-11-13 11:35:44 -07:00
|
|
|
e = HASH_OFFSET (e, elemsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2011-10-01 11:00:53 -06:00
|
|
|
while (e <= last && (e_hash = e->hash) == HASH_NIL) {
|
2008-11-13 11:35:44 -07:00
|
|
|
e = HASH_OFFSET (e, elemsize);
|
|
|
|
}
|
2011-10-01 11:00:53 -06:00
|
|
|
if (e > last) {
|
2008-11-13 11:35:44 -07:00
|
|
|
if (it->i == 0) {
|
|
|
|
it->last_hash = HASH_OFFSET (e, -elemsize)->hash;
|
|
|
|
sub->e = e;
|
|
|
|
return (0);
|
|
|
|
} else {
|
|
|
|
it->i--;
|
|
|
|
sub = &it->subtable_state[it->i];
|
|
|
|
e = sub->e;
|
2011-10-01 11:00:53 -06:00
|
|
|
last = sub->last;
|
2008-11-13 11:35:44 -07:00
|
|
|
}
|
|
|
|
} else if ((e_hash & HASH_MASK) != HASH_SUBHASH) {
|
|
|
|
it->last_hash = e->hash;
|
|
|
|
sub->e = HASH_OFFSET (e, elemsize);
|
|
|
|
return (e->data);
|
|
|
|
} else {
|
|
|
|
struct hash_subtable *st =
|
|
|
|
*(struct hash_subtable **)e->data;
|
|
|
|
sub->e = HASH_OFFSET (e, elemsize);
|
|
|
|
it->i++;
|
|
|
|
assert (it->i < sizeof (it->subtable_state) /
|
|
|
|
sizeof (it->subtable_state[0]));
|
|
|
|
sub = &it->subtable_state[it->i];
|
|
|
|
sub->e = e = st->entry;
|
|
|
|
sub->start = st->entry;
|
2011-10-01 11:00:53 -06:00
|
|
|
sub->last = last = st->last;
|
2008-11-13 11:35:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static void
|
2011-01-31 04:27:28 -07:00
|
|
|
hash_iter_init (Hmap *h, struct hash_iter *it)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
it->elemsize = h->datasize + offsetof (struct hash_entry, data[0]);
|
|
|
|
it->changes = h->changes;
|
|
|
|
it->i = 0;
|
|
|
|
it->h = h;
|
|
|
|
it->last_hash = 0;
|
|
|
|
it->subtable_state[0].e = h->st->entry;
|
|
|
|
it->subtable_state[0].start = h->st->entry;
|
2011-10-01 11:00:53 -06:00
|
|
|
it->subtable_state[0].last = h->st->last;
|
2008-11-13 11:35:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
clean_st (struct hash_subtable *st, int32 *slots, int32 *used)
|
|
|
|
{
|
|
|
|
int32 elemsize = st->datasize + offsetof (struct hash_entry, data[0]);
|
|
|
|
struct hash_entry *e = st->entry;
|
2011-10-01 11:00:53 -06:00
|
|
|
struct hash_entry *last = st->last;
|
|
|
|
int32 lslots = (((byte *) (last+1)) - (byte *) e) / elemsize;
|
2008-11-13 11:35:44 -07:00
|
|
|
int32 lused = 0;
|
|
|
|
|
2011-10-01 11:00:53 -06:00
|
|
|
while (e <= last) {
|
2008-11-13 11:35:44 -07:00
|
|
|
hash_hash_t hash = e->hash;
|
|
|
|
if ((hash & HASH_MASK) == HASH_SUBHASH) {
|
|
|
|
clean_st (*(struct hash_subtable **)e->data, slots, used);
|
|
|
|
} else {
|
|
|
|
lused += (hash != HASH_NIL);
|
|
|
|
}
|
|
|
|
e = HASH_OFFSET (e, elemsize);
|
|
|
|
}
|
|
|
|
free (st);
|
|
|
|
*slots += lslots;
|
|
|
|
*used += lused;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static void
|
2011-01-31 04:27:28 -07:00
|
|
|
hash_destroy (Hmap *h)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
int32 slots = 0;
|
|
|
|
int32 used = 0;
|
|
|
|
|
|
|
|
clean_st (h->st, &slots, &used);
|
|
|
|
free (h);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hash_visit_internal (struct hash_subtable *st,
|
|
|
|
int32 used, int32 level,
|
|
|
|
void (*data_visit) (void *arg, int32 level, void *data),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
int32 elemsize = st->datasize + offsetof (struct hash_entry, data[0]);
|
|
|
|
struct hash_entry *e = st->entry;
|
|
|
|
int32 shift = HASH_BITS - (used + st->power);
|
|
|
|
int32 i = 0;
|
|
|
|
|
2011-10-01 11:00:53 -06:00
|
|
|
while (e <= st->last) {
|
2008-11-13 11:35:44 -07:00
|
|
|
int32 index = ((e->hash >> (shift - 1)) >> 1) & ((1 << st->power) - 1);
|
|
|
|
if ((e->hash & HASH_MASK) == HASH_SUBHASH) {
|
|
|
|
(*data_visit) (arg, level, e->data);
|
|
|
|
hash_visit_internal (*(struct hash_subtable **)e->data,
|
|
|
|
used + st->power, level + 1, data_visit, arg);
|
|
|
|
} else {
|
|
|
|
(*data_visit) (arg, level, e->data);
|
|
|
|
}
|
|
|
|
if (e->hash != HASH_NIL) {
|
|
|
|
assert (i < index + st->max_probes);
|
|
|
|
assert (index <= i);
|
|
|
|
}
|
|
|
|
e = HASH_OFFSET (e, elemsize);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static void
|
2011-01-31 04:27:28 -07:00
|
|
|
hash_visit (Hmap *h, void (*data_visit) (void *arg, int32 level, void *data), void *arg)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
hash_visit_internal (h->st, 0, 0, data_visit, arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
/// interfaces to go runtime
|
|
|
|
//
|
|
|
|
|
2010-05-19 13:57:47 -06:00
|
|
|
// hash requires < 256 bytes of data (key+value) stored inline.
|
|
|
|
// Only basic types can be key - biggest is complex128 (16 bytes).
|
|
|
|
// Leave some room to grow, just in case.
|
|
|
|
enum {
|
|
|
|
MaxValsize = 256 - 64
|
|
|
|
};
|
|
|
|
|
2008-11-13 11:35:44 -07:00
|
|
|
static void
|
|
|
|
donothing(uint32 s, void *a, void *b)
|
|
|
|
{
|
|
|
|
USED(s);
|
|
|
|
USED(a);
|
|
|
|
USED(b);
|
|
|
|
}
|
|
|
|
|
2010-05-19 13:57:47 -06:00
|
|
|
static void
|
|
|
|
freedata(uint32 datavo, void *a, void *b)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
USED(a);
|
|
|
|
p = *(void**)((byte*)b + datavo);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void**
|
|
|
|
hash_indirect(Hmap *h, void *p)
|
|
|
|
{
|
|
|
|
if(h->indirectval)
|
|
|
|
p = *(void**)p;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2008-11-13 11:35:44 -07:00
|
|
|
static int32 debug = 0;
|
|
|
|
|
2011-08-17 12:56:27 -06:00
|
|
|
// makemap(typ *Type, hint uint32) (hmap *map[any]any);
|
2009-07-08 14:55:57 -06:00
|
|
|
Hmap*
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·makemap_c(MapType *typ, int64 hint)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
Hmap *h;
|
2010-05-19 13:57:47 -06:00
|
|
|
int32 keyalg, valalg, keysize, valsize, valsize_in_hash;
|
|
|
|
void (*data_del)(uint32, void*, void*);
|
2011-08-17 12:56:27 -06:00
|
|
|
Type *key, *val;
|
|
|
|
|
|
|
|
key = typ->key;
|
|
|
|
val = typ->elem;
|
2009-09-08 14:46:54 -06:00
|
|
|
|
2010-05-01 14:15:42 -06:00
|
|
|
if(hint < 0 || (int32)hint != hint)
|
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·panicstring("makemap: size out of range");
|
2010-05-01 14:15:42 -06:00
|
|
|
|
2009-09-08 14:46:54 -06:00
|
|
|
keyalg = key->alg;
|
|
|
|
valalg = val->alg;
|
|
|
|
keysize = key->size;
|
|
|
|
valsize = val->size;
|
2008-11-13 11:35:44 -07:00
|
|
|
|
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
|
|
|
if(keyalg >= nelem(runtime·algarray) || runtime·algarray[keyalg].hash == runtime·nohash) {
|
|
|
|
runtime·printf("map(keyalg=%d)\n", keyalg);
|
|
|
|
runtime·throw("runtime.makemap: unsupported map key type");
|
2008-12-19 13:05:22 -07:00
|
|
|
}
|
2008-11-13 11:35:44 -07:00
|
|
|
|
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
|
|
|
if(valalg >= nelem(runtime·algarray)) {
|
|
|
|
runtime·printf("map(valalg=%d)\n", valalg);
|
|
|
|
runtime·throw("runtime.makemap: unsupported map value type");
|
2008-11-13 11:35:44 -07:00
|
|
|
}
|
|
|
|
|
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
|
|
|
h = runtime·mal(sizeof(*h));
|
2009-06-30 21:02:07 -06:00
|
|
|
|
2010-05-19 13:57:47 -06:00
|
|
|
valsize_in_hash = valsize;
|
|
|
|
data_del = donothing;
|
|
|
|
if (valsize > MaxValsize) {
|
|
|
|
h->indirectval = 1;
|
|
|
|
data_del = freedata;
|
|
|
|
valsize_in_hash = sizeof(void*);
|
|
|
|
}
|
|
|
|
|
2009-01-26 16:36:39 -07:00
|
|
|
// align value inside data so that mark-sweep gc can find it.
|
|
|
|
// might remove in the future and just assume datavo == keysize.
|
|
|
|
h->datavo = keysize;
|
2010-05-19 13:57:47 -06:00
|
|
|
if(valsize_in_hash >= sizeof(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
|
|
|
h->datavo = runtime·rnd(keysize, sizeof(void*));
|
2009-01-26 16:36:39 -07:00
|
|
|
|
2010-05-19 13:57:47 -06:00
|
|
|
hash_init(h, h->datavo+valsize_in_hash,
|
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·algarray[keyalg].hash,
|
|
|
|
runtime·algarray[keyalg].equal,
|
2010-05-19 13:57:47 -06:00
|
|
|
data_del,
|
2008-11-13 11:35:44 -07:00
|
|
|
hint);
|
|
|
|
|
|
|
|
h->keysize = keysize;
|
|
|
|
h->valsize = valsize;
|
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
|
|
|
h->keyalg = &runtime·algarray[keyalg];
|
|
|
|
h->valalg = &runtime·algarray[valalg];
|
2009-06-30 21:02:07 -06:00
|
|
|
|
2009-01-26 16:36:39 -07:00
|
|
|
// these calculations are compiler dependent.
|
|
|
|
// figure out offsets of map call arguments.
|
2009-06-30 21:02:07 -06:00
|
|
|
|
|
|
|
// func() (key, val)
|
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
|
|
|
h->ko0 = runtime·rnd(sizeof(h), Structrnd);
|
|
|
|
h->vo0 = runtime·rnd(h->ko0+keysize, val->align);
|
2009-06-30 21:02:07 -06:00
|
|
|
|
|
|
|
// func(key) (val[, pres])
|
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
|
|
|
h->ko1 = runtime·rnd(sizeof(h), key->align);
|
|
|
|
h->vo1 = runtime·rnd(h->ko1+keysize, Structrnd);
|
2011-07-24 23:03:17 -06:00
|
|
|
h->po1 = h->vo1 + valsize;
|
2009-06-30 21:02:07 -06:00
|
|
|
|
|
|
|
// func(key, val[, pres])
|
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
|
|
|
h->ko2 = runtime·rnd(sizeof(h), key->align);
|
|
|
|
h->vo2 = runtime·rnd(h->ko2+keysize, val->align);
|
2011-07-24 23:03:17 -06:00
|
|
|
h->po2 = h->vo2 + valsize;
|
2008-11-13 11:35:44 -07:00
|
|
|
|
|
|
|
if(debug) {
|
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·printf("makemap: map=%p; keysize=%d; valsize=%d; keyalg=%d; valalg=%d; offsets=%d,%d; %d,%d,%d; %d,%d,%d\n",
|
2009-06-30 21:02:07 -06:00
|
|
|
h, keysize, valsize, keyalg, valalg, h->ko0, h->vo0, h->ko1, h->vo1, h->po1, h->ko2, h->vo2, h->po2);
|
2008-11-13 11:35:44 -07:00
|
|
|
}
|
2009-07-08 14:55:57 -06:00
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2010-05-01 14:15:42 -06:00
|
|
|
// makemap(key, val *Type, hint int64) (hmap *map[any]any);
|
2009-07-08 14:55:57 -06:00
|
|
|
void
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·makemap(MapType *typ, int64 hint, Hmap *ret)
|
2009-07-08 14:55:57 -06:00
|
|
|
{
|
2011-08-17 12:56:27 -06:00
|
|
|
ret = runtime·makemap_c(typ, hint);
|
2009-07-08 14:55:57 -06:00
|
|
|
FLUSH(&ret);
|
|
|
|
}
|
|
|
|
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
// For reflect:
|
|
|
|
// func makemap(Type *mapType) (hmap *map)
|
|
|
|
void
|
|
|
|
reflect·makemap(MapType *t, Hmap *ret)
|
|
|
|
{
|
2011-08-17 12:56:27 -06:00
|
|
|
ret = runtime·makemap_c(t, 0);
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
FLUSH(&ret);
|
|
|
|
}
|
|
|
|
|
2009-07-08 14:55:57 -06:00
|
|
|
void
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·mapaccess(MapType *t, Hmap *h, byte *ak, byte *av, bool *pres)
|
2009-07-08 14:55:57 -06:00
|
|
|
{
|
|
|
|
byte *res;
|
2011-08-17 12:56:27 -06:00
|
|
|
Type *elem;
|
2009-07-08 14:55:57 -06:00
|
|
|
|
2011-08-17 12:56:27 -06:00
|
|
|
if(h == nil) {
|
|
|
|
elem = t->elem;
|
|
|
|
runtime·algarray[elem->alg].copy(elem->size, av, nil);
|
|
|
|
*pres = false;
|
|
|
|
return;
|
|
|
|
}
|
2011-02-17 14:08:52 -07:00
|
|
|
|
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
|
|
|
if(runtime·gcwaiting)
|
|
|
|
runtime·gosched();
|
2010-01-09 10:47:45 -07:00
|
|
|
|
2009-07-08 14:55:57 -06:00
|
|
|
res = nil;
|
|
|
|
if(hash_lookup(h, ak, (void**)&res)) {
|
|
|
|
*pres = true;
|
2010-05-19 13:57:47 -06:00
|
|
|
h->valalg->copy(h->valsize, av, hash_indirect(h, res+h->datavo));
|
2009-07-08 14:55:57 -06:00
|
|
|
} else {
|
|
|
|
*pres = false;
|
|
|
|
h->valalg->copy(h->valsize, av, nil);
|
|
|
|
}
|
2008-11-13 11:35:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// mapaccess1(hmap *map[any]any, key any) (val any);
|
2010-03-04 16:34:25 -07:00
|
|
|
#pragma textflag 7
|
2008-11-13 11:35:44 -07:00
|
|
|
void
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·mapaccess1(MapType *t, Hmap *h, ...)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
byte *ak, *av;
|
2009-07-08 14:55:57 -06:00
|
|
|
bool pres;
|
2008-11-13 11:35:44 -07:00
|
|
|
|
2011-08-17 12:56:27 -06:00
|
|
|
if(h == nil) {
|
|
|
|
ak = (byte*)(&h + 1);
|
|
|
|
av = ak + runtime·rnd(t->key->size, Structrnd);
|
|
|
|
} else {
|
|
|
|
ak = (byte*)&h + h->ko1;
|
|
|
|
av = (byte*)&h + h->vo1;
|
|
|
|
}
|
2008-11-13 11:35:44 -07:00
|
|
|
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·mapaccess(t, h, ak, av, &pres);
|
2010-03-23 14:00:02 -06:00
|
|
|
|
2008-11-13 11:35:44 -07:00
|
|
|
if(debug) {
|
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·prints("runtime.mapaccess1: map=");
|
|
|
|
runtime·printpointer(h);
|
|
|
|
runtime·prints("; key=");
|
2008-11-13 11:35:44 -07:00
|
|
|
h->keyalg->print(h->keysize, ak);
|
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·prints("; val=");
|
2008-11-13 11:35:44 -07:00
|
|
|
h->valalg->print(h->valsize, av);
|
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·prints("; pres=");
|
|
|
|
runtime·printbool(pres);
|
|
|
|
runtime·prints("\n");
|
2008-11-13 11:35:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mapaccess2(hmap *map[any]any, key any) (val any, pres bool);
|
2010-03-04 16:34:25 -07:00
|
|
|
#pragma textflag 7
|
2008-11-13 11:35:44 -07:00
|
|
|
void
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·mapaccess2(MapType *t, Hmap *h, ...)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
byte *ak, *av, *ap;
|
|
|
|
|
2011-08-17 12:56:27 -06:00
|
|
|
if(h == nil) {
|
|
|
|
ak = (byte*)(&h + 1);
|
|
|
|
av = ak + runtime·rnd(t->key->size, Structrnd);
|
|
|
|
ap = av + t->elem->size;
|
|
|
|
} else {
|
|
|
|
ak = (byte*)&h + h->ko1;
|
|
|
|
av = (byte*)&h + h->vo1;
|
|
|
|
ap = (byte*)&h + h->po1;
|
|
|
|
}
|
2008-11-13 11:35:44 -07:00
|
|
|
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·mapaccess(t, h, ak, av, ap);
|
2008-11-13 11:35:44 -07:00
|
|
|
|
|
|
|
if(debug) {
|
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·prints("runtime.mapaccess2: map=");
|
|
|
|
runtime·printpointer(h);
|
|
|
|
runtime·prints("; key=");
|
2008-11-13 11:35:44 -07:00
|
|
|
h->keyalg->print(h->keysize, ak);
|
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·prints("; val=");
|
2008-11-13 11:35:44 -07:00
|
|
|
h->valalg->print(h->valsize, av);
|
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·prints("; pres=");
|
|
|
|
runtime·printbool(*ap);
|
|
|
|
runtime·prints("\n");
|
2008-11-13 11:35:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
// For reflect:
|
2011-08-17 12:56:27 -06:00
|
|
|
// func mapaccess(t type, h map, key iword) (val iword, pres bool)
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
// where an iword is the same word an interface value would use:
|
|
|
|
// the actual data if it fits, or else a pointer to the data.
|
|
|
|
void
|
2011-08-17 12:56:27 -06:00
|
|
|
reflect·mapaccess(MapType *t, Hmap *h, uintptr key, uintptr val, bool pres)
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
{
|
|
|
|
byte *ak, *av;
|
|
|
|
|
2011-08-17 12:56:27 -06:00
|
|
|
if(t->key->size <= sizeof(key))
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
ak = (byte*)&key;
|
|
|
|
else
|
|
|
|
ak = (byte*)key;
|
|
|
|
val = 0;
|
|
|
|
pres = false;
|
2011-08-17 12:56:27 -06:00
|
|
|
if(t->elem->size <= sizeof(val))
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
av = (byte*)&val;
|
|
|
|
else {
|
2011-08-17 12:56:27 -06:00
|
|
|
av = runtime·mal(t->elem->size);
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
val = (uintptr)av;
|
|
|
|
}
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·mapaccess(t, h, ak, av, &pres);
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
FLUSH(&val);
|
|
|
|
FLUSH(&pres);
|
|
|
|
}
|
|
|
|
|
2009-07-08 14:55:57 -06:00
|
|
|
void
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·mapassign(MapType *t, Hmap *h, byte *ak, byte *av)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
byte *res;
|
|
|
|
int32 hit;
|
|
|
|
|
2011-08-17 12:56:27 -06:00
|
|
|
USED(t);
|
|
|
|
|
2011-02-17 14:08:52 -07:00
|
|
|
if(h == nil)
|
|
|
|
runtime·panicstring("assignment to entry in nil map");
|
|
|
|
|
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
|
|
|
if(runtime·gcwaiting)
|
|
|
|
runtime·gosched();
|
2010-01-09 10:47:45 -07:00
|
|
|
|
2008-11-13 11:35:44 -07:00
|
|
|
res = nil;
|
2009-07-08 14:55:57 -06:00
|
|
|
if(av == nil) {
|
|
|
|
hash_remove(h, ak, (void**)&res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-13 11:35:44 -07:00
|
|
|
hit = hash_insert(h, ak, (void**)&res);
|
2010-05-19 13:57:47 -06:00
|
|
|
if(!hit && h->indirectval)
|
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
|
|
|
*(void**)(res+h->datavo) = runtime·mal(h->valsize);
|
2008-11-13 11:35:44 -07:00
|
|
|
h->keyalg->copy(h->keysize, res, ak);
|
2010-05-19 13:57:47 -06:00
|
|
|
h->valalg->copy(h->valsize, hash_indirect(h, res+h->datavo), av);
|
2008-11-13 11:35:44 -07:00
|
|
|
|
|
|
|
if(debug) {
|
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·prints("mapassign: map=");
|
|
|
|
runtime·printpointer(h);
|
|
|
|
runtime·prints("; key=");
|
2008-11-13 11:35:44 -07:00
|
|
|
h->keyalg->print(h->keysize, ak);
|
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·prints("; val=");
|
2008-11-13 11:35:44 -07:00
|
|
|
h->valalg->print(h->valsize, av);
|
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·prints("; hit=");
|
|
|
|
runtime·printint(hit);
|
|
|
|
runtime·prints("; res=");
|
|
|
|
runtime·printpointer(res);
|
|
|
|
runtime·prints("\n");
|
2008-11-13 11:35:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-17 12:56:27 -06:00
|
|
|
// mapassign1(mapType *type, hmap *map[any]any, key any, val any);
|
2010-03-04 16:34:25 -07:00
|
|
|
#pragma textflag 7
|
2008-11-13 11:35:44 -07:00
|
|
|
void
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·mapassign1(MapType *t, Hmap *h, ...)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
byte *ak, *av;
|
|
|
|
|
2011-02-17 14:08:52 -07:00
|
|
|
if(h == nil)
|
|
|
|
runtime·panicstring("assignment to entry in nil map");
|
|
|
|
|
2009-06-30 21:02:07 -06:00
|
|
|
ak = (byte*)&h + h->ko2;
|
|
|
|
av = (byte*)&h + h->vo2;
|
2008-11-13 11:35:44 -07:00
|
|
|
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·mapassign(t, h, ak, av);
|
2008-11-13 11:35:44 -07:00
|
|
|
}
|
|
|
|
|
2011-08-17 12:56:27 -06:00
|
|
|
// mapassign2(mapType *type, hmap *map[any]any, key any, val any, pres bool);
|
2010-03-04 16:34:25 -07:00
|
|
|
#pragma textflag 7
|
2008-11-13 11:35:44 -07:00
|
|
|
void
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·mapassign2(MapType *t, Hmap *h, ...)
|
2008-11-13 11:35:44 -07:00
|
|
|
{
|
|
|
|
byte *ak, *av, *ap;
|
|
|
|
|
2011-02-17 14:08:52 -07:00
|
|
|
if(h == nil)
|
|
|
|
runtime·panicstring("assignment to entry in nil map");
|
|
|
|
|
2009-06-30 21:02:07 -06:00
|
|
|
ak = (byte*)&h + h->ko2;
|
|
|
|
av = (byte*)&h + h->vo2;
|
|
|
|
ap = (byte*)&h + h->po2;
|
2008-11-13 11:35:44 -07:00
|
|
|
|
2009-07-08 14:55:57 -06:00
|
|
|
if(*ap == false)
|
|
|
|
av = nil; // delete
|
2008-11-13 11:35:44 -07:00
|
|
|
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·mapassign(t, h, ak, av);
|
2008-11-13 11:35:44 -07:00
|
|
|
|
|
|
|
if(debug) {
|
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·prints("mapassign2: map=");
|
|
|
|
runtime·printpointer(h);
|
|
|
|
runtime·prints("; key=");
|
2008-11-13 11:35:44 -07:00
|
|
|
h->keyalg->print(h->keysize, ak);
|
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·prints("\n");
|
2008-11-13 11:35:44 -07:00
|
|
|
}
|
|
|
|
}
|
2008-12-05 19:24:05 -07:00
|
|
|
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
// For reflect:
|
2011-08-17 12:56:27 -06:00
|
|
|
// func mapassign(t type h map, key, val iword, pres bool)
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
// where an iword is the same word an interface value would use:
|
|
|
|
// the actual data if it fits, or else a pointer to the data.
|
|
|
|
void
|
2011-08-17 12:56:27 -06:00
|
|
|
reflect·mapassign(MapType *t, Hmap *h, uintptr key, uintptr val, bool pres)
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
{
|
|
|
|
byte *ak, *av;
|
|
|
|
|
|
|
|
if(h == nil)
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·panicstring("assignment to entry in nil map");
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
if(h->keysize <= sizeof(key))
|
|
|
|
ak = (byte*)&key;
|
|
|
|
else
|
|
|
|
ak = (byte*)key;
|
|
|
|
if(h->valsize <= sizeof(val))
|
|
|
|
av = (byte*)&val;
|
|
|
|
else
|
|
|
|
av = (byte*)val;
|
|
|
|
if(!pres)
|
|
|
|
av = nil;
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·mapassign(t, h, ak, av);
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
}
|
|
|
|
|
2011-08-17 12:56:27 -06:00
|
|
|
// mapiterinit(mapType *type, hmap *map[any]any, hiter *any);
|
2008-12-05 19:24:05 -07:00
|
|
|
void
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·mapiterinit(MapType*, Hmap *h, struct hash_iter *it)
|
2008-12-05 19:24:05 -07:00
|
|
|
{
|
2009-03-23 19:32:37 -06:00
|
|
|
if(h == nil) {
|
|
|
|
it->data = nil;
|
|
|
|
return;
|
|
|
|
}
|
2008-12-05 19:24:05 -07:00
|
|
|
hash_iter_init(h, it);
|
2010-10-06 09:54:41 -06:00
|
|
|
it->data = hash_next(it);
|
2008-12-05 19:24:05 -07:00
|
|
|
if(debug) {
|
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·prints("runtime.mapiterinit: map=");
|
|
|
|
runtime·printpointer(h);
|
|
|
|
runtime·prints("; iter=");
|
|
|
|
runtime·printpointer(it);
|
|
|
|
runtime·prints("; data=");
|
|
|
|
runtime·printpointer(it->data);
|
|
|
|
runtime·prints("\n");
|
2008-12-05 19:24:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
// For reflect:
|
|
|
|
// func mapiterinit(h map) (it iter)
|
|
|
|
void
|
2011-08-17 12:56:27 -06:00
|
|
|
reflect·mapiterinit(MapType *t, Hmap *h, struct hash_iter *it)
|
2009-07-08 14:55:57 -06:00
|
|
|
{
|
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
|
|
|
it = runtime·mal(sizeof *it);
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
FLUSH(&it);
|
2011-08-17 12:56:27 -06:00
|
|
|
runtime·mapiterinit(t, h, it);
|
2009-07-08 14:55:57 -06:00
|
|
|
}
|
|
|
|
|
2008-12-05 19:24:05 -07:00
|
|
|
// mapiternext(hiter *any);
|
|
|
|
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·mapiternext(struct hash_iter *it)
|
2008-12-05 19:24:05 -07:00
|
|
|
{
|
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
|
|
|
if(runtime·gcwaiting)
|
|
|
|
runtime·gosched();
|
2010-01-09 10:47:45 -07:00
|
|
|
|
2010-10-06 09:54:41 -06:00
|
|
|
it->data = hash_next(it);
|
2008-12-05 19:24:05 -07:00
|
|
|
if(debug) {
|
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·prints("runtime.mapiternext: iter=");
|
|
|
|
runtime·printpointer(it);
|
|
|
|
runtime·prints("; data=");
|
|
|
|
runtime·printpointer(it->data);
|
|
|
|
runtime·prints("\n");
|
2008-12-05 19:24:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
// For reflect:
|
|
|
|
// func mapiternext(it iter)
|
|
|
|
void
|
|
|
|
reflect·mapiternext(struct hash_iter *it)
|
|
|
|
{
|
|
|
|
runtime·mapiternext(it);
|
|
|
|
}
|
|
|
|
|
2008-12-05 19:24:05 -07:00
|
|
|
// mapiter1(hiter *any) (key any);
|
2010-03-04 16:34:25 -07:00
|
|
|
#pragma textflag 7
|
2008-12-05 19:24:05 -07: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·mapiter1(struct hash_iter *it, ...)
|
2008-12-05 19:24:05 -07:00
|
|
|
{
|
|
|
|
Hmap *h;
|
|
|
|
byte *ak, *res;
|
|
|
|
|
|
|
|
h = it->h;
|
2009-06-30 21:02:07 -06:00
|
|
|
ak = (byte*)&it + h->ko0;
|
2008-12-05 19:24:05 -07:00
|
|
|
|
|
|
|
res = it->data;
|
|
|
|
if(res == nil)
|
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("runtime.mapiter1: key:val nil pointer");
|
2008-12-05 19:24:05 -07:00
|
|
|
|
|
|
|
h->keyalg->copy(h->keysize, ak, res);
|
|
|
|
|
|
|
|
if(debug) {
|
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·prints("mapiter2: iter=");
|
|
|
|
runtime·printpointer(it);
|
|
|
|
runtime·prints("; map=");
|
|
|
|
runtime·printpointer(h);
|
|
|
|
runtime·prints("\n");
|
2008-12-05 19:24:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-08 14:55:57 -06:00
|
|
|
bool
|
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·mapiterkey(struct hash_iter *it, void *ak)
|
2009-07-08 14:55:57 -06:00
|
|
|
{
|
|
|
|
Hmap *h;
|
|
|
|
byte *res;
|
|
|
|
|
|
|
|
h = it->h;
|
|
|
|
res = it->data;
|
|
|
|
if(res == nil)
|
|
|
|
return false;
|
|
|
|
h->keyalg->copy(h->keysize, ak, res);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
reflect: more efficient; cannot Set result of NewValue anymore
* Reduces malloc counts during gob encoder/decoder test from 6/6 to 3/5.
The current reflect uses Set to mean two subtly different things.
(1) If you have a reflect.Value v, it might just represent
itself (as in v = reflect.NewValue(42)), in which case calling
v.Set only changed v, not any other data in the program.
(2) If you have a reflect Value v derived from a pointer
or a slice (as in x := []int{42}; v = reflect.NewValue(x).Index(0)),
v represents the value held there. Changing x[0] affects the
value returned by v.Int(), and calling v.Set affects x[0].
This was not really by design; it just happened that way.
The motivation for the new reflect implementation was
to remove mallocs. The use case (1) has an implicit malloc
inside it. If you can do:
v := reflect.NewValue(0)
v.Set(42)
i := v.Int() // i = 42
then that implies that v is referring to some underlying
chunk of memory in order to remember the 42; that is,
NewValue must have allocated some memory.
Almost all the time you are using reflect the goal is to
inspect or to change other data, not to manipulate data
stored solely inside a reflect.Value.
This CL removes use case (1), so that an assignable
reflect.Value must always refer to some other piece of data
in the program. Put another way, removing this case would
make
v := reflect.NewValue(0)
v.Set(42)
as illegal as
0 = 42.
It would also make this illegal:
x := 0
v := reflect.NewValue(x)
v.Set(42)
for the same reason. (Note that right now, v.Set(42) "succeeds"
but does not change the value of x.)
If you really wanted to make v refer to x, you'd start with &x
and dereference it:
x := 0
v := reflect.NewValue(&x).Elem() // v = *&x
v.Set(42)
It's pretty rare, except in tests, to want to use NewValue and then
call Set to change the Value itself instead of some other piece of
data in the program. I haven't seen it happen once yet while
making the tree build with this change.
For the same reasons, reflect.Zero (formerly reflect.MakeZero)
would also return an unassignable, unaddressable value.
This invalidates the (awkward) idiom:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.PointTo(v)
which, when the API changed, turned into:
pv := ... some Ptr Value we have ...
v := reflect.Zero(pv.Type().Elem())
pv.Set(v.Addr())
In both, it is far from clear what the code is trying to do. Now that
it is possible, this CL adds reflect.New(Type) Value that does the
obvious thing (same as Go's new), so this code would be replaced by:
pv := ... some Ptr Value we have ...
pv.Set(reflect.New(pv.Type().Elem()))
The changes just described can be confusing to think about,
but I believe it is because the old API was confusing - it was
conflating two different kinds of Values - and that the new API
by itself is pretty simple: you can only Set (or call Addr on)
a Value if it actually addresses some real piece of data; that is,
only if it is the result of dereferencing a Ptr or indexing a Slice.
If you really want the old behavior, you'd get it by translating:
v := reflect.NewValue(x)
into
v := reflect.New(reflect.Typeof(x)).Elem()
v.Set(reflect.NewValue(x))
Gofix will not be able to help with this, because whether
and how to change the code depends on whether the original
code meant use (1) or use (2), so the developer has to read
and think about the code.
You can see the effect on packages in the tree in
https://golang.org/cl/4423043/.
R=r
CC=golang-dev
https://golang.org/cl/4435042
2011-04-18 12:35:33 -06:00
|
|
|
// For reflect:
|
|
|
|
// func mapiterkey(h map) (key iword, ok bool)
|
|
|
|
// where an iword is the same word an interface value would use:
|
|
|
|
// the actual data if it fits, or else a pointer to the data.
|
|
|
|
void
|
|
|
|
reflect·mapiterkey(struct hash_iter *it, uintptr key, bool ok)
|
|
|
|
{
|
|
|
|
Hmap *h;
|
|
|
|
byte *res;
|
|
|
|
|
|
|
|
key = 0;
|
|
|
|
ok = false;
|
|
|
|
h = it->h;
|
|
|
|
res = it->data;
|
|
|
|
if(res == nil) {
|
|
|
|
key = 0;
|
|
|
|
ok = false;
|
|
|
|
} else {
|
|
|
|
key = 0;
|
|
|
|
if(h->keysize <= sizeof(key))
|
|
|
|
h->keyalg->copy(h->keysize, (byte*)&key, res);
|
|
|
|
else
|
|
|
|
key = (uintptr)res;
|
|
|
|
ok = true;
|
|
|
|
}
|
|
|
|
FLUSH(&key);
|
|
|
|
FLUSH(&ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
// For reflect:
|
|
|
|
// func maplen(h map) (len int32)
|
|
|
|
// Like len(m) in the actual language, we treat the nil map as length 0.
|
|
|
|
void
|
|
|
|
reflect·maplen(Hmap *h, int32 len)
|
|
|
|
{
|
|
|
|
if(h == nil)
|
|
|
|
len = 0;
|
|
|
|
else
|
|
|
|
len = h->count;
|
|
|
|
FLUSH(&len);
|
|
|
|
}
|
|
|
|
|
2008-12-05 19:24:05 -07:00
|
|
|
// mapiter2(hiter *any) (key any, val any);
|
2010-03-04 16:34:25 -07:00
|
|
|
#pragma textflag 7
|
2008-12-05 19:24:05 -07: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·mapiter2(struct hash_iter *it, ...)
|
2008-12-05 19:24:05 -07:00
|
|
|
{
|
|
|
|
Hmap *h;
|
|
|
|
byte *ak, *av, *res;
|
|
|
|
|
|
|
|
h = it->h;
|
2009-06-30 21:02:07 -06:00
|
|
|
ak = (byte*)&it + h->ko0;
|
|
|
|
av = (byte*)&it + h->vo0;
|
2008-12-05 19:24:05 -07:00
|
|
|
|
|
|
|
res = it->data;
|
|
|
|
if(res == nil)
|
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("runtime.mapiter2: key:val nil pointer");
|
2008-12-05 19:24:05 -07:00
|
|
|
|
|
|
|
h->keyalg->copy(h->keysize, ak, res);
|
2010-05-19 13:57:47 -06:00
|
|
|
h->valalg->copy(h->valsize, av, hash_indirect(h, res+h->datavo));
|
2008-12-05 19:24:05 -07:00
|
|
|
|
|
|
|
if(debug) {
|
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·prints("mapiter2: iter=");
|
|
|
|
runtime·printpointer(it);
|
|
|
|
runtime·prints("; map=");
|
|
|
|
runtime·printpointer(h);
|
|
|
|
runtime·prints("\n");
|
2008-12-05 19:24:05 -07:00
|
|
|
}
|
|
|
|
}
|