1 krisbash 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 krisbash 1.1 **==============================================================================
23 */
24
25 #ifndef _memory_man_h
26 #define _memory_man_h
27
28 #include <common.h>
29
30 #if defined(CONFIG_OS_WINDOWS)
31 # include <winbase.h>
32 #else
33 # include <sys/mman.h>
34 #endif
35
36 BEGIN_EXTERNC
37
38 MI_INLINE void MemoryLock(void* addr, size_t len)
39 {
40 #if defined(CONFIG_OS_WINDOWS)
41 VirtualLock(addr, len);
42 #else
43 krisbash 1.1 mlock(addr, len);
44 #endif
45 }
46
47 MI_INLINE void MemoryUnlock(void* addr, size_t len)
48 {
49 #if defined(CONFIG_OS_WINDOWS)
50 VirtualUnlock(addr, len);
51 #else
52 munlock(addr, len);
53 #endif
54 }
55
56 _Check_return_ _Ret_maybenull_ _Post_writable_byte_size_(size)
57 MI_INLINE void *MemoryRealloc(
58 _Pre_valid_ _Post_ptr_invalid_ void ** ptr,
59 _In_ size_t size)
60 {
61 void * _newptr = PAL_Realloc(*ptr, size);
62 if (!_newptr)
63 {
64 krisbash 1.1 // To avoid memory leak, release the memory *ptr
65 PAL_Free(*ptr);
66 }
67 // Set *ptr to NULL, caller should not double release it
68 *ptr = NULL;
69 return _newptr;
70 }
71
72 END_EXTERNC
73
74 #endif /* _memory_man_h */
|