8
8
buffer ,
9
9
)
10
10
11
- from weakref import ref
12
11
import sys
13
12
from functools import reduce
14
13
@@ -55,8 +54,7 @@ def _destroy(self):
55
54
# Actual client count, which doesn't include the reference kept by the manager, nor ours
56
55
# as we are about to be deleted
57
56
try :
58
- num_clients = self ._rlist .client_count () - 2
59
- if num_clients == 0 and len (self ._rlist ) == 0 :
57
+ if len (self ._rlist ) == 0 :
60
58
# Free all resources associated with the mapped file
61
59
self ._manager ._fdict .pop (self ._rlist .path_or_fd ())
62
60
# END remove regions list from manager
@@ -78,7 +76,7 @@ def _copy_from(self, rhs):
78
76
self ._size = rhs ._size
79
77
80
78
if self ._region is not None :
81
- self ._region .increment_usage_count ()
79
+ self ._region .increment_client_count ()
82
80
# END handle regions
83
81
84
82
def __copy__ (self ):
@@ -126,20 +124,22 @@ def use_region(self, offset=0, size=0, flags=0):
126
124
127
125
if need_region :
128
126
self ._region = man ._obtain_region (self ._rlist , offset , size , flags , False )
127
+ self ._region .increment_client_count ()
129
128
# END need region handling
130
129
131
- self ._region .increment_usage_count ()
132
130
self ._ofs = offset - self ._region ._b
133
131
self ._size = min (size , self ._region .ofs_end () - offset )
134
132
135
133
return self
136
134
137
135
def unuse_region (self ):
138
- """Unuse the ucrrent region. Does nothing if we have no current region
136
+ """Unuse the current region. Does nothing if we have no current region
139
137
140
138
**Note:** the cursor unuses the region automatically upon destruction. It is recommended
141
139
to un-use the region once you are done reading from it in persistent cursors as it
142
140
helps to free up resource more quickly"""
141
+ if self ._region is not None :
142
+ self ._region .increment_client_count (- 1 )
143
143
self ._region = None
144
144
# note: should reset ofs and size, but we spare that for performance. Its not
145
145
# allowed to query information if we are not valid !
@@ -184,12 +184,10 @@ def size(self):
184
184
""":return: amount of bytes we point to"""
185
185
return self ._size
186
186
187
- def region_ref (self ):
188
- """:return: weak ref to our mapped region.
187
+ def region (self ):
188
+ """:return: our mapped region, or None if nothing is mapped yet
189
189
:raise AssertionError: if we have no current region. This is only useful for debugging"""
190
- if self ._region is None :
191
- raise AssertionError ("region not set" )
192
- return ref (self ._region )
190
+ return self ._region
193
191
194
192
def includes_ofs (self , ofs ):
195
193
""":return: True if the given absolute offset is contained in the cursors
@@ -311,8 +309,8 @@ def _collect_lru_region(self, size):
311
309
lru_list = None
312
310
for regions in self ._fdict .values ():
313
311
for region in regions :
314
- # check client count - consider that we keep one reference ourselves !
315
- if (region .client_count () - 2 == 0 and
312
+ # check client count - if it's 1, it's just us
313
+ if (region .client_count () == 1 and
316
314
(lru_region is None or region ._uc < lru_region ._uc )):
317
315
lru_region = region
318
316
lru_list = regions
@@ -326,6 +324,7 @@ def _collect_lru_region(self, size):
326
324
327
325
num_found += 1
328
326
del (lru_list [lru_list .index (lru_region )])
327
+ lru_region .increment_client_count (- 1 )
329
328
self ._memory_size -= lru_region .size ()
330
329
self ._handle_count -= 1
331
330
# END while there is more memory to free
@@ -449,7 +448,7 @@ def force_map_handle_removal_win(self, base_path):
449
448
for path , rlist in self ._fdict .items ():
450
449
if path .startswith (base_path ):
451
450
for region in rlist :
452
- region ._mf . close ()
451
+ region .release ()
453
452
num_closed += 1
454
453
# END path matches
455
454
# END for each path
0 commit comments