void u_init_main(void) { tid_t ns_tid, clk_tid, blnk_tid; //tid_t float_tid1, float_tid2, float_tid3; //tid_t hw_tid; //struct serialcfg ttycfg, traincfg; //int rplylen; /* Start the name server. It's important that startup proceeds so that * the TID of the name server can be known at compile time (NS_TID). * The priority strikes me as relatively unimportant - NS does some * work on startup and then probably never again. */ ns_tid = Create(PRIORITY_NS, &ns_main); assertv(ns_tid, ns_tid == NS_TID); /* Start clock server */ clk_tid = Create(PRIORITY_CLOCK, &clksrv_main); assertv(clk_tid, clk_tid >= 0); /* Start Blink Server */ blnk_tid = Create(PRIORITY_BLINK, &blnksrv_main); assertv(blnk_tid, blnk_tid >= 0); /* Floating Point Test Program */ /* float_tid1 = Create(5, &fpu_test_main); assertv(float_tid1, float_tid1 >= 0); float_tid2 = Create(5, &fpu_test_main); assertv(float_tid2, float_tid2 >= 0); float_tid3 = Create(5, &fpu_test_main); assertv(float_tid3, float_tid3 >= 0); */ /* Start serial server for TTY */ /* tty_tid = Create(PRIORITY_SERIAL2, &serialsrv_main); assertv(tty_tid, tty_tid >= 0); ttycfg = (struct serialcfg) { .uart = COM2, .fifos = true, .nocts = true, .baud = 115200, .parity = false, .parity_even = false, .stop2 = false, .bits = 8 }; rplylen = Send(tty_tid, &ttycfg, sizeof (ttycfg), NULL, 0); assertv(rplylen, rplylen == 0); */ }
void push_free(secd_t *secd, cell_t *c) { assertv(c, "push_free(NULL)"); assertv(c->nref == 0, "push_free: [%ld]->nref is %ld\n", cell_index(secd, c), (long)c->nref); assertv(c < secd->fixedptr, "push_free: Trying to free array cell"); if (c + 1 < secd->fixedptr) { /* just add the cell to the list secd->free */ c->type = CELL_FREE; c->as.cons.car = SECD_NIL; c->as.cons.cdr = secd->free; if (not_nil(secd->free)) secd->free->as.cons.car = c; secd->free = c; ++secd->free_cells; memdebugf("FREE[%ld], %zd free\n", cell_index(secd, c), secd->free_cells); } else { memdebugf("FREE[%ld] --\n", cell_index(secd, c)); --c; while (c->type == CELL_FREE) { /* it is a cell adjacent to the free space */ if (c != secd->free) { cell_t *prev = c->as.cons.car; cell_t *next = c->as.cons.cdr; if (not_nil(prev)) { prev->as.cons.cdr = next; } if (not_nil(next)) { next->as.cons.car = prev; } } else { cell_t *next = c->as.cons.cdr; if (not_nil(next)) next->as.cons.car = SECD_NIL; secd->free = next; } memdebugf("FREE[%ld] --\n", cell_index(secd, c)); --c; --secd->free_cells; } secd->fixedptr = c + 1; } }
float delay(int pos) { assertv(size >= pos); int pAl2 = (this->pointer + pos); pAl2 = pAl2%size; if( pAl2 < 0 ){ pAl2 += size;} return this->Buffer[pAl2]; }
void free_array(secd_t *secd, cell_t *mem) { assertv(mem <= secd->arrlist, "free_array: tried to free arrlist"); assertv(secd->arrayptr < mem, "free_array: not an array"); cell_t *meta = arr_meta(mem); cell_t *prev = mcons_prev(meta); assertv(meta->nref == 0, "free_array: someone seems to still use the array"); mark_free(meta, true); if (meta != secd->arrayptr) { if (is_array_free(secd, prev)) { /* merge with the previous array */ cell_t *pprev = prev->as.mcons.prev; pprev->as.mcons.next = meta; meta->as.mcons.prev = pprev; } cell_t *next = mcons_next(meta); if (is_array_free(secd, next)) { /* merge with the next array */ cell_t *newprev = meta->as.mcons.prev; next->as.mcons.prev = newprev; newprev->as.mcons.next = next; } mark_free(meta, true); } else { /* move arrayptr into the array area */ prev->as.mcons.next = SECD_NIL; secd->arrayptr = prev; if (is_array_free(secd, prev)) { /* at most one array after 'arr' may be free */ cell_t *pprev = prev->as.mcons.prev; pprev->as.mcons.next = SECD_NIL; secd->arrayptr = pprev; } } memdebugf("FREE ARR[%ld]", cell_index(secd, meta)); }
void dbg_print_list(secd_t *secd, cell_t *list) { printf(" -= "); while (not_nil(list)) { assertv(is_cons(list), "Not a cons at [%ld]\n", cell_index(secd, list)); printf("[%ld]:%ld\t", cell_index(secd, list), cell_index(secd, get_car(list))); dbg_print_cell(secd, get_car(list)); printf(" -> "); list = list_next(secd, list); } printf("NIL\n"); }
float delay(float pos) { assertv(size >= pos); if (pos != lastdelay){ ptL = CalcCoeffs(pos); lastdelay = pos; } //return this->Buffer[(this->pointer - (int)pos) & this->mask]; float sum = 0; for(int i=0; i < 4; i++){ sum += this->Buffer[(this->pointer + ptL + i) & this->mask]*h[i]; } //DUMPONNAN(sum); return sum; }
float Convol(T &buf) { int AlSize = buf.buffer_size; assertv(AlSize >= kernel_size); float sum=0.; //int pAl2 = (buf.pointer - kernel_size + 1); //pAl2 = pAl2%AlSize; //if( pAl2 < 0 ){ pAl2 += AlSize;} int pAl2 = buf.pointerInRange(buf.pointer + kernel_size - 1); int howmany = std::min(AlSize - pAl2,kernel_size); for(int i=0; i < howmany; i++) sum += Kernel[i]*buf.Buffer[pAl2 + i]; int howmany2 = kernel_size - howmany; for(int i=0; i < howmany2; i++) sum += Kernel[howmany + i]*buf.Buffer[i]; return sum; }
int main(int o_argc, const string const* o_argv) { MyData _data; int _shmd; size_t _off ; const string SEM_NAME = "/posix_sem"; const string SHM_NAME = "/posix_shm"; /* Nowaday, memory is so cheap that I just do not care about the unused memory. * If is it too waste, then just compile with DYN_SEG_SIZE switch. **/ #ifndef DYN_SEG_SIZE const off_t SEG_SIZE = sizeof(mydata_t); #else off_t _segSz = sizeof(size_t); for(size_t i = 1; i < o_argc; i++) { _segSz += strlen(o_argv[i]) + 1; } #endif /* DYN_SEG_SIZE */ /* open/create semaphore and lock */ sem_t* _sem = sem_open(SEM_NAME, O_CREAT, S_IRUSR | S_IWUSR, 1); assertv(_sem, SEM_FAILED); assertz( sem_wait(_sem) ); /* Critical section: */ /* there is no arguments so print shared memory object content */ if(o_argc == 1) { /* obtain the descriptor of shared memory object */ asserts( _shmd = shm_open(SHM_NAME, O_RDONLY, S_IRUSR | S_IWUSR) ); /* map shared memory object into a virtual address */ #ifdef DYN_SEG_SIZE struct stat _buf; assertz( fstat(_shmd, &_buf) ); assertv(_data = mmap(NULL, _segSz = _buf.st_size, PROT_READ, MAP_SHARED, _shmd, 0), MAP_FAILED); #else assertv(_data = mmap(NULL, SEG_SIZE, PROT_READ, MAP_SHARED, _shmd, 0), MAP_FAILED); #endif /* DYN_SEG_SIZE */ /* read from shared memory object */ _off = 0; for(size_t i = 0; i < _data->len; i++) { print(_data->vals + _off); print(" "); _off += strlen(_data->vals + _off) + 1; } println(""); } /* write arguments in the reveres order into shared memory object */ else { #ifdef ALLOW_CLEANUP int _ret; /* if shared memory object already exist obtain its id and destroy, otherwise do nothing */ if( o_argc == 2 && !strcmp(o_argv[1], "cleanup") ) { _ret = shm_unlink(SHM_NAME); if(_ret == -1 && errno != ENOENT) asserts(_ret); /* destroy the semaphore before exit */ _ret = sem_unlink(SEM_NAME); if(_ret == -1 && errno != ENOENT) asserts(_ret); exit(EXIT_SUCCESS); } #endif /* ALLOW_CLEANUP */ /* use existing shared memory object or create the new one */ asserts( _shmd = shm_open(SHM_NAME, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR) ); /* allocate a space for the maximum line length */ #ifdef DYN_SEG_SIZE assertz( ftruncate(_shmd, _segSz) ); #else assertz( ftruncate(_shmd, SEG_SIZE) ); #endif /* DYN_SEG_SIZE */ /* map shared memory object into virtual address */ #ifdef DYN_SEG_SIZE assertv(_data = mmap(NULL, _segSz, PROT_READ | PROT_WRITE, MAP_SHARED, _shmd, 0), MAP_FAILED); #else assertv(_data = mmap(NULL, SEG_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, _shmd, 0), MAP_FAILED); #endif /* DYN_SEG_SIZE */ /* write into the shared memory object */ _data->len = o_argc - 1; _off = 0; for(size_t i = o_argc - 1; i > 0; i--) { /* it is safe to use strcpy, because we got enought memory */ strcpy(_data->vals + _off, o_argv[i]); _off += strlen(o_argv[i]) + 1; } } /* unmap shared memory object from virtual address space */ #ifdef DYN_SEG_SIZE assertz( munmap(_data, _segSz) ); #else assertz( munmap(_data, SEG_SIZE) ); #endif /* DYN_SEG_SIZE */ _data = NULL; /* close the unused descriptor of shared memory object */ assertz( close(_shmd) ); /* unlock the semaphore */ assertz( sem_post(_sem) ); exit(EXIT_SUCCESS); }
float delay(int pos) { assertv(size >= pos); return this->Buffer[(this->pointer + pos) & mask]; }
int main(int o_argc, const string const* o_argv) { string _path = getExecPath(*o_argv); key_t _key = ftok(_path, 'x'); asserts(_key, "ftok"); free(_path); _path = NULL; union semun _arg ; struct sembuf _buf ; int _shmid; MyData _data ; size_t _off ; /* Nowaday, memory is so cheap that I just do not care about the unused memory. * If is it too waste, then just compile with DYN_SEG_SIZE switch. **/ #ifndef DYN_SEG_SIZE const off_t SEG_SIZE = sizeof(mydata_t); #endif /* DYN_SEG_SIZE */ _buf.sem_num = 0; _buf.sem_flg = 0; /* Try to create a set of semaphores. */ int _semid = semget(_key, 1, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); if(_semid == -1) { const int MAX_TRIES = 6; /* If semget failed, and the set does not exist then exit */ if(errno != EEXIST) asserts(_semid, "semget"); _semid = semget(_key, 1, S_IRUSR | S_IWUSR); asserts(_semid); struct semid_ds _ds; _arg.buf = &_ds; for(size_t i = 0; i < MAX_TRIES; i++) { asserts( semctl(_semid, 0, IPC_STAT, _arg) ); if(_ds.sem_otime != 0) break; sleep(5); } if(_ds.sem_otime == 0) fatal ( "The set of semaphores already exists, but it is not initialized.\n" "This is a permanent error, and I have given up." ) ; } /* init semaphore */ else { /* Note: * Some systems, like Linux, implicitly initializes a set of semaphores by value 0, * but unfortunately some does not. For that reason, this operation is necessary to ensure * a portability. **/ _arg.val = 0; asserts( semctl(_semid, 0, SETVAL, _arg) ); /* post semaphore */ _buf.sem_op = 1; asserts( semop(_semid, &_buf, 1) ); } /* lock the semaphore */ _buf.sem_op = -1; asserts( semop(_semid, &_buf, 1) ); /* Critical section: */ /* there is no arguments so print shared memory object content */ if(o_argc == 1) { /* obtain the descriptor of shared memory object */ asserts( _shmid = shmget(_key, 0, S_IRUSR | S_IWUSR | SHM_RDONLY) ); /* map shared memory object into virtual address space */ assertv(_data = shmat(_shmid, NULL, 0), cast(void*)-1); /* read from shared memory object */ _off = 0; for(size_t i = 0; i < _data->len; i++) { print(_data->vals + _off); print(" "); _off += strlen(_data->vals + _off) + 1; } println(""); } /* write arguments in the reveres order into shared memory object */ else { #if (defined ALLOW_CLEANUP || defined DYN_SEG_SIZE) struct shmid_ds _shmds; #endif /* ALLOW_CLEANUP || DYN_SEG_SIZE */ #ifdef ALLOW_CLEANUP union semun _semun; /* if shared memory object already exist obtain its id and destroy, otherwise do nothing */ if( o_argc == 2 && !strcmp(o_argv[1], "cleanup") ) { _shmid = shmget(_key, 0, S_IRUSR | S_IWUSR); if(_shmid == -1) { if(errno != ENOENT) asserts(_shmid); } else asserts( shmctl(_shmid, IPC_RMID, &_shmds) ); /* destroy the semaphore before exit */ asserts( semctl(_semid, 0, IPC_RMID, _semun) ); exit(EXIT_SUCCESS); } #endif /* ALLOW_CLEANUP */ /* use existing shared memory object or create the new one */ #ifdef DYN_SEG_SIZE off_t _segSz = sizeof(size_t); for(size_t i = 1; i < o_argc; i++) { _segSz += strlen(o_argv[i]) + 1; } /* Try to create a new shared memory object. * If such object already exits the destoy it before. **/ _shmid = shmget(_key, _segSz, S_IRUSR | S_IWUSR | IPC_CREAT | IPC_EXCL); if(_shmid == -1) { if(errno == EEXIST) { asserts( _shmid = shmget(_key, 0, S_IRUSR | S_IWUSR) ); asserts( shmctl(_shmid, IPC_RMID, &_shmds) ); asserts( _shmid = shmget(_key, _segSz, S_IRUSR | S_IWUSR | IPC_CREAT) ); } } #else asserts( _shmid = shmget(_key, SEG_SIZE, S_IRUSR | S_IWUSR | IPC_CREAT) ); #endif /* DYN_SEG_SIZE */ /* map shared memory object into virtual address space */ assertv(_data = shmat(_shmid, NULL, 0), cast(void*)-1); /* write into the shared memory object */ _data->len = o_argc - 1; _off = 0; for(size_t i = o_argc - 1; i > 0; i--) { /* it is safe to use strcpy, because we got enought memory */ strcpy(_data->vals + _off, o_argv[i]); _off += strlen(o_argv[i]) + 1; } } /* unmap shared memory object from virtual address space */ assertz( shmdt(_data) ); _data = NULL; /* unlock the semaphore */ _buf.sem_op = 1; asserts( semop(_semid, &_buf, 1) ); exit(EXIT_SUCCESS); }