28 int daq_dta::iterate()
30 if(nitems==0)
return 0 ;
33 sec = (store_cur->sec) ;
34 row = (store_cur->row) ;
35 pad = (store_cur->pad) ;
36 ncontent = (store_cur->nitems) ;
40 Byte = (
unsigned char *) store_cur ;
43 store_cur = (
daq_store *)((
char *)store_cur + ncontent * hdr->obj_bytes) ;
50 void daq_dta::release()
52 if(store && bytes_alloced) {
53 LOG(DBG,
"freeing store") ;
64 daq_store *daq_dta::create(u_int bytes,
const char *name,
int rts_id,
const char *o_name, u_int obj_size)
72 if(bytes < obj_size) bytes = obj_size ;
78 if(bytes_alloced < requested) {
79 u_int b_all_cache = bytes_alloced ;
84 bytes_alloced = requested ;
85 store = (
daq_store *) valloc(bytes_alloced) ;
87 LOG(DBG,
"Allocated %d bytes for %d bytes required (%d was available)",bytes_alloced,bytes,b_all_cache) ;
90 LOG(DBG,
"Reusing %d bytes for %d bytes required",bytes_alloced,bytes) ;
98 nitems = store->nitems ;
103 hdr->hdr_version = DAQ_DTA_C_VERSION ;
104 hdr->endianess = DAQ_DTA_ENDIANESS ;
105 hdr->obj_bytes = obj_size ;
106 hdr->bytes_used =
sizeof(
daq_store) + hdr->hdr_bytes ;
107 strncpy(hdr->obj_name,o_name,
sizeof(hdr->obj_name)-1) ;
108 sprintf(hdr->describe,
"%s[%d]:%s[%d bytes]:%s",name,rts_id,o_name,obj_size,__DATE__) ;
112 LOG(DBG,
"CREATE:%s: nitems %d, bytes %u/%u",hdr->describe,store->nitems,hdr->bytes_used,bytes_alloced) ;
114 store_cur = (
daq_store *)((
char *)store + hdr->bytes_used) ;
119 int daq_dta::is_empty()
121 if(store == 0)
return 1 ;
123 if(store->nitems <= 1)
return 1 ;
131 void daq_dta::clear()
136 nitems = store->nitems ;
139 hdr->bytes_used =
sizeof(
daq_store) + hdr->hdr_bytes ;
142 store_cur = (
daq_store *)((
char *)store + hdr->bytes_used) ;
148 void *daq_dta::request(u_int obj_cou)
152 LOG(DBG,
"Requesting %d objects",obj_cou) ;
154 if(obj_cou <= 0) obj_cou = 16 ;
156 tmp_store =
get(obj_cou) ;
158 LOG(DBG,
"get returns %p",tmp_store) ;
160 return (
void *)(tmp_store + 1) ;
164 void daq_dta::finalize(u_int obj_cou,
int sec,
int row,
int pad)
176 store_cur->sec = sec ;
177 store_cur->row = row ;
178 store_cur->pad = pad ;
179 store_cur->nitems = obj_cou ;
182 LOG(DBG,
"Finilizing %d objects for sec %d, row %d, pad %d [%d]",obj_cou,sec,row,pad,store_cur->pad) ;
184 int bytes =
sizeof(
daq_store) + store_cur->nitems * hdr->obj_bytes ;
200 if(obj_cou==0) obj_cou = 16 ;
204 need =
sizeof(
daq_store) + (obj_cou * hdr->obj_bytes) ;
207 avail = bytes_alloced - hdr->bytes_used ;
209 if(avail < need) do_realloc = 1 ;
212 LOG(DBG,
"get(): avail bytes %d, need %d, objects %d requested of %d bytes each",avail, need, obj_cou,hdr->obj_bytes) ;
218 const int chunk = 16*1024 ;
219 int cou16 = need/chunk ;
221 need = cou16 * chunk ;
223 u_int old_alloced = bytes_alloced ;
225 bytes_alloced = hdr->bytes_used + need ;
227 LOG(DBG,
"Reallocing from %d to %d",old_alloced,bytes_alloced) ;
230 u_int hdr_off = (
char *)hdr-(
char *)store ;
231 u_int Byte_off = (
char *)Byte - (
char *)store ;
232 u_int store_cur_off = (
char *)store_cur - (
char *)store ;
236 new_store = (
char *) realloc(store, bytes_alloced) ;
238 LOG(WARN,
"realloc failed!") ;
244 LOG(DBG,
"Before valloc %d",bytes_alloced) ;
245 new_store = (
char *)valloc(bytes_alloced) ;
247 LOG(DBG,
"Before memcopy of %d",hdr->bytes_used) ;
248 memcpy(new_store,store,hdr->bytes_used) ;
257 Byte = (u_char *)new_store + Byte_off ;
258 store_cur = (
daq_store *) ((
char *)new_store + store_cur_off) ;
262 LOG(DBG,
"Realloc done") ;
265 avail = bytes_alloced - hdr->bytes_used ;
267 store_cur->sec = 0xFF ;
268 store_cur->nitems = 0 ;
274 void daq_dta::commit(u_int bytes)
276 char *mem = (
char *)store_cur ;
278 bytes =
sizeof(
daq_store) + store_cur->nitems * hdr->obj_bytes ;
284 nitems = store->nitems ;
285 hdr->bytes_used = mem - (
char *)store ;
289 LOG(DBG,
"commit: %dth nitem, bytes %d, bytes_used %d/%d",store->nitems,bytes,hdr->bytes_used,bytes_alloced) ;
291 if(hdr->bytes_used > bytes_alloced) {
292 LOG(CRIT,
"Storage overrun: %d > %d",hdr->bytes_used, bytes_alloced) ;
303 void daq_dta::rewind()
305 if(store == 0) return ;
308 nitems = store->nitems ;
311 LOG(NOTE,
"No items %d???",nitems) ;
318 store_cur = (
daq_store *)((
char *)hdr + hdr->hdr_bytes) ;
322 LOG(DBG,
"READY:%s: nitems %d, bytes %u/%u",hdr->describe,store->nitems,hdr->bytes_used,bytes_alloced) ;