| 1 |
from rpython.rtyper.lltypesystem import lltype, llmemory |
| 2 |
from rpython.rlib.objectmodel import free_non_gc_object, we_are_translated |
| 3 |
from rpython.rlib.debug import ll_assert |
| 4 |
from rpython.tool.identity_dict import identity_dict |
| 5 |
from rpython.rtyper.rclass import NONGCOBJECTPTR |
| 6 |
from rpython.rtyper.annlowlevel import cast_nongc_instance_to_base_ptr |
| 7 |
from rpython.rtyper.annlowlevel import cast_base_ptr_to_nongc_instance |
| 8 |
|
| 9 |
|
| 10 |
def mangle_hash(i): |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
return i ^ (i >> 4) |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
DEFAULT_CHUNK_SIZE = 1019 |
| 19 |
|
| 20 |
|
| 21 |
def get_chunk_manager(chunk_size=DEFAULT_CHUNK_SIZE, cache={}): |
| 22 |
try: |
| 23 |
return cache[chunk_size] |
| 24 |
except KeyError: |
| 25 |
pass |
| 26 |
|
| 27 |
CHUNK = lltype.ForwardReference() |
| 28 |
CHUNK.become(lltype.Struct('AddressChunk', |
| 29 |
('next', lltype.Ptr(CHUNK)), |
| 30 |
('items', lltype.FixedSizeArray( |
| 31 |
llmemory.Address, chunk_size)))) |
| 32 |
null_chunk = lltype.nullptr(CHUNK) |
| 33 |
|
| 34 |
class FreeList(object): |
| 35 |
_alloc_flavor_ = "raw" |
| 36 |
|
| 37 |
def __init__(self): |
| 38 |
self.free_list = null_chunk |
| 39 |
|
| 40 |
def get(self): |
| 41 |
if not self.free_list: |
| 42 |
|
| 43 |
|
| 44 |
zero = not we_are_translated() |
| 45 |
return lltype.malloc(CHUNK, flavor="raw", zero=zero, |
| 46 |
track_allocation=False) |
| 47 |
|
| 48 |
result = self.free_list |
| 49 |
self.free_list = result.next |
| 50 |
return result |
| 51 |
|
| 52 |
def put(self, chunk): |
| 53 |
if we_are_translated(): |
| 54 |
chunk.next = self.free_list |
| 55 |
self.free_list = chunk |
| 56 |
else: |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
lltype.free(chunk, flavor="raw", track_allocation=False) |
| 61 |
|
| 62 |
unused_chunks = FreeList() |
| 63 |
cache[chunk_size] = unused_chunks, null_chunk |
| 64 |
|
| 65 |
def partition(array, left, right): |
| 66 |
last_item = array[right] |
| 67 |
pivot = last_item |
| 68 |
storeindex = left |
| 69 |
for i in range(left, right): |
| 70 |
if array[i] >= pivot: |
| 71 |
array[i], array[storeindex] = array[storeindex], array[i] |
| 72 |
storeindex += 1 |
| 73 |
|
| 74 |
array[storeindex], array[right] = last_item, array[storeindex] |
| 75 |
return storeindex |
| 76 |
|
| 77 |
def quicksort(array, left, right): |
| 78 |
|
| 79 |
if right > left: |
| 80 |
pivotnewindex = partition(array, left, right) |
| 81 |
quicksort(array, left, pivotnewindex - 1) |
| 82 |
quicksort(array, pivotnewindex + 1, right) |
| 83 |
|
| 84 |
def sort_chunk(chunk, size): |
| 85 |
quicksort(chunk.items, 0, size - 1) |
| 86 |
|
| 87 |
return unused_chunks, null_chunk, sort_chunk |
| 88 |
|
| 89 |
|
| 90 |
def get_address_stack(chunk_size=DEFAULT_CHUNK_SIZE, cache={}): |
| 91 |
try: |
| 92 |
return cache[chunk_size] |
| 93 |
except KeyError: |
| 94 |
pass |
| 95 |
|
| 96 |
unused_chunks, null_chunk, sort_chunk = get_chunk_manager(chunk_size) |
| 97 |
|
| 98 |
class AddressStack(object): |
| 99 |
_alloc_flavor_ = "raw" |
| 100 |
|
| 101 |
def __init__(self): |
| 102 |
self.chunk = unused_chunks.get() |
| 103 |
self.chunk.next = null_chunk |
| 104 |
self.used_in_last_chunk = 0 |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
def enlarge(self): |
| 109 |
new = unused_chunks.get() |
| 110 |
new.next = self.chunk |
| 111 |
self.chunk = new |
| 112 |
self.used_in_last_chunk = 0 |
| 113 |
enlarge._dont_inline_ = True |
| 114 |
|
| 115 |
def shrink(self): |
| 116 |
old = self.chunk |
| 117 |
self.chunk = old.next |
| 118 |
unused_chunks.put(old) |
| 119 |
self.used_in_last_chunk = chunk_size |
| 120 |
shrink._dont_inline_ = True |
| 121 |
|
| 122 |
def append(self, addr): |
| 123 |
used = self.used_in_last_chunk |
| 124 |
if used == chunk_size: |
| 125 |
self.enlarge() |
| 126 |
used = 0 |
| 127 |
self.chunk.items[used] = addr |
| 128 |
self.used_in_last_chunk = used + 1 |
| 129 |
|
| 130 |
def non_empty(self): |
| 131 |
return self.used_in_last_chunk != 0 |
| 132 |
|
| 133 |
def pop(self): |
| 134 |
used = self.used_in_last_chunk - 1 |
| 135 |
ll_assert(used >= 0, "pop on empty AddressStack") |
| 136 |
result = self.chunk.items[used] |
| 137 |
self.used_in_last_chunk = used |
| 138 |
if used == 0 and self.chunk.next: |
| 139 |
self.shrink() |
| 140 |
return result |
| 141 |
|
| 142 |
def delete(self): |
| 143 |
cur = self.chunk |
| 144 |
while cur: |
| 145 |
next = cur.next |
| 146 |
unused_chunks.put(cur) |
| 147 |
cur = next |
| 148 |
free_non_gc_object(self) |
| 149 |
|
| 150 |
def length(self): |
| 151 |
chunk = self.chunk |
| 152 |
result = 0 |
| 153 |
count = self.used_in_last_chunk |
| 154 |
while chunk: |
| 155 |
result += count |
| 156 |
chunk = chunk.next |
| 157 |
count = chunk_size |
| 158 |
return result |
| 159 |
|
| 160 |
def foreach(self, callback, arg): |
| 161 |
"""Invoke 'callback(address, arg)' for all addresses in the stack. |
| 162 |
Typically, 'callback' is a bound method and 'arg' can be None. |
| 163 |
""" |
| 164 |
chunk = self.chunk |
| 165 |
count = self.used_in_last_chunk |
| 166 |
while chunk: |
| 167 |
while count > 0: |
| 168 |
count -= 1 |
| 169 |
callback(chunk.items[count], arg) |
| 170 |
chunk = chunk.next |
| 171 |
count = chunk_size |
| 172 |
foreach._annspecialcase_ = 'specialize:arg(1)' |
| 173 |
|
| 174 |
def stack2dict(self): |
| 175 |
result = AddressDict(self.length()) |
| 176 |
self.foreach(_add_in_dict, result) |
| 177 |
return result |
| 178 |
|
| 179 |
def tolist(self): |
| 180 |
"""NOT_RPYTHON. Returns the content as a list.""" |
| 181 |
lst = [] |
| 182 |
def _add(obj, lst): |
| 183 |
lst.append(obj) |
| 184 |
self.foreach(_add, lst) |
| 185 |
return lst |
| 186 |
|
| 187 |
def remove(self, addr): |
| 188 |
"""Remove 'addr' from the stack. The addr *must* be in the list, |
| 189 |
and preferrably near the top. |
| 190 |
""" |
| 191 |
got = self.pop() |
| 192 |
chunk = self.chunk |
| 193 |
count = self.used_in_last_chunk |
| 194 |
while got != addr: |
| 195 |
count -= 1 |
| 196 |
if count < 0: |
| 197 |
chunk = chunk.next |
| 198 |
count = chunk_size - 1 |
| 199 |
next = chunk.items[count] |
| 200 |
chunk.items[count] = got |
| 201 |
got = next |
| 202 |
|
| 203 |
def sort(self): |
| 204 |
"""Sorts the items in the AddressStack. They must not be more |
| 205 |
than one chunk of them. This results in a **reverse** order, |
| 206 |
so that the first pop()ped items are the smallest ones.""" |
| 207 |
ll_assert(self.chunk.next == null_chunk, "too big for sorting") |
| 208 |
sort_chunk(self.chunk, self.used_in_last_chunk) |
| 209 |
|
| 210 |
cache[chunk_size] = AddressStack |
| 211 |
return AddressStack |
| 212 |
|
| 213 |
def _add_in_dict(item, d): |
| 214 |
d.add(item) |
| 215 |
|
| 216 |
|
| 217 |
def get_address_deque(chunk_size=DEFAULT_CHUNK_SIZE, cache={}): |
| 218 |
try: |
| 219 |
return cache[chunk_size] |
| 220 |
except KeyError: |
| 221 |
pass |
| 222 |
|
| 223 |
unused_chunks, null_chunk = get_chunk_manager(chunk_size) |
| 224 |
|
| 225 |
class AddressDeque(object): |
| 226 |
_alloc_flavor_ = "raw" |
| 227 |
|
| 228 |
def __init__(self): |
| 229 |
chunk = unused_chunks.get() |
| 230 |
chunk.next = null_chunk |
| 231 |
self.oldest_chunk = self.newest_chunk = chunk |
| 232 |
self.index_in_oldest = 0 |
| 233 |
self.index_in_newest = 0 |
| 234 |
|
| 235 |
def enlarge(self): |
| 236 |
new = unused_chunks.get() |
| 237 |
new.next = null_chunk |
| 238 |
self.newest_chunk.next = new |
| 239 |
self.newest_chunk = new |
| 240 |
self.index_in_newest = 0 |
| 241 |
enlarge._dont_inline_ = True |
| 242 |
|
| 243 |
def shrink(self): |
| 244 |
old = self.oldest_chunk |
| 245 |
self.oldest_chunk = old.next |
| 246 |
unused_chunks.put(old) |
| 247 |
self.index_in_oldest = 0 |
| 248 |
shrink._dont_inline_ = True |
| 249 |
|
| 250 |
def append(self, addr): |
| 251 |
index = self.index_in_newest |
| 252 |
if index == chunk_size: |
| 253 |
self.enlarge() |
| 254 |
index = 0 |
| 255 |
self.newest_chunk.items[index] = addr |
| 256 |
self.index_in_newest = index + 1 |
| 257 |
|
| 258 |
def non_empty(self): |
| 259 |
return (self.oldest_chunk != self.newest_chunk |
| 260 |
or self.index_in_oldest < self.index_in_newest) |
| 261 |
|
| 262 |
def popleft(self): |
| 263 |
ll_assert(self.non_empty(), "pop on empty AddressDeque") |
| 264 |
index = self.index_in_oldest |
| 265 |
if index == chunk_size: |
| 266 |
self.shrink() |
| 267 |
index = 0 |
| 268 |
result = self.oldest_chunk.items[index] |
| 269 |
self.index_in_oldest = index + 1 |
| 270 |
return result |
| 271 |
|
| 272 |
def foreach(self, callback, arg, step=1): |
| 273 |
"""Invoke 'callback(address, arg)' for all addresses in the deque. |
| 274 |
Typically, 'callback' is a bound method and 'arg' can be None. |
| 275 |
If step > 1, only calls it for addresses multiple of 'step'. |
| 276 |
""" |
| 277 |
chunk = self.oldest_chunk |
| 278 |
index = self.index_in_oldest |
| 279 |
while chunk is not self.newest_chunk: |
| 280 |
while index < chunk_size: |
| 281 |
callback(chunk.items[index], arg) |
| 282 |
index += step |
| 283 |
chunk = chunk.next |
| 284 |
index -= chunk_size |
| 285 |
limit = self.index_in_newest |
| 286 |
while index < limit: |
| 287 |
callback(chunk.items[index], arg) |
| 288 |
index += step |
| 289 |
foreach._annspecialcase_ = 'specialize:arg(1)' |
| 290 |
|
| 291 |
def delete(self): |
| 292 |
cur = self.oldest_chunk |
| 293 |
while cur: |
| 294 |
next = cur.next |
| 295 |
unused_chunks.put(cur) |
| 296 |
cur = next |
| 297 |
free_non_gc_object(self) |
| 298 |
|
| 299 |
def _was_freed(self): |
| 300 |
return False |
| 301 |
|
| 302 |
cache[chunk_size] = AddressDeque |
| 303 |
return AddressDeque |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
def AddressDict(length_estimate=0): |
| 308 |
if we_are_translated(): |
| 309 |
from rpython.memory import lldict |
| 310 |
return lldict.newdict(length_estimate) |
| 311 |
else: |
| 312 |
return BasicAddressDict() |
| 313 |
|
| 314 |
def null_address_dict(): |
| 315 |
from rpython.memory import lldict |
| 316 |
return lltype.nullptr(lldict.DICT) |
| 317 |
|
| 318 |
class BasicAddressDict(object): |
| 319 |
|
| 320 |
def __init__(self): |
| 321 |
self.data = identity_dict() |
| 322 |
|
| 323 |
def _key(self, addr): |
| 324 |
"NOT_RPYTHON: prebuilt AddressDicts are not supported" |
| 325 |
return addr._fixup().ptr._obj |
| 326 |
|
| 327 |
def _wrapkey(self, obj): |
| 328 |
return llmemory.cast_ptr_to_adr(obj._as_ptr()) |
| 329 |
|
| 330 |
def delete(self): |
| 331 |
pass |
| 332 |
|
| 333 |
def length(self): |
| 334 |
return len(self.data) |
| 335 |
|
| 336 |
def contains(self, keyaddr): |
| 337 |
return self._key(keyaddr) in self.data |
| 338 |
|
| 339 |
def get(self, keyaddr, default=llmemory.NULL): |
| 340 |
return self.data.get(self._key(keyaddr), default) |
| 341 |
|
| 342 |
def setitem(self, keyaddr, valueaddr): |
| 343 |
assert keyaddr |
| 344 |
self.data[self._key(keyaddr)] = valueaddr |
| 345 |
|
| 346 |
def insertclean(self, keyaddr, valueaddr): |
| 347 |
assert keyaddr |
| 348 |
key = self._key(keyaddr) |
| 349 |
assert key not in self.data |
| 350 |
self.data[key] = valueaddr |
| 351 |
|
| 352 |
def add(self, keyaddr): |
| 353 |
self.setitem(keyaddr, llmemory.NULL) |
| 354 |
|
| 355 |
def clear(self): |
| 356 |
self.data.clear() |
| 357 |
|
| 358 |
def foreach(self, callback, arg): |
| 359 |
"""Invoke 'callback(key, value, arg)' for all items in the dict. |
| 360 |
Typically, 'callback' is a bound method and 'arg' can be None.""" |
| 361 |
for key, value in self.data.iteritems(): |
| 362 |
callback(self._wrapkey(key), value, arg) |
| 363 |
|
| 364 |
|
| 365 |
def copy_and_update(dict, surviving, updated_address): |
| 366 |
"""Make a copy of 'dict' in which the keys are updated as follows: |
| 367 |
* if surviving(key) returns False, the item is removed |
| 368 |
* otherwise, updated_address(key) is inserted in the copy. |
| 369 |
""" |
| 370 |
newdict = AddressDict |
| 371 |
if not we_are_translated(): |
| 372 |
|
| 373 |
if not isinstance(dict, BasicAddressDict): |
| 374 |
from rpython.memory.lldict import newdict |
| 375 |
result = newdict(dict.length()) |
| 376 |
dict.foreach(_get_updater(surviving, updated_address), result) |
| 377 |
return result |
| 378 |
copy_and_update._annspecialcase_ = 'specialize:arg(1,2)' |
| 379 |
|
| 380 |
def _get_updater(surviving, updated_address): |
| 381 |
def callback(key, value, arg): |
| 382 |
if surviving(key): |
| 383 |
newkey = updated_address(key) |
| 384 |
arg.setitem(newkey, value) |
| 385 |
return callback |
| 386 |
_get_updater._annspecialcase_ = 'specialize:memo' |
| 387 |
|
| 388 |
|
| 389 |
def copy_without_null_values(dict): |
| 390 |
"""Make a copy of 'dict' without the key/value pairs where value==NULL.""" |
| 391 |
newdict = AddressDict |
| 392 |
if not we_are_translated(): |
| 393 |
|
| 394 |
if not isinstance(dict, BasicAddressDict): |
| 395 |
from rpython.memory.lldict import newdict |
| 396 |
result = newdict() |
| 397 |
dict.foreach(_null_value_checker, result) |
| 398 |
return result |
| 399 |
|
| 400 |
def _null_value_checker(key, value, arg): |
| 401 |
if value: |
| 402 |
arg.setitem(key, value) |