Remove use of sizeof() for malloced pointer.
Error handling improvements, fix leaks.
619 | 619 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,150 @@ |
0 |
+include(CheckIncludeFile) |
|
1 |
+include(CheckLibraryExists) |
|
2 |
+include(CheckSymbolExists) |
|
3 |
+include(CheckFunctionExists) |
|
4 |
+include(CheckCSourceCompiles) |
|
5 |
+ |
|
6 |
+# Check for mmap() support, required for HAVE_MPOOL. |
|
7 |
+# |
|
8 |
+# checks for private fixed mappings, we don't need fixed mappings, |
|
9 |
+# so check only wether private mappings work. |
|
10 |
+check_include_file(sys/mman.h HAVE_SYS_MMAN_H) |
|
11 |
+if(MMAP_FOR_CROSSCOMPILING) |
|
12 |
+ set(HAVE_MMAP 1) |
|
13 |
+else() |
|
14 |
+ check_c_source_compiles( |
|
15 |
+ " |
|
16 |
+ #include <unistd.h> |
|
17 |
+ #include <stdlib.h> |
|
18 |
+ #include <sys/mman.h> |
|
19 |
+ #ifdef HAVE_SYS_TYPES_H |
|
20 |
+ #include <sys/types.h> |
|
21 |
+ #endif |
|
22 |
+ #ifdef HAVE_SYS_STAT_H |
|
23 |
+ #include <sys/stat.h> |
|
24 |
+ #endif |
|
25 |
+ #include <fcntl.h> |
|
26 |
+ #define ERR(e) do { status = e; goto done; } while(0) |
|
27 |
+ int main(void) |
|
28 |
+ { |
|
29 |
+ char *data = NULL, *data2 = MAP_FAILED, *data3 = NULL; |
|
30 |
+ size_t i, datasize = 1024; |
|
31 |
+ int fd = -1, status = 0; |
|
32 |
+ |
|
33 |
+ /* First, make a file with some known garbage in it. */ |
|
34 |
+ data = (char*) malloc(datasize); |
|
35 |
+ if(!data) |
|
36 |
+ ERR(1); |
|
37 |
+ for(i=0;i<datasize;i++) |
|
38 |
+ *(data + i) = rand(); |
|
39 |
+ umask(0); |
|
40 |
+ fd = creat(\"conftest.mmap\", 0600); |
|
41 |
+ if(fd < 0) |
|
42 |
+ ERR(1); |
|
43 |
+ if(write (fd, data, datasize) != datasize) |
|
44 |
+ ERR(1); |
|
45 |
+ close(fd); |
|
46 |
+ fd = open(\"conftest.mmap\", O_RDWR); |
|
47 |
+ if (fd < 0) |
|
48 |
+ ERR(1); |
|
49 |
+ /* Next, try to create a private map of the file. If we can, also make sure that |
|
50 |
+ we see the same garbage. */ |
|
51 |
+ data2 = mmap(NULL, datasize, PROT_READ | PROT_WRITE, |
|
52 |
+ MAP_PRIVATE, fd, 0L); |
|
53 |
+ if(data2 == MAP_FAILED) |
|
54 |
+ ERR(2); |
|
55 |
+ for(i=0;i<datasize;i++) |
|
56 |
+ if(*(data + i) != *(data2+ i)) |
|
57 |
+ ERR(3); |
|
58 |
+ /* Finally, make sure that changes to the mapped area do not |
|
59 |
+ percolate back to the file as seen by read(). |
|
60 |
+ (This is a bug on some variants of i386 svr4.0.) */ |
|
61 |
+ for (i = 0; i < datasize; ++i) |
|
62 |
+ *(data2 + i) = *(data2 + i) + 1; |
|
63 |
+ data3 = (char*) malloc(datasize); |
|
64 |
+ if(!data3) |
|
65 |
+ ERR(1); |
|
66 |
+ if(read (fd, data3, datasize) != datasize) |
|
67 |
+ ERR(1); |
|
68 |
+ for(i=0;i<datasize;i++) |
|
69 |
+ if(*(data + i) != *(data3 + i)) |
|
70 |
+ ERR(3); |
|
71 |
+ done: |
|
72 |
+ if(fd >= 0) |
|
73 |
+ close(fd); |
|
74 |
+ if(data3) |
|
75 |
+ free(data3); |
|
76 |
+ if(data2 != MAP_FAILED) |
|
77 |
+ munmap(data2, datasize); |
|
78 |
+ if(data) |
|
79 |
+ free(data); |
|
80 |
+ return status; |
|
81 |
+ } |
|
82 |
+ " |
|
83 |
+ HAVE_MMAP |
|
84 |
+ ) |
|
85 |
+endif() |
|
86 |
+ |
|
87 |
+# Check the flag name for the ANONYMOUS_MAP feature. |
|
88 |
+check_c_source_compiles( |
|
89 |
+ " |
|
90 |
+ #include <sys/mman.h> |
|
91 |
+ int main(void) |
|
92 |
+ { |
|
93 |
+ mmap((void *)0, 0, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
|
94 |
+ return 0; |
|
95 |
+ } |
|
96 |
+ " |
|
97 |
+ HAVE_MMAP_MAP_ANONYMOUS |
|
98 |
+) |
|
99 |
+if(HAVE_MMAP_MAP_ANONYMOUS) |
|
100 |
+ set(ANONYMOUS_MAP MAP_ANONYMOUS) |
|
101 |
+else() |
|
102 |
+ check_c_source_compiles( |
|
103 |
+ " |
|
104 |
+ /* OPENBSD WORKAROUND - DND*/ |
|
105 |
+ #include <sys/types.h> |
|
106 |
+ /* OPENBSD WORKAROUND - END*/ |
|
107 |
+ #include <sys/mman.h> |
|
108 |
+ int main(void) |
|
109 |
+ { |
|
110 |
+ mmap((void *)0, 0, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); |
|
111 |
+ return 0; |
|
112 |
+ } |
|
113 |
+ " |
|
114 |
+ HAVE_MMAP_MAP_ANON |
|
115 |
+ ) |
|
116 |
+ if(HAVE_MMAP_MAP_ANON) |
|
117 |
+ set(ANONYMOUS_MAP MAP_ANON) |
|
118 |
+ endif() |
|
119 |
+endif() |
|
120 |
+ |
|
121 |
+# Check for getting the pagesize. |
|
122 |
+check_symbol_exists(getpagesize unistd.h HAVE_GETPAGESIZE) |
|
123 |
+check_c_source_compiles( |
|
124 |
+ " |
|
125 |
+ #include <sys/types.h> |
|
126 |
+ #if HAVE_UNISTD_H |
|
127 |
+ #include <unistd.h> |
|
128 |
+ #endif |
|
129 |
+ int main(void) |
|
130 |
+ { |
|
131 |
+ int x = sysconf(_SC_PAGESIZE); |
|
132 |
+ return 0; |
|
133 |
+ } |
|
134 |
+ " |
|
135 |
+ HAVE_SYSCONF_SC_PAGESIZE |
|
136 |
+) |
|
137 |
+ |
|
138 |
+# Check for mempool support |
|
139 |
+if(DISABLE_MPOOL) |
|
140 |
+ message("****** mempool support disabled (DISABLE_MPOOL enabled)") |
|
141 |
+elseif(NOT HAVE_MMAP) |
|
142 |
+ message("****** mempool support disabled (mmap() not available or not usable)") |
|
143 |
+elseif(NOT HAVE_GETPAGESIZE AND NOT HAVE_SYSCONF_SC_PAGESIZE) |
|
144 |
+ message("****** mempool support disabled (pagesize cannot be determined)") |
|
145 |
+elseif(NOT HAVE_MMAP_MAP_ANON AND NOT HAVE_MMAP_MAP_ANONYMOUS) |
|
146 |
+ message("****** mempool support disabled (anonymous mmap not available)") |
|
147 |
+else() |
|
148 |
+ set(USE_MPOOL 1) |
|
149 |
+endif() |
0 | 150 |
deleted file mode 100644 |
... | ... |
@@ -1,142 +0,0 @@ |
1 |
-include(CheckIncludeFile) |
|
2 |
-include(CheckLibraryExists) |
|
3 |
-include(CheckSymbolExists) |
|
4 |
-include(CheckFunctionExists) |
|
5 |
-include(CheckCSourceCompiles) |
|
6 |
- |
|
7 |
-# Check for mmap() support, required for HAVE_MPOOL. |
|
8 |
-# |
|
9 |
-# checks for private fixed mappings, we don't need fixed mappings, |
|
10 |
-# so check only wether private mappings work. |
|
11 |
-check_include_file(sys/mman.h HAVE_SYS_MMAN_H) |
|
12 |
-if(MMAP_FOR_CROSSCOMPILING) |
|
13 |
- set(HAVE_MMAP 1) |
|
14 |
-else() |
|
15 |
- check_c_source_compiles( |
|
16 |
- " |
|
17 |
- #include <unistd.h> |
|
18 |
- #include <stdlib.h> |
|
19 |
- #include <sys/mman.h> |
|
20 |
- #ifdef HAVE_SYS_TYPES_H |
|
21 |
- #include <sys/types.h> |
|
22 |
- #endif |
|
23 |
- #ifdef HAVE_SYS_STAT_H |
|
24 |
- #include <sys/stat.h> |
|
25 |
- #endif |
|
26 |
- #include <fcntl.h> |
|
27 |
- int main(void) |
|
28 |
- { |
|
29 |
- char *data, *data2, *data3; |
|
30 |
- size_t i, datasize = 1024; |
|
31 |
- int fd; |
|
32 |
- |
|
33 |
- /* First, make a file with some known garbage in it. */ |
|
34 |
- data = (char*) malloc(datasize); |
|
35 |
- if(!data) |
|
36 |
- return 1; |
|
37 |
- for(i=0;i<datasize;i++) |
|
38 |
- *(data + i) = rand(); |
|
39 |
- umask(0); |
|
40 |
- fd = creat(\"conftest.mmap\", 0600); |
|
41 |
- if(fd < 0) |
|
42 |
- return 1; |
|
43 |
- if(write (fd, data, datasize) != datasize) |
|
44 |
- return 1; |
|
45 |
- close(fd); |
|
46 |
- fd = open(\"conftest.mmap\", O_RDWR); |
|
47 |
- if (fd < 0) |
|
48 |
- return 1; |
|
49 |
- /* Next, try to mmap the file at a fixed address which already has |
|
50 |
- something else allocated at it. If we can, also make sure that |
|
51 |
- we see the same garbage. */ |
|
52 |
- data2 = mmap(NULL, sizeof(data), PROT_READ | PROT_WRITE, |
|
53 |
- MAP_PRIVATE, fd, 0L); |
|
54 |
- if(data2 == MAP_FAILED) |
|
55 |
- return 2; |
|
56 |
- for(i=0;i<sizeof(data);i++) |
|
57 |
- if(*(data + i) != *(data2+ i)) |
|
58 |
- return 3; |
|
59 |
- /* Finally, make sure that changes to the mapped area do not |
|
60 |
- percolate back to the file as seen by read(). (This is a bug on |
|
61 |
- some variants of i386 svr4.0.) */ |
|
62 |
- for (i = 0; i < datasize; ++i) |
|
63 |
- *(data2 + i) = *(data2 + i) + 1; |
|
64 |
- data3 = (char*) malloc(datasize); |
|
65 |
- if(!data3) |
|
66 |
- return 1; |
|
67 |
- if(read (fd, data3, datasize) != datasize) |
|
68 |
- return 1; |
|
69 |
- for(i=0;i<sizeof(data);i++) |
|
70 |
- if(*(data + i) != *(data3 + i)) |
|
71 |
- return 3; |
|
72 |
- close(fd); |
|
73 |
- return 0; |
|
74 |
- } |
|
75 |
- " |
|
76 |
- HAVE_MMAP |
|
77 |
- ) |
|
78 |
-endif() |
|
79 |
- |
|
80 |
-# Check the flag name for the ANONYMOUS_MAP feature. |
|
81 |
-check_c_source_compiles( |
|
82 |
- " |
|
83 |
- #include <sys/mman.h> |
|
84 |
- int main(void) |
|
85 |
- { |
|
86 |
- mmap((void *)0, 0, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
|
87 |
- return 0; |
|
88 |
- } |
|
89 |
- " |
|
90 |
- HAVE_MMAP_MAP_ANONYMOUS |
|
91 |
-) |
|
92 |
-if(HAVE_MMAP_MAP_ANONYMOUS) |
|
93 |
- set(ANONYMOUS_MAP MAP_ANONYMOUS) |
|
94 |
-else() |
|
95 |
- check_c_source_compiles( |
|
96 |
- " |
|
97 |
- /* OPENBSD WORKAROUND - DND*/ |
|
98 |
- #include <sys/types.h> |
|
99 |
- /* OPENBSD WORKAROUND - END*/ |
|
100 |
- #include <sys/mman.h> |
|
101 |
- int main(void) |
|
102 |
- { |
|
103 |
- mmap((void *)0, 0, PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANON, -1, 0); |
|
104 |
- return 0; |
|
105 |
- } |
|
106 |
- " |
|
107 |
- HAVE_MMAP_MAP_ANON |
|
108 |
- ) |
|
109 |
- if(HAVE_MMAP_MAP_ANON) |
|
110 |
- set(ANONYMOUS_MAP MAP_ANON) |
|
111 |
- endif() |
|
112 |
-endif() |
|
113 |
- |
|
114 |
-# Check for getting the pagesize. |
|
115 |
-check_symbol_exists(getpagesize unistd.h HAVE_GETPAGESIZE) |
|
116 |
-check_c_source_compiles( |
|
117 |
- " |
|
118 |
- #include <sys/types.h> |
|
119 |
- #if HAVE_UNISTD_H |
|
120 |
- #include <unistd.h> |
|
121 |
- #endif |
|
122 |
- int main(void) |
|
123 |
- { |
|
124 |
- int x = sysconf(_SC_PAGESIZE); |
|
125 |
- return 0; |
|
126 |
- } |
|
127 |
- " |
|
128 |
- HAVE_SYSCONF_SC_PAGESIZE |
|
129 |
-) |
|
130 |
- |
|
131 |
-# Check for mempool support |
|
132 |
-if(DISABLE_MPOOL) |
|
133 |
- message("****** mempool support disabled (DISABLE_MPOOL enabled)") |
|
134 |
-elseif(NOT HAVE_MMAP) |
|
135 |
- message("****** mempool support disabled (mmap() not available or not usable)") |
|
136 |
-elseif(NOT HAVE_GETPAGESIZE AND NOT HAVE_SYSCONF_SC_PAGESIZE) |
|
137 |
- message("****** mempool support disabled (pagesize cannot be determined)") |
|
138 |
-elseif(NOT HAVE_MMAP_MAP_ANON AND NOT HAVE_MMAP_MAP_ANONYMOUS) |
|
139 |
- message("****** mempool support disabled (anonymous mmap not available)") |
|
140 |
-else() |
|
141 |
- set(USE_MPOOL 1) |
|
142 |
-endif() |
... | ... |
@@ -4,7 +4,7 @@ dnl AC_FUNC_MMAP checks for private fixed mappings, we don't need |
4 | 4 |
dnl fixed mappings, so check only wether private mappings work. |
5 | 5 |
dnl AC_FUNC_MMAP would fail on HP-UX for example. |
6 | 6 |
AC_DEFUN([AC_C_FUNC_MMAP_PRIVATE], |
7 |
-[ |
|
7 |
+[ |
|
8 | 8 |
AC_CACHE_CHECK([for working mmap], [ac_cv_c_mmap_private], |
9 | 9 |
[ |
10 | 10 |
AC_RUN_IFELSE([AC_LANG_SOURCE([ |
... | ... |
@@ -17,54 +17,62 @@ AC_DEFUN([AC_C_FUNC_MMAP_PRIVATE], |
17 | 17 |
#ifdef HAVE_SYS_STAT_H |
18 | 18 |
#include <sys/stat.h> |
19 | 19 |
#endif |
20 |
-#include <fcntl.h> |
|
20 |
+#include <fcntl.h> |
|
21 |
+#define ERR(e) do { status = e; goto done; } while(0) |
|
21 | 22 |
int main(void) |
22 | 23 |
{ |
23 |
- char *data, *data2, *data3; |
|
24 |
+ char *data = NULL, *data2 = MAP_FAILED, *data3 = NULL; |
|
24 | 25 |
size_t i, datasize = 1024; |
25 |
- int fd; |
|
26 |
+ int fd = -1, status = 0; |
|
26 | 27 |
|
27 | 28 |
/* First, make a file with some known garbage in it. */ |
28 | 29 |
data = (char*) malloc(datasize); |
29 | 30 |
if(!data) |
30 |
- return 1; |
|
31 |
+ ERR(1); |
|
31 | 32 |
for(i=0;i<datasize;i++) |
32 | 33 |
*(data + i) = rand(); |
33 | 34 |
umask(0); |
34 | 35 |
fd = creat("conftest.mmap", 0600); |
35 | 36 |
if(fd < 0) |
36 |
- return 1; |
|
37 |
+ ERR(1); |
|
37 | 38 |
if(write (fd, data, datasize) != datasize) |
38 |
- return 1; |
|
39 |
+ ERR(1); |
|
39 | 40 |
close(fd); |
40 | 41 |
fd = open("conftest.mmap", O_RDWR); |
41 | 42 |
if (fd < 0) |
42 |
- return 1; |
|
43 |
- /* Next, try to mmap the file at a fixed address which already has |
|
44 |
- something else allocated at it. If we can, also make sure that |
|
43 |
+ ERR(1); |
|
44 |
+ /* Next, try to create a private map of the file. If we can, also make sure that |
|
45 | 45 |
we see the same garbage. */ |
46 |
- data2 = mmap(NULL, sizeof(data), PROT_READ | PROT_WRITE, |
|
47 |
- MAP_PRIVATE, fd, 0L); |
|
46 |
+ data2 = mmap(NULL, datasize, PROT_READ | PROT_WRITE, |
|
47 |
+ MAP_PRIVATE, fd, 0L); |
|
48 | 48 |
if(data2 == MAP_FAILED) |
49 |
- return 2; |
|
50 |
- for(i=0;i<sizeof(data);i++) |
|
49 |
+ ERR(2); |
|
50 |
+ for(i=0;i<datasize;i++) |
|
51 | 51 |
if(*(data + i) != *(data2+ i)) |
52 |
- return 3; |
|
53 |
- /* Finally, make sure that changes to the mapped area do not |
|
54 |
- percolate back to the file as seen by read(). (This is a bug on |
|
55 |
- some variants of i386 svr4.0.) */ |
|
56 |
- for (i = 0; i < datasize; ++i) |
|
57 |
- *(data2 + i) = *(data2 + i) + 1; |
|
52 |
+ ERR(3); |
|
53 |
+ /* Finally, make sure that changes to the mapped area do not |
|
54 |
+ percolate back to the file as seen by read(). |
|
55 |
+ (This is a bug on some variants of i386 svr4.0.) */ |
|
56 |
+ for (i = 0; i < datasize; ++i) |
|
57 |
+ *(data2 + i) = *(data2 + i) + 1; |
|
58 | 58 |
data3 = (char*) malloc(datasize); |
59 | 59 |
if(!data3) |
60 |
- return 1; |
|
60 |
+ ERR(1); |
|
61 | 61 |
if(read (fd, data3, datasize) != datasize) |
62 |
- return 1; |
|
63 |
- for(i=0;i<sizeof(data);i++) |
|
62 |
+ ERR(1); |
|
63 |
+ for(i=0;i<datasize;i++) |
|
64 | 64 |
if(*(data + i) != *(data3 + i)) |
65 |
- return 3; |
|
66 |
- close(fd); |
|
67 |
- return 0; |
|
65 |
+ ERR(3); |
|
66 |
+done: |
|
67 |
+ if(fd >= 0) |
|
68 |
+ close(fd); |
|
69 |
+ if(data3) |
|
70 |
+ free(data3); |
|
71 |
+ if(data2 != MAP_FAILED) |
|
72 |
+ munmap(data2, datasize); |
|
73 |
+ if(data) |
|
74 |
+ free(data); |
|
75 |
+ return status; |
|
68 | 76 |
}])], |
69 | 77 |
[ac_cv_c_mmap_private=yes], |
70 | 78 |
[ac_cv_c_mmap_private=no], |
... | ... |
@@ -127,7 +135,7 @@ AC_CACHE_CHECK([for getpagesize()], [ac_cv_c_getpagesize], [ |
127 | 127 |
AC_LINK_IFELSE([AC_LANG_PROGRAM([[ |
128 | 128 |
#if HAVE_UNISTD_H |
129 | 129 |
#include <unistd.h> |
130 |
-#endif]], [[int x = getpagesize();]])], |
|
130 |
+#endif]], [[int x = getpagesize();]])], |
|
131 | 131 |
[ac_cv_c_getpagesize=yes], [ac_cv_c_getpagesize=no]) |
132 | 132 |
]) |
133 | 133 |
if test "$ac_cv_c_getpagesize" = "yes"; then |