Files
Toy/source/toy_bucket.c
Kayne Ruse 4bcf8e84a9 String literals are being parsed, compiled and printed, read more
Strings, due to their potentially large size, are stored outside of a
routine's code section, in the data section. To access the correct
string, you must read the jump index, then the real address from the
jump table - and extra layer of indirection will result in more flexible
data down the road, I hope.

Other changes include:

* Added string concat operator ..
* Added TOY_STRING_MAX_LENGTH
* Strings can't be created or concatenated longer than the max length
* The parser will display a warning if the bucket is too small for a
  string at max length, but it will continue
* Added TOY_BUCKET_IDEAL to correspend with max string length
* The bucket now allocates an address that is 4-byte aligned
* Fixed missing entries in the parser rule table
* Corrected some failing TOY_BITNESS tests
2024-10-08 00:33:36 +11:00

74 lines
2.0 KiB
C

#include "toy_bucket.h"
#include "toy_console_colors.h"
#include <stdio.h>
#include <stdlib.h>
//buckets of fun
Toy_Bucket* Toy_allocateBucket(unsigned int 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, unsigned int amount) {
if ((*bucketHandle) == NULL) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Expected a 'Toy_Bucket', received NULL\n" TOY_CC_RESET);
exit(1);
}
//BUGFIX: the endpoint must be aligned to the word size, otherwise you'll get a bus error from moving pointers
if (amount % 4 != 0) {
amount += 4 - (amount % 4); //ceil
}
//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;
}