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>
Thu, 20 Jan 2011 04:31:45 +0000 (05:31 +0100)
metze

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

index db71def4c4c44ee3d66481b4f6a707e3e63fed66..35ad49b5323870ab309c2506b321a12f31a34937 100644 (file)
@@ -936,12 +936,24 @@ 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 *anonymous_shared_allocate(size_t orig_bufsz)
 {
+       void *ptr;
        void *buf;
        size_t pagesz = getpagesize();
        size_t pagecnt;
+       size_t bufsz = orig_bufsz;
+       struct anonymous_shared_header *hdr;
+
+       bufsz += sizeof(*hdr);
 
        /* round up to full pages */
        pagecnt = bufsz / pagesz;
@@ -950,6 +962,12 @@ void *anonymous_shared_allocate(size_t bufsz)
        }
        bufsz = pagesz * pagecnt;
 
+       if (orig_bufsz >= bufsz) {
+               /* integer wrap */
+               errno = ENOMEM;
+               return NULL;
+       }
+
 #ifdef MAP_ANON
        /* BSD */
        buf = mmap(NULL, bufsz, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED,
@@ -963,8 +981,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 04ea20d07df55d646aa4cdb312d1e0e330136bfc..8f4fd8f70bc712add87c8847a31c127b033f9476 100644 (file)
@@ -859,6 +859,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.