Port: Add bt_common_get_page_size
authorMichael Jeanson <mjeanson@efficios.com>
Mon, 12 Sep 2016 18:36:58 +0000 (18:36 +0000)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 9 Jun 2017 20:58:14 +0000 (16:58 -0400)
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
common/common.c
include/Makefile.am
include/babeltrace/align-internal.h
include/babeltrace/common-internal.h
include/babeltrace/compat/unistd-internal.h [new file with mode: 0644]
include/babeltrace/ctf-writer/serialize-internal.h
include/babeltrace/mmap-align-internal.h
lib/ctf-ir/fields.c
lib/ctf-writer/serialize.c
plugins/ctf/fs-src/fs.c

index 032a1e06294b65aeed59a34f4276fdd543603808..b92fc89dd660aa85917c55071eedb996789986ce 100644 (file)
 #include <assert.h>
 #include <ctype.h>
 #include <glib.h>
+#include <stdlib.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/common-internal.h>
+#include <babeltrace/compat/unistd-internal.h>
 
 #define SYSTEM_PLUGIN_PATH     INSTALL_LIBDIR "/babeltrace/plugins"
 #define HOME_ENV_VAR           "HOME"
@@ -1125,3 +1127,17 @@ end:
 
        return norm_path;
 }
+
+BT_HIDDEN
+size_t bt_common_get_page_size(void)
+{
+       int page_size;
+
+       page_size = bt_sysconf(_SC_PAGESIZE);
+       if (page_size < 0) {
+               printf_error("Cannot get system page size.");
+               abort();
+       }
+
+       return page_size;
+}
index 86eade69072c24e95def4293ef633ae747745054..bf0ca69cdb2c80173d2e4c94c92068bb1072dc2b 100644 (file)
@@ -86,6 +86,7 @@ noinst_HEADERS = \
        babeltrace/compat/stdio-internal.h \
        babeltrace/compat/stdlib-internal.h \
        babeltrace/compat/string-internal.h \
+       babeltrace/compat/unistd-internal.h \
        babeltrace/compat/utc-internal.h \
        babeltrace/compat/uuid-internal.h \
        babeltrace/compiler-internal.h \
index a2566c9900eaf0855e42fa1729727d7acfc1f4c4..5d86ad57b47ac77c881a681f7dabd6630d62b734 100644 (file)
  */
 
 #include <babeltrace/compiler-internal.h>
-#include <unistd.h>
 #include <babeltrace/compat/limits-internal.h>
 
-#ifndef PAGE_SIZE              /* Cygwin limits.h defines its own PAGE_SIZE */
-#define PAGE_SIZE              sysconf(_SC_PAGE_SIZE)
-#endif
-
 #define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a) - 1)
 #define __ALIGN_MASK(x, mask)  (((x) + (mask)) & ~(mask))
 #define PTR_ALIGN(p, a)                ((typeof(p)) ALIGN((unsigned long) (p), a))
