int memp_init(void) { if (init_mempool(&pbuf_ds, PBUF_CAPACITY, memp_sizes[MEMP_PBUF],"pbuf")) return 1; if (init_mempool(&pbuf_with_payload_ds, MEMP_SIZE, PBUF_WITH_PAYLOAD_SIZE,"pbuf_payload")) return 1; if (init_mempool(&tcp_pcb_ds, MEMP_SIZE, memp_sizes[MEMP_TCP_PCB],"tcp_pcb")) return 1; if (init_mempool(&tcp_seg_ds, MEMP_SIZE, memp_sizes[MEMP_TCP_SEG],"tcp_seg")) return 1; return 0; }
int main(void) { /*********************************************** * initialisation * ***********************************************/ /* Initialise memory heap */ #ifdef __C51__ // Detect KEIL 8051 compiler init_mempool((void xdata *) 0x0400, 0x03FF); #endif dd_initialise(); // see dd_uc845 INTERRUPTS_ON; /************************************************ * Write to ADF7020 Registers * * * * The following code is example code that * * sets the ADF7020 into transmit mode using * * the default settings of config.c * ***********************************************/ config_use_defaults(); //Use standard register configuration as defined in config.c dd_adf7020_chip_sel(TRUE); //ADF7020 CE high dd_short_delay(2*DELAY_1mS); //2ms delay, not strictly required dd_write_7020_reg(&config.r0_tx.byte[0]); //Write to register 0 using value defined in config.c dd_write_7020_reg(&config.r1_rx.byte[0]); //Write to register 1 using value defined in config.c dd_write_7020_reg(&config.r2_tx.byte[0]); //Write to register 2 using value defined in config.c DEBUG_LED = 0; // see device_drivers.h RED_LED = 0; // see device_drivers.h while(1) //Never ending while loop { } return 0; //Should never get here }
void mpc_init(void) { if (!mpc_monostate.initialized) { mpc_monostate.mempool = init_mempool(MPC_PKT_MAX_SIZE, MPC_QUEUE_SIZE); mpc_monostate.rx_queue = xQueueCreate(MPC_QUEUE_SIZE, sizeof(struct mpc_rx_data)); for (uint8_t i = 0; i < MPC_CMD_MAX; ++i) { // Just in case anything calls mpc_register before mpc_init() if (cmds[i] == NULL) cmds[i] = mpc_nop; } mpc_register_drivers(); xTaskCreate(mpc_task, "mpc", 128, NULL, tskIDLE_PRIORITY + 5, NULL); mpc_monostate.initialized = true; } }
int main(int ac,char **av) { int mempoolsize=1024*1024; SE *code; int c; if(ac>1) { while((c=getopt(ac,av,"vh?p:"))!=-1) { switch(c) { case 'v': versioninfo(stdout,av[0]); return 0; case '?': case 'h': versioninfo(stdout,av[0]); helpinfo(stdout,av[0]); return 0; case 'p': mempoolsize=atoi(optarg); break; default: helpinfo(stdout,av[0]); return 1; } } } init_mempool(mempoolsize); init_symbols_list(); gather_symbols((code=read_se(stdin))); assemble(code,stdout,1); printf("\n"); return 0; }
int main(int ac, char **av) { FILE *codefile=stdin; int mempoolsize=1024*1024; int c; SE *prog,*result; silent=0; if(ac<2) { versioninfo(stderr,av[0]); helpinfo(stderr,av[0]); return 1; } while((c=getopt(ac,av,"svch?p:"))!=-1) { switch(c) { case 's': silent=1; break; case 'c': conscellinfo(stdout,av[0]); return 0; case 'v': versioninfo(stdout,av[0]); return 0; case '?': case 'h': versioninfo(stdout,av[0]); helpinfo(stdout,av[0]); return 0; case 'p': mempoolsize=atoi(optarg); break; default: // versioninfo(stderr,av[0]); // helpinfo(stdout,av[0]); return 1; } } if((codefile=fopen(av[optind],"r"))) { if(silent!=1) printf("Initializing mempool (%d cells)...\n",mempoolsize); init_mempool(mempoolsize); if(silent!=1) printf("Loading code from %s...\n",av[optind]); prog=read_se(codefile); if(silent!=1) printf("Allocating %d env-slot(s) for environments...\n",numval(car(prog))); Dreg=(SE **)malloc(sizeof(SE *)*numval(car(prog))); Rreg=NIL; Creg=NIL; C_push(cdr(prog)); if(silent!=1) printf("Running the machine.\n"); result=run(); if(silent!=1) printf("Result: "); write_se(result,stdout); printf("\n"); // ? if(silent!=1) printf("\nAuf wiedersehen!\n"); return 0; } else fprintf(stderr,"Could not open file %s.\n",av[optind]); return 1; }
/* * Create variable size memory pool */ SYSCALL ID _tk_cre_mpl( CONST T_CMPL *pk_cmpl ) { #if CHK_RSATR const ATR VALID_MPLATR = { TA_TPRI |TA_RNG3 |TA_NODISWAI #if USE_OBJECT_NAME |TA_DSNAME #endif }; #endif MPLCB *mplcb; ID mplid; INT mplsz; void *mempool; ER ercd; CHECK_RSATR(pk_cmpl->mplatr, VALID_MPLATR); CHECK_PAR(pk_cmpl->mplsz > 0 && pk_cmpl->mplsz <= MAX_ALLOCATE); CHECK_DISPATCH(); mplsz = roundSize(pk_cmpl->mplsz); /* Allocate memory for memory pool */ mempool = IAmalloc((UINT)mplsz + sizeof(QUEUE)*2, pk_cmpl->mplatr); if ( mempool == NULL ) { return E_NOMEM; } BEGIN_CRITICAL_SECTION; /* Get control block from FreeQue */ mplcb = (MPLCB*)QueRemoveNext(&free_mplcb); if ( mplcb == NULL ) { ercd = E_LIMIT; } else { mplid = ID_MPL(mplcb - mplcb_table); /* Initialize control block */ QueInit(&mplcb->wait_queue); mplcb->mplid = mplid; mplcb->exinf = pk_cmpl->exinf; mplcb->mplatr = pk_cmpl->mplatr; mplcb->mplsz = mplsz; #if USE_OBJECT_NAME if ( (pk_cmpl->mplatr & TA_DSNAME) != 0 ) { strncpy((char*)mplcb->name, (char*)pk_cmpl->dsname, OBJECT_NAME_LENGTH); } #endif /* Initialize memory pool */ init_mempool(mplcb, mempool, mplsz + (INT)sizeof(QUEUE)*2); ercd = mplid; } END_CRITICAL_SECTION; if ( ercd < E_OK ) { IAfree(mempool, pk_cmpl->mplatr); } return ercd; }
int start_server(int port_number, const char* dir_name, int period) { pthread_t tid; /* Passed to pthread_create */ struct thread_arg* targ; /* Used to pass arguments to threads */ pthread_attr_t tattr; /* Specifies that thread should be detached */ fd_set master; /* Keep track of all connections / pipes to multiplex */ fd_set read_fds; /* Copy of master for select to populate */ int fdmax; /* Highest numbered file descriptor */ int i; /* Used to index the master fd list */ int listener; /* Listening socket of the server */ int newfd; /* New connection socket fd */ struct sockaddr_in local_addr; /* Local connection info */ struct sockaddr_in remote_addr; /* Remote connection info */ socklen_t addr_len; /* Address length */ int pipe_buff[1]; /* Get sockets into here */ pipe_buff[0] = 0; // Init signal mask struct sigaction sa; sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = SIG_IGN; // Start each thread in detached mode, since we don't care about // their return values pthread_attr_init(&tattr); pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); // Initialize the memory pool to store the direntries direntry_pool = init_mempool(sizeof(struct direntry), 512); // Set done to 0 done = 0; // Initialize the clients linked list clients = (struct clientlist*)malloc(sizeof(struct clientlist)); clients->head = NULL; clients->tail = NULL; clients->count = 0; // Copy into global init_dir strcpy(init_dir, dir_name); gperiod = period; // Initialize pipe if (pipe(remove_client_pipes) < 0) { syslog(LOG_ERR, "Cannot create IPC in server."); exit(1); } // Get full path of the directory if (realpath(dir_name, full_path) == NULL) { syslog(LOG_ERR, "Cannot resolve full path."); exit(1); } #ifdef DAEMONIZE create_daemon("dirapp"); #endif #ifndef DAEMONIZE openlog("dirapp", LOG_CONS, LOG_DAEMON); #endif // Make sure SIGPIPE is blocked if (sigaction(SIGPIPE, &sa, NULL) < 0) { syslog(LOG_WARNING, "SIGPIPE error"); } // Signals for the signal thread to handle sigemptyset(&mask); sigaddset(&mask, SIGHUP); sigaddset(&mask, SIGTERM); sigaddset(&mask, SIGINT); sigaddset(&mask, SIGALRM); // Set the mask if (pthread_sigmask(SIG_BLOCK, &mask, NULL) != 0) syslog(LOG_WARNING, "pthread_sigmask failed"); // Initialize file descriptor lists FD_ZERO(&master); FD_ZERO(&read_fds); // Setup local connection info memset(&local_addr, 0, sizeof(local_addr)); local_addr.sin_family = AF_INET; local_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); local_addr.sin_port = htons(port_number); // Create listener socket listener = socket(AF_INET, SOCK_STREAM, 0); // Try to bind if (bind(listener, (struct sockaddr*)&local_addr, sizeof(local_addr))) { syslog(LOG_ERR, "Cannot bind socket to address"); exit(1); } // Now listen! if (listen(listener, MAX_CLIENTS) < 0) { syslog(LOG_ERR, "Cannot listen on socket"); exit(1); } syslog(LOG_INFO, "Starting server!"); // Have select check for incoming data FD_SET(remove_client_pipes[0], &master); FD_SET(listener, &master); fdmax = listener; // Initialize the direntry lists prevdir = init_direntrylist(); curdir = init_direntrylist(); // Initially populate list of file entries in monitored directory exploredir(prevdir, (const char*)full_path); // Start signal thread pthread_create(&tid, NULL, signal_thread, NULL); int errno = 0; // Main server loop while (1) { read_fds = master; if (select(fdmax + 1, &read_fds, NULL, NULL, NULL) == -1) { syslog(LOG_ERR, "select: %s", strerror(errno)); exit(1); } for (i = 0; i <= fdmax; i++) { if (FD_ISSET(i, &read_fds)) { if (i == listener) { addr_len = sizeof(remote_addr); newfd = accept(listener, (struct sockaddr*)&remote_addr, &addr_len); if (newfd == -1) { syslog(LOG_WARNING, "Cannot new client."); } else { FD_SET(newfd, &master); if (newfd > fdmax) { fdmax = newfd; } syslog(LOG_INFO, "New connection from: %s:%d", inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port)); // Add client to clients list in order to receive // updates pthread_create(&tid, &tattr, init_client, (void*)newfd); } } else if (i == remove_client_pipes[0]) { // Get the file descriptor of the socket that is to // be removed and store into the first position of // tmp_buff if (read(remove_client_pipes[0], pipe_buff, 1) <= 0) { syslog(LOG_ERR, "Cannot read in socket to close."); } else { // Remove socket from master list pthread_mutex_lock(&slock); FD_CLR(pipe_buff[0], &master); done = 1; pthread_mutex_unlock(&slock); // Tell kill_clients or whoever they // can procede pthread_cond_signal(&sready); } } else { // Handle disconnect targ = (struct thread_arg*)malloc(sizeof(struct thread_arg)); targ->socket = i; targ->pipe = remove_client_pipes[1]; pthread_create(&tid, &tattr, remove_client, (void*)targ); FD_CLR(i, &master); } } } } return 0; }
int kernel_warmup_heap (void) { init_mempool(&heap_mempool, sizeof(heap_mempool)); return 0; }
int main(int argc, char **argv) { struct server *s = NULL; pthread_t pt, ctl; int c, is_forward = 0; const char *config = SR_CONFIG_FILE; int daemon = 0; while ((c = getopt(argc,argv,"c:vhfd")) != -1) { switch(c) { case 'c': config = optarg; break; case 'h': help(argv[0]); exit(0); break; case 'f': is_forward = 1; break; case 'd': daemon = 1; break; case '?': printf("Try -h please\n"); exit(0); break; case 'v': printf("dnspod-sr 0.01\n"); exit(0); break; default: exit(0); break; } } sanity_test(0); drop_privilege("./"); daemonrize(daemon); trig_signals(1); global_now = time(NULL); //for read root.z g_nameservers[0] = g_nameservers[1] = NULL; init_globe(); init_mempool(); s = server_init(); s->is_forward = is_forward; read_config(config, (char *)s->logpath, s->forward, g_nameservers); // add default dns server 8.8.8.8, 114.114.114.114 if (g_nameservers[0] == NULL) { assert(g_nameservers[1] == NULL); g_nameservers[0] = strdup("119.29.29.29"); g_nameservers[1] = strdup("8.8.4.4"); } if (g_nameservers[1] == NULL) { if (strcmp(g_nameservers[0], "119.29.29.29") == 0) { g_nameservers[1] = strdup("8.8.4.4"); } else { g_nameservers[1] = strdup("119.29.29.29"); } } // if (create_fetcher(s, s->nfetcher) < 0) dns_error(0, "create worker"); if (create_author(s, s->nquizzer) < 0) dns_error(0, "create author"); if (pthread_create(&pt, NULL, (void *) time_cron, s) != 0) dns_error(0, "time cron error"); if (pthread_create(&ctl, NULL, (void *)recv_update, s) != 0) { dns_error(0, "recv update thread error"); } read_root(s->datasets, s->ttlexp); print_basic_debug(); global_serv = s; run_sentinel(s); return 0; }