src/core/buffer.c

Go to the documentation of this file.
00001 
00024 #include <misc/result.h>
00025 #include <misc/malloc.h>
00026 #include <misc/pool.h>
00027 #include <misc/debug.h>
00028 #include <core/buffer.h>
00029 
00031 struct txc_buffermgr_s {
00032         txc_pool_t *pool_buffer_circular; 
00033         txc_pool_t *pool_buffer_linear;   
00034 };
00035 
00036 
00037 txc_buffermgr_t *txc_g_buffermgr;
00038 
00039 
00040 static
00041 void
00042 circular_buffer_constructor(void *obj)
00043 {
00044         txc_buffer_circular_t *buffer;
00045         buffer  = (txc_buffer_circular_t *) obj;
00046         buffer->size_max = TXC_BUFFER_CIRCULAR_SIZE;
00047         buffer->buf = (char *) (((char *) obj) + sizeof (txc_buffer_circular_t));
00048 }
00049 
00050 
00051 static
00052 void
00053 linear_buffer_constructor(void *obj)
00054 {
00055         txc_buffer_linear_t *buffer;
00056 
00057         buffer  = (txc_buffer_linear_t *) obj;
00058         buffer->size_max = TXC_BUFFER_LINEAR_SIZE;
00059         buffer->buf = (char *) (((char *) obj) + sizeof (txc_buffer_linear_t));
00060 }
00061 
00062 
00072 txc_result_t
00073 txc_buffermgr_create(txc_buffermgr_t **buffermgrp) 
00074 {
00075         txc_result_t result;
00076 
00077         *buffermgrp = (txc_buffermgr_t *) MALLOC(sizeof(txc_buffermgr_t));
00078         if (*buffermgrp == NULL) {
00079                 return TXC_R_NOMEMORY;
00080         }
00081 
00082         if ((result = txc_pool_create(&((*buffermgrp)->pool_buffer_circular), 
00083                                       TXC_BUFFER_CIRCULAR_SIZE +
00084                                     sizeof(txc_buffer_circular_t),
00085                                       TXC_BUFFER_CIRCULAR_NUM, 
00086                                   circular_buffer_constructor)) != TXC_R_SUCCESS) 
00087         {
00088                 FREE(*buffermgrp);
00089                 return result;
00090         }       
00091 
00092         if ((result = txc_pool_create(&((*buffermgrp)->pool_buffer_linear), 
00093                                       TXC_BUFFER_LINEAR_SIZE + 
00094                                     sizeof(txc_buffer_linear_t),
00095                                       TXC_BUFFER_LINEAR_NUM, 
00096                                   linear_buffer_constructor)) != TXC_R_SUCCESS) 
00097         {
00098                 txc_pool_destroy(&((*buffermgrp)->pool_buffer_circular));
00099                 FREE(*buffermgrp);
00100                 return result;
00101         }       
00102 
00103         return TXC_R_SUCCESS;
00104 }
00105 
00106 
00114 void
00115 txc_buffermgr_destroy(txc_buffermgr_t **buffermgrp)
00116 {
00117         txc_pool_destroy(&((*buffermgrp)->pool_buffer_circular));
00118         txc_pool_destroy(&((*buffermgrp)->pool_buffer_linear));
00119         FREE(*buffermgrp);
00120         *buffermgrp = NULL;
00121 }
00122 
00123 
00124 /*
00125  *****************************************************************************
00126  ***                    CIRCULAR BUFFER IMPLEMENTATION                     ***  
00127  *****************************************************************************
00128  */
00129 
00165 txc_result_t 
00166 txc_buffer_circular_init(txc_buffer_circular_t *buffer)
00167 {
00168 
00169         buffer->primary_head = 0;
00170         buffer->primary_tail = 0;
00171         buffer->secondary_head = 0;
00172         buffer->secondary_tail = 0;
00173         buffer->state = TXC_BUFFER_STATE_NON_SPECULATIVE;
00174 
00175         return TXC_R_SUCCESS;
00176 }
00177 
00185 txc_result_t 
00186 txc_buffer_circular_create(txc_buffermgr_t *buffermgr, 
00187                            txc_buffer_circular_t **bufferp)
00188 {
00189         txc_result_t result;
00190         txc_buffer_circular_t *buffer;
00191 
00192         if ((result = txc_pool_object_alloc(buffermgr->pool_buffer_circular,
00193                                             (void **) &(buffer), 1)) 
00194             != TXC_R_SUCCESS) 
00195         {
00196                 TXC_INTERNALERROR("Could not create buffer object\n");
00197                 return result;
00198         }
00199 
00200         buffer->manager = buffermgr;
00201         txc_buffer_circular_init(buffer);
00202         *bufferp = buffer;
00203 
00204         return TXC_R_SUCCESS;
00205 }
00206 
00207 
00213 void 
00214 txc_buffer_circular_destroy(txc_buffer_circular_t **bufferp)
00215 {
00216         txc_buffermgr_t *buffermgr = (*bufferp)->manager;
00217 
00218         txc_pool_object_free(buffermgr->pool_buffer_circular, (void **) bufferp, 1); 
00219         *bufferp = NULL;
00220 }
00221 
00222 
00223 /*
00224  *****************************************************************************
00225  ***                     LINEAR BUFFER IMPLEMENTATION                      *** 
00226  *****************************************************************************
00227  */
00228 
00229 
00237 txc_result_t 
00238 txc_buffer_linear_init(txc_buffer_linear_t *buffer)
00239 {
00240         buffer->state = TXC_BUFFER_STATE_NON_SPECULATIVE;
00241         buffer->cur_len = 0;
00242 
00243         return TXC_R_SUCCESS;
00244 }
00245 
00246 
00254 txc_result_t 
00255 txc_buffer_linear_create(txc_buffermgr_t *buffermgr, 
00256                          txc_buffer_linear_t **bufferp)
00257 {
00258         txc_result_t result;
00259         txc_buffer_linear_t *buffer;
00260 
00261         if ((result = txc_pool_object_alloc(buffermgr->pool_buffer_linear,
00262                                             (void **) &(buffer), 1)) 
00263             != TXC_R_SUCCESS) 
00264         {
00265                 TXC_INTERNALERROR("Could not create buffer object\n");
00266                 return result;
00267         }
00268 
00269         buffer->manager = buffermgr;
00270         txc_buffer_linear_init(buffer);
00271         *bufferp = buffer;
00272 
00273         return TXC_R_SUCCESS;
00274 }
00275 
00276 
00282 void 
00283 txc_buffer_linear_destroy(txc_buffer_linear_t **bufferp)
00284 {
00285         txc_buffermgr_t *buffermgr = (*bufferp)->manager;
00286 
00287         txc_pool_object_free(buffermgr->pool_buffer_linear, (void **) bufferp, 1); 
00288         *bufferp = NULL;
00289 }
00290 
00291 
00299 void *
00300 txc_buffer_linear_malloc(txc_buffer_linear_t *buffer, unsigned int size)
00301 {
00302         void *ptr;
00303 
00304         if (buffer->cur_len + size > buffer->size_max) {
00305                 ptr = NULL;     
00306         } else {
00307                 ptr = (void *) &buffer->buf[buffer->cur_len];
00308                 buffer->cur_len += size;
00309         }
00310         return ptr;
00311 }
00312 
00313 
00324 void
00325 txc_buffer_linear_free(txc_buffer_linear_t *buffer, unsigned int size)
00326 {
00327         buffer->cur_len -= size;
00328 }

Generated on Wed Dec 9 20:32:39 2009 for xCalls by  doxygen 1.4.7