doc/api/libbabeltrace2/DoxygenLayout.xml: use `topics` tab
[babeltrace.git] / src / compat / mman.c
index d0cea9df10fc50f0912269fe0aeb8ff0648924f0..251e87354d1a40e1908c522d4031d7095b1eed47 100644 (file)
@@ -1,53 +1,42 @@
 /*
- * compat/compat_mman.h
+ * SPDX-License-Identifier: MIT
  *
- * Copyright (C) 2013   JP Ikaheimonen <jp_ikaheimonen@mentor.com>
- *               2016   Michael Jeanson <mjeanson@efficios.com>
+ * Copyright (C) 2013 JP Ikaheimonen <jp_ikaheimonen@mentor.com>
+ * Copyright (C) 2016 Michael Jeanson <mjeanson@efficios.com>
  *
  * These sources are based on ftp://g.oswego.edu/pub/misc/malloc.c
  * file by Doug Lea, released to the public domain.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
  */
 
-#define BT_LOG_TAG "COMPAT-MMAN"
-#include "logging.h"
+#define BT_LOG_OUTPUT_LEVEL (mapping->log_level)
+#define BT_LOG_TAG "COMPAT/MMAN"
+#include "logging/log.h"
+
+#include "common/macros.h"
+#include "common/common.h"
 
 #ifdef __APPLE__
 /*
  * On macOS, we need a dummy symbol so that the linker won't
  * complain of an empty table of contents.
  */
-BT_HIDDEN
 int bt_mman_dummy_symbol;
 #endif /* __APPLE__ */
 
 #ifdef __MINGW32__
 
 #include <errno.h>
+#include <glib.h>
 #include <io.h>
 #include <pthread.h>
 #include <stdlib.h>
 #include <windows.h>
