bad char
[vg.git] / vg_mem_queue.c
1 #include "vg_platform.h"
2 #include "vg_mem.h"
3 #include "vg_mem_queue.h"
4 #include <stddef.h>
5 #include <string.h>
6
7 /*
8 * Allocate memory on the queue. Returns NULL if allocation failed for any
9 * any reason.
10 */
11 vg_queue_frame *vg_queue_alloc( vg_queue *q, u32 size )
12 {
13 u32 total = vg_align8(size) + sizeof(vg_queue_frame);
14 vg_queue_frame *frame = NULL;
15
16 if( total > q->size )
17 return NULL;
18
19 if( q->head )
20 {
21 u32 end = ((u8 *)q->head - q->buffer) + q->head->alloc_size,
22 start = ((u8 *)q->tail - q->buffer),
23 r0 = 0,
24 r1 = 0;
25
26 if( start < end ){
27 r0 = q->size-end;
28 r1 = start;
29 }
30 else
31 r0 = start - end;
32
33 if( total < r0 ){
34 frame = (vg_queue_frame *)(q->buffer + end);
35 }
36 else {
37 if( total < r1 ){
38 q->head->alloc_size += r0;
39 frame = (vg_queue_frame *)q->buffer;
40 }
41 }
42
43 if( !frame ) return NULL;
44 }
45 else{
46 frame = (vg_queue_frame *)q->buffer;
47 q->tail = frame;
48 }
49
50 memset( frame, 0, sizeof(vg_queue_frame) );
51
52 q->head = frame;
53 frame->alloc_size = total;
54 frame->size = size;
55
56 return frame;
57 }
58
59 /*
60 * Free last item from queue
61 */
62 void vg_queue_pop( vg_queue *q )
63 {
64 if( q->head == q->tail ){
65 q->head = NULL;
66 q->tail = NULL;
67 return;
68 }
69
70 u32 start = ((u8 *)q->tail - q->buffer);
71 start += q->tail->alloc_size;
72
73 if( start == q->size )
74 start = 0;
75
76 q->tail = (vg_queue_frame *)(q->buffer + start);
77 }