Fix signed integer overflow in data-streamer.c
[official-gcc.git] / libgo / runtime / go-map-range.c
blob5dbb92ccb89aade9ed3ac11c6a13836b89ec3de2
1 /* go-map-range.c -- implement a range clause over a map.
3 Copyright 2009, 2010 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
7 #include "runtime.h"
8 #include "go-assert.h"
9 #include "map.h"
11 /* Initialize a range over a map. */
13 void
14 __go_mapiterinit (const struct __go_map *h, struct __go_hash_iter *it)
16 it->entry = NULL;
17 if (h != NULL)
19 it->map = h;
20 it->next_entry = NULL;
21 it->bucket = 0;
22 --it->bucket;
23 __go_mapiternext(it);
27 /* Move to the next iteration, updating *HITER. */
29 void
30 __go_mapiternext (struct __go_hash_iter *it)
32 const void *entry;
34 entry = it->next_entry;
35 if (entry == NULL)
37 const struct __go_map *map;
38 uintptr_t bucket;
40 map = it->map;
41 bucket = it->bucket;
42 while (1)
44 ++bucket;
45 if (bucket >= map->__bucket_count)
47 /* Map iteration is complete. */
48 it->entry = NULL;
49 return;
51 entry = map->__buckets[bucket];
52 if (entry != NULL)
53 break;
55 it->bucket = bucket;
57 it->entry = entry;
58 it->next_entry = *(const void * const *) entry;
61 /* Get the key of the current iteration. */
63 void
64 __go_mapiter1 (struct __go_hash_iter *it, unsigned char *key)
66 const struct __go_map *map;
67 const struct __go_map_descriptor *descriptor;
68 const struct __go_type_descriptor *key_descriptor;
69 const char *p;
71 map = it->map;
72 descriptor = map->__descriptor;
73 key_descriptor = descriptor->__map_descriptor->__key_type;
74 p = it->entry;
75 __go_assert (p != NULL);
76 __builtin_memcpy (key, p + descriptor->__key_offset, key_descriptor->__size);
79 /* Get the key and value of the current iteration. */
81 void
82 __go_mapiter2 (struct __go_hash_iter *it, unsigned char *key,
83 unsigned char *val)
85 const struct __go_map *map;
86 const struct __go_map_descriptor *descriptor;
87 const struct __go_map_type *map_descriptor;
88 const struct __go_type_descriptor *key_descriptor;
89 const struct __go_type_descriptor *val_descriptor;
90 const char *p;
92 map = it->map;
93 descriptor = map->__descriptor;
94 map_descriptor = descriptor->__map_descriptor;
95 key_descriptor = map_descriptor->__key_type;
96 val_descriptor = map_descriptor->__val_type;
97 p = it->entry;
98 __go_assert (p != NULL);
99 __builtin_memcpy (key, p + descriptor->__key_offset,
100 key_descriptor->__size);
101 __builtin_memcpy (val, p + descriptor->__val_offset,
102 val_descriptor->__size);