ControlledLeds newControlledLed(Led led) { struct LedGroup group = (struct LedGroup){ &led, 1 }; return newControlledLedGroup(&group); } ControlledLeds newControlledLedGroup(LedGroup group) { if (group == NULL) return Invalid(ControlledLeds); _ControlledLeds leds = kalloc(sizeof(struct _ControlledLeds)); if (!leds) return Invalid(ControlledLeds); LedList *underlying = addNewLedsToList(group); if (!underlying) { free(leds); return Invalid(ControlledLeds); } leds->mask = 0; leds->leds = underlying; leds->count = group->count; leds->next = NULL; ControlledLeds result = As(ControlledLeds, leds); controlLeds(result, LedsDisabled); LL_APPEND(controlled_leds, leds); return result; } static void cleanUnderlyingLeds(ControlledLeds leds) { LedList elem = NULL, tmp = NULL; LL_FOREACH_SAFE(underlying_leds, elem, tmp) { for (int i = 0; i < LEDS->count; i++) { if (elem == LEDS->leds[i]) { reduceRefcount(elem); } } } }
Button newButton(Pin pin, ButtonType flags) { _Button button = kalloc(sizeof(struct _Button)); if (!button) return Invalid(Button); if (!occupyPin(pin, PinButtonInput)) { free(button); return Invalid(Button); } button->pin = pin; button->flags = flags; button->status = 0; initButton(pin, flags); return As(Button, button); }
Player Player::OfGtpStream (istream& in) { string s; in >> s; if (!in) return Invalid (); if (s == "b" || s == "B" || s == "Black" || s == "BLACK "|| s == "black") { return Black(); } if (s == "w" || s == "W" || s == "White" || s == "WHITE "|| s == "white") { return White(); } in.setstate (ios_base::badbit); // TODO undo read? return Invalid(); }
void Rectangle::set_length(int l) { if ((l <= 0) || (l >= 20)){//verify throw Invalid(); } length = l; }
uint Socket::Recv( char *dst, uint dst_bytes ) { if ( Invalid() || type != ST_Datagram ) { Output( "r3::Socket::Recv() invalid socket or socket type" ); return -1; } return (uint)recv( s, dst, dst_bytes, 0 ); }
static ProcessBase initializeProcessInternal(void *stackPointer) { PCB process = kalloc(sizeof(struct PCB)); if (!process) return Invalid(ProcessBase); process->stackPointer = stackPointer; process->extra = NULL; return As(ProcessBase, process); }
Extern void SUFFIX(cubawait)(Spin **pspin) { int cores, core, status; Spin *spin; MasterExit(); if( Invalid(pspin) || (spin = *pspin) == NULL ) return; cores = spin->spec.naccel + spin->spec.ncores; for( core = 0; core < cores; ++core ) { MASTER("closing fd %d", spin->fp[core].fd); close(spin->fp[core].fd); } #ifdef KILL_WORKERS for( core = 0; core < cores; ++core ) { MASTER("killing pid %d", spin->fp[core].pid); kill(spin->fp[core].pid, SIGKILL); } #endif for( core = 0; core < cores; ++core ) { DEB_ONLY(pid_t pid;) MASTER("waiting for child"); DEB_ONLY(pid =) wait(&status); MASTER("pid %d terminated with exit code %d", pid, status); }
Extern void EXPORT(Vegas)(ccount ndim, ccount ncomp, Integrand integrand, void *userdata, cnumber nvec, creal epsrel, creal epsabs, cint flags, cint seed, cnumber mineval, cnumber maxeval, cnumber nstart, cnumber nincrease, cnumber nbatch, cint gridno, cchar *statefile, Spin **pspin, number *pneval, int *pfail, real *integral, real *error, real *prob) { This t; VerboseInit(); t.ndim = ndim; t.ncomp = ncomp; t.integrand = integrand; t.userdata = userdata; t.nvec = nvec; t.epsrel = epsrel; t.epsabs = epsabs; t.flags = MaxVerbose(flags); t.seed = seed; t.mineval = mineval; t.maxeval = maxeval; t.nstart = nstart; t.nincrease = nincrease; t.nbatch = nbatch; t.gridno = gridno; t.statefile = statefile; FORK_ONLY(t.spin = Invalid(pspin) ? NULL : *pspin;)
void test_invalid_timer() { t = Invalid(Timer); TEST_ASSERT_FALSE_MESSAGE(isPwmTimer(t), "invalid timer should not be pwm"); TEST_ASSERT_FALSE_MESSAGE(IsValid(getTimerOutputPin(t)), "invalid timer should not have output pin"); setTimerValue(t, 23); // Check for no segfault. TEST_ASSERT_EQUAL_MESSAGE(0, getTimerValue(t), "invalid timer should have 0 timer value"); }
void Rectangle::set_width(int w) { if ((w <= 0) || (w >= 20)){//verify throw Invalid(); } width = w; }
Button destroyButton(Button button) { if (buttonValid(button)) { deOccupyPin(BUTTON->pin, PinButtonInput); free(BUTTON); } return Invalid(Button); }
void ReadWriteLock::writelock() { if (_valid != RW_LOCK_VALID) throw Invalid(JUST_FILE_LINE); int myStatus = pthread_mutex_lock(&_myMutex); if (myStatus != 0) { throw MutexLockFailed(strerror(myStatus),PLUS_FILE_LINE); } if (_hasActiveWriter || _activeReaderCount > 0) { _waitingWriterCount++; pthread_cleanup_push(write_cleanup,(void*)this); while (_hasActiveWriter || _activeReaderCount > 0) { myStatus = pthread_cond_wait(&_writeProceed,&_myMutex); if (myStatus != 0) { break; } } pthread_cleanup_pop(0); _waitingWriterCount--; } if (myStatus == 0) { _hasActiveWriter = 1; } else { pthread_mutex_unlock(&_myMutex); throw CondWaitFailed(strerror(myStatus),PLUS_FILE_LINE); } pthread_mutex_unlock(&_myMutex); }
glm::mat4 TransformComponent::Transform() const { assert(!Invalid()); glm::mat4 result; result = glm::mat4_cast(mRotation); result[3] = Position(); return result; }
Gate::Gate(Gate* input1, Operand type, int position) : i1(input1), t(type), p(position){ if (!is_gate(i1,t)) throw Invalid(); Vector<bool> v1 = input1->getTable(); for(bool e: v1) table.push_back(!e); }
ControlledLeds destroyControlledLeds(ControlledLeds leds) { if (IsValid(leds)) { LL_DELETE(controlled_leds, LEDS); cleanUnderlyingLeds(leds); free(LEDS); } return Invalid(ControlledLeds); }
MediaDesc& MediaAccessFacade::queryFile (string const& name) const { if (isnil (name)) throw Invalid ("empty filename passed to MediaAccessFacade."); UNIMPLEMENTED ("delegate to vault: query accessability of file"); }
void tearDown() { Pin p = Invalid(Pin); if (IsValid(t)) p = getTimerOutputPin(t); t = destroyTimer(t); TEST_ASSERT_TRUE_MESSAGE(!IsValid(t), "Timer still valid after destroy."); if (IsValid(p)) { TEST_ASSERT_EQUAL_MESSAGE(PinNoOccupation, pinOccupation(p), "Pin still occupied after pwm-timer destroyed."); } destroy_fake_port(); }
void Socket::Close() { if( Invalid() ) { return; } #ifdef _WIN32 closesocket( s ); #else close((int)s); #endif type = ST_Invalid; s = -1; }
ProcessBase createProcessBase(ProcessEntryPoint entryPoint, void *processArgument, uint16_t stackSize) { MockProcess process = malloc(sizeof(struct MockProcess)); if (!process) return Invalid(ProcessBase); process->entryPoint = entryPoint; process->processArgument = processArgument; process->stackSize = stackSize; process->destroyed = FALSE; process->extra = NULL; process->scheduled = 0; process->stackTop = malloc(stackSize); process->stackPointer = process->stackTop - INITIAL_STACK_SIZE; return As(ProcessBase, process); }
void SetColorScheme(char* name) throw (Invalid) { for (unsigned int i = 0; i < color_schemes.size(); i++) { if (!strcmp(color_scheme_names[i], name)) { for (int j = 0; j < NUM_CONFIGURABLE_COLORS; j++) { configuration.colors[j].r = color_schemes[i][j].r; configuration.colors[j].g = color_schemes[i][j].g; configuration.colors[j].b = color_schemes[i][j].b; } return; } } throw Invalid(); }
ProcessBase createProcessBase(ProcessEntryPoint entryPoint, void *parameter, uint16_t stackSize) { // Allocate stack-memory and set the stack-pointer. // The stack-pointer of the new process is the end of the allocated block, // because the stack grows in opposite direction as the allocation. // 2 and sizeof(struct PCB) are subtracted because there is an initial context pushed there. uint8_t *stackTop = (uint8_t*) kcalloc(stackSize, sizeof(uint8_t)); if (!stackTop) { return Invalid(ProcessBase); } uint8_t *stackBottom = stackTop + stackSize - 1; // "Push" the address of the ProcessGraveyard and the actual entryPoint *(stackBottom - 0) = LOBYTE((uint16_t) ProcessGraveyard); *(stackBottom - 1) = HIBYTE((uint16_t) ProcessGraveyard); *(stackBottom - 2) = LOBYTE((uint16_t) entryPoint); *(stackBottom - 3) = HIBYTE((uint16_t) entryPoint); ProcessBase result = initializeProcessInternal((void*) (stackBottom - INITIAL_STACK_SIZE)); if (!IsValid(result)) { free(stackTop); return Invalid(ProcessBase); } // "Push" the process-parameter on r25 and r24, following GCCs calling convention. // 6 bytes are pushed on the initial stack below the first register r0 if (!parameter) parameter = result.pointer; *(stackBottom - (6 + 24)) = LOBYTE((uint16_t) parameter); *(stackBottom - (6 + 25)) = HIBYTE((uint16_t) parameter); return result; }
Gate::Gate(Gate* input1, Gate* input2, Operand type, int position): i1(input1), i2(input2), t(type), p(position) { if (!is_gate(i1,i2,t)) throw Invalid(); Vector<bool> v1 = input1->getTable(); Vector<bool> v2 = input2->getTable(); Vector<bool> vFinal; if(type==Operand::AND){ for(int i=0; i<v1.size(); ++i) vFinal.push_back(v1.at(i)&&v2.at(i));} else{ for(int i=0; i<v1.size(); ++i) vFinal.push_back(v1.at(i)||v2.at(i));} table=vFinal; }
bool Socket::SendTo( uint host, int port, char *src, int bytes ) { INIT_SOCKET_LIB(); if ( Invalid() ) { s = socket(AF_INET, SOCK_DGRAM, 0); if( s < 0 ) { Output( "r3::Socket::SendTo() call to ::socket() failed" ); s = -1; return false; } /* sockaddr_in a; a.sin_family = AF_INET; a.sin_port = 0; a.sin_addr.s_addr = INADDR_ANY; if ( bind( s, (sockaddr *)& a, sizeof( a ) ) == -1 ) { Output( "r3::Socket::SendTo() call to ::bind() failed" ); Close(); return false; } */ type = ST_Datagram; } if ( type != ST_Datagram ) { Output( "r3::Socket::SendTo() called on non-datagram socket" ); return false; } sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = host; if ( sendto( s, src, bytes, 0, (sockaddr*) & addr, sizeof( addr ) ) != bytes ) { perror("sendto error"); Output( "r3::Socket::SendTo() call to sendto() failed" ); return false; } return true; }
void ReadWriteLock::readunlock() { if (_valid != RW_LOCK_VALID) throw Invalid(JUST_FILE_LINE); int myStatus = pthread_mutex_lock(&_myMutex); if (myStatus != 0) { throw MutexLockFailed(strerror(myStatus),PLUS_FILE_LINE); } _activeReaderCount--; if (_activeReaderCount == 0 && _waitingWriterCount > 0) { myStatus = pthread_cond_signal(&_writeProceed); } int myStatus2 = pthread_mutex_unlock(&_myMutex); if (myStatus != 0) { throw CondSignalFailed(strerror(myStatus),PLUS_FILE_LINE); } if (myStatus2 != 0) { throw MutexUnlockFailed(strerror(myStatus2),PLUS_FILE_LINE); } }
GeoBounds GeoBounds::Scale(fixed factor) const { if (!IsValid()) return Invalid(); Angle diff_lat_half = GetHeight() / 2 * (factor - fixed(1)); Angle diff_lon_half = GetWidth() / 2 * (factor - fixed(1)); GeoBounds br = *this; br.longitude.end += diff_lon_half; br.longitude.start -= diff_lon_half; br.latitude.end += diff_lat_half; br.latitude.start -= diff_lat_half; return br; }
void ReadWriteLock::writeunlock() { if (_valid != RW_LOCK_VALID) throw Invalid(JUST_FILE_LINE); int myStatus = pthread_mutex_lock(&_myMutex); if (myStatus != 0) { throw MutexLockFailed(strerror(myStatus),PLUS_FILE_LINE); } _hasActiveWriter = 0; if (_waitingReaderCount > 0) { myStatus = pthread_cond_broadcast(&_readProceed); if (myStatus != 0) { pthread_mutex_unlock(&_myMutex); throw CondBroadcastFailed(strerror(myStatus),PLUS_FILE_LINE); } } else if (_waitingWriterCount > 0) { myStatus = pthread_cond_signal(&_writeProceed); if (myStatus != 0) { pthread_mutex_unlock(&_myMutex); throw CondSignalFailed(strerror(myStatus),PLUS_FILE_LINE); } } pthread_mutex_unlock(&_myMutex); }
static void FreeChannel_m(VP_cl *self, Channel_Endpoint ep ) { dcb_rw_t *rwp = RW(self); dcb_ro_t *rop = RO(self); ep_rw_t *eprw; ep_ro_t *epro; if ( ep >= rop->num_eps ) { DB(eprintf("VP$FreeChannel: invalid EP.\n")); RAISE_Channel$Invalid( ep ); } epro = DCB_EPRO(rop,ep); eprw = DCB_EPRW(rop,ep); if ( epro->peer_dcb != NULL || eprw->state == Channel_State_Free ) { DB(eprintf("VP$FreeChannel: Bad State.\n")); RAISE_Channel$BadState( ep, eprw->state ); } eprw->state = Channel_State_Free; eprw->ack = (word_t)(rwp->free_eps); rwp->free_eps = eprw; }
bool Socket::Connect( uint host, int port ) { INIT_SOCKET_LIB(); sockaddr_in addr; int one = 1; if ( Invalid() == false ) { Output( "r3::Socket::Connect() call to existing socket" ); } addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = host; s = socket(AF_INET, SOCK_STREAM, 0); if( s < 0 ) { Output( "r3::Socket::Connect() call to ::socket() failed" ); s = -1; return false; } type = ST_Stream; if( connect( s, (sockaddr *) & addr, sizeof(addr) ) < 0 ) { perror("connect error"); Output( "r3::Socket::Connect() call to ::connect() failed" ); Close(); return false; } if( setsockopt( s, IPPROTO_TCP, TCP_NODELAY, (char *) & one, sizeof(one) ) < 0 ) { perror("setsockopt error"); Output( "r3::Socket::Connect() call to ::setsockopt() failed" ); Close(); return false; } return true; }
bool Nat<T>::IsValid() const { return *this != Invalid(); }
void QueryDocument::parseQuery ( XProcessor& xproc, bool isQuery ) { ElementRef cookiesList = queryElement.getDocument().createElement ( queryElement, xem_web.cookies() ); queryElement.appendLastChild ( cookiesList ); ElementRef headersList = queryElement.getDocument().createElement ( queryElement, xem_web.headers() ); queryElement.appendLastChild ( headersList ); QParseState state = QParseState_HeadLine; String accu = ""; String method; ElementRef headerElement(*this); ElementRef responseElement(*this); ElementRef urlElement(*this); ElementRef urlParams(*this); ElementRef urlParam(*this); ElementRef cookieElement(*this); __ui64 contentLength = 0; bool transferEncodingChunked = false; #define Invalid() throwException(Exception, "Invalid character %d at state %d\n", r, state ); while ( state != QParseState_FinishedHTTPHeader && !reader.isFinished()) { int r = reader.getNextChar(); if ( r >= 0x80 ) { throwException ( Exception, "Invalid char %d\n", r ); } // Log_QParse ( "state=%d, r=%x (%d, %c)\n", state, r, r, r ); switch ( state ) { case QParseState_HeadLine: if ( r == ' ' ) { Log_QParse ( "Method : '%s'\n", accu.c_str() ); if ( isQuery ) { if ( accu == "GET" || accu == "POST" ) { ElementRef methodElement = addTextualElement ( queryElement, xem_web.method(), accu ); method = methodElement.getText(); state = QParseState_URLStart; accu = ""; continue; } else Invalid(); } else { if ( accu == "HTTP/1.1" ) { Log_QParse ( "Got a Response, protocol=%s\n", accu.c_str() ); addTextualElement ( queryElement, xem_web.protocol(), accu ); accu = ""; state = QParseState_ResponseCode; responseElement = createElement ( headersList, xem_web.response() ); headersList.appendLastChild ( responseElement ); } else { Invalid(); } } } else if ( r == '\r' || r == '\n' ) { Invalid(); } accu.appendUtf8(r); break; case QParseState_URLStart: if ( r == '/' ) { state = QParseState_URL; continue; } Invalid(); break; case QParseState_URL: if ( r == ' ' || r == '?' ) { Log_QParse ( "Url : '%s'\n", accu.c_str() ); urlElement = createElement ( queryElement, xem_web.url() ); queryElement.appendLastChild ( urlElement ); addTextualElement ( urlElement, xem_web.base(), accu ); accu = ""; if ( r == ' ' ) { state = QParseState_Protocol; } else { state = QParseState_URLParam; } continue; } else if ( r == '?' ) { Invalid(); } else if ( r == '\r' || r == '\n' ) { Invalid(); } else if ( r == '%' ) { Invalid (); } accu.appendUtf8(r); break; case QParseState_URLParam: if ( r == '\n' || r == '%' || r == '\r' ) { Invalid(); } else if ( r == '=' || r == ' ' || r == '&' ) { if ( ! urlParams ) { urlParams = createElement ( urlElement, xem_web.parameters() ); urlElement.appendLastChild ( urlParams ); } Log_QParse ( "Param : '%s'\n", accu.c_str() ); urlParam = createElement ( urlParams, xem_web.param() ); urlParams.appendLastChild ( urlParam ); urlParam.addAttr ( xem_web.name(), accu ); accu = ""; if ( r == '=' ) state = QParseState_URLParamValue; else if ( r == '&' ) state = QParseState_URLParam; else if ( r == ' ' ) state = QParseState_Protocol; else { Invalid(); } continue; } accu.appendUtf8(r); break; case QParseState_URLParamValue: if ( r == '&' || r == ' ') { Log_QParse ( "Param Value : '%s'\n", accu.c_str() ); ElementRef valueNode = createTextNode ( urlParam, accu ); urlParam.appendLastChild ( valueNode ); urlParam = ElementRef(*this); accu = ""; if ( r == '&' ) state = QParseState_URLParam; else state = QParseState_Protocol; continue; } accu.appendUtf8(r); break; case QParseState_Protocol: if ( r == ' ' ) { Invalid(); } else if ( r == '\r' ) { continue; } else if ( r == '\n' ) { Log_QParse ( "Protocol = '%s'\n", accu.c_str() ); addTextualElement ( queryElement, xem_web.protocol(), accu ); accu = ""; state = QParseState_BeginLine; continue; } accu.appendUtf8(r); break; case QParseState_ResponseCode: if ( r == ' ' ) { Log_QParse ( "ResponseCode : %s\n", accu.c_str() ); responseElement.addAttr ( xem_web.response_code(), accu ); accu = ""; state = QParseState_ResponseString; continue; } if ( '0' <= r && r <= '9' ) { accu.appendUtf8(r); continue; } Invalid(); break; case QParseState_ResponseString: if ( r == '\r' ) continue; else if ( r == '\n' ) { Log_QParse ( "ResponseString : %s\n", accu.c_str() ); responseElement.addAttr ( xem_web.response_string(), accu ); accu = ""; state = QParseState_BeginLine; continue; } accu.appendUtf8(r); break; case QParseState_BeginLine: if ( r == '\r' ) continue; else if ( r == '\n' ) { state = QParseState_FinishedHTTPHeader; break; } else if ( r == ' ' ) { Invalid(); } accu.appendUtf8(r); state = QParseState_FieldName; continue; case QParseState_FieldName: if ( r == ':' ) { Log_QParse ( "Field : '%s'\n", accu.c_str() ); if ( accu == "Cookie" ) { accu = ""; state = QParseState_CookieStart; continue; } headerElement = createElement ( headersList, xem_web.param() ); headersList.appendLastChild ( headerElement ); headerElement.addAttr ( xem_web.name(), accu ); accu = ""; state = QParseState_PostFieldName; continue; } else if ( r == ' ' || r == '\n' || r == '\r' ) { Invalid(); } accu.appendUtf8(r); break; case QParseState_PostFieldName: if ( r == ' ' ) { state = QParseState_Value; continue; } Invalid(); case QParseState_Value: if ( r == '\r') continue; else if ( r == '\n' ) { Log_QParse ( "Field value : '%s'\n", accu.c_str() ); ElementRef valueNode = createTextNode ( headerElement, accu ); headerElement.appendLastChild ( valueNode ); String fieldName = headerElement.getAttr(xem_web.name()); #ifdef __XEM_WEBSERVER_QUERYDOCUMENT_HAS_HEADERFIELDSMAP headerFieldsMap[fieldName] = valueNode.getText(); #endif if ( fieldName == "Content-Length" ) { contentLength = accu.toUI64(); Log_QParse ( "ContentLength : %llu\n", contentLength ); } else if ( fieldName == "Transfer-Encoding" && accu == "chunked" ) { Log_QParse ( "TransferEncoding chuncked !\n" ); transferEncodingChunked = true; } headerElement = ElementRef(*this); accu = ""; state = QParseState_BeginLine; continue; } accu.appendUtf8(r); break; case QParseState_CookieStart: if ( r == ' ' ) continue; accu.appendUtf8(r); state = QParseState_CookieName; break; case QParseState_CookieName: if ( r == '=' ) { Log_QParse ( "Cookie name : '%s'\n", accu.c_str() ); cookieElement = createElement ( cookiesList, xem_web.cookie() ); cookieElement.addAttr ( xem_web.name(), accu ); cookiesList.appendLastChild ( cookieElement ); accu = ""; state = QParseState_CookieValue; continue; } accu.appendUtf8(r); break; case QParseState_CookieValue: if ( r == '\r' ) continue; if ( r == ';' || r == '\n' ) { Log_QParse ( "Cookie value : '%s'\n", accu.c_str() ); ElementRef cookieValueNode = createTextNode ( cookieElement, accu ); cookieElement.appendLastChild ( cookieValueNode ); accu = ""; if ( r == ';' ) state = QParseState_CookieStart; else state = QParseState_BeginLine; continue; } accu.appendUtf8(r); break; default: Bug ( "Case %d Not implemented !\n", state ); } } if ( state != QParseState_FinishedHTTPHeader ) { throwException ( Exception, "HTTP reader not finished ! state=%d\n", state ); } if ( contentLength || transferEncodingChunked ) { if ( !isQuery || method == "POST" ) { ElementRef content = createElement ( queryElement, xem_web.content() ); queryElement.appendLastChild ( content ); BlobRef blob = content.addBlob(xem_web.blob_contents()); if ( contentLength ) parseToBlob(blob, contentLength); else parseChunkedToBlob(blob); } else { throwException ( Exception, "Invalid content-length with method = %s\n", method.c_str() ); } } }