Beispiel #1
0
/**
 * Runs all PiThread tests.
 */
void PICC_test_pithread()
{
    ALLOC_ERROR(error);
    test_create_pithread(&error);

    if (HAS_ERROR(error))
        PRINT_ERROR(&error);
}
Beispiel #2
0
Datei: ast.c Projekt: mdoug/FIAL
struct FIAL_ast_node *FIAL_create_ast_node(void)
{
	struct FIAL_ast_node *ret = calloc(sizeof(*ret), 1);
	if(!ret) {
		ALLOC_ERROR("couldn't allocate node!");
		return NULL;
	}
	return ret;
}
Beispiel #3
0
void *_realloc_w(void *buf, size_t size, const char *caller, const char *file, int line) {

	void *buffer;

	buffer = realloc(buf, size);
	if (!buffer) // Exit instead of returning the old memory back to the program
		ALLOC_ERROR(caller, file, line);

	return buffer;
}
Beispiel #4
0
void *_calloc_w(size_t size, const char *caller, const char *file, int line) {

	void *buffer;

	buffer = calloc(1, size);
	if (!buffer)
		ALLOC_ERROR(caller, file, line);

	return buffer;
}
Beispiel #5
0
void *_malloc_w(size_t size, const char *caller, const char *file, int line) {

	void *buffer;

	buffer = malloc(size);
	if (!buffer)
		ALLOC_ERROR(caller, file, line); // We exit here

	return buffer;
}
Beispiel #6
0
static struct usb_device_info * usb_device_is_valid(struct libusb_device * dev) {

	int err, i;
	char product[1024];
	libusb_device_handle * udev;
	struct usb_device_info * ret = NULL;
	struct libusb_device_descriptor desc;

	if ( libusb_get_device_descriptor(dev, &desc) < 0 ) {
		PRINTF_LINE("libusb_get_device_descriptor failed");
		PRINTF_END();
		return NULL;
	}

	for ( i = 0; usb_devices[i].vendor; ++i ) {

		if ( desc.idVendor == usb_devices[i].vendor && desc.idProduct == usb_devices[i].product ) {

			printf("\b\b  ");
			PRINTF_END();
			PRINTF_ADD("Found ");
			usb_flash_device_info_print(&usb_devices[i]);
			PRINTF_END();

			PRINTF_LINE("Opening USB...");

			err = libusb_open(dev, &udev);
			if ( err < 0 ) {
				PRINTF_ERROR("libusb_open failed");
				fprintf(stderr, "\n");
				return NULL;
			}

			usb_descriptor_info_print(udev, dev, product, sizeof(product));

			if ( usb_devices[i].interface >= 0 ) {

				PRINTF_LINE("Detaching kernel from USB interface...");
				libusb_detach_kernel_driver(udev, usb_devices[i].interface);

				PRINTF_LINE("Claiming USB interface...");
				if ( libusb_claim_interface(udev, usb_devices[i].interface) < 0 ) {
					PRINTF_ERROR("libusb_claim_interface failed");
					fprintf(stderr, "\n");
					usb_reattach_kernel_driver(udev, usb_devices[i].interface);
					libusb_close(udev);
					return NULL;
				}

			}

			if ( usb_devices[i].alternate >= 0 ) {
				PRINTF_LINE("Setting alternate USB interface...");
				if ( libusb_set_interface_alt_setting(udev, usb_devices[i].interface, usb_devices[i].alternate) < 0 ) {
					PRINTF_ERROR("libusb_claim_interface failed");
					fprintf(stderr, "\n");
					usb_reattach_kernel_driver(udev, usb_devices[i].interface);
					libusb_close(udev);
					return NULL;
				}
			}

			if ( usb_devices[i].configuration >= 0 ) {
				PRINTF_LINE("Setting USB configuration...");
				if ( libusb_set_configuration(udev, usb_devices[i].configuration) < 0 ) {
					PRINTF_ERROR("libusb_set_configuration failed");
					fprintf(stderr, "\n");
					usb_reattach_kernel_driver(udev, usb_devices[i].interface);
					libusb_close(udev);
					return NULL;
				}
			}

			ret = calloc(1, sizeof(struct usb_device_info));
			if ( ! ret ) {
				ALLOC_ERROR();
				usb_reattach_kernel_driver(udev, usb_devices[i].interface);
				libusb_close(udev);
				return NULL;
			}

			if ( strstr(product, "N900") )
				ret->device = DEVICE_RX_51;
			else if ( strstr(product, "N950") )
				ret->device = DEVICE_RM_680;
			else
				ret->device = DEVICE_UNKNOWN;

			/* TODO: Autodetect more devices */

			if ( device_to_string(ret->device) )
				PRINTF_LINE("Detected USB device: %s", device_to_string(ret->device));
			else
				PRINTF_LINE("Detected USB device: (not detected)");
			PRINTF_END();

			if ( ret->device ) {
				enum device * device;
				for ( device = usb_devices[i].devices; *device; ++device )
					if ( *device == ret->device )
						break;
				if ( ! *device ) {
					ERROR("Device mishmash");
					fprintf(stderr, "\n");
					usb_reattach_kernel_driver(udev, usb_devices[i].interface);
					libusb_close(udev);
					free(ret);
					return NULL;
				}
			}

			ret->hwrev = -1;
			ret->flash_device = &usb_devices[i];
			ret->udev = udev;
			break;
		}
	}

