xref: /aosp_15_r20/external/intel-media-driver/cmrtlib/agnostic/share/cm_mem.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2017, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 
23 #ifndef CMRTLIB_AGNOSTIC_SHARE_CM_MEM_H_
24 #define CMRTLIB_AGNOSTIC_SHARE_CM_MEM_H_
25 
26 #include <cstring>
27 #include <new>
28 #include "cm_debug.h"
29 
30 #define CmSafeDeleteArray(_ptr) {if(_ptr) {delete[] (_ptr); (_ptr)=0;}}
31 #define CmSafeRelease(_ptr)     {if(_ptr) {delete (_ptr); (_ptr)=0;}}
32 
33 /*****************************************************************************\
34 Inline Function:
35     CmSafeMemCopy
36 
37 Description:
38     Exception Handler Memory Copy function
39 \*****************************************************************************/
CmSafeMemCopy(void * dst,const void * src,const size_t bytes)40 inline void CmSafeMemCopy( void* dst, const void* src, const size_t bytes )
41 {
42 #ifdef _DEBUG
43     __try
44 #endif
45     {
46         memcpy( dst, src, bytes );
47     }
48 #ifdef _DEBUG
49     // catch exceptions here so they are easily debugged
50     __except(EXCEPTION_EXECUTE_HANDLER)
51     {
52         CmAssert(0);
53     }
54 #endif
55 }
56 
57 /*****************************************************************************\
58 Inline Function:
59     CmSafeMemSet
60 
61 Description:
62     Memory set
63 \*****************************************************************************/
CmSafeMemSet(void * dst,const int data,const size_t bytes)64 inline void CmSafeMemSet( void* dst, const int data, const size_t bytes )
65 {
66 #ifdef _DEBUG
67     __try
68 #endif
69     {
70         ::memset( dst, data, bytes );
71     }
72 #ifdef _DEBUG
73     // catch exceptions here so they are easily debugged
74     __except(EXCEPTION_EXECUTE_HANDLER)
75     {
76         CmAssert(0);
77     }
78 #endif
79 }
80 
CmDwordMemSet(void * dst,const uint32_t data,const size_t bytes)81 inline void CmDwordMemSet( void* dst, const uint32_t data, const size_t bytes )
82 {
83     uint32_t *ptr = reinterpret_cast<uint32_t*>( dst );
84     uint32_t size = (uint32_t)(bytes >> 2); // divide by 4 byte to dword
85     uint32_t *maxPtr = ptr + size;
86     while(ptr < maxPtr)
87         *ptr++ = data;
88 }
89 
90 /*****************************************************************************\
91 Inline Function:
92 CmSafeMemCompare
93 
94 Description:
95 Exception Handler Memory Compare function
96 \*****************************************************************************/
CmSafeMemCompare(const void * dst,const void * src,const size_t bytes)97 inline int CmSafeMemCompare(const void* dst, const void* src, const size_t bytes)
98 {
99 #ifdef _DEBUG
100     __try
101 #endif
102     {
103         return ::memcmp(dst, src, bytes);
104     }
105 #ifdef _DEBUG
106     // catch exceptions here so they are easily debugged
107     __except (EXCEPTION_EXECUTE_HANDLER)
108     {
109         CmAssert(0);
110         return 0x7FFFFFFF;  //  An unreasonably large value indicating errors.
111     }
112 #endif
113 }
114 
115 #endif  // #ifndef CMRTLIB_AGNOSTIC_SHARE_CM_MEM_H_
116