inline static uintptr_t custodian_usage(NewGC*gc, void *custodian) { OTEntry **owner_table; uintptr_t retval = 0; int i; if(!gc->really_doing_accounting) { if (!gc->avoid_collection) { CHECK_PARK_UNUSED(gc); gc->park[0] = custodian; gc->really_doing_accounting = 1; garbage_collect(gc, 1, 0, 0, NULL); custodian = gc->park[0]; gc->park[0] = NULL; } } i = custodian_to_owner_set(gc, (Scheme_Custodian *)custodian); owner_table = gc->owner_table; if (owner_table[i]) retval = (owner_table[i]->memory_use + owner_table[i]->master_memory_use); else retval = 0; return gcWORDS_TO_BYTES(retval); }
void usb_plug_evt(bool plugged, void *param) { if (plugged) { if (usb_initialized == 0) { usb_driver_intf->usb_connect(); usb_driver_intf->usb_driver_init(SOC_USB_BASE_ADDR); f.function_init(f.priv, f.alt_strings); interrupt_enable(SOC_USB_INTERRUPT); usb_initialized = 1; } else { pr_error(LOG_MODULE_USB, "Trying to init already initialized driver"); } } else { if (usb_initialized == 1) { interrupt_disable(SOC_USB_INTERRUPT); usb_driver_intf->usb_disconnect(); usb_initialized = 0; int ret = garbage_collect(); if (ret) { pr_error(LOG_MODULE_USB, "Mem leak avoided (idx: %d)", ret); } } else { pr_error(LOG_MODULE_USB, "Trying to free already freed driver"); } } }
int main (void) { void *dummy; stack_base = &dummy; garbage_collect (); return 0; }
void callid_gc(unsigned int tick, void *param) { /* check the last slice */ if (((ttimeparams*)param)->ibnow + 1 == ((ttimeparams*)param)->ibcir) { garbage_collect(glb_tcallid_table, (((ttimeparams*)param)->ibnow)*((ttimeparams*)param)->ibnum, CALLID_TABLE_ENTRIES-1); /* we step to the first slice */ ((ttimeparams*)param)->ibnow=0; } else { garbage_collect(glb_tcallid_table, (((ttimeparams*)param)->ibnow)*((ttimeparams*)param)->ibnum, ((((ttimeparams*)param)->ibnow+1)*((ttimeparams*)param)->ibnum)-1); /* we step to the next slice */ ((ttimeparams*)param)->ibnow++; } }
void garbage_init() { int number_of_Cells = 1000000; gc_block_start = calloc(number_of_Cells + 1, sizeof(cell)); top_allocated = gc_block_start + number_of_Cells; free_cells = NULL; garbage_collect(); top_allocated = NULL; }
bool try_gc_reserve(uvalue n) { if (!CANGC(n, 0)) { garbage_collect(); if (!CANGC(n, 0)) return FALSE; } return TRUE; }
object make_heap_object(long type, long size) { long i = ALIGN_BYTE_POINTER(size); void *h = (void *)heap_pointer; heap_pointer += i; if (heap_pointer >= heap_end) { garbage_collect(size); h = (void *)heap_pointer; heap_pointer += i; } POINTER_HEADER(h) = (i << 8) + type; return (object)h; }
//removes the children of any node with less than limit work void AgentPNS::garbage_collect(Node * node){ Node * child = node->children.begin(); Node * end = node->children.end(); for( ; child != end; child++){ if(child->terminal() || child->work < gclimit){ //solved or low work, ignore solvedness since it's trivial to re-solve nodes -= child->dealloc(ctmem); }else if(child->children.num() > 0){ garbage_collect(child); } } }
std::shared_ptr<raii_deleter> register_shared_memory_name(std::string m_name) { garbage_collect(); // tag file is located as [system temp directory]/glshm_[userid]/[shmname] // tag file contains a PID in it. auto tagfile = (shared_memory_tagfile_path() / m_name).generic_string(); std::ofstream fout(tagfile); fout << get_my_pid(); fout.close(); return std::make_shared<raii_deleter>(m_name, tagfile); }
static void test_garbage_collect(void) { void* p; p = mini_gc_malloc(100); #if 0 assert(FL_TEST((((Header*)p) - 1), FL_ALLOC)); #endif p = 0; garbage_collect(); }
void stack_reserve(uvalue n) /* Effects: make sure n more bytes can be allocated (for the stack or for the GC), garbage_collect if not. If after garbage collection n bytes are not available, then throw error_no_memory Returns: TRUE if n bytes were available, FALSE if an error was thrown */ { #if 0 if (!CANGC(0, n)) { garbage_collect(); if (!CANGC(0, n)) runtime_error(error_no_memory); } #else if (sp - n < splimit) { garbage_collect(); if (sp - n < splimit) runtime_error(error_no_memory); } #endif }
void * rb_objc_newobj(size_t size) { void *obj; if (stress_gc && !dont_gc) { garbage_collect(); } obj = auto_zone_allocate_object(__auto_zone, size, AUTO_OBJECT_SCANNED, 0, 0); assert(obj != NULL); RBASIC(obj)->klass = (VALUE)__nsobject; return obj; }
//removes the children of any node with less than limit work void SolverPNS::garbage_collect(PNSNode * node){ PNSNode * child = node->children.begin(); PNSNode * end = node->children.end(); for( ; child != end; child++){ if(child->terminal()){ //solved //log heavy nodes? nodes -= child->dealloc(ctmem); }else if(child->work < gclimit){ //low work, ignore solvedness since it's trivial to re-solve nodes -= child->dealloc(ctmem); }else if(child->children.num() > 0){ garbage_collect(child); } } }
/// Try to free memory. Used when trying to recover from out of memory errors. /// @see {xmalloc} static void try_to_free_memory(void) { static bool trying_to_free = false; // avoid recursive calls if (trying_to_free) return; trying_to_free = true; // free any scrollback text clear_sb_text(); // Try to save all buffers and release as many blocks as possible mf_release_all(); // cleanup recursive lists/dicts garbage_collect(); trying_to_free = false; }
inline static void BTC_add_account_hook(int type,void *c1,void *c2,uintptr_t b) { NewGC *gc = GC_get_GC(); AccountHook *work; if(!gc->really_doing_accounting) { if (!gc->avoid_collection) { CHECK_PARK_UNUSED(gc); gc->park[0] = c1; gc->park[1] = c2; gc->really_doing_accounting = 1; garbage_collect(gc, 1, 0, 0, NULL); c1 = gc->park[0]; gc->park[0] = NULL; c2 = gc->park[1]; gc->park[1] = NULL; } } if (type == MZACCT_LIMIT) gc->reset_limits = 1; if (type == MZACCT_REQUIRE) gc->reset_required = 1; for(work = gc->hooks; work; work = work->next) { if((work->type == type) && (work->c2 == c2) && (work->c1 == c1)) { if(type == MZACCT_REQUIRE) { if(b > work->amount) work->amount = b; } else { /* (type == MZACCT_LIMIT) */ if(b < work->amount) work->amount = b; } break; } } if(!work) { work = ofm_malloc(sizeof(AccountHook)); work->type = type; work->c1 = c1; work->c2 = c2; work->amount = b; /* push work onto hooks */ work->next = gc->hooks; gc->hooks = work; } }
void AgentMCTS::garbage_collect(Board & board, Node * node){ Node * child = node->children.begin(), * end = node->children.end(); Side toplay = board.toplay(); for( ; child != end; child++){ if(child->children.num() == 0) continue; if( (node->outcome >= Outcome::DRAW && child->exp.num() > gcsolved && (node->outcome != toplay || child->outcome == toplay || child->outcome == Outcome::DRAW)) || //parent is solved, only keep the proof tree, plus heavy draws (node->outcome < Outcome::DRAW && child->exp.num() > (child->outcome >= Outcome::DRAW ? gcsolved : gclimit)) ){ // only keep heavy nodes, with different cutoffs for solved and unsolved board.move(child->move); garbage_collect(board, child); board.undo(child->move); }else{ nodes -= child->dealloc(ctmem); } } }
cell_t * apply_lambda( lambda_t * lambda, cell_t * operands, struct Env * env ){ struct Env * lenv = bind_operands( lambda, operands, env ); cell_t * s_expression = lambda->body; cell_t * value; while( body_iter != NULL ){ garbage_collect(); value = eval( s_expression, lenv ); s_expression = s_expression->next; } free_env( lenv ); return value; }
void SolverPNS::run_pns(){ while(!timeout && root.phi != 0 && root.delta != 0){ if(!pns(rootboard, &root, 0, INF32/2, INF32/2)){ logerr("Starting solver GC with limit " + to_str(gclimit) + " ... "); Time starttime; garbage_collect(& root); Time gctime; ctmem.compact(1.0, 0.75); Time compacttime; logerr(to_str(100.0*ctmem.meminuse()/memlimit, 1) + " % of tree remains - " + to_str((gctime - starttime)*1000, 0) + " msec gc, " + to_str((compacttime - gctime)*1000, 0) + " msec compact\n"); if(ctmem.meminuse() >= memlimit/2) gclimit = (unsigned int)(gclimit*1.3); else if(gclimit > 5) gclimit = (unsigned int)(gclimit*0.9); //slowly decay to a minimum of 5 } } }
int buffered_reader::read_into_buffer(void) { char *dummy; int n_read = 0; garbage_collect(); dummy = (char *)realloc(buffer, buffer_length + block_size + 1); if (!dummy) { fprintf(stderr, "buffered_reader::read_into_buffer: realloc failed\n"); syslog(LOG_EMERG, "buffered_reader::read_into_buffer: realloc failed"); exit(1); } buffer = dummy; for(;;) { n_read = read(fd, &buffer[buffer_length], block_size); if (n_read == -1) { if (errno == EINTR || errno == EAGAIN) continue; fprintf(stderr, "buffered_reader::read_into_buffer: read failed (%s)\n", strerror(errno)); syslog(LOG_EMERG, "buffered_reader::read_into_buffer: read failed: %m"); exit(1); } buffer_length += n_read; break; } buffer[buffer_length] = 0x00; return n_read; }
/* Ctrl+C will get you here */ void sig_handler(int signum) { int ret; pthread_t thread_id; switch (signum) { case SIGUSR1: /* Rereading config */ if (pthread_mutex_trylock(&mutexconf) == 0) ret = pthread_create(&thread_id, NULL, &reread_config, NULL); break; default: /* Freeing memory and exit */ pthread_mutex_lock(&mutexconf); pthread_mutex_lock(&mutexusock); garbage_collect(); /* Wait for unlocking mutex and destroy them */ pthread_mutex_unlock(&mutexusock); pthread_mutex_destroy(&mutexusock); pthread_mutex_unlock(&mutexconf); pthread_mutex_destroy(&mutexconf); exit(ERR); } }
void * ruby_xrealloc(void *ptr, size_t size) { if ((ssize_t)size < 0) { negative_size_allocation_error("negative re-allocation size"); } if (ptr == NULL) { return ruby_xmalloc(size); } if (size == 0) { size = 1; } if (stress_gc && !dont_gc) { garbage_collect(); } void *mem = malloc_zone_realloc(__auto_zone, ptr, size); if (mem == NULL) { rb_memerror(); } return mem; }
int cmd_rerere(int argc, const char **argv, const char *prefix) { struct string_list merge_rr = { NULL, 0, 0, 1 }; int i, fd; if (argc < 2) return rerere(); fd = setup_rerere(&merge_rr); if (fd < 0) return 0; if (!strcmp(argv[1], "clear")) { for (i = 0; i < merge_rr.nr; i++) { const char *name = (const char *)merge_rr.items[i].util; if (!has_resolution(name)) unlink_rr_item(name); } unlink(git_path("rr-cache/MERGE_RR")); } else if (!strcmp(argv[1], "gc")) garbage_collect(&merge_rr); else if (!strcmp(argv[1], "status")) for (i = 0; i < merge_rr.nr; i++) printf("%s\n", merge_rr.items[i].string); else if (!strcmp(argv[1], "diff")) for (i = 0; i < merge_rr.nr; i++) { const char *path = merge_rr.items[i].string; const char *name = (const char *)merge_rr.items[i].util; diff_two(rr_path(name, "preimage"), path, path, path); } else usage(git_rerere_usage); string_list_clear(&merge_rr, 1); return 0; }
inline static unsigned long custodian_usage(NewGC*gc, void *custodian) { OTEntry **owner_table; unsigned long retval = 0; int i; if(!gc->really_doing_accounting) { gc->park[0] = custodian; gc->really_doing_accounting = 1; garbage_collect(gc, 1, 0); custodian = gc->park[0]; gc->park[0] = NULL; } i = custodian_to_owner_set(gc, (Scheme_Custodian *)custodian); owner_table = gc->owner_table; if (owner_table[i]) retval = owner_table[i]->memory_use; else retval = 0; return gcWORDS_TO_BYTES(retval); }
void MPEGstream::insert_packet(Uint8 * Data, Uint32 Size, double timestamp) { MPEGlist * newbr; /* Discard all packets if not enabled */ if(!enabled) return; SDL_mutexP(mutex); preread_size += Size; /* Seek the last buffer */ for(newbr = br; newbr->Next(); newbr = newbr->Next()); /* Position ourselves at the end of the stream */ newbr = newbr->Alloc(Size); if ( Size ) { memcpy(newbr->Buffer(), Data, Size); } newbr->TimeStamp = timestamp; SDL_mutexV(mutex); garbage_collect(); }
static inline void * ruby_xmalloc_memory(size_t size, int type) { if ((ssize_t)size < 0) { negative_size_allocation_error("negative allocation size (or too big)"); } if (size == 0) { size = 1; } if (__auto_zone == NULL) { rb_objc_no_gc_error(); } if (stress_gc && !dont_gc) { garbage_collect(); } void *mem = auto_zone_allocate_object(__auto_zone, size, type, 0, 0); if (mem == NULL) { rb_memerror(); } return mem; }
char * buffered_reader::read_line(void) { char *out = NULL; #ifdef USE_MMAP if (mmap_addr) { long long int n_bytes; char *lf; char *virtual_0x00 = &mmap_addr[size_of_file]; /* EOF reached? */ if (!cur_offset) return NULL; /* determine length of current line */ lf = (char *)memchr(cur_offset, '\n', (virtual_0x00 - cur_offset)); if (lf) n_bytes = lf - cur_offset; else n_bytes = virtual_0x00 - cur_offset; /* allocate memory & copy string */ out = (char *)malloc(n_bytes + 1); if (!out) { fprintf(stderr, "buffered_reader::read_line: malloc(%lld) failed\n", n_bytes + 1); syslog(LOG_EMERG, "buffered_reader::read_line: malloc(%lld) failed", n_bytes + 1); exit(1); } memcpy(out, cur_offset, n_bytes); out[n_bytes] = 0x00; if (lf) cur_offset = lf + 1; else cur_offset = NULL; } else #endif { long long int lf_offset = -1; long long int n_bytes, search_start; if (number_of_bytes_in_buffer() <= 0) { garbage_collect(); if (read_into_buffer() == 0) { // EOF return NULL; } } search_start = buffer_pointer; for(;;) { char *dummy = strchr(&buffer[buffer_pointer], '\n'); if (dummy) lf_offset = (long long int)(dummy - buffer); if (lf_offset != -1) break; if (read_into_buffer() == 0) { lf_offset = buffer_length; break; } } n_bytes = lf_offset - buffer_pointer; out = strndup(&buffer[buffer_pointer], n_bytes); if (!out) { fprintf(stderr, "buffered_reader::read_line: malloc(%lld) failed\n", n_bytes + 1); syslog(LOG_EMERG, "buffered_reader::read_line: malloc(%lld) failed", n_bytes + 1); exit(1); } buffer_pointer = lf_offset + 1; } return out; }
/***************************************************************************** *Name : *Description : *Parameter : *Return : *Note : *****************************************************************************/ int nftl_test_thread(void *arg) { struct _nftl_blk *nftl_blk = arg; unsigned long time; nftl_blk->time_flush = NFTL_FLUSH_DATA_TIME * HZ; //nftl_blk->time_flush = 30 * HZ; nftl_blk->time_flush = HZ; while (!kthread_should_stop()) { mutex_lock(nftl_blk->blk_lock); if(nftl_get_zone_write_cache_nums(nftl_blk->nftl_zone) > 32){ time = jiffies; //if (time_after(time,nftl_blk->time + nftl_blk->time_flush)){ nftl_blk->flush_write_cache(nftl_blk,8); //} } // else if(nftl_get_zone_write_cache_nums(nftl_blk->nftl_zone) > 200) // { // time = jiffies; // if (time_after(time,nftl_blk->time + nftl_blk->time_flush)){ // nftl_blk->flush_write_cache(nftl_blk,6); // } // } // else if(nftl_get_zone_write_cache_nums(nftl_blk->nftl_zone) > 100) // { // time = jiffies; // if (time_after(time,nftl_blk->time + nftl_blk->time_flush)){ // nftl_blk->flush_write_cache(nftl_blk,8); // } // } else { time = jiffies; if (time_after(time,nftl_blk->time + nftl_blk->time_flush + HZ)){ nftl_blk->flush_write_cache(nftl_blk,2); } } //#if SUPPORT_WEAR_LEVELING // if(do_static_wear_leveling(nftl_blk->nftl_zone) != 0){ // printk("nftl_thread do_static_wear_leveling error!\n"); // } //#endif if(garbage_collect(nftl_blk->nftl_zone) != 0){ printk("nftl_thread garbage_collect error!\n"); } if(do_prio_gc(nftl_blk->nftl_zone) != 0){ printk("nftl_thread do_prio_gc error!\n"); } mutex_unlock(nftl_blk->blk_lock); set_current_state(TASK_INTERRUPTIBLE); schedule_timeout(NFTL_SCHEDULE_TIMEOUT); } nftl_blk->nftl_thread = (void*)NULL; return 0; }
void rb_gc(void) { garbage_collect(); }
/** * NOTE: The technique is not the same as that used in TinyVM. * The return value indicates the impact of the call on the VM * system. EXEC_CONTINUE normal return the system should return to the return * address provided by the VM. EXEC_RUN The call has modified the value of * VM PC and this should be used to restart execution. EXEC_RETRY The call * needs to be re-tried (typically for a GC failure), all global state * should be left intact, the PC has been set appropriately. * */ int dispatch_native(TWOBYTES signature, STACKWORD * paramBase) { STACKWORD p0 = paramBase[0]; switch (signature) { case wait_4_5V: return monitor_wait((Object *) word2ptr(p0), 0); case wait_4J_5V: return monitor_wait((Object *) word2ptr(p0), ((int)paramBase[1] > 0 ? 0x7fffffff : paramBase[2])); case notify_4_5V: return monitor_notify((Object *) word2ptr(p0), false); case notifyAll_4_5V: return monitor_notify((Object *) word2ptr(p0), true); case start_4_5V: // Create thread, allow for instruction restart return init_thread((Thread *) word2ptr(p0)); case yield_4_5V: schedule_request(REQUEST_SWITCH_THREAD); break; case sleep_4J_5V: sleep_thread(((int)p0 > 0 ? 0x7fffffff : paramBase[1])); schedule_request(REQUEST_SWITCH_THREAD); break; case getPriority_4_5I: push_word(get_thread_priority((Thread *) word2ptr(p0))); break; case setPriority_4I_5V: { STACKWORD p = (STACKWORD) paramBase[1]; if (p > MAX_PRIORITY || p < MIN_PRIORITY) return throw_new_exception(JAVA_LANG_ILLEGALARGUMENTEXCEPTION); else set_thread_priority((Thread *) word2ptr(p0), p); } break; case currentThread_4_5Ljava_3lang_3Thread_2: push_ref(ptr2ref(currentThread)); break; case interrupt_4_5V: interrupt_thread((Thread *) word2ptr(p0)); break; case interrupted_4_5Z: { JBYTE i = currentThread->interruptState != INTERRUPT_CLEARED; currentThread->interruptState = INTERRUPT_CLEARED; push_word(i); } break; case isInterrupted_4_5Z: push_word(((Thread *) word2ptr(p0))->interruptState != INTERRUPT_CLEARED); break; case join_4_5V: join_thread((Thread *) word2ptr(p0), 0); break; case join_4J_5V: join_thread((Thread *) word2obj(p0), paramBase[2]); break; case halt_4I_5V: schedule_request(REQUEST_EXIT); break; case shutdown_4_5V: shutdown_program(false); break; case currentTimeMillis_4_5J: push_word(0); push_word(systick_get_ms()); break; case readSensorValue_4I_5I: push_word(sp_read(p0, SP_ANA)); break; case setPowerTypeById_4II_5V: sp_set_power(p0, paramBase[1]); break; case freeMemory_4_5J: push_word(0); push_word(getHeapFree()); break; case totalMemory_4_5J: push_word(0); push_word(getHeapSize()); break; case floatToRawIntBits_4F_5I: // Fall through case intBitsToFloat_4I_5F: push_word(p0); break; case doubleToRawLongBits_4D_5J: // Fall through case longBitsToDouble_4J_5D: push_word(p0); push_word(paramBase[1]); break; case drawString_4Ljava_3lang_3String_2II_5V: { String *p = (String *)word2obj(p0); Object *charArray; if (!p) return throw_new_exception(JAVA_LANG_NULLPOINTEREXCEPTION); charArray = (Object *) word2ptr(get_word_4_ns(fields_start(p))); if (!charArray) return throw_new_exception(JAVA_LANG_NULLPOINTEREXCEPTION); display_goto_xy(paramBase[1], paramBase[2]); display_jstring(p); } break; case drawInt_4III_5V: display_goto_xy(paramBase[1], paramBase[2]); display_int(p0, 0); break; case drawInt_4IIII_5V: display_goto_xy(paramBase[2], paramBase[3]); display_int(p0, paramBase[1]); break; case asyncRefresh_4_5V: display_update(); break; case clear_4_5V: display_clear(0); break; case getDisplay_4_5_1B: push_word(display_get_array()); break; case setAutoRefreshPeriod_4I_5I: push_word(display_set_auto_update_period(p0)); break; case getRefreshCompleteTime_4_5I: push_word(display_get_update_complete_time()); break; case bitBlt_4_1BIIII_1BIIIIIII_5V: { Object *src = word2ptr(p0); Object *dst = word2ptr(paramBase[5]); display_bitblt((byte *)(src != NULL ?jbyte_array(src):NULL), paramBase[1], paramBase[2], paramBase[3], paramBase[4], (byte *)(dst!=NULL?jbyte_array(dst):NULL), paramBase[6], paramBase[7], paramBase[8], paramBase[9], paramBase[10], paramBase[11], paramBase[12]); break; } case getSystemFont_4_5_1B: push_word(display_get_font()); break; case setContrast_4I_5V: nxt_lcd_set_pot(p0); break; case getBatteryStatus_4_5I: push_word(battery_voltage()); break; case getButtons_4_5I: push_word(buttons_get()); break; case getTachoCountById_4I_5I: push_word(nxt_motor_get_count(p0)); break; case controlMotorById_4III_5V: nxt_motor_set_speed(p0, paramBase[1], paramBase[2]); break; case resetTachoCountById_4I_5V: nxt_motor_set_count(p0, 0); break; case i2cEnableById_4II_5V: if (i2c_enable(p0, paramBase[1]) == 0) return EXEC_RETRY; else break; case i2cDisableById_4I_5V: i2c_disable(p0); break; case i2cStatusById_4I_5I: push_word(i2c_status(p0)); break; case i2cStartById_4II_1BIII_5I: { Object *p = word2obj(paramBase[2]); JBYTE *byteArray = p ? jbyte_array(p) + paramBase[3] : NULL; push_word(i2c_start(p0, paramBase[1], (U8 *)byteArray, paramBase[4], paramBase[5])); } break; case i2cCompleteById_4I_1BII_5I: { Object *p = word2ptr(paramBase[1]); JBYTE *byteArray = p ? jbyte_array(p) + paramBase[2] : NULL; push_word(i2c_complete(p0, (U8 *)byteArray, paramBase[3])); } break; case playFreq_4III_5V: sound_freq(p0,paramBase[1], paramBase[2]); break; case btGetBC4CmdMode_4_5I: push_word(bt_get_mode()); break; case btSetArmCmdMode_4I_5V: if (p0 == 0) bt_set_arm7_cmd(); else bt_clear_arm7_cmd(); break; case btSetResetLow_4_5V: bt_set_reset_low(); break; case btSetResetHigh_4_5V: bt_set_reset_high(); break; case btWrite_4_1BII_5I: { Object *p = word2ptr(p0); byte *byteArray = (byte *) jbyte_array(p); push_word(bt_write(byteArray, paramBase[1], paramBase[2])); } break; case btRead_4_1BII_5I: { Object *p = word2ptr(p0); byte *byteArray = (byte *) jbyte_array(p); push_word(bt_read(byteArray, paramBase[1], paramBase[2])); } break; case btPending_4_5I: { push_word(bt_event_check(0xffffffff)); } break; case btEnable_4_5V: if (bt_enable() == 0) return EXEC_RETRY; else break; case btDisable_4_5V: bt_disable(); break; case usbRead_4_1BII_5I: { Object *p = word2ptr(p0); byte *byteArray = (byte *) jbyte_array(p); push_word(udp_read(byteArray,paramBase[1], paramBase[2])); } break; case usbWrite_4_1BII_5I: { Object *p = word2ptr(p0); byte *byteArray = (byte *) jbyte_array(p); push_word(udp_write(byteArray,paramBase[1], paramBase[2])); } break; case usbStatus_4_5I: { push_word(udp_event_check(0xffffffff)); } break; case usbEnable_4I_5V: { udp_enable(p0); } break; case usbDisable_4_5V: { udp_disable(); } break; case usbReset_4_5V: udp_reset(); break; case usbSetSerialNo_4Ljava_3lang_3String_2_5V: { byte *p = word2ptr(p0); int len; Object *charArray = (Object *) word2ptr(get_word_4_ns(fields_start(p))); len = get_array_length(charArray); udp_set_serialno((U8 *)jchar_array(charArray), len); } break; case usbSetName_4Ljava_3lang_3String_2_5V: { byte *p = word2ptr(p0); int len; Object *charArray = (Object *) word2ptr(get_word_4_ns(fields_start(p))); len = get_array_length(charArray); udp_set_name((U8 *)jchar_array(charArray), len); } break; case flashWritePage_4_1BI_5I: { Object *p = word2ptr(p0); unsigned long *intArray = (unsigned long *) jint_array(p); push_word(flash_write_page(intArray,paramBase[1])); } break; case flashReadPage_4_1BI_5I: { Object *p = word2ptr(p0); unsigned long *intArray = (unsigned long *) jint_array(p); push_word(flash_read_page(intArray,paramBase[1])); } break; case flashExec_4II_5I: push_word(run_program((byte *)(&FLASH_BASE[(p0*FLASH_PAGE_SIZE)]), paramBase[1])); break; case playSample_4IIIII_5V: sound_play_sample(((unsigned char *) &FLASH_BASE[(p0*FLASH_PAGE_SIZE)]) + paramBase[1],paramBase[2],paramBase[3],paramBase[4]); break; case playQueuedSample_4_1BIIII_5I: push_word(sound_add_sample((U8 *)jbyte_array(word2obj(p0)) + paramBase[1],paramBase[2],paramBase[3],paramBase[4])); break; case getTime_4_5I: push_word(sound_get_time()); break; case getDataAddress_4Ljava_3lang_3Object_2_5I: if (is_array(word2obj(p0))) push_word (ptr2word ((byte *) array_start(word2ptr(p0)))); else push_word (ptr2word ((byte *) fields_start(word2ptr(p0)))); break; case getObjectAddress_4Ljava_3lang_3Object_2_5I: push_word(p0); break; case gc_4_5V: // Restartable garbage collection return garbage_collect(); case shutDown_4_5V: shutdown(); // does not return case boot_4_5V: display_clear(1); while (1) nxt_avr_firmware_update_mode(); // does not return case arraycopy_4Ljava_3lang_3Object_2ILjava_3lang_3Object_2II_5V: return arraycopy(word2ptr(p0), paramBase[1], word2ptr(paramBase[2]), paramBase[3], paramBase[4]); case executeProgram_4I_5V: // Exceute program, allow for instruction re-start return execute_program(p0); case setDebug_4_5V: set_debug(word2ptr(p0)); break; case eventOptions_4II_5I: { byte old = debugEventOptions[p0]; debugEventOptions[p0] = (byte)paramBase[1]; push_word(old); } break; case suspendThread_4Ljava_3lang_3Object_2_5V: suspend_thread(ref2ptr(p0)); break; case resumeThread_4Ljava_3lang_3Object_2_5V: resume_thread(ref2ptr(p0)); break; case getProgramExecutionsCount_4_5I: push_word(gProgramExecutions); break; case getFirmwareRevision_4_5I: push_word((STACKWORD) getRevision()); break; case getFirmwareRawVersion_4_5I: push_word((STACKWORD) VERSION_NUMBER); break; case hsEnable_4II_5V: { if (hs_enable((int)p0, (int)paramBase[1]) == 0) return EXEC_RETRY; } break; case hsDisable_4_5V: { hs_disable(); } break; case hsWrite_4_1BII_5I: { Object *p = word2ptr(p0); byte *byteArray = (byte *) jbyte_array(p); push_word(hs_write(byteArray, paramBase[1], paramBase[2])); } break; case hsRead_4_1BII_5I: { Object *p = word2ptr(p0); byte *byteArray = (byte *) jbyte_array(p); push_word(hs_read(byteArray, paramBase[1], paramBase[2])); } break; case hsPending_4_5I: { push_word(hs_pending()); } break; case hsSend_4BB_1BII_1C_5I: { Object *p = word2ptr(paramBase[2]); U8 *data = (U8 *)jbyte_array(p); p = word2ptr(paramBase[5]); U16 *crc = (U16 *)jchar_array(p); push_word(hs_send((U8) p0, (U8)paramBase[1], data, paramBase[3], paramBase[4], crc)); } break; case hsRecv_4_1BI_1CI_5I: { Object *p = word2ptr(p0); U8 *data = (U8 *)jbyte_array(p); p = word2ptr(paramBase[2]); U16 *crc = (U16 *)jchar_array(p); push_word(hs_recv(data, paramBase[1], crc, paramBase[3])); } break; case getUserPages_4_5I: push_word(FLASH_MAX_PAGES - flash_start_page); break; case setVMOptions_4I_5V: gVMOptions = p0; break; case getVMOptions_4_5I: push_word(gVMOptions); break; case isAssignable_4II_5Z: push_word(is_assignable(p0, paramBase[1])); break; case cloneObject_4Ljava_3lang_3Object_2_5Ljava_3lang_3Object_2: { Object *newObj = clone((Object *)ref2obj(p0)); if (newObj == NULL) return EXEC_RETRY; push_word(obj2ref(newObj)); } break; case memPeek_4III_5I: push_word(mem_peek(p0, paramBase[1], paramBase[2])); break; case memCopy_4Ljava_3lang_3Object_2IIII_5V: mem_copy(word2ptr(p0), paramBase[1], paramBase[2], paramBase[3], paramBase[4]); break; case memGetReference_4II_5Ljava_3lang_3Object_2: push_word(mem_get_reference(p0, paramBase[1])); break; case setSensorPin_4III_5V: sp_set(p0, paramBase[1], paramBase[2]); break; case getSensorPin_4II_5I: push_word(sp_get(p0, paramBase[1])); break; case setSensorPinMode_4III_5V: sp_set_mode(p0, paramBase[1], paramBase[2]); break; case readSensorPin_4II_5I: push_word(sp_read(p0, paramBase[1])); break; case nanoTime_4_5J: { U64 ns = systick_get_ns(); push_word(ns >> 32); push_word(ns); } break; case createStackTrace_4Ljava_3lang_3Thread_2Ljava_3lang_3Object_2_5_1I: { Object *trace = create_stack_trace((Thread *)ref2obj(p0), ref2obj(paramBase[1])); if (trace == NULL) return EXEC_RETRY; push_word(obj2ref(trace)); } break; case registerEvent_4_5I: push_word(register_event((NXTEvent *) ref2obj(p0))); break; case unregisterEvent_4_5I: push_word(unregister_event((NXTEvent *) ref2obj(p0))); break; case changeEvent_4II_5I: push_word(change_event((NXTEvent *) ref2obj(p0), paramBase[1], paramBase[2])); break; case isInitialized_4I_5Z: push_word(is_initialized_idx(p0)); break; case allocate_4II_5Ljava_3lang_3Object_2: { Object *allocated; if(paramBase[1]>0){ allocated=new_single_array(p0,paramBase[1]); }else{ allocated=new_object_for_class(p0); } if(allocated == NULL) return EXEC_RETRY; push_word(obj2ref(allocated)); } break; case memPut_4IIII_5V: store_word_ns((byte *)(memory_base[p0] + paramBase[1]), paramBase[2],paramBase[3]); break; case notifyEvent_4ILjava_3lang_3Thread_2_5Z: push_word(debug_event(paramBase[1], NULL, (Thread*) ref2obj(paramBase[2]), 0, 0, 0, 0)); break; case setThreadRequest_4Ljava_3lang_3Thread_2Llejos_3nxt_3debug_3SteppingRequest_2_5V: { Thread *th = (Thread*) ref2obj(p0); th->debugData = (REFERENCE) paramBase[1]; // currently we only get stepping requests if(paramBase[1]) th->flags |= THREAD_STEPPING; else th->flags &= ~THREAD_STEPPING; } break; case isStepping_4Ljava_3lang_3Thread_2_5Z: { Thread *th = (Thread*) ref2obj(p0); push_word(is_stepping(th)); } break; case setBreakpointList_4_1Llejos_3nxt_3debug_3Breakpoint_2I_5V: breakpoint_set_list((Breakpoint**) array_start(p0), paramBase[1]); break; case enableBreakpoint_4Llejos_3nxt_3debug_3Breakpoint_2Z_5V: breakpoint_enable((Breakpoint*) word2ptr(p0), (boolean) paramBase[1]); break; case firmwareExceptionHandler_4Ljava_3lang_3Throwable_2II_5V: firmware_exception_handler((Throwable *)p0, paramBase[1], paramBase[2]); break; case exitThread_4_5V: currentThread->state = DEAD; schedule_request(REQUEST_SWITCH_THREAD); break; case updateThreadFlags_4Ljava_3lang_3Thread_2II_5I: ((Thread *)p0)->flags |= paramBase[1]; ((Thread *)p0)->flags &= ~paramBase[2]; //printf("m %x %d\n", p0, ((Thread *)p0)->flags); push_word(((Thread *)p0)->flags); break; default: return throw_new_exception(JAVA_LANG_NOSUCHMETHODERROR); } return EXEC_CONTINUE; }
int mainloop(int toplevel) { static int socket_cleanup = 0; int xx, i, eggbusy = 1, tclbusy = 0; char buf[520]; /* Lets move some of this here, reducing the numer of actual * calls to periodic_timers */ now = time(NULL); /* * FIXME: Get rid of this, it's ugly and wastes lots of cpu. * * pre-1.3.0 Eggdrop had random() in the once a second block below. * * This attempts to keep random() more random by constantly * calling random() and updating the state information. */ random(); /* Woop, lets really jumble things */ /* If we want to restart, we have to unwind to the toplevel. * Tcl will Panic if we kill the interp with Tcl_Eval in progress. * This is done by returning -1 in tickle_WaitForEvent. */ if (do_restart && do_restart != -2 && !toplevel) return -1; /* Once a second */ if (now != then) { call_hook(HOOK_SECONDLY); then = now; } /* Only do this every so often. */ if (!socket_cleanup) { socket_cleanup = 5; /* Remove dead dcc entries. */ dcc_remove_lost(); /* Check for server or dcc activity. */ dequeue_sockets(); } else socket_cleanup--; /* Free unused structures. */ garbage_collect(); xx = sockgets(buf, &i); if (xx >= 0) { /* Non-error */ int idx; for (idx = 0; idx < dcc_total; idx++) if (dcc[idx].sock == xx) { if (dcc[idx].type && dcc[idx].type->activity) { /* Traffic stats */ if (dcc[idx].type->name) { if (!strncmp(dcc[idx].type->name, "BOT", 3)) itraffic_bn_today += strlen(buf) + 1; else if (!strcmp(dcc[idx].type->name, "SERVER")) itraffic_irc_today += strlen(buf) + 1; else if (!strncmp(dcc[idx].type->name, "CHAT", 4)) itraffic_dcc_today += strlen(buf) + 1; else if (!strncmp(dcc[idx].type->name, "FILES", 5)) itraffic_dcc_today += strlen(buf) + 1; else if (!strcmp(dcc[idx].type->name, "SEND")) itraffic_trans_today += strlen(buf) + 1; else if (!strncmp(dcc[idx].type->name, "GET", 3)) itraffic_trans_today += strlen(buf) + 1; else itraffic_unknown_today += strlen(buf) + 1; } dcc[idx].type->activity(idx, buf, i); } else putlog(LOG_MISC, "*", "!!! untrapped dcc activity: type %s, sock %d", dcc[idx].type->name, dcc[idx].sock); break; } } else if (xx == -1) { /* EOF from someone */ int idx; if (i == STDOUT && !backgrd) fatal("END OF FILE ON TERMINAL", 0); for (idx = 0; idx < dcc_total; idx++) if (dcc[idx].sock == i) { if (dcc[idx].type && dcc[idx].type->eof) dcc[idx].type->eof(idx); else { putlog(LOG_MISC, "*", "*** ATTENTION: DEAD SOCKET (%d) OF TYPE %s UNTRAPPED", i, dcc[idx].type ? dcc[idx].type->name : "*UNKNOWN*"); killsock(i); lostdcc(idx); } idx = dcc_total + 1; } if (idx == dcc_total) { putlog(LOG_MISC, "*", "(@) EOF socket %d, not a dcc socket, not anything.", i); close(i); killsock(i); } } else if (xx == -2 && errno != EINTR) { /* select() error */ putlog(LOG_MISC, "*", "* Socket error #%d; recovering.", errno); for (i = 0; i < dcc_total; i++) { if ((fcntl(dcc[i].sock, F_GETFD, 0) == -1) && (errno == EBADF)) { putlog(LOG_MISC, "*", "DCC socket %d (type %d, name '%s') expired -- pfft", dcc[i].sock, dcc[i].type, dcc[i].nick); killsock(dcc[i].sock); lostdcc(i); i--; } } } else if (xx == -3) { call_hook(HOOK_IDLE); socket_cleanup = 0; /* If we've been idle, cleanup & flush */ eggbusy = 0; } else if (xx == -5) { eggbusy = 0; tclbusy = 1; } if (do_restart) { if (do_restart == -2) rehash(); else if (!toplevel) return -1; /* Unwind to toplevel before restarting */ else { /* Unload as many modules as possible */ int f = 1; module_entry *p; Function startfunc; char name[256]; /* oops, I guess we should call this event before tcl is restarted */ check_tcl_event("prerestart"); while (f) { f = 0; for (p = module_list; p != NULL; p = p->next) { dependancy *d = dependancy_list; int ok = 1; while (ok && d) { if (d->needed == p) ok = 0; d = d->next; } if (ok) { strcpy(name, p->name); if (module_unload(name, botnetnick) == NULL) { f = 1; break; } } } } /* Make sure we don't have any modules left hanging around other than * "eggdrop" and the two that are supposed to be. */ for (f = 0, p = module_list; p; p = p->next) { if (strcmp(p->name, "eggdrop") && strcmp(p->name, "encryption") && strcmp(p->name, "uptime")) { f++; } } if (f != 0) { putlog(LOG_MISC, "*", MOD_STAGNANT); } flushlogs(); kill_tcl(); init_tcl(argc, argv); init_language(0); /* this resets our modules which we didn't unload (encryption and uptime) */ for (p = module_list; p; p = p->next) { if (p->funcs) { startfunc = p->funcs[MODCALL_START]; startfunc(NULL); } } rehash(); #ifdef TLS ssl_cleanup(); ssl_init(); #endif restart_chons(); call_hook(HOOK_LOADED); } eggbusy = 1; do_restart = 0; } #ifdef USE_TCL_EVENTS if (!eggbusy) { /* Process all pending tcl events */ # ifdef REPLACE_NOTIFIER if (Tcl_ServiceAll()) tclbusy = 1; # else while (Tcl_DoOneEvent(TCL_DONT_WAIT | TCL_ALL_EVENTS)) tclbusy = 1; # endif /* REPLACE_NOTIFIER */ #endif /* USE_TCL_EVENTS */ } return (eggbusy || tclbusy); }