Skip to main content
Fixed typos and split drawings from code
Source Link
Toby Speight
  • 88.7k
  • 14
  • 104
  • 327

But note: There is extra memoeymemory being used.

 // LetsLet's examine this specific call:
 Memory_Pool  memory;
 if (mp_init(&memory, sizeof(void*) * 4, 5) != MEMORY_POOL_SUCCESS)
 {
     exit(1);
 }


    if((mp->memory = malloc(size * slots)) == NULL)
        return MEMORY_POOL_ERROR;

      mp->head----->Random
      mp->memory--->        ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********

    mp->head = NULL;

      mp->head-----|
      mp->memory--->        ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********


    char *end = (char *)mp->memory + size * slots;

      mp->head-----|
      mp->memory----------->***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->

    for(char *ite = mp->memory; ite < end; ite += size)
      mp->head----->Random
      mp->memory--->        ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
    mp->head = NULL;
      mp->head-----|
      mp->memory--->        ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
    char *end = (char *)mp->memory + size * slots;
      mp->head-----|
      mp->memory----------->***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
    for(char *ite = mp->memory; ite < end; ite += size)
        mp_release(mp, ite);

 // Iteration 1:
      mp->head----------------|
                              \/
      mp->memory----------->**( null )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->

   // Iteration 2:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 3:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *         *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 4:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 5:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
 char *end----------------->


    return MEMORY_POOL_SUCCESS;
 // Iteration 1:
      mp->head----------------|
                              \/
      mp->memory----------->**( null )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 2:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 3:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *         *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 4:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 5:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
 char *end----------------->
    return MEMORY_POOL_SUCCESS;
}

But note: There is extra memoey being used.

 // Lets examine this specific call:
 Memory_Pool  memory;
 if (mp_init(&memory, sizeof(void*) * 4, 5) != MEMORY_POOL_SUCCESS)
 {
     exit(1);
 }


    if((mp->memory = malloc(size * slots)) == NULL)
        return MEMORY_POOL_ERROR;

      mp->head----->Random
      mp->memory--->        ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********

    mp->head = NULL;

      mp->head-----|
      mp->memory--->        ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********


    char *end = (char *)mp->memory + size * slots;

      mp->head-----|
      mp->memory----------->***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->

    for(char *ite = mp->memory; ite < end; ite += size)
        mp_release(mp, ite);

 // Iteration 1:
      mp->head----------------|
                              \/
      mp->memory----------->**( null )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->

   // Iteration 2:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 3:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *         *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 4:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 5:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
 char *end----------------->


    return MEMORY_POOL_SUCCESS;
}

But note: There is extra memory being used.

 // Let's examine this specific call:
 Memory_Pool  memory;
 if (mp_init(&memory, sizeof(void*) * 4, 5) != MEMORY_POOL_SUCCESS)
 {
     exit(1);
 }


    if((mp->memory = malloc(size * slots)) == NULL)
        return MEMORY_POOL_ERROR;
      mp->head----->Random
      mp->memory--->        ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
    mp->head = NULL;
      mp->head-----|
      mp->memory--->        ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
    char *end = (char *)mp->memory + size * slots;
      mp->head-----|
      mp->memory----------->***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
    for(char *ite = mp->memory; ite < end; ite += size)
        mp_release(mp, ite);
 // Iteration 1:
      mp->head----------------|
                              \/
      mp->memory----------->**( null )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 2:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 3:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *         *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 4:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 5:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
 char *end----------------->
    return MEMORY_POOL_SUCCESS;
}
added 8746 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

How it is implemented:

A call to mp_init() allocated a chunk of memory. This chunk of memory is slots count number of items each of size size.

 // Lets examine this specific call:
 Memory_Pool  memory;
 if (mp_init(&memory, sizeof(void*) * 4, 5) != MEMORY_POOL_SUCCESS)
 {
     exit(1);
 }


    if((mp->memory = malloc(size * slots)) == NULL)
        return MEMORY_POOL_ERROR;

      mp->head----->Random
      mp->memory--->        ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********

    mp->head = NULL;

      mp->head-----|
      mp->memory--->        ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********


    char *end = (char *)mp->memory + size * slots;

      mp->head-----|
      mp->memory----------->***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->

    for(char *ite = mp->memory; ite < end; ite += size)
        mp_release(mp, ite);

 // Iteration 1:
      mp->head----------------|
                              \/
      mp->memory----------->**( null )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->

   // Iteration 2:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 3:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *         *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 4:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 5:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
 char *end----------------->


    return MEMORY_POOL_SUCCESS;
}

How it is implemented:

A call to mp_init() allocated a chunk of memory. This chunk of memory is slots count number of items each of size size.

 // Lets examine this specific call:
 Memory_Pool  memory;
 if (mp_init(&memory, sizeof(void*) * 4, 5) != MEMORY_POOL_SUCCESS)
 {
     exit(1);
 }


    if((mp->memory = malloc(size * slots)) == NULL)
        return MEMORY_POOL_ERROR;

      mp->head----->Random
      mp->memory--->        ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********

    mp->head = NULL;

      mp->head-----|
      mp->memory--->        ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********


    char *end = (char *)mp->memory + size * slots;

      mp->head-----|
      mp->memory----------->***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->

    for(char *ite = mp->memory; ite < end; ite += size)
        mp_release(mp, ite);

 // Iteration 1:
      mp->head----------------|
                              \/
      mp->memory----------->**( null )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->

   // Iteration 2:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 3:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *         *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 4:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
                            *         *
                            *         *
                            *         *
                            *         *
                            ***********
 char *end----------------->
   // Iteration 5:
      mp->memory----------->**( null )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
                            *     |   *
                            *     |   *
                            **(   *  )*
                            *     /\  *
                            *     |   *
      mp->head--------------*-|   |   *
                            * \/  |   *
                            **(   *  )*
 char *end----------------->


    return MEMORY_POOL_SUCCESS;
}
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

The only addition I would make is:

int mp_init(Memory_Pool *mp, size_t size, size_t slots)
{
    if (size < MEMORY_POOL_MINIMUM_SIZE)
    {   return MEMORY_POOL_ERROR;
    }

But note: There is extra memoey being used.

typedef struct {
    void **head;
    void *memory;
} Memory_Pool;

You need space to store the above structure.