Объясните эту реализацию malloc из книги K & amp; R

Если вам нужен префикс, который вам может понравиться

 for ((i=7;i<=12;i++)); do echo `printf "%2.0d\n" $i |sed "s/ /0/"`;done

, который даст

07
08
09
10
11
12
29
задан M.M 20 July 2015 в 03:54
поделиться

4 ответа

1120 Хорошо, у нас есть кусок действительно плохо написанного кода. То, что я сделаю в этом посте, лучше всего описать как программную археологию.

Шаг 1: исправить форматирование.

Отступ и компактный формат никому не помогают. Различные пробелы и пустые строки должны быть вставлены. Комментарии могут быть написаны более читабельными способами. Я начну с исправления.

В то же время я меняю стиль фигурной скобки с стиля K & amp; R - пожалуйста, обратите внимание, что стиль фигурной скобки K & amp; R является приемлемым, это просто мое личное предпочтение. Другое личное предпочтение - написать * для указателей рядом с указанным типом. Я не буду спорить о (субъективных) вопросах стиля здесь.

Кроме того, определение типа Header полностью нечитаемо, оно нуждается в радикальном исправлении.

И я заметил нечто совершенно неясное: кажется, они объявили прототип функции внутри функции. Header* morecore(unsigned);. Это очень старый и очень плохой стиль, и я не уверен, что C даже позволяет это больше. Давайте просто удалим эту строку, что бы ни делала эта функция, она должна быть определена в другом месте.

typedef long Align;                      /* for alignment to long boundary */

typedef union header                     /* block header */
{
  struct
  {
    union header *ptr;                   /* next block if on free list */
    unsigned size;                       /* size of this block */
  } s;

  Align x;                               /* force alignment of blocks */

} Header;


static Header base;                      /* empty list to get started */
static Header* freep = NULL;             /* start of free list */


/* malloc: general-purpose storage allocator */
void* malloc (unsigned nbytes)
{
  Header*   p;
  Header*   prevp;
  unsigned  nunits;

  nunits = (nbytes + sizeof(Header) - 1) / sizeof(header) + 1;

  if ((prevp = freep) == NULL)           /* no free list yet */
  {
    base.s.ptr = freeptr = prevptr = &base;
    base.s.size = 0;
  }

  for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr)
  {
    if (p->s.size >= nunits)             /* big enough */
    {
      if (p->s.size == nunits)           /* exactly */
        prevp->s.ptr = p->s.ptr;
      else                               /* allocate tail end */
      {
        p->s.size -= nunits;
        p += p->s.size;
        p->s.size = nunits
      }

      freep = prevp;
      return (void *)(p+1);
    }

    if (p == freep)                      /* wrapped around free list */
      if ((p = morecore(nunits)) == NULL)
        return NULL;                     /* none left */
  }
}

Хорошо, теперь мы можем прочитать код.

Шаг 2: отсеять широко признанную плохую практику.

Этот код заполнен вещами, которые в настоящее время считаются плохой практикой. Их необходимо удалить, поскольку они ставят под угрозу безопасность, удобочитаемость и обслуживание кода. Если вам нужна ссылка на авторитет, проповедующий ту же практику, что и я, ознакомьтесь с широко признанным стандартом кодирования MISRA-C .

Я обнаружил и удалил следующие плохие практики:

1) Просто набрать unsigned в коде может привести к путанице: это была опечатка программистом или было намерение написать unsigned int? Мы должны заменить все unsigned на unsigned int. Но когда мы это сделаем, мы обнаружим, что он используется в этом контексте для определения размера различных двоичных данных. Правильным типом, который следует использовать для таких целей, является стандартный тип C size_t. По сути, это также просто unsigned int, но он гарантированно будет «достаточно большим» для конкретной платформы. Оператор sizeof возвращает результат типа size_t, и если мы посмотрим на определение реального malloc в стандарте C, это будет void *malloc(size_t size);. Таким образом, size_t является наиболее правильным типом для использования.

2) Плохая идея использовать то же имя для нашей собственной функции malloc, что и в stdlib.h. Если нам нужно будет включить stdlib.h, все будет грязно. Как правило, никогда не используйте имена идентификаторов функций стандартной библиотеки Си в своем собственном коде. Я поменяю имя на kr_malloc.

3) Код злоупотребляет тем фактом, что все статические переменные гарантированно инициализируются нулями. Это хорошо определено стандартом Си, но довольно тонкое правило. Давайте явным образом инициализируем все статические данные, чтобы показать, что мы не забыли инициировать их случайно.

4) Распределение внутри условий опасно и трудно читаемо. Этого следует избегать, если это возможно, поскольку это также может привести к ошибкам, таким как классическая ошибка = vs ==.

