From 01f2c023f7d2a4b0e016676638a062a5ba29ec0b Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Mon, 22 Mar 2010 08:27:58 +0100 Subject: lib/util: add allocate_anonymous_shared() metze --- lib/util/util.c | 28 ++++++++++++++++++++++++++++ lib/util/util.h | 5 +++++ 2 files changed, 33 insertions(+) (limited to 'lib') diff --git a/lib/util/util.c b/lib/util/util.c index 0064cef8f2..d645f7edc9 100644 --- a/lib/util/util.c +++ b/lib/util/util.c @@ -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; + +} diff --git a/lib/util/util.h b/lib/util/util.h index e1160d5a3e..2d4a02549f 100644 --- a/lib/util/util.h +++ b/lib/util/util.h @@ -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. -- cgit