mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
The following structures are now more independant: - Toy_Array - Toy_Stack - Toy_Bucket - Toy_String I reworked a lot of the memory allocation, so now there are more direct calls to malloc() or realloc(), rather than relying on the macros from toy_memory.h. I've also split toy_memory into proper array and bucket files, because it makes more sense this way, rather than having them both jammed into one file. This means the eventual hashtable structure can also stand on its own. Toy_Array is a new wrapper around raw array pointers, and all of the structures have their metadata embedded into their allocated memory now, using variable length array members. A lot of 'capacity' and 'count' variables were changed to 'size_t' types, but this doesn't seem to be a problem anywhere. If the workflow fails, then I'll leave it for tonight - I'm too tired, and I don't want to overdo myself.
69 lines
1.8 KiB
C
69 lines
1.8 KiB
C
#include "toy_bucket.h"
|
|
#include "toy_console_colors.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
//buckets of fun
|
|
Toy_Bucket* Toy_allocateBucket(size_t capacity) {
|
|
if (capacity == 0) {
|
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Cannot allocate a 'Toy_Bucket' with zero capacity\n" TOY_CC_RESET);
|
|
exit(1);
|
|
}
|
|
|
|
Toy_Bucket* bucket = malloc(sizeof(Toy_Bucket) + capacity);
|
|
|
|
if (bucket == NULL) {
|
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate a 'Toy_Bucket' of %d capacity\n" TOY_CC_RESET, (int)capacity);
|
|
exit(1);
|
|
}
|
|
|
|
//initialize the bucket
|
|
bucket->next = NULL;
|
|
bucket->capacity = capacity;
|
|
bucket->count = 0;
|
|
|
|
return bucket;
|
|
}
|
|
|
|
void* Toy_partitionBucket(Toy_Bucket** bucketHandle, size_t amount) {
|
|
if ((*bucketHandle) == NULL) {
|
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Expected a 'Toy_Bucket', received NULL\n" TOY_CC_RESET);
|
|
exit(1);
|
|
}
|
|
|
|
//if you try to allocate too much space
|
|
if ((*bucketHandle)->capacity < amount) {
|
|
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to partition a 'Toy_Bucket': requested %d from a bucket of %d capacity\n" TOY_CC_RESET, (int)amount, (int)((*bucketHandle)->capacity));
|
|
exit(1);
|
|
}
|
|
|
|
//if you're out of space in this bucket
|
|
if ((*bucketHandle)->capacity < (*bucketHandle)->count + amount) {
|
|
//move to the next bucket
|
|
Toy_Bucket* tmp = Toy_allocateBucket((*bucketHandle)->capacity);
|
|
tmp->next = (*bucketHandle); //it's buckets all the way down
|
|
(*bucketHandle) = tmp;
|
|
}
|
|
|
|
//track the new count, and return the specified memory space
|
|
(*bucketHandle)->count += amount;
|
|
return ((*bucketHandle)->data + (*bucketHandle)->count - amount);
|
|
}
|
|
|
|
void Toy_freeBucket(Toy_Bucket** bucketHandle) {
|
|
Toy_Bucket* iter = (*bucketHandle);
|
|
|
|
while (iter != NULL) {
|
|
//run down the chain
|
|
Toy_Bucket* last = iter;
|
|
iter = iter->next;
|
|
|
|
//clear the previous bucket from memory
|
|
free(last);
|
|
}
|
|
|
|
//for safety
|
|
(*bucketHandle) = NULL;
|
|
}
|