5) Несколько заданий в одном ряду трудно читать, а также, возможно, опасно из-за порядка оценки.

6) Несколько объявлений в одной строке трудно читать и опасно, поскольку это может привести к ошибкам при смешивании данных и объявлений указателей. Всегда объявляйте каждую переменную в отдельной строке.

7) Всегда использует фигурные скобки после каждого утверждения. Невыполнение этого требования приведет к ошибкам. Ошибки.

8) Никогда не вводите приведение из определенного типа указателя в void *. В C это не нужно и может скрывать ошибки, которые иначе компилятор обнаружил бы.

9) Избегайте использования нескольких операторов return внутри функции. Иногда они приводят к более четкому коду, но в большинстве случаев они приводят к спагетти. Поскольку код стоит, мы не можем изменить это без переписывания цикла, поэтому я исправлю это позже.

10) Держите петли простыми. Они должны содержать один оператор init, одно условие цикла и одну итерацию, больше ничего. Цикл for с оператором запятой и всем остальным очень неясен. Опять же, мы замечаем необходимость переписать этот цикл в нечто вменяемое. Я сделаю это дальше, но сейчас у нас есть:

typedef long Align;                      /* for alignment to long boundary */

typedef union header                     /* block header */
{
  struct
  {
    union header *ptr;                   /* next block if on free list */
    size_t size;                         /* size of this block */
  } s;

  Align x;                               /* force alignment of blocks */

} Header;


static Header base = {0};                /* empty list to get started */
static Header* freep = NULL;             /* start of free list */


/* malloc: general-purpose storage allocator */
void* kr_malloc (size_t nbytes)
{
  Header*  p;
  Header*  prevp;
  size_t   nunits;

  nunits = (nbytes + sizeof(Header) - 1) / sizeof(header) + 1;

  prevp = freep;
  if (prevp == NULL)                     /* no free list yet */
  {
    base.s.ptr  = &base;
    freeptr     = &base;
    prevptr     = &base;
    base.s.size = 0;
  }

  for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr)
  {
    if (p->s.size >= nunits)             /* big enough */
    {
      if (p->s.size == nunits)           /* exactly */
      {
        prevp->s.ptr = p->s.ptr;
      }
      else                               /* allocate tail end */
      {
        p->s.size -= nunits;
        p += p->s.size;
        p->s.size = nunits
      }

      freep = prevp;
      return p+1;
    }

    if (p == freep)                      /* wrapped around free list */
    {
      p = morecore(nunits);
      if (p == NULL)
      {
        return NULL;                     /* none left */
      }
    }
  } /* for */
}

Шаг 3: переписать скрытый цикл.

По причинам, упомянутым ранее. Мы можем видеть, что этот цикл продолжается вечно, он завершается возвратом из функции, либо когда выделение завершено, либо когда не осталось памяти. Итак, давайте создадим это как условие цикла и перенесем возврат в конец функции, где она должна быть. И давайте избавимся от этого уродливого оператора запятой.

Я введу две новые переменные: одну переменную результата, чтобы хранить результирующий указатель, и другую, чтобы отслеживать, должен ли цикл продолжаться или нет. Я взорву умы K & amp; R, используя тип bool, который является частью языка C с 1999 года.

(Я надеюсь, что я не изменил алгоритм с этим изменением, я думаю, что не сделал)

#include <stdbool.h>

typedef long Align;                      /* for alignment to long boundary */

typedef union header                     /* block header */
{
  struct
  {
    union header *ptr;                   /* next block if on free list */
    size_t size;                         /* size of this block */
  } s;

  Align x;                               /* force alignment of blocks */

} Header;


static Header base = {0};                /* empty list to get started */
static Header* freep = NULL;             /* start of free list */


/* malloc: general-purpose storage allocator */
void* kr_malloc (size_t nbytes)
{
  Header*  p;
  Header*  prevp;
  size_t   nunits;
  void*    result;
  bool     is_allocating;

  nunits = (nbytes + sizeof(Header) - 1) / sizeof(header) + 1;

  prevp = freep;
  if (prevp == NULL)                     /* no free list yet */
  {
    base.s.ptr  = &base;
    freeptr     = &base;
    prevptr     = &base;
    base.s.size = 0;
  }

  is_allocating = true;
  for (p = prevp->s.ptr; is_allocating; p = p->s.ptr)
  {
    if (p->s.size >= nunits)             /* big enough */
    {
      if (p->s.size == nunits)           /* exactly */
      {
        prevp->s.ptr = p->s.ptr;
      }
      else                               /* allocate tail end */
      {
        p->s.size -= nunits;
        p += p->s.size;
        p->s.size = nunits
      }

      freep = prevp;
      result = p+1;
      is_allocating = false;             /* we are done */
    }

    if (p == freep)                      /* wrapped around free list */
    {
      p = morecore(nunits);
      if (p == NULL)
      {
        result = NULL;                   /* none left */
        is_allocating = false;
      }
    }
    prevp = p;
  } /* for */

  return result;
}

