/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- mtcp_sys_memmove
- mtcp_sys_memcpy
- mtcp_sys_memcmp
1 /*****************************************************************************
2 * Copyright (C) 2010-2014 Kapil Arya <kapil@ccs.neu.edu> *
3 * Copyright (C) 2010-2014 Gene Cooperman <gene@ccs.neu.edu> *
4 * *
5 * DMTCP is free software: you can redistribute it and/or *
6 * modify it under the terms of the GNU Lesser General Public License as *
7 * published by the Free Software Foundation, either version 3 of the *
8 * License, or (at your option) any later version. *
9 * *
10 * DMTCP 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 Lesser General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU Lesser General Public *
16 * License along with DMTCP. If not, see <http://www.gnu.org/licenses/>. *
17 *****************************************************************************/
18
19 #ifndef _MTCP_UTIL_H
20 #define _MTCP_UTIL_H
21
22 #include "procmapsarea.h"
23
24 #define MTCP_PRINTF(args...) \
25 do { \
26 mtcp_printf("[%d] %s:%d %s:\n ", \
27 mtcp_sys_getpid(), __FILE__, __LINE__, __FUNCTION__); \
28 (void)mtcp_sys_errno; /* prevent compiler warning if we don't use it */ \
29 mtcp_printf(args); \
30 } while (0)
31
32 #define MTCP_ASSERT(condition) \
33 if (! (condition)) { \
34 MTCP_PRINTF("Assertion failed: %s\n", #condition); \
35 mtcp_abort(); \
36 }
37
38 #ifdef DEBUG
39 # define DPRINTF MTCP_PRINTF
40 #else
41 # define DPRINTF(args...) // debug printing
42 #endif
43
44 #if 0
45 // From glibc-2.5/sysdeps/generic/memcopy.h:BYTE_COPY_FWD
46 // From glibc-2.5/sysdeps/generic/memcopy.h:BYTE_COPY_BWD
47 #define MTCP_BYTE_COPY_FWD(dst_bp, src_bp, nbytes) \
48 do \
49 { \
50 size_t __nbytes = (nbytes); \
51 while (__nbytes > 0) \
52 { \
53 byte __x = ((byte *) src_bp)[0]; \
54 src_bp += 1; \
55 __nbytes -= 1; \
56 ((byte *) dst_bp)[0] = __x; \
57 dst_bp += 1; \
58 } \
59 } while (0)
60 #define MTCP_BYTE_COPY_BWD(dst_ep, src_ep, nbytes) \
61 do \
62 { \
63 size_t __nbytes = (nbytes); \
64 while (__nbytes > 0) \
65 { \
66 byte __x; \
67 src_ep -= 1; \
68 __x = ((byte *) src_ep)[0]; \
69 dst_ep -= 1; \
70 __nbytes -= 1; \
71 ((byte *) dst_ep)[0] = __x; \
72 } \
73 } while (0)
74
75 #ifdef MTCP_SYS_MEMMOVE
76 # ifndef _MTCP_MEMMOVE_
77 # define _MTCP_MEMMOVE_
78 // From glibc-2.5/string/memmove.c
79 static void *
80 mtcp_sys_memmove (void *a1, const void *a2, size_t len)
81 {
82 unsigned long int dstp = (long int) a1 /* dest */;
83 unsigned long int srcp = (long int) a2 /* src */;
84
85 /* This test makes the forward copying code be used whenever possible.
86 Reduces the working set. */
87 if (dstp - srcp >= len) /* *Unsigned* compare! */
88 {
89 /* Copy from the beginning to the end. */
90
91 /* There are just a few bytes to copy. Use byte memory operations. */
92 MTCP_BYTE_COPY_FWD (dstp, srcp, len);
93 }
94 else
95 {
96 /* Copy from the end to the beginning. */
97 srcp += len;
98 dstp += len;
99
100 /* There are just a few bytes to copy. Use byte memory operations. */
101 MTCP_BYTE_COPY_BWD (dstp, srcp, len);
102 }
103
104 return (a1 /* dest */);
105 }
106 # endif
107 #endif
108
109 #ifdef MTCP_SYS_MEMCPY
110 # ifndef _MTCP_MEMCPY_
111 # define _MTCP_MEMCPY_
112 // From glibc-2.5/string/memcpy.c; and
113 /* Copy exactly NBYTES bytes from SRC_BP to DST_BP,
114 without any assumptions about alignment of the pointers. */
115 static void *
116 mtcp_sys_memcpy (void *dstpp, const void *srcpp, size_t len)
117 {
118 unsigned long int dstp = (long int) dstpp;
119 unsigned long int srcp = (long int) srcpp;
120
121 /* SHOULD DO INITIAL WORD COPY BEFORE THIS. */
122 /* There are just a few bytes to copy. Use byte memory operations. */
123 MTCP_BYTE_COPY_FWD(dstp, srcp, len);
124 return dstpp;
125 }
126 # endif
127 #endif
128 #if 0 /* DEMONSTRATE_BUG */
129
130 // From glibc-2.5/string/memcmp.c:memcmp at end.
131 #ifndef _MTCP_MEMCMP_
132 # define _MTCP_MEMCMP_
133 static int
134 mtcp_sys_memcmp (s1, s2, len)
135 const __ptr_t s1;
136 const __ptr_t s2;
137 size_t len;
138 {
139 op_t a0;
140 op_t b0;
141 long int srcp1 = (long int) s1;
142 long int srcp2 = (long int) s2;
143 op_t res;
144
145 /* There are just a few bytes to compare. Use byte memory operations. */
146 while (len != 0)
147 {
148 a0 = ((byte *) srcp1)[0];
149 b0 = ((byte *) srcp2)[0];
150 srcp1 += 1;
151 srcp2 += 1;
152 res = a0 - b0;
153 if (res != 0)
154 return res;
155 len -= 1;
156 }
157
158 return 0;
159 }
160 #endif
161
162 #endif /* DEMONSTRATE_BUG */
163 #endif
164
165 void mtcp_printf (char const *format, ...);
166 ssize_t mtcp_read_all(int fd, void *buf, size_t count);
167 int mtcp_readfile(int fd, void *buf, size_t size);
168 void mtcp_skipfile(int fd, size_t size);
169 unsigned long mtcp_strtol (char *str);
170 char mtcp_readchar (int fd);
171 char mtcp_readdec (int fd, VA *value);
172 char mtcp_readhex (int fd, VA *value);
173 ssize_t mtcp_write_all(int fd, const void *buf, size_t count);
174 size_t mtcp_strlen (const char *s1);
175 const void *mtcp_strstr(const char *string, const char *substring);
176 void mtcp_strncpy(char *targ, const char* source, size_t len);
177 void mtcp_strcpy(char *dest, const char *src);
178 void mtcp_strncat(char *dest, const char *src, size_t n);
179 int mtcp_strncmp (const char *s1, const char *s2, size_t n);
180 int mtcp_strcmp (const char *s1, const char *s2);
181 char *mtcp_strchr(const char *s, int c);
182 int mtcp_strstartswith (const char *s1, const char *s2);
183 int mtcp_strendswith (const char *s1, const char *s2);
184 int mtcp_readmapsline (int mapsfd, Area *area);
185 void mtcp_sys_memcpy (void *dstpp, const void *srcpp, size_t len);
186 #endif