A list of helpful libraries and code

You may want to check for overflow when implementing calloc. It's one of the reason it's best to use calloc versus malloc with a total size to avoid buffer overruns (although on PlayDate it probably will not be an issue given the lack of user input on these calls).

Something like this:

void* pd_calloc(size_t nb_of_items, size_t item_size)
{
    if (item_size && (nb_of_items > (SIZE_MAX / item_size))) {
        return NULL;
    }

    size_t size = nb_of_items * item_size;
    void* memory = pd_malloc(size);
    if(memory != NULL) {
        memset(memory, 0, size);
    }
    
    return memory;
}
2 Likes