1 #Copyright (c) 2008 Vincent Povirk
3 #Permission is hereby granted, free of charge, to any person
4 #obtaining a copy of this software and associated documentation
5 #files (the "Software"), to deal in the Software without
6 #restriction, including without limitation the rights to use,
7 #copy, modify, merge, publish, distribute, sublicense, and/or sell
8 #copies of the Software, and to permit persons to whom the
9 #Software is furnished to do so, subject to the following
12 #The above copyright notice and this permission notice shall be
13 #included in all copies or substantial portions of the Software.
15 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 #EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 #OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 #NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 #WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 #FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 #OTHER DEALINGS IN THE SOFTWARE.
24 # Global memory definitions
25 # see http://msdn.microsoft.com/en-us/library/aa366596(VS.85).aspx
27 from ctypes
import windll
, WinError
28 from winliteutils
import get_symbols
, NONZERO
, ZERO
29 from windef
import UINT
, SIZE_T
, HGLOBAL
, LPCVOID
, LPVOID
31 _kernel32
= windll
.kernel32
34 GMEM_MOVEABLE
= 0x0002
35 GMEM_NOCOMPACT
= 0x0010
36 GMEM_NODISCARD
= 0x0020
37 GMEM_ZEROINIT
= 0x0040
39 GMEM_DISCARDABLE
= 0x0100
40 GMEM_NOT_BANKED
= 0x1000
42 GMEM_DDESHARE
= 0x2000
44 GMEM_LOWER
= GMEM_NOT_BANKED
45 GMEM_DISCARDED
= 0x4000
46 GMEM_LOCKCOUNT
= 0x00ff
47 GMEM_INVALID_HANDLE
= 0x8000
49 GHND
= (GMEM_MOVEABLE | GMEM_ZEROINIT
)
50 GPTR
= (GMEM_FIXED | GMEM_ZEROINIT
)
53 if x
== GMEM_INVALID_HANDLE
:
57 get_symbols(globals(), _kernel32
, ['GlobalAlloc', 'GlobalFlags', 'GlobalFree', 'GlobalHandle', 'GlobalLock', 'GlobalReAlloc', 'GlobalSize', 'GlobalUnlock'])
59 GlobalAlloc
.argtypes
= [UINT
, SIZE_T
]
60 GlobalAlloc
.restype
= NONZERO
62 GlobalFlags
.argtypes
= [HGLOBAL
]
63 GlobalFlags
.restype
= gmem_handle
65 GlobalFree
.argtypes
= [HGLOBAL
]
66 GlobalFree
.restype
= ZERO
68 GlobalHandle
.argtypes
= [LPCVOID
]
69 GlobalHandle
.restype
= NONZERO
71 GlobalLock
.argtypes
= [HGLOBAL
]
72 GlobalLock
.restype
= NONZERO
74 GlobalReAlloc
.argtypes
= [HGLOBAL
, SIZE_T
, UINT
]
75 GlobalReAlloc
.restype
= NONZERO
77 GlobalSize
.argtypes
= [HGLOBAL
]
78 GlobalSize
.restype
= NONZERO
80 GlobalUnlock
.argtypes
= [HGLOBAL
]
81 GlobalUnlock
.restype
= NONZERO
84 #GlobalLRUNewest = lambda x: x
85 #GlobalLRUOldest = lambda x: x
87 def GlobalDiscard(handle
):
88 return GlobalReAlloc(handle
, 0, GMEM_MOVEABLE
)
90 class GlobalMemoryObject(object):
91 __slots__
= ['handle', 'needs_free', '_as_parameter_']
93 def __init__(self
, flags
=GMEM_MOVEABLE
, size
=0, handle
=None, needs_free
=True):
95 handle
= GlobalAlloc(flags
, size
)
96 self
.handle
= self
._as
_parameter
_ = handle
97 self
.needs_free
= needs_free
101 if GlobalFree(self
) != 0:
103 self
.needs_free
= False
104 self
.handle
= self
._as
_parameter
_ = 0
107 return GlobalLock(self
)
115 def __exit__(self
, exc_type
, exc_val
, exc_tb
):
118 def realloc(self
, size
, flags
=GMEM_MOVEABLE
):
119 GlobalReAlloc(self
, size
, flags
)
125 return GlobalFlags(self
)
128 return GlobalSize(self
)