Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : remove arg std_heap_addr in mmalloc_compare_heap and mmalloc_compare_...
[simgrid.git] / src / xbt / mmalloc / mmprivate.h
index 423c57f..28bb297 100644 (file)
 
 #define MMALLOC_MAGIC          "mmalloc"       /* Mapped file magic number */
 #define MMALLOC_MAGIC_SIZE     8       /* Size of magic number buf */
-#define MMALLOC_VERSION                1       /* Current mmalloc version */
+#define MMALLOC_VERSION                2       /* Current mmalloc version */
 
 /* The allocator divides the heap into blocks of fixed size; large
    requests receive one or more whole blocks, and small requests
    receive a fragment of a block.  Fragment sizes are powers of two,
    and all fragments of a block are the same size.  When all the
-   fragments in a block have been freed, the block itself is freed.  */
+   fragments in a block have been freed, the block itself is freed.
+
+   FIXME: we are not targeting 16bits machines anymore; update values */
 
 #define INT_BIT                (CHAR_BIT * sizeof(int))
 #define BLOCKLOG       (INT_BIT > 16 ? 12 : 9)
 #define BLOCKSIZE      ((unsigned int) 1 << BLOCKLOG)
 #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
 
+/* We keep fragment-specific meta-data for introspection purposes, and these
+ * information are kept in fixed lenght arrays. Here is the computation of
+ * that size.
+ *
+ * Never make SMALLEST_POSSIBLE_MALLOC smaller than sizeof(list) because we
+ * need to enlist the free fragments.
+ */
+
+#define SMALLEST_POSSIBLE_MALLOC (sizeof(struct list))
+#define MAX_FRAGMENT_PER_BLOCK (BLOCKSIZE / SMALLEST_POSSIBLE_MALLOC)
+
 /* The difference between two pointers is a signed int.  On machines where
    the data addresses have the high bit set, we need to ensure that the
    difference becomes an unsigned int when we are using the address as an
@@ -58,8 +71,8 @@
 #define HEAP           (INT_BIT > 16 ? 4194304 : 65536)
 
 /* Number of contiguous free blocks allowed to build up at the end of
-   memory before they will be returned to the system.  */
-
+   memory before they will be returned to the system.
+   FIXME: this is not used anymore: we never return memory to the system. */
 #define FINAL_FREE_BLOCKS      8
 
 /* Where to start searching the free list when looking for new memory.
 
 #define ADDRESS(B) ((void*) (((ADDR2UINT(B)) - 1) * BLOCKSIZE + (char*) mdp -> heapbase))
 
-const char *xbt_thread_self_name(void);
+/* Doubly linked lists of free fragments.  */
+struct list {
+       struct list *next;
+       struct list *prev;
+};
 
 /* Data structure giving per-block information.
  *
- * There is one such structure in the mdp->heapinfo array,
- * that is addressed by block number.
+ * There is one such structure in the mdp->heapinfo array per block used in that heap,
+ *    the array index is the block number.
  *
  * There is several types of blocks in memory:
  *  - full busy blocks: used when we are asked to malloc a block which size is > BLOCKSIZE/2
@@ -101,7 +118,6 @@ const char *xbt_thread_self_name(void);
  * You can crawl the array and rely on that value.
  *
  * TODO:
- *  - add an indication of the requested size in each fragment, similarly to busy_block.busy_size
  *  - make room to store the backtrace of where the blocks and fragment were malloced, too.
  */
 typedef struct {
@@ -113,10 +129,14 @@ typedef struct {
                struct {
                        size_t nfree;           /* Free fragments in a fragmented block.  */
                        size_t first;           /* First free fragment of the block.  */
+                       unsigned short frag_size[MAX_FRAGMENT_PER_BLOCK];
+      //void *bt[XBT_BACKTRACE_SIZE][MAX_FRAGMENT_PER_BLOCK]; /* Where it was malloced (or realloced lastly) */
                } busy_frag;
                struct {
                        size_t size; /* Size (in blocks) of a large cluster.  */
                        size_t busy_size; /* Actually used space, in bytes */
+                       void *bt[XBT_BACKTRACE_SIZE]; /* Where it was malloced (or realloced lastly) */
+                       int bt_size;
                } busy_block;
                /* Heap information for a free block (that may be the first of a free cluster).  */
                struct {
@@ -127,12 +147,6 @@ typedef struct {
        };
 } malloc_info;
 
-/* Doubly linked lists of free fragments.  */
-struct list {
-       struct list *next;
-       struct list *prev;
-};
-
 /* 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,
@@ -142,6 +156,7 @@ struct mdesc {
 
        /* Semaphore locking the access to the heap */
        sem_t sem;
+       char locked;
 
        /* Number of processes that attached the heap */
        unsigned int refcount;
@@ -208,7 +223,7 @@ struct mdesc {
 
 };
 
-int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2, void *std_heap_addr);
+int mmalloc_compare_mdesc(struct mdesc *mdp1, struct mdesc *mdp2);
 
 void mmalloc_display_info(void *h);
 
@@ -231,11 +246,24 @@ extern void *__mmalloc_remap_core(xbt_mheap_t mdp);
     like sbrk(), but using mmap(). */
 extern void *mmorecore(struct mdesc *mdp, int size);
 
-/* Thread-safety (if the sem is already created) FIXME: KILLIT*/
-#define LOCK(mdp)                                        \
-               sem_wait(&mdp->sem)
-
-#define UNLOCK(mdp)                                        \
-               sem_post(&mdp->sem)
+/* Thread-safety (if the sem is already created)
+ *
+ * This is mandatory in the case where the user runs a parallel simulation
+ * in a model-checking enabled tree. Without this protection, our malloc
+ * implementation will not like multi-threading AT ALL.
+ */
+#define LOCK(mdp)   do {    \
+    if (0 && mdp->locked) {      \
+      fprintf(stderr,"panic! deadlock detected because %s is not reintrant.\n",__FUNCTION__); \
+      abort();              \
+    }                       \
+               sem_wait(&mdp->sem);    \
+               mdp->locked=1;          \
+  } while(0)
+
+#define UNLOCK(mdp)  do {  \
+               sem_post(&mdp->sem);   \
+    mdp->locked=0;         \
+  } while (0)
 
 #endif                          /* __MMPRIVATE_H */