Шаг 4: сделайте это дерьмо скомпилированным.

1145] Так как это из K & amp; R, оно заполнено опечатками. sizeof(header) должно быть sizeof(Header). Есть пропущенные точки с запятой. Они используют разные имена freep, prevp и freeptr, prevptr, но явно обозначают одну и ту же переменную. Я считаю, что последние на самом деле были лучшими именами, поэтому давайте воспользуемся ими.

#include <stdbool.h>

typedef long Align;                      /* for alignment to long boundary */

typedef union header                     /* block header */
{
  struct
  {
    union header *ptr;                   /* next block if on free list */
    size_t size;                         /* size of this block */
  } s;

  Align x;                               /* force alignment of blocks */

} Header;


static Header base = {0};                /* empty list to get started */
static Header* freeptr = NULL;           /* start of free list */


/* malloc: general-purpose storage allocator */
void* kr_malloc (size_t nbytes)
{
  Header*  p;
  Header*  prevptr;
  size_t   nunits;
  void*    result;
  bool     is_allocating;

  nunits = (nbytes + sizeof(Header) - 1) / sizeof(Header) + 1;

  prevptr = freeptr;
  if (prevptr == NULL)                   /* no free list yet */
  {
    base.s.ptr  = &base;
    freeptr     = &base;
    prevptr     = &base;
    base.s.size = 0;
  }

  is_allocating = true;
  for (p = prevptr->s.ptr; is_allocating; p = p->s.ptr)
  {
    if (p->s.size >= nunits)             /* big enough */
    {
      if (p->s.size == nunits)           /* exactly */
      {
        prevptr->s.ptr = p->s.ptr;
      }
      else                               /* allocate tail end */
      {
        p->s.size -= nunits;
        p += p->s.size;
        p->s.size = nunits;
      }

      freeptr = prevptr;
      result = p+1;
      is_allocating = false;             /* we are done */
    }

    if (p == freeptr)                    /* wrapped around free list */
    {
      p = morecore(nunits);
      if (p == NULL)
      {
        result = NULL;                   /* none left */
        is_allocating = false;
      }
    }
    prevptr = p;
  } /* for */

  return result;
}

И теперь у нас есть несколько читаемый, поддерживаемый код, без многочисленных опасных практик, который даже скомпилируется! Так что теперь мы можем начать размышлять о том, что на самом деле делает код.

Структура «Заголовок» - это, как вы уже догадались, объявление узла в связанном списке. Каждый такой узел содержит указатель на следующий. Я не совсем понимаю ни функцию morecore, ни «обход», я никогда не использовал ни эту функцию, ни sbrk. Но я предполагаю, что он выделяет заголовок, как указано в этой структуре, а также некоторый кусок необработанных данных, следующих за этим заголовком. Если это так, это объясняет, почему нет фактического указателя данных: предполагается, что данные следуют за заголовком, смежно в памяти. Таким образом, для каждого узла мы получаем заголовок и получаем фрагмент необработанных данных после заголовка.

Сама итерация довольно проста: они проходят через односвязный список, по одному узлу за раз.

В конце цикла они устанавливают указатель так, чтобы он указывал на один конец конца «чанка», затем сохраняют его в статической переменной, чтобы программа запомнила, где она ранее выделяла память, в следующий раз, когда функция называется.

Они используют уловку, чтобы заголовок заканчивался на выровненном адресе памяти: они хранят всю служебную информацию в объединении вместе с переменной, достаточно большой, чтобы соответствовать требованию выравнивания платформы. Таким образом, если размер «ptr» плюс размер «size» слишком мал для точного выравнивания, объединение гарантирует, что будет выделено как минимум sizeof (Align) байтов. Я полагаю, что весь этот трюк сегодня устарел, поскольку стандарт C требует автоматического заполнения struct / union.

57
ответ дан Lundin 20 July 2015 в 03:54
поделиться

Я также нашел это упражнение большим и интересным.

По моему мнению, визуализация структуры может очень помочь с пониманием логики - или, по крайней мере, это сработало для меня. Ниже приведен мой код, который печатает как можно больше о потоке K & amp; R malloc.

