From b5b3a2b317fb1a39a0978230486d4f606e21bbfa Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja.osipov@gmail.com>
Date: Tue, 15 Feb 2011 17:02:50 +0300
Subject: [PATCH] Blueprint 'unmmap-after-fork': review comments.

Make a system specific check for MAP_ANONYMOUS
part of configuration process.
Follow up on a broken ENABLE_BACKTRACE switch
and fix a few cases which didn't compile with it.
---
 CMakeLists.txt         |  4 ++++
 core/coro.c            |  5 +----
 core/fiber.c           |  8 +++-----
 core/salloc.c          |  1 +
 include/config.h.cmake | 11 +++++++++++
 include/fiber.h        |  3 ++-
 include/util.h         |  5 -----
 7 files changed, 22 insertions(+), 15 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index cfc4fdfc25..2d48a3e014 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 2.6)
 project(Tarantool)
 include(CheckLibraryExists)
 include(CheckIncludeFile)
+include(CheckSymbolExists)
 find_program(ECHO echo)
 find_program(CAT cat)
 find_program(GIT git)
@@ -36,6 +37,9 @@ else()
     message (FATAL_ERROR "Unsupported platform -- ${CMAKE_SYSTEM_NAME}")
 endif()
 
+check_symbol_exists(MAP_ANON sys/mman.h HAVE_MAP_ANON)
+check_symbol_exists(MAP_ANONYMOUS sys/mman.h HAVE_MAP_ANONYMOUS)
+
 #
 # Tarantool uses 'coro' (coroutines) library # to implement
 # cooperative multi-tasking. Since coro.h is included
diff --git a/core/coro.c b/core/coro.c
index 13f0045f43..46834a4bc9 100644
--- a/core/coro.c
+++ b/core/coro.c
@@ -24,6 +24,7 @@
  * SUCH DAMAGE.
  */
 
+#include "config.h"
 #include <unistd.h>
 #include <string.h>
 #include <sys/mman.h>
@@ -33,10 +34,6 @@
 #include <coro.h>
 #include <palloc.h>
 
-#ifndef MAP_ANONYMOUS
-#define MAP_ANONYMOUS MAP_ANON
-#endif
-
 struct tarantool_coro *
 tarantool_coro_create(struct tarantool_coro *coro, void (*f) (void *), void *data)
 {
diff --git a/core/fiber.c b/core/fiber.c
index ceb7647f53..f056ff2397 100644
--- a/core/fiber.c
+++ b/core/fiber.c
@@ -24,6 +24,8 @@
  * SUCH DAMAGE.
  */
 
+#include "config.h"
+#include <fiber.h>
 #include <arpa/inet.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -42,7 +44,6 @@
 #include <third_party/queue.h>
 #include <third_party/khash.h>
 
-#include <fiber.h>
 #include <palloc.h>
 #include <salloc.h>
 #include <say.h>
@@ -93,7 +94,7 @@ static void
 update_last_stack_frame(struct fiber *fiber)
 {
 #ifdef ENABLE_BACKTRACE
-	fiber->last_stack_frame = frame_addess();
+	fiber->last_stack_frame = __builtin_frame_address(0);
 #else
 	(void)fiber;
 #endif /* ENABLE_BACKTRACE */
@@ -335,9 +336,6 @@ fiber_loop(void *data __unused__)
 	}
 }
 
-#ifndef MAP_ANONYMOUS
-#define MAP_ANONYMOUS MAP_ANON
-#endif
 
 /* fiber never dies, just become zombie */
 struct fiber *
diff --git a/core/salloc.c b/core/salloc.c
index b30a6f26cb..d5ba9d91f1 100644
--- a/core/salloc.c
+++ b/core/salloc.c
@@ -24,6 +24,7 @@
  * SUCH DAMAGE.
  */
 
+#include "config.h"
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
diff --git a/include/config.h.cmake b/include/config.h.cmake
index 3bd80eb234..8473ba40e7 100644
--- a/include/config.h.cmake
+++ b/include/config.h.cmake
@@ -31,6 +31,17 @@
  * Set if the system has bfd.h header and GNU bfd library.
  */
 #cmakedefine HAVE_BFD 1
+#cmakedefine HAVE_MAP_ANON 1
+#cmakedefine HAVE_MAP_ANONYMOUS 1
+#if !defined(HAVE_MAP_ANONYMOUS) && defined(HAVE_MAP_ANON)
+/*
+ * MAP_ANON is deprecated, MAP_ANONYMOUS should be used instead.
+ * Unfortunately, it's not universally present (e.g. not present
+ * on FreeBSD.
+ */
+#define MAP_ANONYMOUS MAP_ANON
+#endif
+
 /*
  * vim: syntax=c
  */
diff --git a/include/fiber.h b/include/fiber.h
index d228fd3d3f..e9626b68ac 100644
--- a/include/fiber.h
+++ b/include/fiber.h
@@ -27,6 +27,7 @@
 #ifndef TARANTOOL_FIBER_H
 #define TARANTOOL_FIBER_H
 
+#include "config.h"
 #include <stdint.h>
 #include <unistd.h>
 #include <sys/uio.h>
@@ -55,7 +56,7 @@ struct ring {
 
 struct fiber {
 	ev_io io;
-#ifdef BACKTRACE
+#ifdef ENABLE_BACKTRACE
 	void *last_stack_frame;
 #endif
 	int csw;
diff --git a/include/util.h b/include/util.h
index fa75311758..dc3ccbd9ca 100644
--- a/include/util.h
+++ b/include/util.h
@@ -139,9 +139,4 @@ void assert_fail(const char *assertion, const char *file,
 		 unsigned int line, const char *function) __attribute__ ((noreturn));
 #endif
 
-#ifndef MAP_ANONYMOUS
-#define MAP_ANONYMOUS MAP_ANON
-#endif
-
-
 #endif
-- 
GitLab