6
* Buffer Manager for managing buffers in any application
21
/*#define STATBUF 1 */ /* set if static buffers will be used */
29
/** buffer management initialization routine */
31
buf_context_t *ctxt, int nbuf, int buf_size, void (*fptr)(char*))
35
if (nbuf < 1 || nbuf > MAXBUF) {
36
printf("Too many (or too few) buffers requested, using default number (%d) of buffers", DEFBUF);
40
/* create a context */
41
ctxt->ctxt_id = ctxt_count++;
42
if (ctxt->ctxt_id > MAX_CTXT) {
43
printf("Max number of contexts reached!\n");
48
ctxt->size = buf_size;
49
ctxt->buf = (_buffer_t *)malloc(sizeof(_buffer_t) * nbuf);
53
double buffers[MAXBUF][DBL_BUF_SIZE];
54
for (i = 0; i < nbuf; i++) {
55
ctxt->buf[i].buffer = (char *) buffers[i];
56
bzero(buffers[i], sizeof(buffers[i]));
60
/* get buffer memory */
61
for (i = 0; i < nbuf; i++) {
62
ctxt->buf[i].buffer = (char*) malloc((buf_size + ALIGN-1) *sizeof(double));
64
if (ctxt->buf[i].buffer == NULL) {
65
printf("Could not allocate memory for buffers!\n");
68
bzero(ctxt->buf[i].buffer, sizeof(ctxt->buf[i].buffer));
70
/* align buffer address */
71
diff = ((long)(ctxt->buf[i].buffer)) % (sizeof(double)*ALIGN);
74
ctxt->buf[i].align_off = (int) (sizeof(double)*ALIGN - diff);
77
ctxt->buf[i].align_off = (int) 0;
79
ctxt->buf[i].buffer += ctxt->buf[i].align_off;
83
for (i = 0; i < nbuf; i++) {
84
ctxt->buf[i].active = 0;
85
ctxt->buf[i].group_id = 0;
88
printf("Created a context\n\n");
93
/** internal function to return empty buffer handle */
94
int get_buf_hdl(buf_context_t *ctxt)
97
for (i = 0; i < ctxt->nbuf; i++) {
98
if (ctxt->buf[i].active == 0) {
99
ctxt->buf[i].active = 1;
107
char* get_buf(buf_context_t *ctxt, int call_id)
112
hdl = get_buf_hdl(ctxt);
115
/* no buffer is available, wait for the oldest buffer to become free */
116
cur_buf = (ctxt->last_buf + 1) % ctxt->nbuf;
118
/* free this buffer by calling the callback function provided by the application */
119
ctxt->fptr(ctxt->buf[cur_buf].buffer);
122
buf = ctxt->buf[hdl].buffer;
123
ctxt->buf[hdl].buf_hdl = hdl;
124
ctxt->buf[hdl].call_id = call_id;
125
ctxt->buf[hdl].active = 1;
126
ctxt->last_buf = hdl;
128
printf("Giving a buffer with internal handle: %d\n", hdl);
134
/** function to free a buffer */
135
void free_buf(buf_context_t *ctxt, char *buf)
138
for (i = 0; i < ctxt->nbuf; i++) {
139
if (ctxt->buf[i].buffer == buf) {
140
ctxt->buf[i].active = 0;
147
/** function to complete an entire call */
148
void buf_complete_call(buf_context_t *ctxt, int call_id)
152
printf("Completing call with call id: %d\n", call_id);
154
for (i = 0; i < ctxt->nbuf; i++) {
155
if (ctxt->buf[i].call_id == call_id && ctxt->buf[i].active == 1) {
156
/* force completion by calling user function */
157
ctxt->fptr(ctxt->buf[i].buffer);
158
ctxt->buf[i].active = 0;
164
/** function to return the call_id associated with a particular buffer */
165
int buf_get_call_id(buf_context_t *ctxt, char *buf)
168
for (i = 0; i < ctxt->nbuf; i++)
169
if (ctxt->buf[i].buffer == buf)
170
return(ctxt->buf[i].call_id);
171
printf("Buf_man error: Cannot find call_id for this buffer\n");
177
* Function to return an array of buffers associated with a call_id
178
* the last two parameters are output
180
int get_bufs_of_call_id(
181
buf_context_t *ctxt, int call_id, int *n_buf, char *bufs[])
185
for (i = 0; i < ctxt->nbuf; i++)
186
if (ctxt->buf[i].call_id == call_id) {
187
bufs[count++] = ctxt->buf[i].buffer;
192
printf("Buf_man: No active buffer found for call_id %d\n", call_id);
194
return -1; /* no buffer found */
197
return 0; /* success */
201
/** terminates an application context */
202
void buf_terminate(buf_context_t *ctxt)
206
for (i = 0; i < ctxt->nbuf; i++) {
207
ctxt->buf[i].buffer -= ctxt->buf[i].align_off;
208
free(ctxt->buf[i].buffer);
214
ctxt_count--; /* this context can be reallocated */