예제 #1
0
파일: dbmem.c 프로젝트: cfrantz/pongo
pgctx_t *dbfile_open(const char *filename, uint32_t initsize)
{
	int ret, i;
	dbroot_t *r;
	pgctx_t *ctx;
	mempool_t *pool;
	memheap_t *heap;
	if (initsize == 0) initsize = 16;

	initsize *= 1024*1024;
	// Create a new context
	ctx = malloc(sizeof(*ctx));
	memset(ctx, 0, sizeof(*ctx));
	for(i=0; i<NR_DB_CONTEXT; i++) {
		if (dbctx[i] == NULL) {
			dbctx[i] = ctx;
			break;
		}
	}
	ret = mm_open(&ctx->mm, filename, initsize);
	if (ret < 0)
		return NULL;
	ctx->root = ctx->mm.map[0].ptr;
	pmem_more_memory = dbfile_more_mem;
#ifdef WANT_UUID_TYPE
	ctx->newkey = _newkey;
#endif

	if (strcmp((char*)ctx->root->signature, DBROOT_SIG)) {
		r = ctx->root;
		strcpy((char*)r->signature, DBROOT_SIG);

		pool = pmem_pool_init((char*)ctx->mm.map[0].ptr+4096, ctx->mm.map[0].size-4096);
		heap = pmem_pool_alloc(pool, 4096);
		heap->nr_procheap = (4096-sizeof(*heap)) / sizeof(procheap_t);
		heap->mempool = _offset(ctx, pool);
		pmem_relist_pools(&ctx->mm, heap);
		r->heap = _offset(ctx, heap);

		// Create the root data dictionary
		ctx->data = dbcollection_new(ctx, 0);
		r->data = ctx->data;

		// Not sure about the atom cache...
		//ctx->cache = DBNULL;
		//ctx->cache = dbcache_new(ctx, 0, 0);
		//r->cache = ctx->cache;

		r->pidcache = dbcollection_new(ctx, 0);
		
		r->meta.chunksize = initsize;
		r->meta.id = dbstring_new(ctx, "_id", 3);
	} else {
		ctx->data = ctx->root->data;
		ctx->cache = ctx->root->cache;
		log_verbose("data=%" PRIx64 " cache=%" PRIx64, ctx->data.all, ctx->cache.all);
		// Probably don't want to run the GC here...
		//db_gc(ctx, NULL);
	}

	// Pidcache in "ctx" points to this process' pidcache.
	// The pidcache in root points to the global pidcache (container
	// of pidcaches)
	ctx->pidcache = DBNULL;
	ctx->sync = 1;
	return ctx;
}
int
main(int argc, char **argv)
{
	char *file = NULL;
	int have_video = 0, have_audio = 0;
	unsigned h = 0, w = 0;
	float fps = 0.0;
	unsigned ch = 0, hz = 0;
	struct audiobuf abuf;
	int end = 0;
	SDL_Surface *display = NULL;
	SDL_Overlay *ovl = NULL;
	SDL_Event event;
	mm_file media;

	if (argc > 1)
		file = argv[1];
	else
		usage(argv[0]);

	if (mm_open(&media, file) <= 0)
		eprintf("No audio or video in `%s'\n", file);

	if (mm_video_info(&media, &w, &h, &fps) >= 0)
	{
		printf("Video data: %dx%d, %gfps\n", w, h, fps);
		have_video = 1;
	}

	if (mm_audio_info(&media, &ch, &hz) >= 0)
	{
		printf("Audio data: %s, %dHz\n", (ch == 1) ? "mono" : "stereo", hz);
		have_audio = 1;
	}

	if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0)
		eprintf("Sdl init failed: %s\n", SDL_GetError());

	if (have_video)
	{
		if ((display = SDL_SetVideoMode(w, h, 24,
					SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL)
			eprintf("SDL_SetVideoMode: %s\n", SDL_GetError());

		if ((ovl = SDL_CreateYUVOverlay(w, h,
					SDL_YV12_OVERLAY, display)) == NULL)
			eprintf("SDL_CreateYUVOverlay: %s\n", SDL_GetError());
	}

	if (have_audio)
	{
		int bytes;
		int bps;
		double tdiff;
		SDL_AudioSpec desired;

		desired.channels = ch;
		desired.freq = hz;
		desired.format = AUDIO_U8;
		bps = ch * 1;
		desired.samples = 4096;
		desired.callback = audio_cb;
		desired.userdata = &abuf;
		if (SDL_OpenAudio(&desired, NULL) < 0)
			eprintf("SDL_OpenAudio: %s\n", SDL_GetError());

		abuf.size = 4 * 4096;
		abuf.off = 0;
		abuf.bytes = 0;
		abuf.buf = xmalloc(abuf.size);

		tdiff = get_time();
		while ((bytes = mm_decode_audio(&media,
					abuf.buf + abuf.bytes, abuf.size - abuf.bytes)) > 0)
		{
			abuf.bytes += bytes;
			if (abuf.size - abuf.bytes <= 4096)
				abuf.buf = xrealloc(abuf.buf, abuf.size *= 2);
		};
		if (bytes < 0)
			eperror("convert_audio");

		printf("Decoding: %.3f seconds\n", get_time() - tdiff);
		printf("Audio: %d samples, %.2f seconds\n", abuf.bytes / bps,
			(double) (abuf.bytes) / bps / desired.freq);

		SDL_PauseAudio(0);
	}

	while (!end)
	{
		while (SDL_PollEvent(&event))
			switch (event.type)
			{
				case SDL_QUIT:
					end = 1;
					break;
				case SDL_KEYDOWN:
					if (event.key.keysym.sym == SDLK_q
						|| event.key.keysym.sym == SDLK_ESCAPE)
						end = 1;
					break;
				default:
					break;
			}

		if (have_video && !end)
		{
			static double oldt, newt;

			if (mm_decode_video(&media, ovl) > 0)
			{
				SDL_Rect r = { 0, 0, w, h };
				newt = 1 / fps + oldt - get_time();
				if (newt > 0)
					SDL_Delay(newt * 1000);
				SDL_DisplayYUVOverlay(ovl, &r);
				oldt = get_time();
			}
			else
				end = 1;
		}

		if (have_audio && abuf.bytes <= 0)
			end = 1;

		if (!have_video)
			SDL_Delay(100);
	}

	if (have_audio)
	{
		SDL_PauseAudio(1);
		free(abuf.buf);
	}

	if (have_video)
	{
		SDL_FreeYUVOverlay(ovl);
	}

	SDL_Quit();
	mm_close(&media);

	return EXIT_SUCCESS;
}
예제 #3
0
int
main(int argc, char **argv)
{
    MM_INFO *mm;
    char *mmtype = NULL;
    int ch;
    SSIZE_T imgoff = 0;
    char *imgtype = NULL;
    IMG_INFO *img;

    progname = argv[0];

    while ((ch = getopt(argc, argv, "i:o:t:vV")) > 0) {
	switch (ch) {
	case 'i':
	    imgtype = optarg;
	    break;

	case 'o':
	    if ((imgoff = parse_offset(optarg)) == -1) {
		tsk_error_print(stderr);
		exit(1);
	    }

	    break;
	case 't':
	    mmtype = optarg;
	    break;
	case 'v':
	    verbose++;
	    break;
	case 'V':
	    print_version(stdout);
	    exit(0);
	case '?':
	default:
	    fprintf(stderr, "Unknown argument\n");
	    usage();
	}
    }

    /* We need at least one more argument */
    if (optind >= argc) {
	fprintf(stderr, "Missing image name\n");
	usage();
    }

    /* open the image */
    if ((img =
	    img_open(imgtype, argc - optind,
		(const char **) &argv[optind])) == NULL) {
	tsk_error_print(stderr);
	exit(1);
    }


    /* process the partition tables */
    if ((mm = mm_open(img, imgoff, mmtype)) == NULL) {
	tsk_error_print(stderr);
	if (tsk_errno == TSK_ERR_MM_UNSUPTYPE)
	    mm_print_types(stderr);

	exit(1);
    }

    print_stats(mm);

    mm->close(mm);
    img->close(img);
    exit(0);
}