Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mmalloc] Add documentation
authorGabriel Corona <gabriel.corona@loria.fr>
Fri, 11 Jul 2014 10:06:47 +0000 (12:06 +0200)
committerGabriel Corona <gabriel.corona@loria.fr>
Fri, 11 Jul 2014 10:06:47 +0000 (12:06 +0200)
src/xbt/mmalloc/mmorecore.c
src/xbt/mmalloc/mmprivate.h

index 9fbf7a9..4a9cbb5 100644 (file)
                              ? -1                                 \
                              : (MDP) -> fd)
 
-/*  Get core for the memory region specified by MDP, using SIZE as the
-    amount to either add to or subtract from the existing region.  Works
-    like sbrk(), but using mmap().
-
-    It never returns NULL. Instead, it dies verbosely on errors. */
-
+/** @brief Add memoty to this heap
+ *
+ *  Get core for the memory region specified by MDP, using SIZE as the
+ *  amount to either add to or subtract from the existing region.  Works
+ *  like sbrk(), but using mmap().
+ *
+ *  It never returns NULL. Instead, it dies verbosely on errors.
+ *
+ *  @param mdp  The heap
+ *  @param size Bytes to allocate for this heap (or <0 to free memory from this heap)
+ */
 void *mmorecore(struct mdesc *mdp, ssize_t size)
 {
   ssize_t test = 0;
@@ -59,8 +64,6 @@ void *mmorecore(struct mdesc *mdp, ssize_t size)
   void *mapto;                  /* Address we actually mapped to */
   char buf = 0;                 /* Single byte to write to extend mapped file */
 
-//  fprintf(stderr,"increase %p by %u\n",mdp,size);
-
   if (size == 0) {
     /* Just return the current "break" value. */
     result = mdp->breakval;
@@ -108,12 +111,15 @@ void *mmorecore(struct mdesc *mdp, ssize_t size)
       }
 
       /* Let's call mmap. Note that it is possible that mdp->top
-         is 0. In this case mmap will choose the address for us */
+         is 0. In this case mmap will choose the address for us.
+         This call might very well overwrite an already existing memory mapping
+         (leading to weird bugs).
+       */
       mapto = mmap(mdp->top, mapbytes, PROT_READ | PROT_WRITE,
                    MAP_PRIVATE_OR_SHARED(mdp) | MAP_IS_ANONYMOUS(mdp) |
                    MAP_FIXED, MAP_ANON_OR_FD(mdp), foffset);
 
-      if (mapto == (void *) -1/* That's MAP_FAILED */) {
+      if (mapto == MAP_FAILED) {
         char buff[1024];
         fprintf(stderr,"Internal error: mmap returned MAP_FAILED! error: %s\n",strerror(errno));
         sprintf(buff,"cat /proc/%d/maps",getpid());
@@ -131,6 +137,7 @@ void *mmorecore(struct mdesc *mdp, ssize_t size)
       result = (void *) mdp->breakval;
       mdp->breakval = (char *) mdp->breakval + size;
     } else {
+      /* Memory is already mapped, we only need to increase the breakval: */
       result = (void *) mdp->breakval;
       mdp->breakval = (char *) mdp->breakval + size;
     }
index abf6140..69a6a75 100644 (file)
@@ -176,86 +176,98 @@ typedef struct {
   };
 } malloc_info;
 
-/* Internal structure that defines the format of the malloc-descriptor.
-   This gets written to the base address of the region that mmalloc is
-   managing, and thus also becomes the file header for the mapped file,
-   if such a file exists. */
-
+/** @brief Descriptor of a mmalloc area
+ *
+ * Internal structure that defines the format of the malloc-descriptor.
+ * This gets written to the base address of the region that mmalloc is
+ * managing, and thus also becomes the file header for the mapped file,
+ * if such a file exists.
+ * */
 struct mdesc {
 
-  /* Semaphore locking the access to the heap */
+  /** @brief Semaphore locking the access to the heap */
   sem_t sem;
 
-  /* Number of processes that attached the heap */
+  /** @brief Number of processes that attached the heap */
   unsigned int refcount;
 
-  /* Chained lists of mdescs */
+  /** @brief Chained lists of mdescs */
   struct mdesc *next_mdesc;
 
-  /* The "magic number" for an mmalloc file. */
+  /** @brief The "magic number" for an mmalloc file. */
   char magic[MMALLOC_MAGIC_SIZE];
 
-  /* The size in bytes of this structure, used as a sanity check when reusing
-     a previously created mapped file. */
+  /** @brief The size in bytes of this structure
+   *
+   * Used as a sanity check when reusing a previously created mapped file.
+   * */
   unsigned int headersize;
 
-  /* The version number of the mmalloc package that created this file. */
+  /** @brief Version number of the mmalloc package that created this file. */
   unsigned char version;
 
   unsigned int options;
 
-  /* Some flag bits to keep track of various internal things. */
+  /** @brief Some flag bits to keep track of various internal things. */
   unsigned int flags;
 
-  /* Number of info entries.  */
+  /** @brief Number of info entries.  */
   size_t heapsize;
 
-  /* Pointer to first block of the heap (base of the first block).  */
+  /** @brief Pointer to first block of the heap (base of the first block).  */
   void *heapbase;
 
-  /* Current search index for the heap table.  */
-  /* Search index in the info table.  */
+  /** @brief Current search index for the heap table.
+   *
+   *  Search index in the info table.
+   */
   size_t heapindex;
 
-  /* Limit of valid info table indices.  */
+  /** @brief Limit of valid info table indices.  */
   size_t heaplimit;
 
-  /* Block information table. */
-  /* Table indexed by block number giving per-block information.  */
+  /** @brief Block information table.
+   *
+   * Table indexed by block number giving per-block information.
+   */
   malloc_info *heapinfo;
 
-  /* List of all blocks containing free fragments of this size.
+  /* @brief List of all blocks containing free fragments of a given size.
+   *
    * The array indice is the log2 of requested size.
    * Actually only the sizes 8->11 seem to be used, but who cares? */
   s_xbt_swag_t fraghead[BLOCKLOG];
 
-  /* The base address of the memory region for this malloc heap.  This
-     is the location where the bookkeeping data for mmap and for malloc
-     begins. */
-
+  /* @brief Base address of the memory region for this malloc heap
+   *
+   * This is the location where the bookkeeping data for mmap and
+   * for malloc begins.
+   */
   void *base;
 
-  /* The current location in the memory region for this malloc heap which
-     represents the end of memory in use. */
-
+  /** @brief End of memory in use
+   *
+   *  Some memory might be already mapped by the OS but not used
+   *  by the heap.
+   * */
   void *breakval;
 
-  /* The end of the current memory region for this malloc heap.  This is
-     the first location past the end of mapped memory.
-     Compared to breakval, this value is rounded to the next memory page.
-      */
-
+  /** @brief End of the current memory region for this malloc heap.
+   *
+   *  This is the first location past the end of mapped memory.
+   *
+   *  Compared to breakval, this value is rounded to the next memory page.
+   */
   void *top;
 
-  /* Open file descriptor for the file to which this malloc heap is mapped.
-     This will always be a valid file descriptor, since /dev/zero is used
-     by default if no open file is supplied by the client.  Also note that
-     it may change each time the region is mapped and unmapped. */
-
+  /** @brief Open file descriptor for the file to which this malloc heap is mapped
+   *
+   * If this value is negative, MAP_ANONYMOUS memory is used.
+   *
+   * Also note that it may change each time the region is mapped and unmapped. */
   int fd;
 
-  /* Instrumentation.  */
-
+  /* @brief Instrumentation */
   struct mstats heapstats;
 
 };
@@ -274,9 +286,6 @@ XBT_PUBLIC( struct mdesc ) *__mmalloc_default_mdp;
 
 XBT_PUBLIC( void *)__mmalloc_remap_core(xbt_mheap_t mdp);
 
-/*  Get core for the memory region specified by MDP, using SIZE as the
-    amount to either add to or subtract from the existing region.  Works
-    like sbrk(), but using mmap(). */
 XBT_PUBLIC( void *)mmorecore(struct mdesc *mdp, ssize_t size);
 
 /* Thread-safety (if the sem is already created)