Самым значительным изменением, которое я сделал в K & amp; R malloc, является изменение 'free', чтобы убедиться, что старый указатель больше не будет использоваться. Кроме этого я добавил комментарии и исправил несколько мелких опечаток.

Эксперименты с NALLOC, MAXMEM и тестовыми переменными в 'main' также могут помочь.

На моем компьютере (Ubuntu 16.04.3) это скомпилировано без ошибок с:

gcc -g -std=c99 -Wall -Wextra -pedantic-errors krmalloc.c

krmalloc.c:

#include <stdio.h>
#include <unistd.h>

typedef long Align;             /* for alignment to long boundary */
union header {                  /* block header */
    struct {
        union header *ptr;      /* next block if on free list */
        size_t size;            /* size of this block */
                                /*      including the Header itself */
                                /*      measured in count of Header chunks */
                                /*      not less than NALLOC Header's */
    } s;
    Align x;                    /* force alignment of blocks */
};
typedef union header Header;

static Header *morecore(size_t);
void *mmalloc(size_t);
void _mfree(void **);
void visualize(const char*);
size_t getfreem(void);
size_t totmem = 0;              /* total memory in chunks */

static Header base;             /* empty list to get started */
static Header *freep = NULL;    /* start of free list */

#define NALLOC 1                /* minimum chunks to request */
#define MAXMEM 2048             /* max memory available (in bytes) */

#define mfree(p) _mfree((void **)&p)

void *sbrk(__intptr_t incr);


int main(void)
{
    char *pc, *pcc, *pccc, *ps;
    long *pd, *pdd;
    int dlen = 100;
    int ddlen = 50;

    visualize("start");


    /* trying to fragment as much as possible to get a more interesting view */

    /* claim a char */
    if ((pc = (char *) mmalloc(sizeof(char))) == NULL)
        return -1;

    /* claim a string */
    if ((ps = (char *) mmalloc(dlen * sizeof(char))) == NULL)
        return -1;

    /* claim some long's */
    if ((pd = (long *) mmalloc(ddlen * sizeof(long))) == NULL)
        return -1;

    /* claim some more long's */
    if ((pdd = (long *) mmalloc(ddlen * 2 * sizeof(long))) == NULL)
        return -1;

    /* claim one more char */
    if ((pcc = (char *) mmalloc(sizeof(char))) == NULL)
        return -1;

    /* claim the last char */
    if ((pccc = (char *) mmalloc(sizeof(char))) == NULL)
        return -1;


    /* free and visualize */
    printf("\n");
    mfree(pccc);
    /*      bugged on purpose to test free(NULL) */
    mfree(pccc);
    visualize("free(the last char)");

    mfree(pdd);
    visualize("free(lot of long's)");

    mfree(ps);
    visualize("free(string)");

    mfree(pd);
    visualize("free(less long's)");

    mfree(pc);
    visualize("free(first char)");

    mfree(pcc);
    visualize("free(second char)");


    /* check memory condition */
    size_t freemem = getfreem();
    printf("\n");
    printf("--- Memory claimed  : %ld chunks (%ld bytes)\n",
                totmem, totmem * sizeof(Header));
    printf("    Free memory now : %ld chunks (%ld bytes)\n",
                freemem, freemem * sizeof(Header));
    if (freemem == totmem)
        printf("    No memory leaks detected.\n");
    else
        printf("    (!) Leaking memory: %ld chunks (%ld bytes).\n",
                    (totmem - freemem), (totmem - freemem) * sizeof(Header));

    printf("// Done.\n\n");
    return 0;
}


/* visualize: print the free list (educational purpose) */
void visualize(const char* msg)
{
    Header *tmp;

    printf("--- Free list after \"%s\":\n", msg);

    if (freep == NULL) {                   /* does not exist */
        printf("\tList does not exist\n\n");
        return;
    }

    if  (freep == freep->s.ptr) {          /* self-pointing list = empty */
        printf("\tList is empty\n\n");
        return;
    }

    printf("  ptr: %10p size: %-3lu -->  ", (void *) freep, freep->s.size);

    tmp = freep;                           /* find the start of the list */
    while (tmp->s.ptr > freep) {           /* traverse the list */
        tmp = tmp->s.ptr;
        printf("ptr: %10p size: %-3lu -->  ", (void *) tmp, tmp->s.size);
    }
    printf("end\n\n");
}


/* calculate the total amount of available free memory */
size_t getfreem(void)
{
    if (freep == NULL)
        return 0;

    Header *tmp;
    tmp = freep;
    size_t res = tmp->s.size;

    while (tmp->s.ptr > tmp) {
        tmp = tmp->s.ptr;
        res += tmp->s.size;
    }

    return res;
}


