How to Implement a Salami Memory Allocator in C?

In summary, the my_malloc() function should allocate memory from a pool, keep track of how much memory is in use, and free memory when it is no longer needed.
  • #1
carl123
56
0
Salami Allocator

In this warm-up submission you will write a totally trivial allocator:

The function init allocator() reserves a given portion of memory for the allocator. Every call to my_malloc() cuts off the requested amount of memory from the remainder of the reserved memory. The call to my_free() does not free any memory. When all the memory has been used, and no more memory is left, subsequent calls to my_malloc() return NULL. For the implementation all you need is a pointer to the beginning of the remaining memory. Whenever memory is allocated, advance the pointer, until you reach the end.
C:
int memAvail;  // amount of memory to be made available to allocator

unsigned int init_allocator(unsigned int _basic_block_size, unsigned int _length){

/* This function initializes the memory allocator and makes a portion of
   ’_length’ bytes available. The allocator uses a ’_basic_block_size’ as
   its minimal unit of allocation. The function returns the amount of
   memory made available to the allocator. If an error occurred,
   it returns 0.
*/

if (_basic_block_size < _length){
    memAvail = _length/_basic_block_size;
    return memAvail;
}
else{

    return 0;
  }

}

void *my_malloc(size_t _length) {
 
 /* This preliminary implementation simply hands the call over the
     the C standard library!
     Of course this needs to be replaced by your implementation.
  */
 
 int *ptr;
  ptr = (int *) malloc(_length * sizeof(_length));
      return ptr;
  if (_length >= memAvail){
   return NULL;
 }
     return malloc(_length);
}
This my interpretation of the problem so far. It seems right to me but I don't know if I'm doing something wrong. Any help will be appreciated.
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
carl123 said:
Salami Allocator

In this warm-up submission you will write a totally trivial allocator:

The function init allocator() reserves a given portion of memory for the allocator. Every call to my_malloc() cuts off the requested amount of memory from the remainder of the reserved memory. The call to my_free() does not free any memory. When all the memory has been used, and no more memory is left, subsequent calls to my_malloc() return NULL. For the implementation all you need is a pointer to the beginning of the remaining memory. Whenever memory is allocated, advance the pointer, until you reach the end.
C:
int memAvail;  // amount of memory to be made available to allocator

unsigned int init_allocator(unsigned int _basic_block_size, unsigned int _length){

/* This function initializes the memory allocator and makes a portion of
   ’_length’ bytes available. The allocator uses a ’_basic_block_size’ as
   its minimal unit of allocation. The function returns the amount of
   memory made available to the allocator. If an error occurred,
   it returns 0.
*/

if (_basic_block_size < _length){
    memAvail = _length/_basic_block_size;
    return memAvail;
}
else{

    return 0;
  }

}

void *my_malloc(size_t _length) {
 
 /* This preliminary implementation simply hands the call over the
     the C standard library!
     Of course this needs to be replaced by your implementation.
  */
 
 int *ptr;
  ptr = (int *) malloc(_length * sizeof(_length));
      return ptr;
  if (_length >= memAvail){
   return NULL;
 }
     return malloc(_length);
}
This my interpretation of the problem so far. It seems right to me but I don't know if I'm doing something wrong. Any help will be appreciated.
I don't think your implementation is what the problem is calling for. My reason for thinking this is that your my_malloc() function is not much more than a wrapper around the standard library function malloc().

As I interpret this problem, you need to allocate a reasonably large chunk of memory, possibly from stack memory using an array. Your my_malloc() function should process requests for storage out of this memory pool, keeping track of the section of memory that is being used. You should also return the memory when it is no longer needed, by implementing a my_free() function that makes the formerly used memory available again.
 
  • #3
Mark44 said:
As I interpret this problem, you need to allocate a reasonably large chunk of memory, possibly from stack memory using an array. Your my_malloc() function should process requests for storage out of this memory pool, keeping track of the section of memory that is being used..
I would use the normal malloc once in the initialization to get this memory.
You also have to store the parameters of the initialization function to use them in the my_malloc() function.
You should also return the memory when it is no longer needed, by implementing a my_free() function that makes the formerly used memory available again.
The exercise says "The call to my_free() does not free any memory". Keeping track of which blocks are freed would make this much harder.
 
  • #4
