title: Memory Allocator implementation segfault
body: <p>I am currently developing a memory allocator, I have come with this code a the moment but this code below segfault on almost every commands when i do a LD_PRELOAD of my implementation.</p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include "malloc.h"
  
  t_mem_map       g_map;
  int         g_empty_map = TRUE;
  
  static t_ctrl       *get_free_block(size_t size)
  {
    t_ctrl        *tmp;
  
    tmp = g_map.head;
    while (tmp != NULL)
      {
        if (tmp-&gt;is_free == TRUE &amp;&amp; tmp-&gt;size &lt; size)
      {
        tmp-&gt;is_free = FALSE;
        return (tmp);
      }
        tmp = tmp-&gt;next;
      }
    return (NULL);
  }
  
  static void     *init_map(size_t size)
  {
    t_ctrl        *tmp;
  
    g_map.map_size = DEFAULT_MAP_SIZE;
    while (g_map.map_size &lt; size + sizeof(t_ctrl))
      g_map.map_size += DEFAULT_MAP_SIZE;
    if ((g_map.head = (t_ctrl *)sbrk(g_map.map_size)) == (void *)-1)
      return (NULL);
    tmp = g_map.head;
    tmp-&gt;next = NULL;
    tmp-&gt;prev = NULL;
    tmp-&gt;size = size;
    tmp-&gt;is_free = FALSE;
    g_map.free_space = g_map.map_size - size - sizeof(t_ctrl);
    g_empty_map = FALSE;
    unlock_thread();
    return ((void *)((char *)tmp + sizeof(t_ctrl)));
  }
  
  static void     *add_block(size_t size)
  {
    t_ctrl        *tmp;
    t_ctrl        *new;
  
    tmp = get_free_block(size);
    if (tmp != NULL)
      return ((void *)((char *)tmp + sizeof(t_ctrl)));
    tmp = g_map.head;
    while (tmp-&gt;next != NULL)
      tmp = tmp-&gt;next;
    new = (t_ctrl *)((char *)tmp + sizeof(t_ctrl) + tmp-&gt;size);
    new-&gt;prev = tmp;
    new-&gt;next = NULL;
    tmp-&gt;next = new;
    new-&gt;size = size;
    new-&gt;is_free = FALSE;
    g_map.free_space -= (new-&gt;size + sizeof(t_ctrl));
    unlock_thread();
    return ((void *)((char *)new + sizeof(t_ctrl)));
  }
  
  static void     *resize_map(size_t size)
  {
    size_t        size_shift;
  
    size_shift = 0;
    while (g_map.free_space &lt; size + sizeof(t_ctrl))
      {
        g_map.map_size += DEFAULT_MAP_SIZE;
        g_map.free_space += DEFAULT_MAP_SIZE;
        size_shift += DEFAULT_MAP_SIZE;
      }
    if (sbrk(size_shift) == (void *)-1)
      return (NULL);
    return (add_block(size));
  }
  
  void            *malloc(size_t size)
  {
    size_t        a_size;
  
    lock_thread();
    a_size = ALIGN(size);
    if (g_empty_map == TRUE)
      return (init_map(a_size));
    else
      {
        if ((a_size + sizeof(t_ctrl)) &lt;= g_map.free_space)
          return (add_block(a_size));
        else
      return (resize_map(a_size));
      }
    return (NULL);
  }
  </code></pre>
  
  <p>Here is the malloc.h :</p>
  
  <pre><code># include &lt;stdio.h&gt;
  # include &lt;stddef.h&gt;
  # include &lt;unistd.h&gt;
  
  # define TRUE 0
  # define FALSE 1
  
  # define SUCCESS 0
  # define FAILURE 1
  
  
  # ifndef __X86_64__
  #  define ALIGNMENT (16)
  # else
  #  define ALIGNMENT (8)
  # endif
  
  # define ALIGN(size) (((size) + (ALIGNMENT - 1)) &amp;~ (ALIGNMENT - 1))
  # define DEFAULT_MAP_SIZE (ALIGN(sysconf(_SC_PAGESIZE)))
  
  typedef struct      s_ctrl
  {
    size_t        is_free;
    size_t        size;
    struct s_ctrl     *next;
    struct s_ctrl     *prev;
  }           t_ctrl;
  
  typedef struct      s_mem_map
  {
    size_t        map_size;
    size_t        free_space;
    //  int           free_blocks;
    //int         nb_blocks;
    t_ctrl        *head;
  }           t_mem_map;
  </code></pre>
  
  <p>With some research I found out that the segfault is likely coming from the while loop in the get_free_block() function but i can't understand why.</p>
  
  <pre><code>static t_ctrl       *get_free_block(size_t size)
  {
    t_ctrl        *tmp;
  
    tmp = g_map.head;
    while (tmp != NULL)
      {
        if (tmp-&gt;is_free == TRUE &amp;&amp; tmp-&gt;size &lt; size)
          {
            tmp-&gt;is_free = FALSE;
            return (tmp);
          }
        tmp = tmp-&gt;next;
      }
    return (NULL);
  }
  </code></pre>