win32-mmap 0.2.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +3 -2
- data/README +39 -40
- data/Rakefile +5 -5
- data/lib/win32/mmap.rb +392 -397
- data/lib/win32/windows/constants.rb +34 -0
- data/lib/win32/windows/functions.rb +50 -0
- data/lib/win32/windows/structs.rb +28 -0
- data/test/test_win32_mmap.rb +96 -96
- data/win32-mmap.gemspec +2 -4
- metadata +45 -50
    
        data/lib/win32/mmap.rb
    CHANGED
    
    | @@ -1,397 +1,392 @@ | |
| 1 | 
            -
            require 'windows | 
| 2 | 
            -
            require 'windows | 
| 3 | 
            -
            require 'windows | 
| 4 | 
            -
             | 
| 5 | 
            -
             | 
| 6 | 
            -
             | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            -
            # | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 41 | 
            -
             | 
| 42 | 
            -
             | 
| 43 | 
            -
             | 
| 44 | 
            -
             | 
| 45 | 
            -
             | 
| 46 | 
            -
             | 
| 47 | 
            -
             | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 50 | 
            -
             | 
| 51 | 
            -
             | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 54 | 
            -
             | 
| 55 | 
            -
             | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 58 | 
            -
             | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
             | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 74 | 
            -
             | 
| 75 | 
            -
             | 
| 76 | 
            -
             | 
| 77 | 
            -
             | 
| 78 | 
            -
             | 
| 79 | 
            -
             | 
| 80 | 
            -
             | 
| 81 | 
            -
             | 
| 82 | 
            -
             | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 85 | 
            -
             | 
| 86 | 
            -
             | 
| 87 | 
            -
             | 
| 88 | 
            -
             | 
| 89 | 
            -
             | 
| 90 | 
            -
             | 
| 91 | 
            -
             | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 98 | 
            -
             | 
| 99 | 
            -
             | 
| 100 | 
            -
             | 
| 101 | 
            -
             | 
| 102 | 
            -
             | 
| 103 | 
            -
             | 
| 104 | 
            -
             | 
| 105 | 
            -
             | 
| 106 | 
            -
             | 
| 107 | 
            -
             | 
| 108 | 
            -
             | 
| 109 | 
            -
             | 
| 110 | 
            -
             | 
| 111 | 
            -
             | 
| 112 | 
            -
             | 
| 113 | 
            -
             | 
| 114 | 
            -
             | 
| 115 | 
            -
             | 
| 116 | 
            -
             | 
| 117 | 
            -
             | 
| 118 | 
            -
             | 
| 119 | 
            -
             | 
| 120 | 
            -
             | 
| 121 | 
            -
             | 
| 122 | 
            -
             | 
| 123 | 
            -
             | 
| 124 | 
            -
             | 
| 125 | 
            -
             | 
| 126 | 
            -
             | 
| 127 | 
            -
             | 
| 128 | 
            -
             | 
| 129 | 
            -
             | 
| 130 | 
            -
             | 
| 131 | 
            -
             | 
| 132 | 
            -
             | 
| 133 | 
            -
             | 
| 134 | 
            -
                   | 
| 135 | 
            -
             | 
| 136 | 
            -
             | 
| 137 | 
            -
                   | 
| 138 | 
            -
             | 
| 139 | 
            -
                   | 
| 140 | 
            -
                   | 
| 141 | 
            -
                   | 
| 142 | 
            -
             | 
| 143 | 
            -
                  # | 
| 144 | 
            -
                   | 
| 145 | 
            -
             | 
| 146 | 
            -
             | 
| 147 | 
            -
             | 
| 148 | 
            -
             | 
| 149 | 
            -
             | 
| 150 | 
            -
             | 
| 151 | 
            -
             | 
| 152 | 
            -
             | 
| 153 | 
            -
             | 
| 154 | 
            -
             | 
| 155 | 
            -
             | 
| 156 | 
            -
             | 
| 157 | 
            -
             | 
| 158 | 
            -
             | 
| 159 | 
            -
             | 
| 160 | 
            -
             | 
| 161 | 
            -
             | 
| 162 | 
            -
             | 
| 163 | 
            -
             | 
| 164 | 
            -
             | 
| 165 | 
            -
             | 
| 166 | 
            -
             | 
| 167 | 
            -
             | 
| 168 | 
            -
             | 
| 169 | 
            -
             | 
| 170 | 
            -
             | 
| 171 | 
            -
             | 
| 172 | 
            -
             | 
| 173 | 
            -
             | 
| 174 | 
            -
             | 
| 175 | 
            -
             | 
| 176 | 
            -
             | 
| 177 | 
            -
             | 
| 178 | 
            -
             | 
