Some more pointer magic with Memory Arenas

I like implementing low-level things in C. It sort of helps me visualize how things work at a low memory level.

Consider the following example:

int *p = malloc(3); 

This pointer now holds the first address of a contiguous memory block of three bytes.

123.png

Now this memory is allocated on the heap. It has to be freed eventually, else we'll end up with a memory leak.

The Problem and the Solution

Keeping track of multiple pointers that need to be freed is hard.

Memory arenas provide a simple solution to this problem.

This approach makes it possible for us to avoid having to call free multiple times. We only call it once when we are done with the program and no longer need to create pointers.

The Implementation

We need 5 functions.

Function Return Type Parameters Description
ArenaInitialize (Arena *) (size_t size) To initialize/allocate the memory arena
ArenaAllocate (char *) (Arena *arena, size_t size, datatype value) To allocate pointers on the memory arena
ArenaPrinter void (Arena *arena, size_t size) To print the contents of the memory arena
ArenaReset void (Arena *arena) To clear the memory arena (clear the pointers that we have previously allocated)
ArenaFree void (Arena *arena) To deallocate the memory arena

Now let's step through some code!

typedef struct {
  size_t size;
  size_t currentOffset;
  char *buffer;
} Arena;

First, we have a struct that is used to initialize the Arena.

Arena *ArenaInit(size_t size) {

  // if size == 0 then return NULL
  if (!size) {
    return NULL;
  }

  // allocate the arena pointer
  Arena *arenaPtr = malloc(sizeof(Arena));

  // allocate the buffer to 1 byte (char) * number of elements (size)
  arenaPtr->buffer = calloc(size, sizeof(char));
  arenaPtr->size = size;
  arenaPtr->currentOffset = 0;

  return arenaPtr;
}
char *ArenaAlloc(Arena *arena, size_t size, int value) {

  // if the arena offset is greater than or equal to the arena size then return
  // NULL
  if ((arena->currentOffset + size) > arena->size) {
    return NULL;
  }

  // get the current offset
  char *currentOffset = arena->buffer + arena->currentOffset;

  // set the value and increment the offset
  memcpy(currentOffset, &value, size);
  arena->currentOffset += size;

  // return the current offset
  return currentOffset;
}

After we are done initializing and allocating memory to the arena, we can now allocate space on it.

void ArenaPrinter(Arena *arena, size_t size) {

  size_t currentOffset = arena->currentOffset;
  size_t counter = 0;

  if (arena->currentOffset == 0) {
    fprintf(stderr, "Error: Arena is empty");
    exit(1);
  }

  while (counter != currentOffset) {
    printf("%d\n", *(arena->buffer + counter));
    counter += size;
  }
}
void ArenaReset(Arena *arena) {
  // clear the buffer
  memset(arena->buffer, 0, arena->size);

  // reset the current
  arena->currentOffset = 0;
}
void ArenaFree(Arena *arena) {
  // first free the buffer in arena
  free(arena->buffer);

  // then free the arena itself
  free(arena);
}

Conclusion

And that's it. A clean, simple and memory-safe solution for allocating and deallocating pointers in pure C :)