@@ -4,7 +4,7 @@ use crate::alloc::{GlobalAlloc, Layout, System};
4
4
use crate :: ffi:: c_void;
5
5
use crate :: ptr;
6
6
use crate :: sync:: atomic:: { AtomicPtr , Ordering } ;
7
- use crate :: sys:: c;
7
+ use crate :: sys:: c:: { self , windows_targets } ;
8
8
use crate :: sys:: common:: alloc:: { realloc_fallback, MIN_ALIGN } ;
9
9
use core:: mem:: MaybeUninit ;
10
10
@@ -17,74 +17,71 @@ mod tests;
17
17
// Flag to indicate that the memory returned by `HeapAlloc` should be zeroed.
18
18
const HEAP_ZERO_MEMORY : c:: DWORD = 0x00000008 ;
19
19
20
- #[ link( name = "kernel32" ) ]
21
- extern "system" {
22
- // Get a handle to the default heap of the current process, or null if the operation fails.
23
- //
24
- // SAFETY: Successful calls to this function within the same process are assumed to
25
- // always return the same handle, which remains valid for the entire lifetime of the process.
26
- //
27
- // See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-getprocessheap
28
- fn GetProcessHeap ( ) -> c:: HANDLE ;
20
+ // Get a handle to the default heap of the current process, or null if the operation fails.
21
+ //
22
+ // SAFETY: Successful calls to this function within the same process are assumed to
23
+ // always return the same handle, which remains valid for the entire lifetime of the process.
24
+ //
25
+ // See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-getprocessheap
26
+ windows_targets:: link!( "kernel32.dll" "system" fn GetProcessHeap ( ) -> c:: HANDLE ) ;
29
27
30
- // Allocate a block of `dwBytes` bytes of memory from a given heap `hHeap`.
31
- // The allocated memory may be uninitialized, or zeroed if `dwFlags` is
32
- // set to `HEAP_ZERO_MEMORY`.
33
- //
34
- // Returns a pointer to the newly-allocated memory or null if the operation fails.
35
- // The returned pointer will be aligned to at least `MIN_ALIGN`.
36
- //
37
- // SAFETY:
38
- // - `hHeap` must be a non-null handle returned by `GetProcessHeap`.
39
- // - `dwFlags` must be set to either zero or `HEAP_ZERO_MEMORY`.
40
- //
41
- // Note that `dwBytes` is allowed to be zero, contrary to some other allocators.
42
- //
43
- // See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heapalloc
44
- fn HeapAlloc ( hHeap : c:: HANDLE , dwFlags : c :: DWORD , dwBytes : c :: SIZE_T ) -> c :: LPVOID ;
28
+ // Allocate a block of `dwBytes` bytes of memory from a given heap `hHeap`.
29
+ // The allocated memory may be uninitialized, or zeroed if `dwFlags` is
30
+ // set to `HEAP_ZERO_MEMORY`.
31
+ //
32
+ // Returns a pointer to the newly-allocated memory or null if the operation fails.
33
+ // The returned pointer will be aligned to at least `MIN_ALIGN`.
34
+ //
35
+ // SAFETY:
36
+ // - `hHeap` must be a non-null handle returned by `GetProcessHeap`.
37
+ // - `dwFlags` must be set to either zero or `HEAP_ZERO_MEMORY`.
38
+ //
39
+ // Note that `dwBytes` is allowed to be zero, contrary to some other allocators.
40
+ //
41
+ // See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heapalloc
42
+ windows_targets :: link! ( "kernel32.dll" "system" fn HeapAlloc ( hheap : c:: HANDLE , dwflags : u32 , dwbytes : usize ) -> * mut core :: ffi :: c_void ) ;
45
43
46
- // Reallocate a block of memory behind a given pointer `lpMem` from a given heap `hHeap`,
47
- // to a block of at least `dwBytes` bytes, either shrinking the block in place,
48
- // or allocating at a new location, copying memory, and freeing the original location.
49
- //
50
- // Returns a pointer to the reallocated memory or null if the operation fails.
51
- // The returned pointer will be aligned to at least `MIN_ALIGN`.
52
- // If the operation fails the given block will never have been freed.
53
- //
54
- // SAFETY:
55
- // - `hHeap` must be a non-null handle returned by `GetProcessHeap`.
56
- // - `dwFlags` must be set to zero.
57
- // - `lpMem` must be a non-null pointer to an allocated block returned by `HeapAlloc` or
58
- // `HeapReAlloc`, that has not already been freed.
59
- // If the block was successfully reallocated at a new location, pointers pointing to
60
- // the freed memory, such as `lpMem`, must not be dereferenced ever again.
61
- //
62
- // Note that `dwBytes` is allowed to be zero, contrary to some other allocators.
63
- //
64
- // See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heaprealloc
65
- fn HeapReAlloc (
66
- hHeap : c:: HANDLE ,
67
- dwFlags : c :: DWORD ,
68
- lpMem : c :: LPVOID ,
69
- dwBytes : c :: SIZE_T ,
70
- ) -> c :: LPVOID ;
44
+ // Reallocate a block of memory behind a given pointer `lpMem` from a given heap `hHeap`,
45
+ // to a block of at least `dwBytes` bytes, either shrinking the block in place,
46
+ // or allocating at a new location, copying memory, and freeing the original location.
47
+ //
48
+ // Returns a pointer to the reallocated memory or null if the operation fails.
49
+ // The returned pointer will be aligned to at least `MIN_ALIGN`.
50
+ // If the operation fails the given block will never have been freed.
51
+ //
52
+ // SAFETY:
53
+ // - `hHeap` must be a non-null handle returned by `GetProcessHeap`.
54
+ // - `dwFlags` must be set to zero.
55
+ // - `lpMem` must be a non-null pointer to an allocated block returned by `HeapAlloc` or
56
+ // `HeapReAlloc`, that has not already been freed.
57
+ // If the block was successfully reallocated at a new location, pointers pointing to
58
+ // the freed memory, such as `lpMem`, must not be dereferenced ever again.
59
+ //
60
+ // Note that `dwBytes` is allowed to be zero, contrary to some other allocators.
61
+ //
62
+ // See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heaprealloc
63
+ windows_targets :: link! ( "kernel32.dll" "system" fn HeapReAlloc (
64
+ hheap : c:: HANDLE ,
65
+ dwflags : u32 ,
66
+ lpmem : * const core :: ffi :: c_void ,
67
+ dwbytes : usize
68
+ ) -> * mut core :: ffi :: c_void ) ;
71
69
72
- // Free a block of memory behind a given pointer `lpMem` from a given heap `hHeap`.
73
- // Returns a nonzero value if the operation is successful, and zero if the operation fails.
74
- //
75
- // SAFETY:
76
- // - `hHeap` must be a non-null handle returned by `GetProcessHeap`.
77
- // - `dwFlags` must be set to zero.
78
- // - `lpMem` must be a pointer to an allocated block returned by `HeapAlloc` or `HeapReAlloc`,
79
- // that has not already been freed.
80
- // If the block was successfully freed, pointers pointing to the freed memory, such as `lpMem`,
81
- // must not be dereferenced ever again.
82
- //
83
- // Note that `lpMem` is allowed to be null, which will not cause the operation to fail.
84
- //
85
- // See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heapfree
86
- fn HeapFree ( hHeap : c:: HANDLE , dwFlags : c:: DWORD , lpMem : c:: LPVOID ) -> c:: BOOL ;
87
- }
70
+ // Free a block of memory behind a given pointer `lpMem` from a given heap `hHeap`.
71
+ // Returns a nonzero value if the operation is successful, and zero if the operation fails.
72
+ //
73
+ // SAFETY:
74
+ // - `hHeap` must be a non-null handle returned by `GetProcessHeap`.
75
+ // - `dwFlags` must be set to zero.
76
+ // - `lpMem` must be a pointer to an allocated block returned by `HeapAlloc` or `HeapReAlloc`,
77
+ // that has not already been freed.
78
+ // If the block was successfully freed, pointers pointing to the freed memory, such as `lpMem`,
79
+ // must not be dereferenced ever again.
80
+ //
81
+ // Note that `lpMem` is allowed to be null, which will not cause the operation to fail.
82
+ //
83
+ // See https://docs.microsoft.com/windows/win32/api/heapapi/nf-heapapi-heapfree
84
+ windows_targets:: link!( "kernel32.dll" "system" fn HeapFree ( hheap: c:: HANDLE , dwflags: u32 , lpmem: * const core:: ffi:: c_void) -> c:: BOOL ) ;
88
85
89
86
// Cached handle to the default heap of the current process.
90
87
// Either a non-null handle returned by `GetProcessHeap`, or null when not yet initialized or `GetProcessHeap` failed.
0 commit comments