| 179 | 
            -
             | 
| 180 | 
            -
             | 
| 181 | 
            -
             | 
| 182 | 
            -
             | 
| 183 | 
            -
             | 
| 184 | 
            -
             | 
| 185 | 
            -
             | 
| 186 | 
            -
             | 
| 187 | 
            -
             | 
| 188 | 
            -
             | 
| 189 | 
            -
             | 
| 190 | 
            -
             | 
| 191 | 
            -
             | 
| 192 | 
            -
             | 
| 193 | 
            -
             | 
| 194 | 
            -
             | 
| 195 | 
            -
             | 
| 196 | 
            -
             | 
| 197 | 
            -
             | 
| 198 | 
            -
             | 
| 199 | 
            -
             | 
| 200 | 
            -
             | 
| 201 | 
            -
             | 
| 202 | 
            -
             | 
| 203 | 
            -
             | 
| 204 | 
            -
             | 
| 205 | 
            -
             | 
| 206 | 
            -
             | 
| 207 | 
            -
             | 
| 208 | 
            -
             | 
| 209 | 
            -
             | 
| 210 | 
            -
             | 
| 211 | 
            -
             | 
| 212 | 
            -
             | 
| 213 | 
            -
             | 
| 214 | 
            -
             | 
| 215 | 
            -
             | 
| 216 | 
            -
             | 
| 217 | 
            -
             | 
| 218 | 
            -
             | 
| 219 | 
            -
             | 
| 220 | 
            -
             | 
| 221 | 
            -
             | 
| 222 | 
            -
                      | 
| 223 | 
            -
             | 
| 224 | 
            -
             | 
| 225 | 
            -
             | 
| 226 | 
            -
             | 
| 227 | 
            -
             | 
| 228 | 
            -
             | 
| 229 | 
            -
             | 
| 230 | 
            -
             | 
| 231 | 
            -
             | 
| 232 | 
            -
             | 
| 233 | 
            -
             | 
| 234 | 
            -
             | 
| 235 | 
            -
             | 
| 236 | 
            -
             | 
| 237 | 
            -
             | 
| 238 | 
            -
             | 
| 239 | 
            -
             | 
| 240 | 
            -
                   | 
| 241 | 
            -
                   | 
| 242 | 
            -
             | 
| 243 | 
            -
             | 
| 244 | 
            -
             | 
| 245 | 
            -
             | 
| 246 | 
            -
             | 
| 247 | 
            -
             | 
| 248 | 
            -
             | 
| 249 | 
            -
             | 
| 250 | 
            -
             | 
| 251 | 
            -
             | 
| 252 | 
            -
             | 
| 253 | 
            -
             | 
| 254 | 
            -
                  
         | 
| 255 | 
            -
             | 
| 256 | 
            -
                   | 
| 257 | 
            -
             | 
| 258 | 
            -
                   | 
| 259 | 
            -
             | 
| 260 | 
            -
             | 
| 261 | 
            -
             | 
| 262 | 
            -
             | 
| 263 | 
            -
             | 
| 264 | 
            -
             | 
| 265 | 
            -
             | 
| 266 | 
            -
             | 
| 267 | 
            -
             | 
| 268 | 
            -
             | 
| 269 | 
            -
             | 
| 270 | 
            -
             | 
| 271 | 
            -
             | 
| 272 | 
            -
                   | 
| 273 | 
            -
             | 
| 274 | 
            -
                   | 
| 275 | 
            -
             | 
| 276 | 
            -
             | 
| 277 | 
            -
             | 
| 278 | 
            -
             | 
| 279 | 
            -
             | 
| 280 | 
            -
             | 
| 281 | 
            -
             | 
| 282 | 
            -
                   | 
| 283 | 
            -
             | 
| 284 | 
            -
             | 
| 285 | 
            -
             | 
| 286 | 
            -
             | 
| 287 | 
            -
             | 
| 288 | 
            -
             | 
| 289 | 
            -
             | 
| 290 | 
            -
             | 
| 291 | 
            -
             | 
| 292 | 
            -
                   | 
| 293 | 
            -
             | 
| 294 | 
            -
             | 
| 295 | 
            -
             | 
| 296 | 
            -
             | 
| 297 | 
            -
             | 
| 298 | 
            -
             | 
| 299 | 
            -
             | 
| 300 | 
            -
             | 
| 301 | 
            -
             | 
| 302 | 
            -
             | 
| 303 | 
            -
             | 
| 304 | 
            -
             | 
| 305 | 
            -
             | 
| 306 | 
            -
             | 
| 307 | 
            -
                  
         | 
| 308 | 
            -
                   | 
| 309 | 
            -
             | 
| 310 | 
            -
                   | 