index eb055f5e5eeffc0db92eb5e8d3b7641cad3bff9d..880b2e47357b33a0328686dc73a4b4e2c8b8a6f7 100644 (file)
@@ -199,4 +199,10 @@ bool bt_common_star_glob_match(const char *pattern, size_t pattern_len,
 BT_HIDDEN
 GString *bt_common_normalize_path(const char *path, const char *wd);
 
+/*
+ * Return the system page size.
+ */
+BT_HIDDEN
+size_t bt_common_get_page_size(void);
+
 #endif /* BABELTRACE_COMMON_INTERNAL_H */
diff --git a/include/babeltrace/compat/unistd-internal.h b/include/babeltrace/compat/unistd-internal.h
new file mode 100644 (file)
index 0000000..b06cdc8
--- /dev/null
@@ -0,0 +1,61 @@
+#ifndef _BABELTRACE_COMPAT_UNISTD_H
+#define _BABELTRACE_COMPAT_UNISTD_H
+
+/*
+ * babeltrace/compat/unistd.h
+ *
+ * (C) Copyright 2016 - Michael Jeanson <mjeanson@efficios.com>
+ *
+ * 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.
+ */
+
+
+#include <unistd.h>
+
+#ifdef __MINGW32__
+#include <windows.h>
+#include <errno.h>
+
+#define _SC_PAGESIZE 30
+
+static inline
+long bt_sysconf(int name)
+{
+       SYSTEM_INFO si;
+
+       switch(name) {
+       case _SC_PAGESIZE:
+               GetNativeSystemInfo(&si);
+               return si.dwPageSize;
+       default:
+               errno = EINVAL;
+               return -1;
+       }
+}
+
+#else
+
+static inline
+long bt_sysconf(int name)
+{
+       return sysconf(name);
+}
+
+#endif
+#endif /* _BABELTRACE_COMPAT_UNISTD_H */
index 5cf8c5bd660a787a54d7f61df37df993dff5371b..e029764e53b2c917b0e664bac31a16f2fd06a426 100644 (file)
 #include <babeltrace/ctf-ir/fields.h>
 #include <babeltrace/ctf-ir/fields-internal.h>
 #include <babeltrace/align-internal.h>
+#include <babeltrace/common-internal.h>
 #include <babeltrace/mmap-align-internal.h>
 #include <babeltrace/types.h>
 
+#define PACKET_LEN_INCREMENT   (bt_common_get_page_size() * 8 * CHAR_BIT)
+
 struct bt_ctf_stream_pos {
        int fd;
        int prot;               /* mmap protection */
index d89affb592884a67f64507eee31c0493043891ae..0667ab0c8933ad7a8db91c659fd0977c3294ae9e 100644 (file)
@@ -29,6 +29,7 @@
 #include <stdlib.h>
 #include <stdint.h>
 #include <sys/mman.h>
+#include <babeltrace/common-internal.h>
 
 /*
  * This header implements a wrapper over mmap (mmap_align) that memory
@@ -53,19 +54,22 @@ struct mmap_align *mmap_align(size_t length, int prot,
 {
        struct mmap_align *mma;
        off_t page_aligned_offset;      /* mmap offset, aligned to floor */
+       size_t page_size;
+
+       page_size = bt_common_get_page_size();
 
        mma = malloc(sizeof(*mma));
        if (!mma)
                return MAP_FAILED;
        mma->length = length;
-       page_aligned_offset = ALIGN_FLOOR(offset, PAGE_SIZE);
+       page_aligned_offset = ALIGN_FLOOR(offset, page_size);
        /*
         * Page aligned length needs to contain the requested range.
         * E.g., for a small range that fits within a single page, we might
         * require a 2 pages page_aligned_length if the range crosses a page
         * boundary.
         */
-       mma->page_aligned_length = ALIGN(length + offset - page_aligned_offset, PAGE_SIZE);
+       mma->page_aligned_length = ALIGN(length + offset - page_aligned_offset, page_size);
        mma->page_aligned_addr = mmap(NULL, mma->page_aligned_length,
                prot, flags, fd, page_aligned_offset);
        if (mma->page_aligned_addr == (void *) -1UL) {
index d95fe3351af5199bf249e926b0038f050ce08c8c..e9c6d8bee399d9f0844e33dce121bf52ed466f22 100644 (file)
@@ -39,8 +39,6 @@
 #include <babeltrace/align-internal.h>
 #include <inttypes.h>
 
-#define PACKET_LEN_INCREMENT   (getpagesize() * 8 * CHAR_BIT)
-
 static
 struct bt_ctf_field *bt_ctf_field_integer_create(struct bt_ctf_field_type *);
 static
index f20ad405cb99cdcebbb76ac1a4f9ae0c53d9e6a7..11ce8b443531a5783dc6d8a80bff101ba0b4ff8f 100644 (file)
@@ -42,6 +42,7 @@
 #include <babeltrace/bitfield-internal.h>
 #include <babeltrace/compat/fcntl-internal.h>
 #include <babeltrace/types.h>
+#include <babeltrace/common-internal.h>
 #include <glib.h>
 
 #if (FLT_RADIX != 2)
@@ -284,7 +285,7 @@ void bt_ctf_stream_pos_packet_seek(struct bt_ctf_stream_pos *pos, size_t index,
 
        /* The writer will add padding */
        pos->mmap_offset += pos->packet_size / CHAR_BIT;
-       pos->packet_size = getpagesize() * 8 * CHAR_BIT;
+       pos->packet_size = PACKET_LEN_INCREMENT;
        do {
                ret = bt_posix_fallocate(pos->fd, pos->mmap_offset,
                        pos->packet_size / CHAR_BIT);
index 0e199e92e773fbc165af731c6c2e9b28a532e9db..bcb3302657c71c3206652e35d5840a502d84ed3f 100644 (file)
@@ -42,7 +42,6 @@
 #include <assert.h>
 #include <inttypes.h>
 #include <stdbool.h>
-#include <unistd.h>
 #include "fs.h"
 #include "metadata.h"
 #include "data-stream-file.h"
@@ -1277,7 +1276,7 @@ struct ctf_fs_component *ctf_fs_create(struct bt_private_component *priv_comp,
        }
 
        ctf_fs->error_fp = stderr;
-       ctf_fs->page_size = (size_t) getpagesize();
+       ctf_fs->page_size = bt_common_get_page_size();
        ctf_fs->port_data = g_ptr_array_new_with_free_func(port_data_destroy);
        if (!ctf_fs->port_data) {
                goto error;
This page took 0.02985 seconds and 4 git commands to generate.