1

For the project i need to make my own malloc/free program. But i really don't know how to start making the functions. The teacher included some what every function should do but i still don't know how to begin. We only had 1 lesson for C so i'm also not that familiar with the language. These are the 2 first function of the program.

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

#define HEAP_SIZE   1024
#define BLOCK_SIZE  64

#define NUMBER_OF_BLOCKS  ((HEAP_SIZE) / (BLOCK_SIZE))

struct block
{
  uint8_t *address;
  uint32_t alloc_count;
  struct block *prev;
  struct block *next;
};

struct list
{
  struct block *first;
  struct block *last;
};

/* Initializes the given list to be the empty list.
 *
 * Preconditions:
 *  - the given list is a valid pointer to an object of type struct list
 *
 * This function is already implemented for you.
 */
static void list_init(struct list *list)
{ 
  list->first = NULL;
  list->last = NULL;
}

/* Returns true when the given block is valid.
 *
 * This function is already implemented for you.
 */
static bool block_is_valid(const struct block *block)
{
}

if anyone could provide me some more tips to get me on the right path or even a answer and explain me a bit so i can do the rest f the program on my own. It would be really helpfull.

EDIT: i may have asked a too vague of a question. So here is to point what i do know: - The uint8/32_t is a type for int that are 8/32 bits in size. - i know what pointers, Ive done the exercises on www.learn-c.org/ since that's the thing that we did in class and also the only thing. so i have a basic understanding of what i learned the that site.

What I'm confused about: - I need to make a empty list. but i'm confused since i can't use malloc unless the teacher means i have to make a normal list. But i'm pretty sure there is no built in list() command in C.

mark12
  • 23
  • 6
  • thx for the advice. – mark12 Dec 14 '19 at 19:05
  • 1
    Notice that on Linux, you can find the code of `malloc` and of `free`, e.g. inside [musl-libc](http://musl-libc.org). It uses system calls like [mmap(2)](http://man7.org/linux/man-pages/man2/mmap.2.html) – Basile Starynkevitch Dec 14 '19 at 20:05
  • @mark12 If your teacher told you not to use `malloc`, they likely would have told you some other way to allocate memory. Maybe they want you to use `mmap`? – David Schwartz Dec 15 '19 at 00:29
  • I'm not allowed to add headers to the program so i can't use mmap. – mark12 Dec 15 '19 at 01:38
  • You can simply define a `uint8_t` array of appropriate size (your teacher gave you `HEAP_SIZE`) and manage it through your implementations of `malloc()` and `free()`. – the busybee Dec 15 '19 at 15:02

1 Answers1

0

If you have a copy of C Programming Language (2nd Edition), chapter 5 has a simple malloc() and free() implementation.

You should have a pointer to the beginning of the free memory block, and you need to advance the pointer with each memory allocation request. Since you are given the total heap and the block sizes, you can do something like

static struct block malloc_region[NUMBER_OF_BLOCKS];

Then treat this statically allocated memory as your heap and advance the pointer as new memory allocation request is done.

fnisi
  • 1,181
  • 1
  • 14
  • 24