| 311 | 
            -
             | 
| 312 | 
            -
             | 
| 313 | 
            -
             | 
| 314 | 
            -
             | 
| 315 | 
            -
             | 
| 316 | 
            -
             | 
| 317 | 
            -
             | 
| 318 | 
            -
             | 
| 319 | 
            -
             | 
| 320 | 
            -
                         | 
| 321 | 
            -
             | 
| 322 | 
            -
             | 
| 323 | 
            -
             | 
| 324 | 
            -
             | 
| 325 | 
            -
             | 
| 326 | 
            -
             | 
| 327 | 
            -
             | 
| 328 | 
            -
             | 
| 329 | 
            -
             | 
| 330 | 
            -
             | 
| 331 | 
            -
             | 
| 332 | 
            -
             | 
| 333 | 
            -
             | 
| 334 | 
            -
                         | 
| 335 | 
            -
             | 
| 336 | 
            -
                         | 
| 337 | 
            -
                         | 
| 338 | 
            -
             | 
| 339 | 
            -
             | 
| 340 | 
            -
             | 
| 341 | 
            -
             | 
| 342 | 
            -
             | 
| 343 | 
            -
             | 
| 344 | 
            -
             | 
| 345 | 
            -
             | 
| 346 | 
            -
             | 
| 347 | 
            -
             | 
| 348 | 
            -
             | 
| 349 | 
            -
             | 
| 350 | 
            -
             | 
| 351 | 
            -
             | 
| 352 | 
            -
             | 
| 353 | 
            -
             | 
| 354 | 
            -
             | 
| 355 | 
            -
             | 
| 356 | 
            -
                   | 
| 357 | 
            -
             | 
| 358 | 
            -
                   | 
| 359 | 
            -
             | 
| 360 | 
            -
             | 
| 361 | 
            -
             | 
| 362 | 
            -
             | 
| 363 | 
            -
             | 
| 364 | 
            -
             | 
| 365 | 
            -
             | 
| 366 | 
            -
             | 
| 367 | 
            -
             | 
| 368 | 
            -
             | 
| 369 | 
            -
             | 
| 370 | 
            -
             | 
| 371 | 
            -
             | 
| 372 | 
            -
                   | 
| 373 | 
            -
             | 
| 374 | 
            -
                   | 
| 375 | 
            -
             | 
| 376 | 
            -
             | 
| 377 | 
            -
             | 
| 378 | 
            -
             | 
| 379 | 
            -
             | 
| 380 | 
            -
             | 
| 381 | 
            -
             | 
| 382 | 
            -
             | 
| 383 | 
            -
             | 
| 384 | 
            -
             | 
| 385 | 
            -
             | 
| 386 | 
            -
             | 
| 387 | 
            -
                  
         | 
| 388 | 
            -
                   | 
| 389 | 
            -
                   | 
| 390 | 
            -
             | 
| 391 | 
            -
             | 
| 392 | 
            -
             | 
| 393 | 
            -
                     VirtualQuery(@address, mbi, mbi.size)
         | 
| 394 | 
            -
                     mbi[12,4].unpack('L').first # RegionSize
         | 
| 395 | 
            -
                  end
         | 
| 396 | 
            -
               end
         | 
| 397 | 
            -
            end
         | 
| 1 | 
            +
            require File.join(File.dirname(__FILE__), 'windows', 'constants')
         | 
| 2 | 
            +
            require File.join(File.dirname(__FILE__), 'windows', 'structs')
         | 
| 3 | 
            +
            require File.join(File.dirname(__FILE__), 'windows', 'functions')
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            # The Win32 module serves as a namespace only.
         | 
| 6 | 
            +
            #
         | 
| 7 | 
            +
            module Win32
         | 
| 8 | 
            +
              # The MMap class encapsulates functions for memory mapped files.
         | 
| 9 | 
            +
              #
         | 
| 10 | 
            +
              class MMap
         | 
| 11 | 
            +
                # The version of the win32-mmap library.
         | 
| 12 | 
            +
                VERSION = '0.3.0'
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                include Windows::Constants
         | 
| 15 | 
            +
                include Windows::Functions
         | 
| 16 | 
            +
                include Windows::Structs
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                # The name of the file from which to create a mapping object.  This
         | 
| 19 | 
            +
                # value may be nil.
         | 
| 20 | 
            +
                #
         | 
| 21 | 
            +
                attr_accessor :file
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                # The protection for the file view.  This may be any of the following
         | 
| 24 | 
            +
                # values:
         | 
| 25 | 
            +
                #
         | 
| 26 | 
            +
                # * PAGE_READONLY
         | 
| 27 | 
            +
                # * PAGE_READWRITE
         | 
