1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
/* Copyright (C) 2014 Timo Dritschler <timo.dritschler@kit.edu>
(Karlsruhe Institute of Technology)
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc., 51
Franklin St, Fifth Floor, Boston, MA 02110, USA
*/
/**
* SECTION: kiro-trb
* @Short_description: KIRO 'Transmittable Ring Buffer'
* @Title: KiroTrb
*
* KiroTrb implements a 'Transmittable Ring Buffer' that holds all necessary information
* about its content inside itself, so its data can be exchanged between different
* instances of the KiroTrb Class and/or sent over a network.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "kiro-trb.h"
/*
* Definition of 'private' structures and members and macro to access them
*/
#define KIRO_TRB_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), KIRO_TYPE_TRB, KiroTrbPrivate))
struct _KiroTrbPrivate {
/* Properties */
// PLACEHOLDER //
/* 'Real' private structures */
/* (Not accessible by properties) */
int initialized; // 1 if Buffer is Valid, 0 otherwise
void* mem; // Access to the actual buffer in Memory
void* frame_top; // First byte of the buffer storage
void* current; // Pointer to the current fill state
uint64_t element_size;
uint64_t max_elements;
uint64_t iteration; // How many times the buffer has wraped around
/* easy access */
uint64_t buff_size;
};
G_DEFINE_TYPE_WITH_PRIVATE (KiroTrb, kiro_trb, G_TYPE_OBJECT);
static
void kiro_trb_init (KiroTrb *self)
{
KiroTrbPrivate *priv = KIRO_TRB_GET_PRIVATE(self);
priv->initialized = 0;
}
static void
kiro_trb_finalize (GObject *object)
{
KiroTrb *self = KIRO_TRB(object);
KiroTrbPrivate *priv = KIRO_TRB_GET_PRIVATE(self);
if(priv->mem)
free(priv->mem);
}
static void
kiro_trb_class_init (KiroTrbClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
gobject_class->finalize = kiro_trb_finalize;
}
/* Privat functions */
void write_header (KiroTrbPrivate* priv)
{
if(!priv)
return;
struct KiroTrbInfo* tmp_info = (struct KiroTrbInfo*)priv->mem;
tmp_info->buffer_size_bytes = priv->buff_size;
tmp_info->element_size = priv->element_size;
tmp_info->offset = (priv->iteration * priv->max_elements) + ((priv->current - priv->frame_top) / priv->element_size);
memcpy(priv->mem, tmp_info, sizeof(struct KiroTrbInfo));
}
/* TRB functions */
uint64_t kiro_trb_get_element_size (KiroTrb* self)
{
KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self);
if(priv->initialized != 1)
return 0;
return priv->element_size;
}
uint64_t kiro_trb_get_max_elements (KiroTrb* self)
{
KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self);
if(priv->initialized != 1)
return 0;
return priv->max_elements;
}
uint64_t kiro_trb_get_raw_size (KiroTrb* self)
{
KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self);
if(priv->initialized != 1)
return 0;
return priv->buff_size;
}
void* kiro_trb_get_raw_buffer (KiroTrb* self)
{
KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self);
if(priv->initialized != 1)
return NULL;
write_header(priv);
return priv->mem;
}
void* kiro_trb_get_element (KiroTrb* self, uint64_t element)
{
KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self);
if(priv->initialized != 1)
return NULL;
uint64_t relative = 0;
if(priv->iteration == 0)
relative = element * priv->element_size;
else
relative = ((priv->current - priv->frame_top) + (priv->element_size * element)) % (priv->buff_size - sizeof(struct KiroTrbInfo));
return priv->frame_top + relative;
}
void kiro_trb_flush (KiroTrb *self)
{
KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self);
priv->iteration = 0;
priv->current = priv->frame_top;
}
int kiro_trb_reshape (KiroTrb *self, uint64_t element_size, uint64_t element_count)
{
size_t new_size = (element_size * element_count) + sizeof(struct KiroTrbInfo);
void* newmem = malloc(new_size);
if(!newmem)
return -1;
((struct KiroTrbInfo *)newmem)->buffer_size_bytes = new_size;
((struct KiroTrbInfo *)newmem)->element_size = element_size;
((struct KiroTrbInfo *)newmem)->offset = 0;
kiro_trb_ingest(self, newmem);
return 0;
}
int kiro_trb_push (KiroTrb *self, void *element_in)
{
KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self);
if(priv->initialized != 1)
return -1;
if((priv->current + priv->element_size) > (priv->mem + priv->buff_size))
return -1;
memcpy(priv->current, element_in, priv->element_size);
priv->current += priv->element_size;
if(priv->current >= priv->frame_top + (priv->element_size * priv->max_elements))
{
priv->current = priv->frame_top;
priv->iteration++;
}
return 0;
}
void kiro_trb_ingest (KiroTrb *self, void *buff_in)
{
KiroTrbPrivate* priv = KIRO_TRB_GET_PRIVATE(self);
struct KiroTrbInfo *tmp = (struct KiroTrbInfo *)buff_in;
if(priv->mem)
free(priv->mem);
priv->mem = buff_in;
priv->buff_size = tmp->buffer_size_bytes;
priv->element_size = tmp->element_size;
priv->max_elements = (tmp->buffer_size_bytes - sizeof(struct KiroTrbInfo)) / tmp->element_size;
priv->iteration = tmp->offset / priv->max_elements;
priv->frame_top = buff_in + sizeof(struct KiroTrbInfo);
priv->current = priv->frame_top + ((tmp->offset % priv->max_elements) * priv->element_size);
priv->initialized = 1;
}
|