Skip to content

Commit

Permalink
[zdx_arena] Add arena_create_from_buf() function
Browse files Browse the repository at this point in the history
  • Loading branch information
zeusdeux committed Jul 5, 2024
1 parent 2edf46f commit ad4521b
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion zdx_simple_arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ typedef struct Arena {
} arena_t;

arena_t arena_create(const size_t sz);
arena_t arena_create_from_buf(void *const buf, const size_t sz);
bool arena_free(arena_t *const ar);
bool arena_reset(arena_t *const ar);
void *arena_alloc(arena_t *const ar, const size_t sz);
Expand Down Expand Up @@ -86,7 +87,8 @@ typedef enum {
ARENA_ENOMEM = 0,
ARENA_EINVAL,
ARENA_EACQFAIL,
ARENA_ERELFAIL
ARENA_ERELFAIL,
ARENA_ERR_CODE_COUNT,
} arena_err_code_t;


Expand Down Expand Up @@ -192,6 +194,47 @@ arena_t arena_create(size_t sz)
return ar;
}

/*
* DESCRIPTION
*
* This function creates an arena with the provided buffer as the backing memory of size sz.
*
* RETURN VALUES
*
* Success: It returns an arena that will allocate into the buffer that was provided until size sz.
* Error: If buffer is NULL or if size is 0, arena.err will be populated with ARENA_EINVAL message
*
* NOTES
*
* If DEBUG is defined, then the whole arena is also memset to SA_DEBUG_BYTE which is
* 0xcd by default to help with debugging.
*/
arena_t arena_create_from_buf(void *const buf, const size_t sz)
{
dbg(">> provided buffer %p (size = %zu)", buf, sz);

arena_t ar = {0};

if (!buf || !sz) {
ar.err = arena_get_err_msg_(ARENA_EINVAL);
ar.arena = NULL;

ar_dbg("<<", &ar);
return ar;
}

ar.arena = buf;
ar.size = sz;

#if defined(DEBUG)
memset(ar.arena, SA_DEBUG_BYTE, sz); // in debug mode memset whole arena to 0xcd to show up in debug tooling. It's also the value used by msvc I think
#endif


ar_dbg("<<", &ar);
return ar;
}

/*
* DESCRIPTION
*
Expand Down

0 comments on commit ad4521b

Please sign in to comment.