| 28 | 
            +
                # * PAGE_WRITECOPY
         | 
| 29 | 
            +
                # * PAGE_WRITECOPY
         | 
| 30 | 
            +
                # * PAGE_EXECUTE_READ
         | 
| 31 | 
            +
                # * PAGE_EXECUTE_READWRITE
         | 
| 32 | 
            +
                #
         | 
| 33 | 
            +
                # You can OR the protection value with any of these section attributes:
         | 
| 34 | 
            +
                #
         | 
| 35 | 
            +
                # * SEC_COMMIT
         | 
| 36 | 
            +
                # * SEC_IMAGE
         | 
| 37 | 
            +
                # * SEC_LARGE_PAGES
         | 
| 38 | 
            +
                # * SEC_NOCACHE
         | 
| 39 | 
            +
                # * SEC_RESERVE
         | 
| 40 | 
            +
                #
         | 
| 41 | 
            +
                # The default protection is PAGE_READWRITE.
         | 
| 42 | 
            +
                #
         | 
| 43 | 
            +
                attr_accessor :protection
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                # A string that determines the name of the mapping object.
         | 
| 46 | 
            +
                # By default this value is nil, i.e. anonymous.  If you specify a +name+
         | 
| 47 | 
            +
                # that already exists then an attempt is made to access that object.
         | 
| 48 | 
            +
                #
         | 
| 49 | 
            +
                attr_accessor :name
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                # The maximum size for the file mapping object.  If a +file+ is
         | 
| 52 | 
            +
                # specified, this value defaults to the size of +file+. Otherwise
         | 
| 53 | 
            +
                # it defaults to zero (though you should set it manually).
         | 
| 54 | 
            +
                #
         | 
| 55 | 
            +
                attr_accessor :size
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                # Access desired to the file mapping object.  This may be
         | 
| 58 | 
            +
                # any of the following values:
         | 
| 59 | 
            +
                #
         | 
| 60 | 
            +
                # * FILE_MAP_WRITE
         | 
| 61 | 
            +
                # * FILE_MAP_READ
         | 
| 62 | 
            +
                # * FILE_MAP_COPY
         | 
| 63 | 
            +
                # * FILE_MAP_ALL_ACCESS
         | 
| 64 | 
            +
                #
         | 
| 65 | 
            +
                # The default access is FILE_MAP_WRITE.
         | 
| 66 | 
            +
                #
         | 
| 67 | 
            +
                attr_accessor :access
         | 
| 68 | 
            +
             | 
| 69 | 
            +
                # The address of the file view mapping.
         | 
| 70 | 
            +
                #
         | 
| 71 | 
            +
                attr_reader :address
         | 
| 72 | 
            +
             | 
| 73 | 
            +
                # Suggested starting address of mapped view.  If this value is not
         | 
| 74 | 
            +
                # specified, the system will choose the base mapping address.  If you do
         | 
| 75 | 
            +
                # specify a value, it must be a multiple of the system's allocation
         | 
| 76 | 
            +
                # granularity.
         | 
| 77 | 
            +
                #
         | 
| 78 | 
            +
                # Note: The MSDN documentation recommends that, in most case cases, you
         | 
| 79 | 
            +
                # should not set this value.
         | 
| 80 | 
            +
                #--
         | 
| 81 | 
            +
                # A system's allocation granularity can be found via GetSystemInfo()
         | 
| 82 | 
            +
                # and the dwAllocationGranularity member of the SYSTEM_INFO struct.
         | 
| 83 | 
            +
                #
         | 
| 84 | 
            +
                attr_accessor :base_address
         | 
| 85 | 
            +
             | 
| 86 | 
            +
                # Sets whether or not a semaphore lock is automatically placed on the
         | 
| 87 | 
            +
                # mapped view for read and write operations.  This is true by default.
         | 
| 88 | 
            +
                #
         | 
| 89 | 
            +
                attr_writer :autolock
         | 
| 90 | 
            +
             | 
| 91 | 
            +
                # The value, in milliseconds, used to wait on the semaphore lock.  Only
         | 
| 92 | 
            +
                # used if the +autolock+ option is true. The default is 10 milliseconds.
         | 
| 93 | 
            +
                #
         | 
| 94 | 
            +
                attr_accessor :timeout
         | 
| 95 | 
            +
             | 
| 96 | 
            +
                # :call-seq:
         | 
| 97 | 
            +
                #     MMap.new(opts = {})
         | 
| 98 | 
            +
                #     MMap.new(opts = {}){ |address| block }
         | 
| 99 | 
            +
                #
         | 
| 100 | 
            +
                # Creates and returns a new Win32::MMap object.  If +file+ is set, then
         | 
| 101 | 
            +
                # that file is used to create the mapping.  If a block is provided the
         | 
| 102 | 
            +
                # address is yielded and the mapping object is automatically closed at the
         | 
| 103 | 
            +
                # end of the block.
         | 
| 104 | 
            +
                #
         | 
| 105 | 
            +
                # Accepts any one of the following hash attributes:
         | 
| 106 | 
            +
                #
         | 
| 107 | 
            +
                # * protection
         | 
| 108 | 
            +
                # * size
         | 
| 109 | 
            +
                # * access
         | 
| 110 | 
            +
                # * inherit
         | 
| 111 | 
            +
                # * name
         | 
| 112 | 
            +
                # * base_address
         | 
| 113 | 
            +
                # * autolock
         | 
| 114 | 
            +
                # * timeout
         | 
| 115 | 
            +
                # * file
         | 
| 116 | 
            +
                #
         | 
| 117 | 
            +
                # Please see the documentation on the individual attributes for
         | 
| 118 | 
            +
                # further details.  Note that, although these are accessors, they
         | 
| 119 | 
            +
                # *must* be set in the constructor (if set at all).  Setting them
         | 
| 120 | 
            +
                # after the call to MMap.new will not have any effect.
         | 
| 121 | 
            +
                #
         | 
| 122 | 
            +
                # == Example
         | 
| 123 | 
            +
                #    require 'win32/mmap'
         | 
| 124 | 
            +
                #    require 'windows/msvcrt/string'
         | 
| 125 | 
            +
                #    include Windows::MSVCRT::String
         | 
| 126 | 
            +
                #    include Win32
         | 
| 127 | 
            +
                #
         | 
| 128 | 
            +
                #    # Reverse the contents of a file.
         | 
| 129 | 
            +
                #    mmap = MMap.new(:file => 'test.txt') do |addr|
         | 
| 130 | 
            +
                #       strrev(addr)
         | 
| 131 | 
            +
                #    end
         | 
| 132 | 
            +
                #
         | 
| 133 | 
            +
                def initialize(opts = {})
         | 
| 134 | 
            +
                  valid = %w[
         | 
| 135 | 
            +
                    file name protection access inherit size
         | 
| 136 | 
            +
                    base_address open autolock timeout
         | 
| 137 | 
            +
                  ]
         | 
| 138 | 
            +
             | 
| 139 | 
            +
                  @open     = nil
         | 
| 140 | 
            +
                  @file     = nil
         | 
| 141 | 
            +
                  @autolock = nil
         | 
| 142 | 
            +
             | 
| 143 | 
            +
                  # Validate the keys, handle inherit specially.
         | 
| 144 | 
            +
                  opts.each{ |key, value|
         | 
| 145 | 
            +
                    key = key.to_s.downcase
         | 
| 146 | 
            +
             | 
| 147 | 
            +
                    unless valid.include?(key)
         | 
| 148 | 
            +
                      raise ArgumentError, "Invalid key '#{key}'"
         | 
| 149 | 
            +
                    end
         | 
| 150 | 
            +
             | 
| 151 | 
            +
                    if key == 'inherit'
         | 
| 152 | 
            +
                      self.inherit = value # To force inherit= method call
         | 
| 153 | 
            +
                    else
         | 
| 154 | 
            +
                      instance_variable_set("@#{key}", value)
         | 
| 155 | 
            +
                    end
         | 
| 156 | 
            +
                  }
         | 
| 157 | 
            +
             | 
| 158 | 
            +
                  @protection   ||= PAGE_READWRITE
         | 
| 159 | 
            +
                  @access       ||= FILE_MAP_WRITE
         | 
| 160 | 
            +
                  @size         ||= 0
         | 
| 161 | 
            +
                  @inherit      ||= nil
         | 
| 162 | 
            +
                  @base_address ||= 0
         | 
| 163 | 
            +
                  @timeout      ||= 10 # Milliseconds
         | 
| 164 | 
            +
             | 
| 165 | 
            +
                  self.inherit = false unless @inherit
         | 
| 166 | 
            +
             | 
| 167 | 
            +
                  @hash = {}
         | 
| 168 | 
            +
             | 
| 169 | 
            +
                  # Anything except an explicit false means the autolock is on.
         | 
| 170 | 
            +
                  @autolock = true unless @autolock == false
         | 
| 171 | 
            +
             | 
| 172 | 
            +
                  @lock_flag = 0 # Internal use only
         | 
| 173 | 
            +
             | 
| 174 | 
            +
                  if @file
         | 
| 175 | 
            +
                    if File.exists?(@file)
         | 
| 176 | 
            +
                      fsize = File.size(@file)
         | 
| 177 | 
            +
                      raise ArgumentError, 'cannot open 0 byte file' if fsize.zero?
         | 
| 178 | 
            +
                      @size = fsize if @size < fsize
         | 
| 179 | 
            +
                    end
         | 
| 180 | 
            +
             | 
| 181 | 
            +
                    rights = GENERIC_READ|GENERIC_WRITE
         | 
| 182 | 
            +
             | 
| 183 | 
            +
                    @fh = CreateFile(@file, rights, 0, 0, OPEN_ALWAYS, 0, 0)
         | 
| 184 | 
            +
             | 
| 185 | 
            +
                    if @fh == INVALID_HANDLE_VALUE
         | 
| 186 | 
            +
                      raise SystemCallError.new('CreateFile', FFI.errno)
         | 
| 187 | 
            +
                    end
         | 
| 188 | 
            +
                  else
         | 
| 189 | 
            +
                    @fh = INVALID_HANDLE_VALUE
         | 
| 190 | 
            +
                  end
         | 
| 191 | 
            +
             | 
| 192 | 
            +
                  if @open
         | 
| 193 | 
            +
                    @mh = OpenFileMapping(@access, @inherit[:bInheritHandle], @name)
         | 
| 194 | 
            +
                    raise SystemCallError.new('OpenFileMapping', FFI.errno) if @mh == 0
         | 
| 195 | 
            +
                  else
         | 
| 196 | 
            +
                    @mh = CreateFileMapping(@fh, @inherit, @protection, 0, @size, @name)
         | 
| 197 | 
            +
                    raise SystemCallError.new('CreateFileMapping', FFI.errno) if @mh == 0
         | 
| 198 | 
            +
                  end
         | 
| 199 | 
            +
             | 
| 200 | 
            +
                  if @open
         | 
| 201 | 
            +
                    @address = MapViewOfFileEx(@mh, @access, 0, 0, 0, @base_address)
         | 
| 202 | 
            +
                    @size    = get_view_size()
         | 
| 203 | 
            +
                  else
         | 
| 204 | 
            +
                    @address = MapViewOfFileEx(@mh, @access, 0, 0, 0, @base_address)
         | 
| 205 | 
            +
                  end
         | 
| 206 | 
            +
             | 
| 207 | 
            +
                  if @address == 0
         | 
| 208 | 
            +
                    raise SystemCallError.new('MapviewOfFileEx', FFI.errno)
         | 
| 209 | 
            +
                  end
         | 
| 210 | 
            +
             | 
| 211 | 
            +
                  if @autolock
         | 
| 212 | 
            +
                    @semaphore = CreateSemaphore(nil, 1, 1, "#{@name}.ruby_lock")
         | 
| 213 | 
            +
                    if @semaphore == 0
         | 
| 214 | 
            +
                      raise Error, get_last_error
         | 
| 215 | 
            +
                    end
         | 
| 216 | 
            +
                  end
         | 
| 217 | 
            +
             | 
| 218 | 
            +
                  if block_given?
         | 
| 219 | 
            +
                    begin
         | 
| 220 | 
            +
                      yield @address
         | 
| 221 | 
            +
                    ensure
         | 
| 222 | 
            +
                     close
         | 
| 223 | 
            +
                    end
         | 
| 224 | 
            +
                  end
         | 
| 225 | 
            +
             | 
| 226 | 
            +
                  @address
         | 
| 227 | 
            +
                end
         | 
| 228 | 
            +
             | 
| 229 | 
            +
                # Opens an existing file mapping using +name+.  You may pass a hash
         | 
| 230 | 
            +
                # of +opts+ as you would to MMap.new.  If you don't specify a size
         | 
| 231 | 
            +
                # as part of the +opts+, it will be dynamically determined for you
         | 
| 232 | 
            +
                # (in blocks equal to your system's page size, typically 4k).
         | 
| 233 | 
            +
                #
         | 
| 234 | 
            +
                # This method is otherwise identical to MMap.new.
         | 
| 235 | 
            +
                #--
         | 
| 236 | 
            +
                # This forces MMap.new to use OpenFileMapping() behind the scenes.
         | 
| 237 | 
            +
                #
         | 
| 238 | 
            +
                def self.open(name, opts={}, &block)
         | 
| 239 | 
            +
                  opts[:name] = name
         | 
| 240 | 
            +
                  opts[:open] = true
         | 
| 241 | 
            +
                  self.new(opts, &block)
         | 
| 242 | 
            +
                end
         | 
| 243 | 
            +
             | 
| 244 | 
            +
                # Sets whether or not the mapping handle can be inherited
         | 
| 245 | 
            +
                # by child processes.
         | 
| 246 | 
            +
                #--
         | 
| 247 | 
            +
                # If true, we have to create a SECURITY_ATTRIBUTES struct and set
         | 
| 248 | 
            +
                # its nLength member to 12 and its bInheritHandle member to TRUE.
         | 
| 249 | 
            +
                #
         | 
| 250 | 
            +
                def inherit=(bool)
         | 
| 251 | 
            +
                  @inherit = SECURITY_ATTRIBUTES.new
         | 
| 252 | 
            +
                  @inherit[:nLength] = SECURITY_ATTRIBUTES.size
         | 
| 253 | 
            +
             | 
| 254 | 
            +
                  if bool
         | 
| 255 | 
            +
                    @inherit[:bInheritHandle] = true
         | 
| 256 | 
            +
                  else
         | 
| 257 | 
            +
                    @inherit[:bInheritHandle] = false
         | 
| 258 | 
            +
                  end
         | 
| 259 | 
            +
                end
         | 
| 260 | 
            +
             | 
| 261 | 
            +
                # Returns whether or not the mapping handle can be
         | 
| 262 | 
            +
                # inherited by child processes.  The default is false.
         | 
| 263 | 
            +
                #
         | 
| 264 | 
            +
                def inherit?
         | 
| 265 | 
            +
                  @inherit and @inherit[:bInheritHandle]
         | 
| 266 | 
            +
                end
         | 
| 267 | 
            +
             | 
| 268 | 
            +
                # Writes +num_bytes+ to the disk within a mapped view of a file, or to
         | 
| 269 | 
            +
                # the end of the mapping if +num_bytes+ is not specified.
         | 
| 270 | 
            +
                #
         | 
| 271 | 
            +
                def flush(num_bytes = 0)
         | 
| 272 | 
            +
                  unless FlushViewOfFile(@address, num_bytes)
         | 
| 273 | 
            +
                    SystemCallError.new('FlushViewOfFile', FFI.errno)
         | 
| 274 | 
            +
                  end
         | 
| 275 | 
            +
                end
         | 
| 276 | 
            +
             | 
| 277 | 
            +
                # Unmaps the file view and closes all open file handles.  You should
         | 
| 278 | 
            +
                # always call this when you are finished with the object (when using
         | 
| 279 | 
            +
                # the non-block form).
         | 
| 280 | 
            +
                #
         | 
| 281 | 
            +
                def close
         | 
| 282 | 
            +
                  UnmapViewOfFile(@address) if @address
         | 
| 283 | 
            +
                  CloseHandle(@fh) if @fh
         | 
| 284 | 
            +
                  CloseHandle(@mh) if @mh
         | 
| 285 | 
            +
                  ReleaseSemaphore(@semaphore, 1, nil) if @semaphore
         | 
| 286 | 
            +
                end
         | 
| 287 | 
            +
             | 
| 288 | 
            +
                # Returns whether or not a semaphore lock is automatically placed on the
         | 
| 289 | 
            +
                # mapped view for read and write operations.  This is true by default.
         | 
| 290 | 
            +
                #
         | 
| 291 | 
            +
                def autolock?
         | 
| 292 | 
            +
                  @autolock
         | 
| 293 | 
            +
                end
         | 
| 294 | 
            +
             | 
| 295 | 
            +
                private
         | 
| 296 | 
            +
             | 
| 297 | 
            +
                # :stopdoc:
         | 
| 298 | 
            +
             | 
| 299 | 
            +
                # This is used to allow dynamic getters and setters between memory
         | 
| 300 | 
            +
                # mapped objects.
         | 
| 301 | 
            +
                #--
         | 
| 302 | 
            +
                # This replaces the getvar/setvar API from 0.1.0.
         | 
| 303 | 
            +
                #
         | 
| 304 | 
            +
                # TODO: FIX!
         | 
| 305 | 
            +
                #
         | 
| 306 | 
            +
                def method_missing(method_id, *args)
         | 
| 307 | 
            +
                  method = method_id.id2name
         | 
| 308 | 
            +
                  args = args.first if args.length == 1
         | 
| 309 | 
            +
             | 
| 310 | 
            +
                  if method[-1,1] == '=' # Setter
         | 
| 311 | 
            +
                    method.chop!
         | 
| 312 | 
            +
                    @hash["#{method}"] = args
         | 
| 313 | 
            +
             | 
| 314 | 
            +
                    if @autolock
         | 
| 315 | 
            +
                      if mmap_lock
         | 
| 316 | 
            +
                        instance_variable_set("@#{method}", args)
         | 
| 317 | 
            +
                        marshal = Marshal.dump(@hash)
         | 
| 318 | 
            +
                        ptr = FFI::Pointer.new(:char, @address)
         | 
