X-Git-Url: https://harrygodden.com/git/?p=vg.git;a=blobdiff_plain;f=vg_mem_queue.h;fp=vg_mem_queue.h;h=f898ada2b7fb0da1139ac4cbe69d65b79c80cee7;hp=1b556ae9affdfe5c5a1d7919a7579bfbc07894c7;hb=3b14f3dcd5bf9dd3c85144f2123d667bfa4bb63f;hpb=fce86711735b15bff37de0f70716808410fcf269 diff --git a/vg_mem_queue.h b/vg_mem_queue.h index 1b556ae..f898ada 100644 --- a/vg_mem_queue.h +++ b/vg_mem_queue.h @@ -1,91 +1,21 @@ -#ifndef VG_MEM_QUEUE_H -#define VG_MEM_QUEUE_H - -#include "vg_mem.h" -#include "vg_stdint.h" +#pragma once typedef struct vg_queue vg_queue; typedef struct vg_queue_frame vg_queue_frame; -struct vg_queue_frame { +struct vg_queue_frame +{ u32 alloc_size,size; u8 data[]; }; -struct vg_queue { +struct vg_queue +{ u8 *buffer; u32 size; vg_queue_frame *head, *tail; }; -/* - * Allocate memory on the queue. Returns NULL if allocation failed for any - * any reason. - */ -static vg_queue_frame *vg_queue_alloc( vg_queue *q, u32 size ){ - u32 total = vg_align8(size) + sizeof(vg_queue_frame); - vg_queue_frame *frame = NULL; - - if( total > q->size ) - return NULL; - - if( q->head ){ - u32 end = ((u8 *)q->head - q->buffer) + q->head->alloc_size, - start = ((u8 *)q->tail - q->buffer), - r0 = 0, - r1 = 0; - - if( start < end ){ - r0 = q->size-end; - r1 = start; - } - else - r0 = start - end; - - if( total < r0 ){ - frame = (vg_queue_frame *)(q->buffer + end); - } - else { - if( total < r1 ){ - q->head->alloc_size += r0; - frame = (vg_queue_frame *)q->buffer; - } - } - - if( !frame ) return NULL; - } - else{ - frame = (vg_queue_frame *)q->buffer; - q->tail = frame; - } - - memset( frame, 0, sizeof(vg_queue_frame) ); - - q->head = frame; - frame->alloc_size = total; - frame->size = size; - - return frame; -} - -/* - * Free last item from queue - */ -static void vg_queue_pop( vg_queue *q ){ - if( q->head == q->tail ){ - q->head = NULL; - q->tail = NULL; - return; - } - - u32 start = ((u8 *)q->tail - q->buffer); - start += q->tail->alloc_size; - - if( start == q->size ) - start = 0; - - q->tail = (vg_queue_frame *)(q->buffer + start); -} - -#endif /* VG_MEM_QUEUE_H */ +vg_queue_frame *vg_queue_alloc( vg_queue *q, u32 size ); +void vg_queue_pop( vg_queue *q );