void removeFromFreeList( heapAllocator* heap, block* b ) { //validateFreeList( heap ); block* const pFree = prevFree( b ); block* const nFree = nextFree( b ); //printf( "prev: " xPTRf ", next: " xPTRf "\n", (uintptr_t)pFree, (uintptr_t)nFree ); //vAssert( nFree || pFree ); if ( pFree ) { vAssert( pFree->free ); //vAssert( nFree || pFree->prevFree ); setNextFree( pFree, nFree ); } else { vAssert( heap->free == b ); if (!nFree ) { block* bl = heap->first; while (bl) { vAssert( bl == b || !bl->free ); bl = bl->next; } } heap->free = nFree; } if ( nFree ) { vAssert( nFree->free ); setPrevFree( nFree, pFree ); } setNextFree( b, NULL ); setPrevFree( b, NULL ); b->free = false; //validateFreeList( heap ); b->free = true; }
// ----------------------------------------------------------------------------- static void prvvSetRtc (void) { printf_P(PSTR("\nDate (DDMMYYYY) ? ")); xSetTime.ucDate = (uint8_t) iTermGetDec (stdin, 2); putchar ('/'); xSetTime.ucMonth = (uint8_t) iTermGetDec (stdin, 2); putchar ('/'); xSetTime.usYear = (uint16_t) iTermGetDec (stdin, 4); printf_P(PSTR("\nJour (1=Dim..7=Sam) ? ")); xSetTime.ucWeekDay = (uint8_t) iTermGetDec (stdin, 1); printf_P(PSTR("\nHeure (HHMMSS) ? ")); xSetTime.ucHour = (uint8_t) iTermGetDec (stdin, 2); putchar (':'); xSetTime.ucMinute = (uint8_t) iTermGetDec (stdin, 2); putchar (':'); xSetTime.ucSecond = (uint8_t) iTermGetDec (stdin, 2); putchar ('\n'); vAssert (iRtcSetTime (&xSetTime) == 0); vAssert (iRtcGetTime (&xGetTime) == 0); vAssert (memcmp (&xSetTime, &xGetTime, sizeof(xRtcTime)) == 0); }
/* main ===================================================================== */ int main (void) { vLedInit (); #if defined(AVRIO_DEBUG_STREAM) /* Init terminal */ vSerialInit (TEST_BAUDRATE / 100, SERIAL_DEFAULT + SERIAL_WR); stderr = &xSerialPort; fputc('\r', stderr); #endif vTwiInit (); vAssert(eTwiSetSpeed (400) == TWI_SUCCESS); vAssert(iWHubInit (WDEV_RATE_16KBPS, FRAM_SIZE, &xFRAM) == 0); vWHubSetStatusFlag (WHUB_AUTOBIND, false); // vWSdBaseClear (); for (;;) { vLedSet (LOOP_LED); pxMsg = pxWHubLoop (); vLedClear (LOOP_LED); } return 0; }
// ----------------------------------------------------------------------------- static void prvvPrintRtc (void) { vAssert (iRtcGetTime (&xGetTime) == 0); vAssert (iRtcPrintDate (&xGetTime) > 0); printf_P (PSTR(" - ")); vAssert (iRtcPrintTime (&xGetTime) > 0); putchar('\n'); }
void assertBlockInvariants( block* b ) { #ifdef MEM_GUARD_BLOCK vAssert( b->guard == kGuardValue ); #endif // MEM_GUARD_BLOCK vAssert( b ); vAssert( !b->next || b->next->prev == b ); vAssert( !b->prev || b->prev->next == b ); validateBlockNext( b ); validateBlockPrev( b ); }
// Insert a block *after* into a linked-list after the block *before* // Both *before* and *after* must be valid void block_insertAfter( block* before, block* after ) { vAssert( before ); vAssert( after ); after->next = before->next; after->prev = before; before->next = after; if ( after->next ) after->next->prev = after; }
/* main ===================================================================== */ int main (void) { eTwiStatus eError; uint8_t ucWaitTime = 10; // Temps d'attente en secondes vLedInit (); vSerialInit (TEST_BAUDRATE / 100, SERIAL_DEFAULT + SERIAL_RW); stdout = &xSerialPort; stdin = &xSerialPort; printf_P (PSTR("\n\nTest unitaire RTC DS1339\n")); vTwiInit (); eError = eTwiSetSpeed (100); vAssert (eError == TWI_SUCCESS); vAssert (iRtcInit (TEST_DS1339) == 0); printf_P(PSTR("Date courante: ")); prvvPrintRtc(); printf_P(PSTR("Modification Date ? (y/n) ")); while (ucWaitTime--) { uint8_t ucCount = 10; while ((ucCount--) && (usSerialHit() == 0)) { // Boucle en attente appui d'une touche delay_ms(100); } if (usSerialHit() != 0) { char cKey; cKey = getchar(); if ((cKey == 'y') || (cKey == 'Y')) { prvvSetRtc(); } break; } putchar('.'); } putchar('\n'); for (;;) { if (usSerialHit() != 0) { (void) getchar(); // flush last char prvvSetRtc(); } prvvPrintRtc(); vLedToggle (LED_LED1); delay_ms (1000); } return 0; }
void addToFreeList( heapAllocator* heap, block* b ) { b->free = true; block* oldFree = heap->free; vAssert( oldFree != b ); setNextFree( b, oldFree ); vAssert( oldFree != b ); vAssert( oldFree == NULL || prevFree( oldFree ) == NULL ); // It should have been first in the list setPrevFree( oldFree, b ); heap->free = b; setPrevFree( b, NULL ); }
// Create and initialise a block in a given piece of memory of *size* bytes block* block_create( heapAllocator* heap, void* data, size_t size ) { block* b = (block*)data; memset( b, 0, sizeof( block )); b->size = size - sizeof( block ); b->data = ((u8*)data) + sizeof( block ); b->free = true; b->prev = b->next = NULL; vAssert( size > sizeof( void* ) * 2 ); vAssert( b->size > sizeof( block* ) * 2 ); addToFreeList( heap, b ); #ifdef MEM_GUARD_BLOCK b->guard = kGuardValue; #endif return b; }
void test_keyboard() { // Key array test key_array data; memset( &data, 0, sizeof( key_array )); // Test setting key arrays for ( int key = 0; key < 256; ++key ) { vAssert( keyArray_get( &data, key ) == 0 ); keyArray_set( &data, key, 1 ); vAssert( keyArray_get( &data, key ) == 1) keyArray_set( &data, key, 0 ); vAssert( keyArray_get( &data, key ) == 0); } test( true, "Read and Wrote to all keys in key array successfully.", NULL ); }
/* internal public functions ================================================ */ int main (void) { eWNetErrorCode eError; vLedInit (); prvvDbgInit (); vAssert (iWNetTestSetup () == 0); (void) xWNetSetChannel (WNET_BIND_CHANNEL); (void) xWNetSetPnCode (WNET_BIND_PNCODE); vWNetSetCrcSeed (WPKT_BIND_CRC_SEED); vWNetSetChecksumSeed (WPKT_BIND_CHECKSUM_SEED); do { eError = eWNetTestSensorBind (10, 100, &xTestResult); prvvPrintResult (&xTestResult); } while (eError < 0); for (;;) { eError = eWNetTestSensorPing (1000, 10, &xTestResult); prvvPrintResult (&xTestResult); vLedToggle (TEST_LED); } return 0; }
// TODO worker task adding/removing should be lock free void worker_addTask( worker_task t ) { vmutex_lock( &worker_task_mutex ); { vAssert( worker_task_count < kMaxWorkerTasks ); worker_tasks[worker_task_count++] = t; vthread_broadcastCondition( work_exists ); } vmutex_unlock( &worker_task_mutex ); }
// ----------------------------------------------------------------------------- void vWDevIsr (void) { for(;;) { vAssert(0); } }
void keyArray_set( key_array* keys, int key, int state ) { // only two permissible options for state vAssert( state == 0x0 || state == 0x1 ); if ( state ) keys->keys[ key / 8 ] |= ( 0x1 << ( key % 8 )); else keys->keys[ key / 8 ] &= ~( 0x1 << ( key % 8 )); }
void heapContains( heapAllocator* heap, block* bl ) { block* b = heap->first; bool found = false; while ( b && !found ) { if ( b == bl ) found = true; b = b->next; } vAssert( found ); }
/* internal public functions ================================================ */ int main (void) { vLedInit(); for (;;) { vWIfcInit (); GetMid (ucMid); GetPnCode (ucPnCode); vAssert (memcmp_P (ucPnCode, ucPnCodeDef_P, sizeof(ucPnCodeDef_P)) == 0); SetPnCode_P (ucPnCode_P); GetPnCode (ucPnCode); vAssert (memcmp_P (ucPnCode, ucPnCode_P, sizeof(ucPnCode_P)) == 0); SetPnCode (ucPnCode); vLedToggle (LED_LED1); delay_ms (500); } return 0; }
int countFree( heapAllocator* heap ) { int i = 0; block* b = heap->free; while ( b ) { vAssert( b->free ); ++i; b = b->nextFree; } return i; }
// Immediate tasks void worker_addImmediateTask( worker_task t ) { vmutex_lock( &worker_task_mutex ); { vAssert( worker_immediate_task_count < kMaxWorkerTasks ); //printf("Adding immediate worker task\n"); worker_immediate_tasks[worker_immediate_task_count++] = t; vthread_broadcastCondition( work_exists ); } vmutex_unlock( &worker_task_mutex ); }
void checkFree( heapAllocator* heap, block* bl ) { block* b = heap->free; bool found = false; while ( b && !found ) { if ( b == bl ) found = true; b = nextFree( b ); } if ( !found ) printError( "Block 0x" xPTRf " is free but not in the free list\n", (uintptr_t)bl ); vAssert( found ); }
int printFree( heapAllocator* heap ) { int i = 0; block* b = heap->free; while ( b ) { vAssert( b->free ); printf( "Free : " xPTRf "\n", (uintptr_t)b ); ++i; b = b->nextFree; } return i; }
// Create a heapAllocator of *size* bytes // Initialised with one block pointing to the whole memory heapAllocator* heap_create( int heap_size ) { // We add space for the first block header, so we do get the correct total size // ie. this means that heap_create (size), followed by heap_Allocate( size ) should work void* data = malloc( sizeof( heapAllocator ) + sizeof( block ) + heap_size ); memset( data, 0, sizeof( heapAllocator ) + sizeof( block ) + heap_size ); heapAllocator* allocator = (heapAllocator*)data; data = (uint8_t*)data + sizeof( heapAllocator ); allocator->total_size = heap_size; allocator->total_free = heap_size; allocator->total_allocated = 0; allocator->bitpool_count = 0; // Should not be possible to fail creating the first block header allocator->free = NULL; allocator->first = block_create( allocator, data, heap_size ); vAssert( allocator->first ); vAssert( allocator->free == allocator->first ); vAssert( allocator->free == allocator->first ); vAssert( nextFree( allocator->free ) == NULL ); return allocator; }
/* internal public functions ================================================ */ int main(void) { int i; vLedInit (); // Configuration du port série par défaut (8N1, sans RTS/CTS) xSerialIos settings = SERIAL_SETTINGS (BAUDRATE); // Ouverture du port série en entrée et en sortie FILE * serial_port = xFileOpen (PORT, O_RDWR | O_NONBLOCK, &settings); vTncInit (&tnc, serial_port, serial_port); sei(); for (i = 0; i < TNC_RXBUFSIZE; i++) msg[i] = i; xScheduler = xTaskCreate (xTaskConvertMs (TRANSMIT_PERIOD), vScheduler); vTaskStart (xScheduler); for (;;) { i = iTncPoll (&tnc); vAssert (i >= 0); if (i == TNC_EOT) { for (i = 0; i < tnc.len; i++) vAssert (tnc.rxbuf[i] == i); vLedToggle (LED_LED1); } if (xMutexTryLock(&xMutexTx) == 0) { i = iTncWrite (&tnc, msg, sizeof(msg)); vAssert (i == sizeof(msg)); } } return 0; }
EGLConfig eglConfig( EGLDisplay display, const EGLint* attribs ) { (void)attribs; EGLConfig config; EGLint numConfigs; /* const EGLint attribs2[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_DEPTH_SIZE, 8, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; */ EGLBoolean r = eglChooseConfig( display, attribs, &config, 1, &numConfigs ); vAssertMsg( numConfigs > 0 , "Could not find a valid EGL config."); vAssert( r == EGL_TRUE ); return config; }
// Release a block from the heapAllocator void heap_deallocate( heapAllocator* heap, void* data ) { if ( data == NULL ) return; vmutex_lock( &allocator_mutex ); // Check if it's in a bitpool bitpool* bit_pool = heap_findBitpoolForData( heap, data ); if ( bit_pool ) { bitpool_free( bit_pool, data ); vmutex_unlock( &allocator_mutex ); return; } block* b = (block*)((uint8_t*)data - sizeof( block )); vAssert( !b->free ); assertBlockInvariants( b ); #ifdef MEM_DEBUG_VERBOSE printf("Allocator freed address: " xPTRf ".\n", (uintptr_t)b->data ); #endif b->free = true; addToFreeList( heap, b ); heap->total_free += b->size; heap->total_allocated -= b->size; checkFree( heap, b ); // Try to merge blocks if ( b->next && b->next->free ) { checkFree( heap, b->next ); blockMerge( heap, b, b->next ); } if ( b->prev && b->prev->free ) { checkFree( heap, b->prev ); blockMerge( heap, b->prev, b ); } --heap->allocations; vmutex_unlock( &allocator_mutex ); }
/* internal public functions ================================================ */ int main(void) { vLedInit (); // Configuration du port série par défaut (8N1, sans RTS/CTS) xSerialIos settings = SERIAL_SETTINGS (BAUDRATE); // Ouverture du port série en entrée et en sortie FILE * serial_port = xFileOpen (PORT, O_RDWR | O_NONBLOCK, &settings); sei(); vTncInit (&tnc, serial_port, serial_port); sei(); for (;;) { i = iTncPoll (&tnc); vAssert (i >= 0); if (i == TNC_EOT) { uint16_t usLen = tnc.len; i = iTncWrite (&tnc, &usLen, sizeof(usLen)); } } return 0; }
void heap_dumpBlocks( heapAllocator* heap ) { block* b = heap->first; FILE* memlog = fopen( "memlog", "w" ); while ( b ) { #ifdef TRACK_ALLOCATIONS if ( !b->free ) fprintf( memlog, "Block: ptr 0x" xPTRf ", data: 0x" xPTRf ", size " dPTRf ", free " dPTRf "\t\tSource: %s\n", (uintptr_t)b, (uintptr_t)b->data, b->size, b->free, b->source ); else fprintf( memlog, "Block: ptr 0x" xPTRf ", data: 0x" xPTRf ", size " dPTRf ", free " dPTRf "\n", (uintptr_t)b, (uintptr_t)b->data, b->size, b->free ); #else #ifdef MEM_STACK_TRACE if ( b->stack && !b->free ) printf( "Block: ptr 0x" xPTRf ", data: 0x" xPTRf ", size " dPTRf ", free " dPTRf "\t\tStack: %s\n", (uintptr_t)b, (uintptr_t)b->data, b->size, b->free, b->stack ); else printf( "Block: ptr 0x" xPTRf ", data: 0x" xPTRf ", size " dPTRf ", free " dPTRf "\n", (uintptr_t)b, (uintptr_t)b->data, b->size, b->free ); #else // MEM_STACK_TRACE printf( "Block: ptr 0x" xPTRf ", data: 0x" xPTRf ", size " dPTRf ", free " dPTRf "\n", (uintptr_t)b, (uintptr_t)b->data, b->size, b->free ); #endif // MEM_STACK_TRACE #endif vAssert( b->next == 0 || (uintptr_t)b->next > 0x1ff ); b = b->next; } fclose( memlog ); }
/* main ===================================================================== */ int main (void) { eGifamMode eNewMode = ModeConfort; // Mode de départ eGifamMode eCurrentMode = ModeUnknown; int iTimeOut = 16; // Initialisation des fonctions vLedInit(); vButInit(); vTwiInit (); eTwiSetSpeed (400); // Attente de réponse du tiny45, nécessaire lors d'un démarrage de l'alim. while (iGifamInit () != 0) { if (iTimeOut-- <= 0) { vAssert (0); // bloque et fait clignoter la led 7 } delay_ms (100); } for (;;) { if (eNewMode != eCurrentMode) { while (eNewMode != eCurrentMode) { // Le nouveau mode est différent du courant vGifamSet (eNewMode); // modification du mode eCurrentMode = eGifamGet(); // lecture du mode if (eNewMode != eCurrentMode) { delay_ms (500); vLedToggle (LED_LED1); } } vLedClear (LED_LED1); } // Attente appui BP while (xButGet (BUTTON_BUTTON1) == 0) ; if ( (eNewMode == ModeEco) || (eNewMode == ModeConfortM1)) { // Remets le fil au repos afin de pouvoir mesurer la durée d'activation // des modes étendus vGifamSet (ModeConfort); // Attente appui BP delay_ms (250); while (xButGet (BUTTON_BUTTON1) == 0) { vLedToggle (LED_LED1); delay_ms (50); } } vLedClear (LED_LED1); delay_ms (250); // Passe au mode suivant if (eNewMode < ModeConfortM2) { eNewMode++; } else { eNewMode = ModeConfort; } } return 0; }
// *** Internal private functions, where they key has been translated into a keycode int keyCode( enum key k ) { if ( k >= kMaxKeyCodes ) printf( "ERROR: Requesting keycode %d\n", k ); vAssert( k < kMaxKeyCodes ); return key_codes[k]; }
void validateBlockNext( block* b ) { vAssert( !b->next || b->next == (void*)((uint8_t*)b->data + b->size )); }
void validateBlockPrev( block* b ) { vAssert( !b->prev || b->prev->next == (void*)((uint8_t*)b->prev->data + b->prev->size )); }