/* mmalloc: general-purpose storage allocator */
void *mmalloc(size_t nbytes)
{
    Header *p, *prevp;
    size_t nunits;

    /* smallest count of Header-sized memory chunks */
    /*  (+1 additional chunk for the Header itself) needed to hold nbytes */
    nunits = (nbytes + sizeof(Header) - 1) / sizeof(Header) + 1;

    /* too much memory requested? */
    if (((nunits + totmem + getfreem())*sizeof(Header)) > MAXMEM) {
        printf("Memory limit overflow!\n");
        return NULL;
    }

    if ((prevp = freep) == NULL) {          /* no free list yet */
        /* set the list to point to itself */
        base.s.ptr = freep = prevp = &base;
        base.s.size = 0;
    }

    /* traverse the circular list */
    for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr) {

        if (p->s.size >= nunits) {          /* big enough */
            if (p->s.size == nunits)        /* exactly */
                prevp->s.ptr = p->s.ptr;
            else {                          /* allocate tail end */
                /* adjust the size */
                p->s.size -= nunits;
                /* find the address to return */
                p += p->s.size;
                p->s.size = nunits;
            }
            freep = prevp;
            return (void *)(p+1);
        }

        /* back where we started and nothing found - we need to allocate */
        if (p == freep)                     /* wrapped around free list */
            if ((p = morecore(nunits)) == NULL)
                return NULL;                /* none left */
    }
}


/* morecore: ask system for more memory */
/*      nu: count of Header-chunks needed */
static Header *morecore(size_t nu)
{
    char *cp;
    Header *up;

    /* get at least NALLOC Header-chunks from the OS */
    if (nu < NALLOC)
        nu = NALLOC;

    cp = (char *) sbrk(nu * sizeof(Header));

    if (cp == (char *) -1)                  /* no space at all */
        return NULL;

    printf("... (sbrk) claimed %ld chunks.\n", nu);
    totmem += nu;                           /* keep track of allocated memory */

    up = (Header *) cp;
    up->s.size = nu;

    /* add the free space to the circular list */
    void *n = (void *)(up+1);
    mfree(n);

    return freep;
}