| 319 | 
            +
                        ptr.write_string(marshal,marshal.length)
         | 
| 320 | 
            +
                        mmap_unlock
         | 
| 321 | 
            +
                      end
         | 
| 322 | 
            +
                    else
         | 
| 323 | 
            +
                      instance_variable_set("@#{method}", args)
         | 
| 324 | 
            +
                      marshal = Marshal.dump(@hash)
         | 
| 325 | 
            +
                      ptr = FFI::Pointer.new(:char, @address)
         | 
| 326 | 
            +
                      ptr.write_string(marshal,marshal.length)
         | 
| 327 | 
            +
                    end
         | 
| 328 | 
            +
                  else # Getter
         | 
| 329 | 
            +
                    buf = FFI::MemoryPointer.new(:char, @size)
         | 
| 330 | 
            +
             | 
| 331 | 
            +
                    if @autolock
         | 
| 332 | 
            +
                      if mmap_lock
         | 
| 333 | 
            +
                        ptr = FFI::Pointer.new(:char, @address)
         | 
| 334 | 
            +
                        buf = ptr.read_string(@size)
         | 
| 335 | 
            +
                        hash = Marshal.load(buf)
         | 
| 336 | 
            +
                        val  = hash["#{method}"]
         | 
| 337 | 
            +
                        instance_variable_set("@#{method}", val)
         | 
| 338 | 
            +
                        mmap_unlock
         | 
| 339 | 
            +
                      end
         | 
| 340 | 
            +
                    else
         | 
| 341 | 
            +
                       ptr = FFI::Pointer.new(:char, @address)
         | 
| 342 | 
            +
                       buf = ptr.read_string(@size)
         | 
| 343 | 
            +
                       hash = Marshal.load(buf)
         | 
| 344 | 
            +
                       val  = hash["#{method}"]
         | 
| 345 | 
            +
                       instance_variable_set("@#{method}", val)
         | 
| 346 | 
            +
                    end
         | 
| 347 | 
            +
             | 
| 348 | 
            +
                    return instance_variable_get("@#{method}")
         | 
| 349 | 
            +
                  end
         | 
| 350 | 
            +
                end
         | 
| 351 | 
            +
             | 
| 352 | 
            +
                # Adds a semaphore lock the mapping.  Only used if +autolock+ is set
         | 
| 353 | 
            +
                # to true in the constructor.
         | 
| 354 | 
            +
                #
         | 
| 355 | 
            +
                def mmap_lock
         | 
| 356 | 
            +
                  bool = false
         | 
| 357 | 
            +
             | 
| 358 | 
            +
                  if(@lock_flag == 0)
         | 
| 359 | 
            +
                    if WaitForSingleObject(@semaphore, @timeout) == WAIT_OBJECT_0
         | 
| 360 | 
            +
                      bool = true
         | 
| 361 | 
            +
                    end
         | 
| 362 | 
            +
                  end
         | 
| 363 | 
            +
             | 
| 364 | 
            +
                  @lock_flag += 1
         | 
| 365 | 
            +
                  bool
         | 
| 366 | 
            +
                end
         | 
| 367 | 
            +
             | 
| 368 | 
            +
                # Releases a semaphore lock on the view.  Only used if +autolock+ is
         | 
| 369 | 
            +
                # set to true in the constructor.
         | 
| 370 | 
            +
                #
         | 
| 371 | 
            +
                def mmap_unlock
         | 
| 372 | 
            +
                  @lock_flag -= 1
         | 
| 373 | 
            +
             | 
| 374 | 
            +
                  return false if @lock_flag != 0
         | 
| 375 | 
            +
             | 
| 376 | 
            +
                  if ReleaseSemaphore(@semaphore, 1, nil) == 0
         | 
| 377 | 
            +
                    raise SystemCallError.new('ReleaseSemaphore', FFI.errno)
         | 
| 378 | 
            +
                  end
         | 
| 379 | 
            +
             | 
| 380 | 
            +
                  true
         | 
| 381 | 
            +
                end
         | 
| 382 | 
            +
             | 
| 383 | 
            +
                # Gets the size of an existing mapping based on the address. This
         | 
| 384 | 
            +
                # is used by the MMap.open method when a size isn't specified.
         | 
| 385 | 
            +
                #
         | 
| 386 | 
            +
                def get_view_size
         | 
| 387 | 
            +
                  mbi = MEMORY_BASIC_INFORMATION.new
         | 
| 388 | 
            +
                  VirtualQuery(@address, mbi, mbi.size)
         | 
| 389 | 
            +
                  mbi[:RegionSize]
         | 
| 390 | 
            +
                end
         | 
| 391 | 
            +
              end # MMap
         | 
| 392 | 
            +
            end # Win32
         |