API documentation  2.0rc1
memory.h
1 /****************************************************************
2  *
3  * Copyright (C) 2018 Alessandro Pignotti <alessandro@leaningtech.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  ***************************************************************/
20 
21 #ifndef _CHEERP_MEMORY_H_2a82c4dc
22 #define _CHEERP_MEMORY_H_2a82c4dc
23 
24 #define CHEERP_MEMCMP(s1, s2, n) \
25 ({ \
26  /* Type safety check, types should be the same */ \
27  int ret = 0; \
28  typeof(s1) tmp1 = s1; \
29  typeof(s1) tmp2 = s2; \
30  for (size_t i=0;i<n/sizeof(*tmp1);i++) \
31  { \
32  if (tmp1[i] != tmp2[i]) \
33  { \
34  ret = tmp1[i] < tmp2[i] ? -1 : 1; \
35  break; \
36  } \
37  } \
38  ret; \
39 })
40 
41 #ifdef __cplusplus
42 namespace cheerp
43 {
44 
45 template<class T>
46 inline int memcmp(const T* s1, const T* s2, size_t n)
47 {
48  if (n == 0)
49  return 0;
50  for (size_t i=0; i<n/sizeof(T); i++)
51  {
52  if (s1[i] != s2[i])
53  return s1[i] < s2[i] ? -1 : 1;
54  }
55  return 0;
56 }
57 
58 }
59 #endif
60 
61 #endif
Definition: client.h:32