vc3/link/ref.c
2025-04-17 13:27:20 +02:00

44 lines
961 B
C

#include "ref.h"
#include "sym.h"
#include <stdio.h>
#include <stdlib.h>
void ref_queue_construct(RefQueue* queue)
{
const size_t start_capacity = 128;
*queue = (RefQueue) {
.data = malloc(sizeof(Ref) * start_capacity),
.capacity = start_capacity,
.back = 0,
.front = 0,
};
}
void ref_queue_destroy(RefQueue* queue)
{
free(queue->data);
}
void ref_queue_push(RefQueue* queue, const Sym* sym, uint16_t offset)
{
if (queue->front + 1 > queue->capacity) {
queue->capacity *= 2;
queue->data = realloc(queue->data, sizeof(Ref) * queue->capacity);
}
queue->data[queue->front++] = (Ref) { sym, offset };
}
Ref ref_queue_pop(RefQueue* queue)
{
if (queue->back == queue->front) {
fprintf(stderr, "panic: queue underflow\n");
exit(1);
}
return queue->data[queue->back++];
}
size_t ref_queue_size(const RefQueue* queue)
{
return queue->front - queue->back;
}