lib/util: add anonymous_shared_free()
authorStefan Metzmacher <metze@samba.org>
Tue, 26 Oct 2010 20:45:19 +0000 (22:45 +0200)
committerStefan Metzmacher <metze@samba.org>
Wed, 27 Oct 2010 07:33:35 +0000 (09:33 +0200)
metze

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

index 2bd772b03d921e92bb0754c95af5c64a8a1e80b0..25eed694e85941afe6c0c290cc7b3c60b372e7f6 100644 (file)
@@ -936,11 +936,22 @@ bool next_token_no_ltrim_talloc(TALLOC_CTX *ctx,
        return next_token_internal_talloc(ctx, ptr, pp_buff, sep, false);
 }
 
+struct anonymous_shared_header {
+       union {
+               size_t length;
+               uint8_t pad[16];
+       } u;
+};
+
 /* Map a shared memory buffer of at least nelem counters. */
 void *anonymous_shared_allocate(size_t bufsz)
 {
+       void *ptr;
        void *buf;
        size_t pagesz = getpagesize();
+       struct anonymous_shared_header *hdr;
+
+       bufsz += sizeof(*hdr);
 
        if (bufsz % pagesz) {
                bufsz = (bufsz + pagesz) % pagesz; /* round up to pagesz */
@@ -959,8 +970,27 @@ void *anonymous_shared_allocate(size_t bufsz)
                return NULL;
        }
 
-       return buf;
+       hdr = (struct anonymous_shared_header *)buf;
+       hdr->u.length = bufsz;
+
+       ptr = (void *)(&hdr[1]);
+
+       return ptr;
+}
+
+void anonymous_shared_free(void *ptr)
+{
+       struct anonymous_shared_header *hdr;
+
+       if (ptr == NULL) {
+               return;
+       }
+
+       hdr = (struct anonymous_shared_header *)ptr;
+
+       hdr--;
 
+       munmap(hdr, hdr->u.length);
 }
 
 #ifdef DEVELOPER
index 93224e53a3fdc9a67c0e4759fdf217045ddd6042..c7300108b803583bbf0865233be5779b95cee52f 100644 (file)
@@ -858,6 +858,7 @@ bool add_gid_to_array_unique(TALLOC_CTX *mem_ctx, gid_t gid,
  * Allocate anonymous shared memory of the given size
  */
 void *anonymous_shared_allocate(size_t bufsz);
+void anonymous_shared_free(void *ptr);
 
 /*
   run a command as a child process, with a timeout.