lib/util: add allocate_anonymous_shared()
authorStefan Metzmacher <metze@samba.org>
Mon, 22 Mar 2010 07:27:58 +0000 (08:27 +0100)
committerStefan Metzmacher <metze@samba.org>
Mon, 22 Mar 2010 16:15:10 +0000 (17:15 +0100)
metze

lib/util/util.c
lib/util/util.h

index 0064cef8f2c74eecb8e202505aa660cba763edc5..d645f7edc91c7e73081d593f50ac568014055bc6 100644 (file)
@@ -25,6 +25,8 @@
 #include "system/network.h"
 #include "system/filesys.h"
 #include "system/locale.h"
+#include "system/shmem.h"
+
 #undef malloc
 #undef strcasecmp
 #undef strncasecmp
@@ -862,4 +864,30 @@ bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx,
        return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false);
 }
 
+/* Map a shared memory buffer of at least nelem counters. */
+void *allocate_anonymous_shared(size_t bufsz)
+{
+       void *buf;
+       size_t pagesz = getpagesize();
+
+       if (bufsz % pagesz) {
+               bufsz = (bufsz + pagesz) % pagesz; /* round up to pagesz */
+       }
+
+#ifdef MAP_ANON
+       /* BSD */
+       buf = mmap(NULL, bufsz, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED,
+                       -1 /* fd */, 0 /* offset */);
+#else
+       buf = mmap(NULL, bufsz, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED,
+                       open("/dev/zero", O_RDWR), 0 /* offset */);
+#endif
+
+       if (buf == MAP_FAILED) {
+               return NULL;
+       }
+
+       return buf;
+
+}
 
index e1160d5a3e4e7dfe07ea13a6800280e6db87aba2..2d4a02549fe624b82e6ee469627db0f28aebac11 100644 (file)
@@ -880,6 +880,11 @@ bool add_uid_to_array_unique(TALLOC_CTX *mem_ctx, uid_t uid,
 bool add_gid_to_array_unique(TALLOC_CTX *mem_ctx, gid_t gid,
                             gid_t **gids, size_t *num_gids);
 
+/**
+ * Allocate anonymous shared memory of the given size
+ */
+void *allocate_anonymous_shared(size_t bufsz);
+
 /*
   run a command as a child process, with a timeout.