Class: ReservedNames::Win32::Memory

Inherits:
Object
  • Object
show all
Extended by:
Chef::ReservedNames::Win32::API::Memory
Includes:
Chef::ReservedNames::Win32::API::Memory
Defined in:
lib/chef/win32/memory.rb

Constant Summary

Constants included from Chef::ReservedNames::Win32::API::Memory

Chef::ReservedNames::Win32::API::Memory::LHND, Chef::ReservedNames::Win32::API::Memory::LMEM_DISCARDABLE, Chef::ReservedNames::Win32::API::Memory::LMEM_DISCARDED, Chef::ReservedNames::Win32::API::Memory::LMEM_FIXED, Chef::ReservedNames::Win32::API::Memory::LMEM_INVALID_HANDLE, Chef::ReservedNames::Win32::API::Memory::LMEM_LOCKCOUNT, Chef::ReservedNames::Win32::API::Memory::LMEM_MODIFY, Chef::ReservedNames::Win32::API::Memory::LMEM_MOVEABLE, Chef::ReservedNames::Win32::API::Memory::LMEM_NOCOMPACT, Chef::ReservedNames::Win32::API::Memory::LMEM_NODISCARD, Chef::ReservedNames::Win32::API::Memory::LMEM_VALID_FLAGS, Chef::ReservedNames::Win32::API::Memory::LMEM_ZEROINIT, Chef::ReservedNames::Win32::API::Memory::LPTR, Chef::ReservedNames::Win32::API::Memory::NONZEROLHND, Chef::ReservedNames::Win32::API::Memory::NONZEROLPTR

Class Method Summary collapse

Class Method Details

.local_alloc(length, flags = LPTR, &block) ⇒ Object

local_alloc(length[, flags]) [BLOCK] Allocates memory using LocalAlloc If BLOCK is specified, the memory will be passed to the block and freed afterwards.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chef/win32/memory.rb', line 32

def self.local_alloc(length, flags = LPTR, &block)
  result = LocalAlloc(flags, length)
  if result.null?
    Chef::ReservedNames::Win32::Error.raise!
  end
  # If a block is passed, handle freeing the memory at the end
  if !block.nil?
    begin
      yield result
    ensure
      local_free(result)
    end
  else
    result
  end
end

.local_discard(pointer) ⇒ Object

local_discard(pointer) Discard memory. Equivalent to local_realloc(pointer, 0)



51
52
53
# File 'lib/chef/win32/memory.rb', line 51

def self.local_discard(pointer)
  local_realloc(pointer, 0, LMEM_MOVEABLE)
end

.local_flags(pointer) ⇒ Object

local_flags(pointer) Get lock count and Windows flags for local_alloc allocated memory. Use: flags, lock_count = local_flags(pointer)



58
59
60
61
62
63
64
# File 'lib/chef/win32/memory.rb', line 58

def self.local_flags(pointer)
  result = LocalFlags(pointer)
  if result == LMEM_INVALID_HANDLE
    Chef::ReservedNames::Win32::Error.raise!
  end
  [ result & ~LMEM_LOCKCOUNT, result & LMEM_LOCKCOUNT ]
end

.local_free(pointer) ⇒ Object

local_free(pointer) Free memory allocated using local_alloc



68
69
70
71
72
73
# File 'lib/chef/win32/memory.rb', line 68

def self.local_free(pointer)
  result = LocalFree(pointer)
  unless result.null?
    Chef::ReservedNames::Win32::Error.raise!
  end
end

.local_free_finalizer(pointer) ⇒ Object



95
96
97
# File 'lib/chef/win32/memory.rb', line 95

def self.local_free_finalizer(pointer)
  proc { local_free(pointer) }
end

.local_realloc(pointer, size, flags = LMEM_MOVEABLE | LMEM_ZEROINIT) ⇒ Object

local_realloc(pointer, size[, flags]) Resizes memory allocated using LocalAlloc.



77
78
79
80
81
82
83
# File 'lib/chef/win32/memory.rb', line 77

def self.local_realloc(pointer, size, flags = LMEM_MOVEABLE | LMEM_ZEROINIT)
  result = LocalReAlloc(pointer, size, flags)
  if result.null?
    Chef::ReservedNames::Win32::Error.raise!
  end
  result
end

.local_size(pointer) ⇒ Object

local_size(pointer) Gets the size of memory allocated using LocalAlloc.



87
88
89
90
91
92
93
# File 'lib/chef/win32/memory.rb', line 87

def self.local_size(pointer)
  result = LocalSize(pointer)
  if result == 0
    Chef::ReservedNames::Win32::Error.raise!
  end
  result
end