/* mfree: put block ap in free list */
void _mfree(void **ap)
{
    if (*ap == NULL)
        return;

    Header *bp, *p;
    bp = (Header *)*ap - 1;                 /* point to block header */

    if (bp->s.size == 0 || bp->s.size > totmem) {
        printf("_mfree: impossible value for size\n");
        return;
    }

    /* the free space is only marked as free, but 'ap' still points to it */
    /* to avoid reusing this address and corrupt our structure set it to '\0' */
    *ap = NULL;

    /* look where to insert the free space */

    /* (bp > p && bp < p->s.ptr)    => between two nodes */
    /* (p > p->s.ptr)               => this is the end of the list */
    /* (p == p->p.str)              => list is one element only */
    for (p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
        if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
            /* freed block at start or end of arena */
            break;

    if (bp + bp->s.size == p->s.ptr) {      /* join to upper nbr */
    /* the new block fits perfect up to the upper neighbor */

        /* merging up: adjust the size */
        bp->s.size += p->s.ptr->s.size;
        /* merging up: point to the second next */
        bp->s.ptr = p->s.ptr->s.ptr;

    } else
        /* set the upper pointer */
        bp->s.ptr = p->s.ptr;

    if (p + p->s.size == bp) {              /* join to lower nbr */
    /* the new block fits perfect on top of the lower neighbor */

        /* merging below: adjust the size */
        p->s.size += bp->s.size;
        /* merging below: point to the next */
        p->s.ptr = bp->s.ptr;

    } else
        /* set the lower pointer */
        p->s.ptr = bp;

    /* reset the start of the free list */
    freep = p;
}
0
ответ дан Vladimir 20 July 2015 в 03:54
поделиться

Основы malloc ()

В Linux есть два типичных способа запроса памяти: sbrk и mmap . Эти системные вызовы имеют серьезные ограничения на частые небольшие выделения. malloc () - это библиотечная функция для решения этой проблемы. Он запрашивает большие куски памяти с помощью sbrk / mmap и возвращает маленькие блоки памяти внутри больших кусков. Это гораздо более эффективно и гибко, чем прямой вызов sbrk / mmap.

K & amp; R malloc ()

В реализации K & amp; R ядро ​​ (чаще называемое арена ) представляет собой большой кусок памяти. morecore() запрашивает ядро ​​из системы через sbrk(). Когда вы вызываете malloc () / free () несколько раз, некоторые блоки в ядре используются / выделяются, а другие свободны. K & amp; R malloc хранит адреса свободных блоков в циклическом единственном связанном списке. В этом списке каждый узел является блоком свободной памяти. Первые sizeof(Header) байтов сохраняют размер блока и указатель на следующий свободный блок. Остальные байты в свободном блоке неинициализированы. В отличие от типичных списков в учебниках, узлы в свободном списке являются лишь указателями на некоторые неиспользуемые области в ядрах; Вы фактически не выделяете каждый узел, кроме ядер. Этот список является ключом к пониманию алгоритма.

Следующая диаграмма показывает пример схемы памяти с двумя ядрами / аренами. На диаграмме каждый символ занимает sizeof(Header) байтов. @ представляет собой Header, + отмечает выделенную память и - отмечает свободную память внутри ядер. В примере есть три выделенных блока и три свободных блока. Три свободных блока хранятся в круговом списке. Для трех выделенных блоков только их размеры хранятся в Header.

            This is core 1                             This is core 2

@---------@+++++++++@++++++++++++        @----------@+++++++++++++++++@------------
|                                        |                            |
p->ptr->ptr                              p = p->ptr->ptr->ptr         p->ptr

В вашем коде freep является точкой входа в свободный список. Если вы несколько раз будете следовать freep->ptr, вы вернетесь к freep - оно круговое. Как только вы поймете круговой односвязный список, остальное сравнительно легко. malloc() находит свободный блок и, возможно, разбивает его. free() добавляет свободный блок обратно в список и может объединить его с соседними свободными блоками. Они оба пытаются сохранить структуру списка.

Другие комментарии по реализации

  • В комментариях к коду упоминается «обтекание» в malloc(). Эта строка происходит, когда вы прошли весь свободный список, но не можете найти свободный блок, превышающий запрошенную длину. В этом случае вам нужно добавить новое ядро ​​с morecore().

  • base - это блок нулевого размера, который всегда включен в свободный список. Это хитрость, чтобы избежать специального корпуса. Это не является строго необходимым.

  • free() может показаться немного сложным, поскольку для объединения недавно освобожденного блока с другими свободными блоками в списке необходимо рассмотреть четыре разных случая. Эта деталь не так важна, если вы не хотите переопределить ее сами.

  • Этот пост в блоге объясняет K & amp; R malloc более подробно.

PS: K & amp; R malloc - один из самых элегантных фрагментов кода на мой взгляд. Когда я впервые понял код, это действительно открыло глаза. Мне грустно, что некоторые современные программисты, даже не понимая основ этой реализации, называют шедевр дерьмом исключительно на основании его стиля кодирования.

8
ответ дан user172818 20 July 2015 в 03:54
поделиться

Я изучаю K & amp; R, как я себе представлял, когда О.П. задал этот вопрос, и я пришел сюда, потому что я также нашел, что эти реализации сбивают с толку. Несмотря на то, что принятый ответ очень подробный и полезный, я попытался пойти другим путем, который заключался в том, чтобы понять код в том виде, в котором он был изначально написан - я просмотрел код и добавил комментарии к трудным для меня разделам кода. , Это включает в себя код для других подпрограмм в разделе (которые являются функциями free и memcore - я переименовал их в kandr_malloc и kandr_free, чтобы избежать конфликтов с stdlib). Я подумал, что оставлю это здесь в качестве дополнения к принятому ответу для других студентов, которые могут найти его полезным.

Я признаю, что комментарии в этом кодексе являются чрезмерными. Пожалуйста, знайте, что я делаю это только как учебное упражнение, и я не предполагаю, что это хороший способ на самом деле писать код.

Я позволил себе сменить имена некоторых переменных на более интуитивные; кроме этого код по существу оставлен без изменений. Похоже, что он компилируется и работает нормально для тестовых программ, которые я использовал, хотя у valgrind были претензии к некоторым приложениям.

Также: часть текста в комментариях взята непосредственно из K & amp; R или справочных страниц - я не собираюсь брать на себя ответственность за эти разделы.

#include <unistd.h>  // sbrk

#define NALLOC 1024  // Number of block sizes to allocate on call to sbrk
#ifdef NULL
#undef NULL
#endif
#define NULL 0


// long is chosen as an instance of the most restrictive alignment type
typedef long Align;

/* Construct Header data structure.  To ensure that the storage returned by
 * kandr_malloc is aligned properly for the objects that are stored in it, all
 * blocks are multiples of the header size, and the header itself is aligned
 * properly.  This is achieved through the use of a union; this data type is big
 * enough to hold the "widest" member, and the alignment is appropriate for all
 * of the types in the union.  Thus by including a member of type Align, which
 * is an instance of the most restrictive type, we guarantee that the size of
 * Header is aligned to the worst-case boundary.  The Align field is never used;
 * it just forces each header to the desired alignment.
 */
union header {
  struct {
    union header *next;
    unsigned size;
  } s;

  Align x;
};
typedef union header Header;


static Header base;           // Used to get an initial member for free list
static Header *freep = NULL;  // Free list starting point


static Header *morecore(unsigned nblocks);
void kandr_free(void *ptr);




void *kandr_malloc(unsigned nbytes) {

  Header *currp;
  Header *prevp;
  unsigned nunits;

  /* Calculate the number of memory units needed to provide at least nbytes of
   * memory.
   *
   * Suppose that we need n >= 0 bytes and that the memory unit sizes are b > 0
   * bytes.  Then n / b (using integer division) yields one less than the number
   * of units needed to provide n bytes of memory, except in the case that n is
   * a multiple of b; then it provides exactly the number of units needed.  It
   * can be verified that (n - 1) / b provides one less than the number of units
   * needed to provide n bytes of memory for all values of n > 0.  Thus ((n - 1)
   * / b) + 1 provides exactly the number of units needed for n > 0.
   *
   * The extra sizeof(Header) in the numerator is to include the unit of memory
   * needed for the header itself.
   */
  nunits = ((nbytes + sizeof(Header) - 1) / sizeof(Header)) + 1;

  // case: no free list yet exists; we have to initialize.
  if (freep == NULL) {

    // Create degenerate free list; base points to itself and has size 0
    base.s.next = &base;
    base.s.size = 0;

    // Set free list starting point to base address
    freep = &base;
  }

  /* Initialize pointers to two consecutive blocks in the free list, which we
   * call prevp (the previous block) and currp (the current block)
   */
  prevp = freep;
  currp = prevp->s.next;

  /* Step through the free list looking for a block of memory large enough to
   * fit nunits units of memory into.  If the whole list is traversed without
   * finding such a block, then morecore is called to request more memory from
   * the OS.
   */
  for (; ; prevp = currp, currp = currp->s.next) {

    /* case: found a block of memory in free list large enough to fit nunits
     * units of memory into.  Partition block if necessary, remove it from the
     * free list, and return the address of the block (after moving past the
     * header).
     */
    if (currp->s.size >= nunits) {

      /* case: block is exactly the right size; remove the block from the free
       * list by pointing the previous block to the next block.
       */
      if (currp->s.size == nunits) {
    /* Note that this line wouldn't work as intended if we were down to only
     * 1 block.  However, we would never make it here in that scenario
     * because the block at &base has size 0 and thus the conditional will
     * fail (note that nunits is always >= 1).  It is true that if the block
     * at &base had combined with another block, then previous statement
     * wouldn't apply - but presumably since base is a global variable and
     * future blocks are allocated on the heap, we can be sure that they
     * won't border each other.
     */
    prevp->s.next = currp->s.next;
      }
      /* case: block is larger than the amount of memory asked for; allocate
       * tail end of the block to the user.
       */
      else {
    // Changes the memory stored at currp to reflect the reduced block size
    currp->s.size -= nunits;
    // Find location at which to create the block header for the new block
    currp += currp->s.size;
    // Store the block size in the new header
    currp->s.size = nunits;
      }

      /* Set global starting position to the previous pointer.  Next call to
       * malloc will start either at the remaining part of the partitioned block
       * if a partition occurred, or at the block after the selected block if
       * not.
       */
      freep = prevp;

      /* Return the location of the start of the memory, i.e. after adding one
       * so as to move past the header
       */
      return (void *) (currp + 1);

    } // end found a block of memory in free list case

    /* case: we've wrapped around the free list without finding a block large
     * enough to fit nunits units of memory into.  Call morecore to request that
     * at least nunits units of memory are allocated.
     */
    if (currp == freep) {
      /* morecore returns freep; the reason that we have to assign currp to it
       * again (since we just tested that they are equal), is that there is a
       * call to free inside of morecore that can potentially change the value
       * of freep.  Thus we reassign it so that we can be assured that the newly
       * added block is found before (currp == freep) again.
       */
      if ((currp = morecore(nunits)) == NULL) {
    return NULL;
      }
    } // end wrapped around free list case
  } // end step through free list looking for memory loop
}




static Header *morecore(unsigned nunits) {

  void *freemem;    // The address of the newly created memory
  Header *insertp;  // Header ptr for integer arithmatic and constructing header

  /* Obtaining memory from OS is a comparatively expensive operation, so obtain
   * at least NALLOC blocks of memory and partition as needed
   */
  if (nunits < NALLOC) {
    nunits = NALLOC;
  }

  /* Request that the OS increment the program's data space.  sbrk changes the
   * location of the program break, which defines the end of the process's data
   * segment (i.e., the program break is the first location after the end of the
   * uninitialized data segment).  Increasing the program break has the effect
   * of allocating memory to the process.  On success, brk returns the previous
   * break - so if the break was increased, then this value is a pointer to the
   * start of the newly allocated memory.
   */
  freemem = sbrk(nunits * sizeof(Header));
  // case: unable to allocate more memory; sbrk returns (void *) -1 on error
  if (freemem == (void *) -1) {
    return NULL;
  }

  // Construct new block
  insertp = (Header *) freemem;
  insertp->s.size = nunits;

  /* Insert block into the free list so that it is available for malloc.  Note
   * that we add 1 to the address, effectively moving to the first position
   * after the header data, since of course we want the block header to be
   * transparent for the user's interactions with malloc and free.
   */
  kandr_free((void *) (insertp + 1));

  /* Returns the start of the free list; recall that freep has been set to the
   * block immediately preceeding the newly allocated memory (by free).  Thus by
   * returning this value the calling function can immediately find the new
   * memory by following the pointer to the next block.
   */
  return freep;
}




void kandr_free(void *ptr) {

  Header *insertp, *currp;

  // Find address of block header for the data to be inserted
  insertp = ((Header *) ptr) - 1;

  /* Step through the free list looking for the position in the list to place
   * the insertion block.  In the typical circumstances this would be the block
   * immediately to the left of the insertion block; this is checked for by
   * finding a block that is to the left of the insertion block and such that
   * the following block in the list is to the right of the insertion block.
   * However this check doesn't check for one such case, and misses another.  We
   * still have to check for the cases where either the insertion block is
   * either to the left of every other block owned by malloc (the case that is
   * missed), or to the right of every block owned by malloc (the case not
   * checked for).  These last two cases are what is checked for by the
   * condition inside of the body of the loop.
   */
  for (currp = freep; !((currp < insertp) && (insertp < currp->s.next)); currp = currp->s.next) {

    /* currp >= currp->s.ptr implies that the current block is the rightmost
     * block in the free list.  Then if the insertion block is to the right of
     * that block, then it is the new rightmost block; conversely if it is to
     * the left of the block that currp points to (which is the current leftmost
     * block), then the insertion block is the new leftmost block.  Note that
     * this conditional handles the case where we only have 1 block in the free
     * list (this case is the reason that we need >= in the first test rather
     * than just >).
     */
    if ((currp >= currp->s.next) && ((currp < insertp) || (insertp < currp->s.next))) {
      break;
    }
  }

  /* Having found the correct location in the free list to place the insertion
   * block, now we have to (i) link it to the next block, and (ii) link the
   * previous block to it.  These are the tasks of the next two if/else pairs.
   */

  /* case: the end of the insertion block is adjacent to the beginning of
   * another block of data owned by malloc.  Absorb the block on the right into
   * the block on the left (i.e. the previously existing block is absorbed into
   * the insertion block).
   */
  if ((insertp + insertp->s.size) == currp->s.next) {
    insertp->s.size += currp->s.next->s.size;
    insertp->s.next = currp->s.next->s.next;
  }
  /* case: the insertion block is not left-adjacent to the beginning of another
   * block of data owned by malloc.  Set the insertion block member to point to
   * the next block in the list.
   */
  else {
    insertp->s.next = currp->s.next;
  }

  /* case: the end of another block of data owned by malloc is adjacent to the
   * beginning of the insertion block.  Absorb the block on the right into the
   * block on the left (i.e. the insertion block is absorbed into the preceeding
   * block).
   */
  if ((currp + currp->s.size) == insertp) {
    currp->s.size += insertp->s.size;
    currp->s.next = insertp->s.next;
  }
  /* case: the insertion block is not right-adjacent to the end of another block
   * of data owned by malloc.  Set the previous block in the list to point to
   * the insertion block.
   */
  else {
    currp->s.next = insertp;
  }

  /* Set the free pointer list to start the block previous to the insertion
   * block.  This makes sense because calls to malloc start their search for
   * memory at the next block after freep, and the insertion block has as good a
   * chance as any of containing a reasonable amount of memory since we've just
   * added some to it.  It also coincides with calls to morecore from
   * kandr_malloc because the next search in the iteration looks at exactly the
   * right memory block.
   */
  freep = currp;
}
27
ответ дан dpritch 20 July 2015 в 03:54
поделиться
Другие вопросы по тегам:

Похожие вопросы: