+#if ENABLE_FEATURE_INSMOD_TRY_MMAP
+void* FAST_FUNC try_to_mmap_module(const char *filename, size_t *image_size_p)
+{
+ /* We have user reports of failure to load 3MB module
+ * on a 16MB RAM machine. Apparently even a transient
+ * memory spike to 6MB during module load
+ * is too big for that system. */
+ void *image;
+ struct stat st;
+ int fd;
+
+ fd = xopen(filename, O_RDONLY);
+ fstat(fd, &st);
+ image = NULL;
+ /* st.st_size is off_t, we can't just pass it to mmap */
+ if (st.st_size <= *image_size_p) {
+ size_t image_size = st.st_size;
+ image = mmap(NULL, image_size, PROT_READ, MAP_PRIVATE, fd, 0);
+ if (image == MAP_FAILED) {
+ image = NULL;
+ } else if (*(uint32_t*)image != SWAP_BE32(0x7f454C46)) {
+ /* No ELF signature. Compressed module? */
+ munmap(image, image_size);
+ image = NULL;
+ } else {
+ /* Success. Report the size */
+ *image_size_p = image_size;
+ }
+ }
+ close(fd);
+ return image;
+}
+#endif
+