+
 #include "compat/mman.h"
 
 struct mmap_mapping {
+       int log_level;
+
        /* The duplicated handle. */
        HANDLE file_handle;
        /* Handle returned by CreateFileMapping. */
@@ -63,14 +52,15 @@ GHashTable *mmap_mappings = NULL;
 static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 static
-struct mmap_mapping *mapping_create(void)
+struct mmap_mapping *mapping_create(int log_level)
 {
        struct mmap_mapping *mapping;
 
        mapping = malloc(sizeof(struct mmap_mapping));
-       if (mapping != NULL) {
+       if (mapping) {
                mapping->file_handle = NULL;
                mapping->map_handle = NULL;
+               mapping->log_level = log_level;
        }
 
        return mapping;
@@ -82,11 +72,11 @@ void mapping_clean(struct mmap_mapping *mapping)
        if (mapping) {
                if (!CloseHandle(mapping->map_handle)) {
                        BT_LOGF_STR("Failed to close mmap map_handle.");
-                       abort();
+                       bt_common_abort();
                }
                if (!CloseHandle(mapping->file_handle)) {
                        BT_LOGF_STR("Failed to close mmap file_handle.");
-                       abort();
+                       bt_common_abort();
                }
                free(mapping);
                mapping = NULL;
@@ -98,26 +88,31 @@ void addr_clean(void *addr)
 {
        /* Cleanup of handles should never fail. */
        if (!UnmapViewOfFile(addr)) {
-               BT_LOGF_STR("Failed to unmap mmap mapping.");
-               abort();
+               /*
+                * FIXME: We don't have access to the mapping's log
+                * level here, so force a FATAL level.
+                */
+               BT_LOG_WRITE_CUR_LVL(BT_LOG_FATAL, BT_LOG_FATAL, BT_LOG_TAG,
+                       "Failed to unmap mmap mapping.");
+               bt_common_abort();
        }
 }
 
 static
-void mmap_lock(void)
+void mmap_lock(int log_level)
 {
        if (pthread_mutex_lock(&mmap_mutex)) {
-               BT_LOGF_STR("Failed to acquire mmap_mutex.");
-               abort();
+               BT_LOG_WRITE_CUR_LVL(BT_LOG_FATAL, log_level, BT_LOG_TAG, "Failed to acquire mmap_mutex.");
+               bt_common_abort();
        }
 }
 
 static
-void mmap_unlock(void)
+void mmap_unlock(int log_level)
 {
        if (pthread_mutex_unlock(&mmap_mutex)) {
-               BT_LOGF_STR("Failed to release mmap_mutex.");
-               abort();
+               BT_LOG_WRITE_CUR_LVL(BT_LOG_FATAL, log_level, BT_LOG_TAG, "Failed to release mmap_mutex.");
+               bt_common_abort();
        }
 }
 
@@ -157,9 +152,8 @@ DWORD map_prot_flags(int prot, DWORD *dwDesiredAccess)
        return 0;
 }
 
-BT_HIDDEN
-void *bt_mmap(void *addr, size_t length, int prot, int flags, int fd,
-               off_t offset)
+void *bt_mmap(size_t length, int prot, int flags, int fd, off_t offset,
+               int log_level)
 {
        struct mmap_mapping *mapping = NULL;
        void *mapping_addr;
@@ -187,9 +181,10 @@ void *bt_mmap(void *addr, size_t length, int prot, int flags, int fd,
        }
 
        /* Allocate the mapping struct. */
-       mapping = mapping_create();
+       mapping = mapping_create(log_level);
        if (!mapping) {
-               BT_LOGE_STR("Failed to allocate mmap mapping.");
+               BT_LOG_WRITE_CUR_LVL(BT_LOG_ERROR, log_level, BT_LOG_TAG,
+                       "Failed to allocate mmap mapping.");
                _set_errno(ENOMEM);
                goto error;
        }
@@ -229,7 +224,7 @@ void *bt_mmap(void *addr, size_t length, int prot, int flags, int fd,
                goto error;
        }
 
-       mmap_lock();
+       mmap_lock(log_level);
 
        /* If we have never done any mappings, allocate the hashtable. */
        if (!mmap_mappings) {
@@ -246,26 +241,29 @@ void *bt_mmap(void *addr, size_t length, int prot, int flags, int fd,
        /* Add the new mapping to the hashtable. */
        g_hash_table_insert(mmap_mappings, mapping_addr, mapping);
 
-       mmap_unlock();
+       mmap_unlock(log_level);
 
        return mapping_addr;
 
 error_mutex_unlock:
-       mmap_unlock();
+       mmap_unlock(log_level);
 error:
        mapping_clean(mapping);
        return MAP_FAILED;
 }
 
-BT_HIDDEN
-int bt_munmap(void *addr, size_t length)
+int bt_munmap(void *addr, size_t length __attribute__((unused)))
 {
        int ret = 0;
+       struct mmap_mapping *mapping = addr;
+       int log_level;
 
-       mmap_lock();
+       BT_ASSERT(mapping);
+       log_level = mapping->log_level;
+       mmap_lock(log_level);
 
        /* Check if the mapping exists in the hashtable. */
-       if (g_hash_table_lookup(mmap_mappings, addr) == NULL) {
+       if (!g_hash_table_lookup(mmap_mappings, addr)) {
                _set_errno(EINVAL);
                ret = -1;
                goto end;
@@ -274,12 +272,24 @@ int bt_munmap(void *addr, size_t length)
        /* Remove it. */
        if (!g_hash_table_remove(mmap_mappings, addr)) {
                BT_LOGF_STR("Failed to remove mapping from hashtable.");
-               abort();
+               bt_common_abort();
        }
 
 end:
-       mmap_unlock();
+       mmap_unlock(log_level);
        return ret;
 }
 
+size_t bt_mmap_get_offset_align_size(int log_level)
+{
+       SYSTEM_INFO sysinfo;
+
+       GetNativeSystemInfo(&sysinfo);
+       BT_LOG_WRITE_PRINTF_CUR_LVL(BT_LOG_DEBUG, log_level, BT_LOG_TAG,
+               "Allocator granularity is %lu.",
+               sysinfo.dwAllocationGranularity);
+
+       return sysinfo.dwAllocationGranularity;
+}
+
 #endif
This page took 0.028252 seconds and 4 git commands to generate.