copymem.h
1 /*************************************************************************/
2 /* copymem.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef COPYMEM_H
30 #define COPYMEM_H
31 
32 #include "typedefs.h"
33 
35 
36 #define copymem(m_to,m_from,m_count) \
37  do { \
38  unsigned char * _from=(unsigned char*)m_from; \
39  unsigned char * _to=(unsigned char*)m_to; \
40  int _count=m_count; \
41  for (int _i=0;_i<_count;_i++) \
42  _to[_i]=_from[_i]; \
43  } while (0);
44  /*
45  case 0: *_dto++ = *_dfrom++; \
46  case 7: *_dto++ = *_dfrom++; \
47  case 6: *_dto++ = *_dfrom++; \
48  case 5: *_dto++ = *_dfrom++; \
49  case 4: *_dto++ = *_dfrom++; \
50  case 3: *_dto++ = *_dfrom++; \
51  case 2: *_dto++ = *_dfrom++; \
52  case 1: *_dto++ = *_dfrom++; \
53  */
54 #define movemem_duff(m_to, m_from, m_count) \
55  do { \
56  if (m_to<m_from) { \
57  unsigned char* _dto = (unsigned char*)m_to; \
58  unsigned char* _dfrom = (unsigned char*)m_from; \
59  int n = (m_count + 7) / 8; \
60  switch (m_count % 8) { \
61  do { \
62  case 0: *_dto++ = *_dfrom++; \
63  case 7: *_dto++ = *_dfrom++; \
64  case 6: *_dto++ = *_dfrom++; \
65  case 5: *_dto++ = *_dfrom++; \
66  case 4: *_dto++ = *_dfrom++; \
67  case 3: *_dto++ = *_dfrom++; \
68  case 2: *_dto++ = *_dfrom++; \
69  case 1: *_dto++ = *_dfrom++; \
70  } while (--n > 0); \
71  }; \
72  } else if (m_to>m_from) { \
73  unsigned char* _dto = &((unsigned char*)m_to)[m_count-1]; \
74  unsigned char* _dfrom = &((unsigned char*)m_from)[m_count-1]; \
75  int n = (m_count + 7) / 8; \
76  switch (m_count % 8) { \
77  do { \
78  case 0: *_dto-- = *_dfrom--; \
79  case 7: *_dto-- = *_dfrom--; \
80  case 6: *_dto-- = *_dfrom--; \
81  case 5: *_dto-- = *_dfrom--; \
82  case 4: *_dto-- = *_dfrom--; \
83  case 3: *_dto-- = *_dfrom--; \
84  case 2: *_dto-- = *_dfrom--; \
85  case 1: *_dto-- = *_dfrom--; \
86  } while (--n > 0); \
87  }; \
88  } \
89  } while(0) \
90 
91 #define movemem_conventional(m_to,m_from,m_count) \
92  do { \
93  if (m_to<m_from) { \
94  unsigned char * _from=(unsigned char*)m_from; \
95  unsigned char * _to=(unsigned char*)m_to; \
96  int _count=m_count; \
97  for (int _i=0;_i<_count;_i++) \
98  _to[_i]=_from[_i]; \
99  \
100  } else if (m_to>m_from) { \
101  unsigned char * _from=(unsigned char*)m_from; \
102  unsigned char * _to=(unsigned char*)m_to; \
103  int _count=m_count; \
104  while (_count--) \
105  _to[_count]=_from[_count]; \
106  \
107  \
108  } \
109  } while (0); \
110 
111 void movemem_system(void*,void*,int);
112 
113 #define movemem movemem_system
114 
115 
116 void zeromem(void* p_mem,size_t p_bytes);
117 
118 #endif
119