void ArrayU8_cleanup(ArrayU8 **a) { if ((*a)->data) { Mem_free((*a)->data); } Mem_free(*a); *a = 0L; }
static void _free_object(oe_scalar *object) { for ( int i = 0; object[i]; i++ ) { Mem_free( object[i], __FILE__, __LINE__ ); } Mem_free(object, __FILE__, __LINE__); }
void Audio_buffer_release(Audio_buffer **audio) { Audio_buffer *w = *audio; if (w->data) { Mem_free(w->data); } memset(w, 0, sizeof(*w)); Mem_free(w); *audio = 0L; }
void Oec_IdSet_free(T *_this_p) { assert(_this_p && *_this_p); T _this_ = *_this_p; //Set_T s = _this_->members; void **array = Set_toArray(_this_->members, NULL); for ( int i = 0; array[i]; i++ ) { ID_ITEM *titem = (ID_ITEM *) array[i]; if (titem) { Mem_free( titem, __FILE__, __LINE__ ); } } Mem_free(array, __FILE__, __LINE__); Set_free(&_this_->members); Mem_free(_this_, __FILE__, __LINE__); }
/* all is an Oec_AList of DataObject entries */ oe_id Oed_Dispatcher_reg_group(T _this_, DataObjectList all, oe_time dur, bool consume, user_callback *match_handler, user_callback *timeout_handler, user_callback_arg args) { oe_id sid = 0; assert(all); List_T group = List_list(NULL); for (Iterator iter = DataObjectList_iterator(all, true); Iterator_hasMore(iter);) { DataObject o = Iterator_next(iter); oe_scalar *templ_p = DataObject_toArray(o); item_ *item = _create_reg_item(_this_, templ_p, dur, consume, match_handler, timeout_handler, args); sid = item->sid; group = List_append(group, List_list(item, NULL)); } item_ **groupitems = (item_**) List_toArray(group, NULL); for (int i = 0; i < List_length(group); i++) { groupitems[i]->group = group; _schedule_item(_this_, groupitems[i]); } Mem_free(groupitems, __FILE__, __LINE__); return sid; //you can cancel the whole group by unreg'ing this one sid }
/* * This function calls Sbrk and returns * the amount of Pages asked for * */ chunk_t *MorePages(int bytes) { int num_pages; int new_bytes; chunk_t *Space = NULL; num_pages = ((bytes%PAGESIZE)==0)?bytes/PAGESIZE:bytes/PAGESIZE +1; new_bytes = PAGESIZE * num_pages; Space = morecore(new_bytes); //Check to make sure an address was returned if(Space == NULL){ printf("\n---------------- Out of memory ----------------\n"); exit(0); } Space->size = new_bytes/sizeof(chunk_t); //add memory to free list if(Rover != NULL){ chunk_t *Marker; Marker = Space + 1; // move to one unit past start of block Mem_free( Marker ); // add new segment to free list } return Space; }
static void _remove_item(Table_T items, item_ *rmitem, bool timeout) { if (rmitem == NULL) { return; } if (rmitem->group) { // get each item from the list, null out the list field and // recursively call this until the whole set is removed _remove_group(items, rmitem->group, timeout); return; } Set_T s = rmitem->items; item_ *item = NULL; if (s) { item = Set_remove(s, (void *) rmitem); //if this was a dummy item from a timeout, get the real one } if (!item) return; if (item->timer_event) { timeout_del(item->timer_event); Mem_free(item->timer_event, __FILE__, __LINE__); } if ( timeout && item->timeout_handler!=NULL ) { _process_fun(item->timeout_handler, item->args); } if (rmitem != item) { Mem_free(rmitem, __FILE__, __LINE__); //a clone } if (item->key_idx) { oe_scalar key = item->object[item->key_idx]; if (s) if (key && Set_length(s) <= 0) { if (items) { Table_remove(items, key); } if (s) { Set_free( &s ); } } } Mem_free(item, __FILE__, __LINE__); }
void ArrayU8_take_data(ArrayU8 *a, uint8_t **data, int data_len) { if (a->data) { Mem_free(a->data); } a->data = *data; a->len = a->available = data_len; *data = 0L; /* Clear caller copy, data belongs to array now. */ }
void Control_msg_release(Control_msg * msg) { if (msg->label) { String_free(&msg->label); } Value_free(msg->value); Mem_free(msg); }
static void _timed_exec_cb( int fd, short event, void *arg ) { THOLDER *holder = (THOLDER *) arg; assert(holder); assert(holder->args); assert(holder->fun); _process_fun(holder->fun, holder->args); Mem_free( holder, __FILE__, __LINE__ ); }
void Oed_Dispatcher_free(T *_this_p) { assert(_this_p && *_this_p); T _this_ = *_this_p; if (_this_->name) { Mem_free((void *) _this_->name, __FILE__, __LINE__); } Table_T items = _this_->items; Table_free( &items ); event_base_free( (struct event_base *) Oed_Dispatcher_get_event_base(_this_) ); #ifdef OE_USE_THREADS Mem_free( _this_->notify_event, __FILE__, __LINE__); #endif Mem_free( _this_->que, __FILE__, __LINE__); Mem_free( _this_, __FILE__, __LINE__); }
oe_id Oec_IdSet_remove(T _this_, oe_id mid) { ID_ITEM item; item.mid = mid; ID_ITEM *removeme = (ID_ITEM *) Set_remove(_this_->members, &item); oe_id retid; if (removeme != NULL) { retid = mid; } else { retid = (oe_id) 0; } Mem_free(removeme, __FILE__, __LINE__); return retid; }
static void _cleanup(OeSession session) { T _this_ = OeNet_get_proto(OeSession_get_net(session)); int sz = Table_length(_this_->pending); void **array = Table_toArray(_this_->pending, NULL); for ( int i = 0; i < sz && array[i]; i+= 2 ) { CID_KEY *key = (CID_KEY *) array[i]; ACContext *pc = Table_remove(_this_->pending, key); _send_con_closed(pc->context); } Mem_free(array, __FILE__, __LINE__); Table_free(&_this_->pending); //OepClient_free(&_this_); //closing the proto is hard and buggy. }
void *Mem_resize(void *ptr,long nbytes,const char *file,int line) { struct descriptor *bp; void *newptr; assert(ptr); assert(nbytes>0); if(((unsigned long)ptr)%(sizeof(union align))!=0||(bp=find(ptr))==NULL||bp->free) Except_raise(&Assert_Failed,file,line); newptr=Mem_alloc(nbytes,file,line); memcpy(newptr,ptr,nbytes<bp->size? nbytes:bp->size); Mem_free(ptr,file,line); return newptr; }
static void _free_items(Table_T items) { assert(items); int sz = Table_length(items); if (sz > 0) { void **array = Table_toArray(items, NULL); for ( int i = 0; i < sz && array[i]; i+= 2 ) { char *key = (char *) array[i]; Set_T titem = Table_remove(items, key); if (titem) { Set_free( &titem ); } } Mem_free(array, __FILE__, __LINE__); } }
static void _remove_group(Table_T items, List_T group, bool timeout) { if (group == NULL) return; item_ **gitems = (item_ **) List_toArray(group, NULL); if (gitems == NULL) return; size_t sz = List_length(group); for (int i = 0; i < sz; i++) { item_ *gitem = gitems[i]; gitem->group = NULL; if (i > 0) { //don't run 'em twice gitem->match_handler = NULL; gitem->timeout_handler = NULL; } _remove_item(items, gitem, timeout); } Mem_free(gitems, __FILE__, __LINE__); List_free(&group); }
void * Mem_resize(void *ptr, long nbytes, const char *file, int line) { assert(ptr); assert(nbytes > 0); struct descriptor *bp = find(ptr); if (((unsigned long long)ptr) % (sizeof(union align)) != 0 || bp == NULL || bp->free) { if (log_file) { fprintf(log_file, "** resizing unallocated memory\n" "Mem_resize(0x%p, %ld) called from %s:%d\n", \ ptr, nbytes, file, line); } else Except_raise(&Assert_Failed, file, line); } void *newptr = Mem_alloc(nbytes, file, line); memcpy(newptr, ptr, nbytes < bp->size ? nbytes : bp->size); Mem_free(ptr, file, line); return newptr; }
int JsonNet_peek_cid(OeSession client, oe_id *cid) { OeNetConn conn = OeSession_get_conn(client); int avail = OeNetConn_available(conn); int pos = OeNetConn_get_pos(conn, CID_TAG, 5); if (pos < 0) { if ( OeSession_state_is_set( client, OENET_CLIENT_CONNECTED ) ) { return 0; } *cid = 1; return 1;//magic cor id :( } int lpos = pos + 6; int llen = lpos + 8; llen = llen < avail ? llen : avail - lpos - 1; void *peekdata = OeNetConn_peek(conn, lpos, llen); *cid = (oe_id) atol(peekdata); Mem_free(peekdata, __FILE__, __LINE__); #ifdef OE_TRACE OE_DLOG(0, OEP_JSON_PEEK_CID, *cid); #endif return 1; }
//todo: skip testing the idx keys of both item and titem static bool _processSet( Table_T items, Set_T itemset, item_ *item, int key_idx ) { OE_TLOG(0, "Oed_Dispatcher _processSet\n"); //i18n assert(item); assert(items); assert(itemset); bool done = false; //when you get a match, remove template and invoke callback void **array = Set_toArray(itemset, NULL); for ( int i = 0; array[i]; i++ ) { OE_TLOG(0, "Oed_Dispatcher _processSet template %i\n", i); //i18n item_ *titem = (item_ *) array[i]; int titem_key_idx = titem->key_idx; bool matched = true; for (int j = 0; matched && titem->object[j]; j++) { if (j == titem_key_idx) continue; //this was the key that put this // template into this set, don't // re-test it char *tobj = (char *) titem->object[j]; bool kmatched = false; for (int k = 0; !kmatched && item->object[k]; k++) { if (k == key_idx) continue; //this was the key that got us the set, // don't re-test it char *iobj = (char *) item->object[k]; OE_TLOG(0, " _processSet template %i fld: %s tst: %s\n", i, iobj, tobj); //i18n if (strcmp(iobj, tobj) == 0) { kmatched = true; } else if (strcmp("_", tobj) == 0) kmatched = true; //wildcard } matched = kmatched; } if (matched) { OE_TLOG(0, " _processSet matched %i\n", i); //i18n //a match if (titem->match_handler) { OE_TLOG(0, " _processSet matched running handler %i\n", i); //i18n _process_fun(titem->match_handler, titem->args); //this might happen if someone registered //something that should only timeout like //a timer but idiotically specified a //possible pattern and the pattern hit. } bool consume = titem->consume; // note, this is the point to implement persistent subs _remove_item(items, titem, false); //always remove the item. if (consume) { done = true; break; //must be a take } } } Mem_free(array, __FILE__, __LINE__); return done; }
static void _process_que_item(T _this_, item_ *item ) { Table_T items = _this_->items; if (item == NULL) { return; } switch (item->type) { case REG: OE_TLOG(0, "Oed_Dispatcher _process_que_item REG\n"); //i18n int rc = _add_template(_this_, item); //schedule timeout _watch_for_timeout(item, _this_->net_obj); break; case UNREG: _remove_item(items, item, false); break; case PROCESS: OE_TLOG(0, "Oed_Dispatcher _process_que_item PROCESS\n"); //i18n //run match tests _process(items, item); if (item->free_alloc) { _free_object(item->object); } Mem_free(item, __FILE__, __LINE__); break; case EXECUTE: //run now if (item->match_handler) { _process_fun(item->match_handler, item->args); } Mem_free(item, __FILE__, __LINE__); break; case HEARTBEAT: timeout_del(item->timer_event); Mem_free(item->timer_event, __FILE__, __LINE__); Mem_free(item, __FILE__, __LINE__); _schedule_heartbeat(_this_); break; case DIAG: Mem_free(item, __FILE__, __LINE__); _print_diag(_this_); break; case TQUIT: Mem_free(item, __FILE__, __LINE__); event_base_loopexit(_this_->net_obj, 0); _free_items(items); break; default: assert(0); //should never get here break; } }
void MotionDetect_result_release(MotionDetect_result **md) { Mem_free(*md); *md = 0L; }
ST_TYPE void _oekfree(T *_this_p) { assert(_this_p && *_this_p); T _this_ = *_this_p; Mem_free(_this_, __FILE__, __LINE__); }
static void ALSACapture_tick(Instance *pi) { ALSAio_private *priv = (ALSAio_private *)pi; int wait_flag; if (!priv->c.enable || !priv->c.handle) { wait_flag = 1; } else { wait_flag = 0; } Handler_message *hm; hm = GetData(pi, wait_flag); if (hm) { hm->handler(pi, hm->data); ReleaseMessage(&hm,pi); } if (!priv->c.enable || !priv->c.handle) { /* Not enabled or no handle, don't try to capture anything. */ return; } /* Read a block of data. */ int rc; snd_pcm_sframes_t n; int state; snd_pcm_uframes_t frames = priv->c.frames_per_io; int dir = 0; state = snd_pcm_state(priv->c.handle); dpf("%s: state(1)=%s\n", __func__, ALSAio_state_to_string(state)); if (state == SND_PCM_STATE_OPEN || state == SND_PCM_STATE_SETUP) { /* One time capture setup. */ fprintf(stderr, "%s: state=%s\n", __func__, ALSAio_state_to_string(state)); rc = snd_pcm_hw_params_set_period_size_near(priv->c.handle, priv->c.hwparams, &frames, &dir); fprintf(stderr, "%s: set_period_size_near returns %d (frames %d:%d)\n", __func__, rc, (int)priv->c.frames_per_io, (int)frames); rc = snd_pcm_hw_params(priv->c.handle, priv->c.hwparams); if (rc < 0) { fprintf(stderr, "*** snd_pcm_hw_params %s: %s\n", s(priv->c.device), snd_strerror(rc)); } state = snd_pcm_state(priv->c.handle); fprintf(stderr, "%s: state=%s\n", __func__, ALSAio_state_to_string(state)); rc = snd_pcm_prepare(priv->c.handle); state = snd_pcm_state(priv->c.handle); fprintf(stderr, "%s: state=%s\n", __func__, ALSAio_state_to_string(state)); /* Initialize running_timestamp. */ cti_getdoubletime(&priv->c.running_timestamp); } snd_pcm_hw_params_get_period_size(priv->c.hwparams, &frames, &dir); int size = frames * priv->c.format_bytes * priv->c.channels; if (!size) { // This can happen if channels or format_bytes was not set... fprintf(stderr, "%s: size error - %ld * %d * %d\n", __func__, frames , priv->c.format_bytes , priv->c.channels); while (1) { sleep(1); } } if (!priv->c.rate) { fprintf(stderr, "%s: error - rate is 0!\n", __func__); while (1) { sleep(1); } } /* Allocate buffer for PCM samples. */ uint8_t *buffer = Mem_malloc(size+1); buffer[size] = 0x55; //int val = hwparams->rate; //snd_pcm_hw_params_get_period_time(params, &val, &dir); /* Read the data. */ n = snd_pcm_readi(priv->c.handle, buffer, frames); if (buffer[size] != 0x55) { fprintf(stderr, "*** overwrote audio buffer!\n"); } if (n != frames) { fprintf(stderr, "*** snd_pcm_readi %s: %s\n", s(priv->c.device), snd_strerror((int)n)); fprintf(stderr, "*** attempting snd_pcm_prepare() to correct...\n"); snd_pcm_prepare(priv->c.handle); goto out; } // fprintf(stderr, "*** read %d frames\n", (int) n); double calculated_period = frames*1.0/(priv->c.rate); priv->c.running_timestamp += calculated_period; double tnow; cti_getdoubletime(&tnow); /* Do coarse adjustment if necessary, this can happen after a system date change via ntp or htpdate. */ if (fabs(priv->c.running_timestamp - tnow) > 5.0) { fprintf(stderr, "coarse timestamp adjustment, %.3f -> %.3f\n", priv->c.running_timestamp, tnow); priv->c.running_timestamp = tnow; } /* Adjust running timestamp if it slips too far either way. Smoothing, I guess. */ if (priv->c.running_timestamp - tnow > calculated_period) { dpf("priv->c.rate=%d, %.3f - %.3f (%.5f) > %.5f : - running timestamp\n", priv->c.rate, priv->c.running_timestamp, tnow, (tnow - priv->c.running_timestamp), calculated_period); priv->c.running_timestamp -= (calculated_period/2.0); } else if (tnow - priv->c.running_timestamp > calculated_period) { dpf("priv->c.rate=%d, %.3f - %.3f (%.5f) > %.5f : + running timestamp\n", priv->c.rate, tnow, priv->c.running_timestamp, (tnow - priv->c.running_timestamp), calculated_period); priv->c.running_timestamp += (calculated_period/2.0); } int buffer_bytes = n * priv->c.format_bytes * priv->c.channels; if (pi->outputs[OUTPUT_AUDIO].destination) { Audio_buffer * audio = Audio_buffer_new(priv->c.rate, priv->c.channels, priv->c.atype); Audio_buffer_add_samples(audio, buffer, buffer_bytes); audio->timestamp = priv->c.running_timestamp; //fprintf(stderr, "posting audio buffer %d bytes\n", buffer_bytes); PostData(audio, pi->outputs[OUTPUT_AUDIO].destination); /* TESTING: disable after posting once */ //pi->outputs[OUTPUT_AUDIO].destination = NULL; } if (pi->outputs[OUTPUT_WAV].destination) { Wav_buffer *wav = Wav_buffer_new(priv->c.rate, priv->c.channels, priv->c.format_bytes); wav->timestamp = priv->c.running_timestamp; dpf("%s allocated wav @ %p\n", __func__, wav); wav->data = buffer; buffer = 0L; /* Assign, do not free below. */ wav->data_length = buffer_bytes; Wav_buffer_finalize(wav); if (priv->c.analyze) { analyze_rate(priv, wav); } PostData(wav, pi->outputs[OUTPUT_WAV].destination); static int x = 0; wav->seq = x++; wav = 0L; } out: if (buffer) { Mem_free(buffer); } }