When you by process cured meat that comes in a large chub, you often want to make sandwiches with it at home. So the store has a slicing machine with a large round blade. They whack off as many slices from the chub and as thick or thin as you want. This is why the algorithm is called a salami allocator.

So. A salami allocator does this:
Code:
1. get a large chub of memory.  Use malloc().  Check the return code from malloc().  Important!
     a. Remember the start of the chub as a pointer - call it keepme.  This means storage for the 
          pointer has to stay "live", maybe in static or global memory.
     b. Remember the end of the chub - call it  the_end. (keepme + size) 
          This means storage for the pointer has to stay "live", maybe in static memory.
2. create a "blade" - a pointer in memory that keeps moving, starting from the beginning of the chub.  Call it blade, keep in non-volatile (static) memory
3. Every request for another slice of memory:
    a. check to see if your slice is small enough to be taken from the chub -
        i.e., check to see if adding the size of the slice to blade goes past the_end.
    b. if not,  copy blade to a temp pointer, add the size to blade, return temp - this is a single slice.
    c. if the request is too big or there is no more chub left, return null.
4. When everything is done (you decide what "done" means here) , call free() using keepme.  
      keepme should never change until you do this.
So there are lessons in here:
Code:
1. watch out where you ask the program to store variables. 
       Memory in a function is created on the stack, then destroyed when the function ends.

2. if keepme was declared as an automatic variable in a function, the whole program 
    would bomb or freeze - this is called undefined behavior. Undefined behavior means the 
    program can do anything - stack dump, reformat a disk, call Marios and order a pizza.  
    Anything.  
    Most modern compilers favor option one, which is to cause the program to abort somehow, 
     hopefully harmlessly.  Some aborts can cause a kernel panic (crash) on 
    older multiprocessing systems.  So then you have 50 other users mad at you.  
    Got the point?  If your compiler does option #3 feed them the pizza. 
 
3. When you get more experience you will find that keeping your never-to-lose variables in 
    a struct makes housekeeping simpler.
 
  • #5
Thanks for the input everyone. All your suggestions have given me a clear idea of what I'm supposed to do. Appreciate it
 

1. What is a "Salami memory allocator" in C?

A Salami memory allocator is a type of dynamic memory allocation algorithm used in the C programming language. It is named after the way it allocates memory, by slicing off small pieces of memory from a larger "salami" block.

2. How does a Salami memory allocator work?

A Salami memory allocator works by maintaining a list of allocated and free memory blocks. When a request for memory allocation is made, it searches for a free block that is large enough to hold the requested size. If it finds a block that is larger than needed, it splits it into two smaller blocks. When a block is freed, it is merged with any adjacent free blocks to create a larger block.

3. What are the advantages of using a Salami memory allocator?

One advantage of using a Salami memory allocator is that it can efficiently allocate and deallocate small blocks of memory, which can be useful for programs that require frequent memory allocation and deallocation. It also helps prevent memory fragmentation, as it merges adjacent free blocks to create larger blocks.

4. Are there any drawbacks to using a Salami memory allocator?

One potential drawback of using a Salami memory allocator is that it may not be as efficient when allocating larger blocks of memory. This is because it may have to search for a large enough free block or split a larger block, which can be time-consuming. Additionally, the algorithm may not be as well-suited for certain types of data structures, such as binary trees.

5. How does a Salami memory allocator compare to other memory allocation algorithms?

Compared to other memory allocation algorithms, a Salami memory allocator may be more efficient at allocating and deallocating small blocks of memory, but may not perform as well with larger blocks. It also helps prevent memory fragmentation, which can be an issue with other algorithms. However, the best memory allocation algorithm to use ultimately depends on the specific needs and requirements of your program.

Similar threads

  • Programming and Computer Science
Replies
16
Views
5K
  • Programming and Computer Science
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Programming and Computer Science
Replies
8
Views
4K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
26
Views
3K
  • Programming and Computer Science
Replies
15
Views
6K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
16
Views
4K
Back
Top