	return ret;

}
Beispiel #7
0
/**
 * Decrements the global reference count of a managed value
 *
 * @pre h != NULL
 *
 * @post h->global_rc = h->global_rc@pre - 1
 *
 * @param Handle to update
 */
void PICC_handle_dec_ref_count(PICC_Handle **h)
{
    /* #ifdef CONTRACT_PRE_INV */
    /*     //inv */
    /*     PICC_Channel_inv(*channel); */
    /* #endif */

    #ifdef CONTRACT_PRE
        //pre
        ASSERT(*h != NULL );
    #endif

    #ifdef CONTRACT_POST
        // capture
        // int global_rc_at_pre = (*h)->global_rc;
    #endif

	// I think there is a problem here: what happens if global_rc is equal to one
	// and another thread was about to increment it but lost the race to acquire the lock ?
	//
	// moreover, for now the lock is alocated in the channel/handle with the previous
	// case in mind we cannot free it in the reclaim
	// I think a solution would be to have a pool of Lock witch contain the lock itself
	// and the value it's suposed to lock
	//
	// kind of:
	// struct{
	//   atomic_int cpt;
	//   void *content;
        // }
	//
	// in the function lock_alloc we would keep track of all the locks and we could
	// reuse the ones that have cpt == 0 && content == NULL
	//

    LOCK_HANDLE(*h);
    (*h)->global_rc--;

    if ((*h)->global_rc == 0) {
	RELEASE_HANDLE(*h);

	ALLOC_ERROR(reclaim_error);
        (*h)->reclaim(*h, &reclaim_error);
	*h = NULL;
        if (HAS_ERROR(reclaim_error))
            CRASH(&reclaim_error);
    }
    else{
	RELEASE_HANDLE(*h);
    }

    /* #ifdef CONTRACT_POST_INV */
    /*     if (*h != NULL) */
    /*         PICC_Channel_inv(*channel); */
    /* #endif */

    /* #ifdef CONTRACT_POST */
    /*     if(global_rc_at_pre > 1) */
    /*         ASSERT((*channel)->global_rc == global_rc_at_pre - 1 ); */
    //cannot be asserted see incr for the same comment
    /* #endif */
}
Beispiel #8
0
static struct usb_device_info * usb_device_is_valid(struct usb_device * dev) {

	int i;
	char product[1024];
	struct usb_device_info * ret = NULL;

	for ( i = 0; usb_devices[i].vendor; ++i ) {

		if ( dev->descriptor.idVendor == usb_devices[i].vendor && dev->descriptor.idProduct == usb_devices[i].product ) {

			printf("\b\b  ");
			PRINTF_END();
			PRINTF_ADD("Found ");
			usb_flash_device_info_print(&usb_devices[i]);
			PRINTF_END();

			PRINTF_LINE("Opening USB...");
			usb_dev_handle * udev = usb_open(dev);
			if ( ! udev ) {
				PRINTF_ERROR("usb_open failed");
				fprintf(stderr, "\n");
				return NULL;
			}

			usb_descriptor_info_print(udev, dev, product, sizeof(product));

#if defined(LIBUSB_HAS_GET_DRIVER_NP) && defined(LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP)
			PRINTF_LINE("Detaching kernel from USB interface...");
			usb_detach_kernel_driver_np(udev, usb_devices[i].interface);
#endif

			PRINTF_LINE("Claiming USB interface...");
			if ( usb_claim_interface(udev, usb_devices[i].interface) < 0 ) {
				PRINTF_ERROR("usb_claim_interface failed");
				fprintf(stderr, "\n");
				usb_close(udev);
				return NULL;
			}

			if ( usb_devices[i].alternate >= 0 ) {
				PRINTF_LINE("Setting alternate USB interface...");
				if ( usb_set_altinterface(udev, usb_devices[i].alternate) < 0 ) {
					PRINTF_ERROR("usb_claim_interface failed");
					fprintf(stderr, "\n");
					usb_close(udev);
					return NULL;
				}
			}

			if ( usb_devices[i].configuration >= 0 ) {
				PRINTF_LINE("Setting USB configuration...");
				if ( usb_set_configuration(udev, usb_devices[i].configuration) < 0 ) {
					PRINTF_ERROR("usb_set_configuration failed");
					fprintf(stderr, "\n");
					usb_close(udev);
					return NULL;
				}
			}

			ret = calloc(1, sizeof(struct usb_device_info));
			if ( ! ret ) {
				ALLOC_ERROR();
				usb_close(udev);
				return NULL;
			}

			if ( strstr(product, "N900") )
				ret->device = DEVICE_RX_51;
			else
				ret->device = DEVICE_UNKNOWN;

			/* TODO: Autodetect more devices */

			if ( device_to_string(ret->device) )
				PRINTF_LINE("Detected USB device: %s", device_to_string(ret->device));
			else
				PRINTF_LINE("Detected USB device: (not detected)");
			PRINTF_END();

			if ( ret->device ) {
				enum device * device;
				for ( device = usb_devices[i].devices; *device; ++device )
					if ( *device == ret->device )
						break;
				if ( ! *device ) {
					ERROR("Device mishmash");
					fprintf(stderr, "\n");
					usb_close(udev);
					return NULL;
				}
			}

			ret->hwrev = -1;
			ret->flash_device = &usb_devices[i];
			ret->udev = udev;
			break;
		}
	}

	return ret;

}