void sys_init(NppData nppd)
{
    // Save NPP handles
    _hNpp = nppd._nppHandle;
    _hSci1 = nppd._scintillaMainHandle;
    _hSci2 = nppd._scintillaSecondHandle;
    _hHeap = ::GetProcessHeap();

    // Allocate buffers
    _cfgDir = (LPWSTR)sys_alloc(MAX_PATH * sizeof WCHAR);
    _cfgFile = (LPWSTR)sys_alloc(MAX_PATH * sizeof WCHAR);
    _glbFile = (LPWSTR)sys_alloc(MAX_PATH * sizeof WCHAR);
    _ctxFile = (LPWSTR)sys_alloc(MAX_PATH * sizeof WCHAR);

    _nppVersion = ::SendMessage(_hNpp, NPPM_GETNPPVERSION, 0, 0);
    _winVersion = ::SendMessage(_hNpp, NPPM_GETWINDOWSVERSION, 0, 0);

    // Get NPP's "plugins\Config" directory.
    ::SendMessage(_hNpp, NPPM_GETPLUGINSCONFIGDIR, MAX_PATH, (LPARAM)_ctxFile);

    // Get SessionMgr config directory and create it if missing.
    ::StringCchCopyW(_cfgDir, MAX_PATH, _ctxFile);
    pth::appendSlash(_cfgDir, MAX_PATH);
    ::StringCchCatW(_cfgDir, MAX_PATH, PLUGIN_DLL_NAME);
    ::StringCchCatW(_cfgDir, MAX_PATH, L"\\");
    ::CreateDirectoryW(_cfgDir, NULL);

    // Get the global.xml file pathname and create it if missing.
    ::StringCchCopyW(_glbFile, MAX_PATH, _cfgDir);
    ::StringCchCatW(_glbFile, MAX_PATH, GLB_FILE_NAME);
    pth::createFileIfMissing(_glbFile, GLB_DEFAULT_CONTENT);

    // Get the settings.xml file pathname and load the configuration.
    ::StringCchCopyW(_cfgFile, MAX_PATH, _cfgDir);
    ::StringCchCatW(_cfgFile, MAX_PATH, CFG_FILE_NAME);
    cfg::loadSettings();
    // Create sessions directory if missing.
    ::CreateDirectoryW(cfg::getStr(kSessionDirectory), NULL);
    // Create default session file if missing.
    app_confirmDefaultSession();

    // Get NPP's contextMenu.xml pathname (two directories up from "plugins\Config").
    LPWSTR p = ::wcsstr(_ctxFile, L"plugins\\Config");
    ::StringCchCopyW(p, MAX_PATH, CTX_FILE_NAME);

    // Backup existing config and session files.
    if (cfg::getBool(kBackupOnStartup)) {
        backupFiles();
    }
}
示例#2
0
文件: storage.c 项目: aarzho/mkernel
STORAGE* storage_create(const STORAGE_DEVICE_DESCRIPTOR *device_descriptor, STORAGE_DRIVER_CB *driver_cb,
							void *driver_param)
{
	STORAGE* storage = (STORAGE*)sys_alloc(sizeof(STORAGE));
	if (storage)
	{
		storage->rx_event = event_create();
		storage->tx_event = event_create();
		storage->device_descriptor = device_descriptor;

		storage->driver_cb = driver_cb;
		storage->driver_param = driver_param;

		storage->media_descriptor = NULL;

		storage->host_cb = NULL;
		storage->host_io_cb = NULL;
		storage->host_io_param = NULL;
		storage->reading = false;
		storage->writing = false;
		storage->queue = NULL;
		storage->block_size = 0;
	}
	else
		error(ERROR_MEM_OUT_OF_SYSTEM_MEMORY, "STORAGE");
	return storage;
}
示例#3
0
文件: equeue.c 项目: dnozay/CEnsemble
static void
grow(equeue_t q) {
    void * oarr = q->arr ;
    int oalen = q->alen ;
    int ohead = q->head ;
    ofs_t i ;

    q->head = 0 ;
    q->alen = 2 * q->alen ;
    q->arr = sys_alloc(q->size * q->alen) ;
    memset(q->arr, 0, q->size * q->alen) ;

#ifndef MINIMIZE_CODE
    if (q->alen * q->size > 1<<12) {
	eprintf("QUEUE:expand:getting large:len=%d:%s\n", q->alen * q->size, q->debug) ;
    }
#endif

    /* This could be done with 2 block copies.
     */
    for (i=0;i<q->len;i++) {
	memcpy(buf_ofs(q->arr, i * q->size), 
	       buf_ofs(oarr, ((ohead + i) & (oalen - 1)) * q->size),
	       q->size) ;
    }
    sys_free(oarr) ;
}
示例#4
0
static void* do_alloc_large(size_t len)
{
	node_t** ppn = &heap->index[MEM_SIZ_LARGE];
	ALIGN4096(len);
	void* mem = sys_alloc(NULL, len);
	
	while (*ppn)
	{
		if ((*ppn)->next && ((*ppn)->next->addr > mem))
		{
			node_t* tmp = get_free_node();
			tmp->next = (*ppn)->next;
			tmp->addr = mem;
			tmp->len = len;
			tmp->stat = MEM_STAT_USED;
			(*ppn)->next = tmp;
			return mem;
		}
		ppn = &(*ppn)->next;
	}
	*ppn = get_free_node();
	(*ppn)->addr = mem;
	(*ppn)->len = len;
	(*ppn)->next = NULL;
	(*ppn)->stat = MEM_STAT_USED;
	return mem;
}
示例#5
0
static ErlDrvData
s1_start(ErlDrvPort port, char *args)
{
    descriptor_t        *desc;
    int                 i = 0;

    i = i;
    edtk_debug("%s: starting, port = %ld, args = 0x%lx, %s", __FUNCTION__,
               port, (unsigned long) args, args);

    if ((desc = (descriptor_t *) sys_alloc(sizeof(descriptor_t))) == NULL) {
        return ERL_DRV_ERROR_GENERAL;
    }
    memset(desc, 0, sizeof(descriptor_t));
    desc->port = port;
    desc->nextxid = 1;
    for (i = 0; i < 32; i++) {
        desc->valmap_fd[i] = -1;
    }
    /* TODO: Finish initializing descriptor members */

    /* TODO: Take care of other port initialization tasks */

    return (ErlDrvData) desc;
}
示例#6
0
文件: exp1_drv.c 项目: e42s/jungerl
PRIVATESTUFF int
exp1_read(descriptor *d, char *buf, int buflen)
{
    char		cmd;
    struct t_data	*td = sys_alloc(sizeof(struct t_data));

    assert(td != NULL);
    assert(buflen >= 4);
    cmd = *buf++;
    td->cmd = cmd;
    td->id = get_int32(buf);
    buf += 4;
    td->name = NULL;

    fprintf(DEBUGIO, "exp1_read: cmd = %d, id = %ld\r\n", cmd, td->id);
    switch (cmd) {
    case EXP1_REQ_GETHOSTBYNAME:
        /* buf = null-terminated hostname */
        /* XXX use something other than strdup()? */
        td->name = strdup(buf);
        driver_async(d->port, KEY, invoke_gethostbyname, (void *) td,
                     free_t_data);
        break;
    }
    /*
    ** XXX I guess we'll ignore everything else, let the caller hang.
    */
    fprintf(DEBUGIO, "exp1_read: done\r\n");
    return 0;
}
示例#7
0
static ErlDrvData tun_start(ErlDrvPort port, char *args)
{
    struct tun_state *state;
    int fd;

    int mode;
    char dev_name[IFNAMSIZ];

    state = (struct tun_state*) sys_alloc(sizeof(struct tun_state));
    if (state == NULL) {
        errno = ENOMEM;         /* appropriate to set errno? */
        return ERL_DRV_ERROR_ERRNO;
    }

    if (!parse_args(args, &mode, state->dev, IFNAMSIZ - 1)) {
        return ERL_DRV_ERROR_BADARG;
    }

    fd = make_if(mode, state->dev);
    if (fd < 0) {
        return ERL_DRV_ERROR_GENERAL;
    }
    state->port = port;
    state->fd = fd;
    state->active = ACTIVE_FALSE;
    set_input(state, 0);

    return (ErlDrvData)state;
}
void ui_menucreate(ui_widget_t* widget)
{
	ui_menudata_t* data=(ui_menudata_t*)sys_alloc(sizeof(ui_menudata_t));
	memset(data,0,sizeof(ui_menudata_t));
	data->selected=-1;
	data->type=UI_MENU_VERTICAL;
	widget->data=data;
}
mt_divider_t* mt_createdivider(unsigned int depth,const mt_vector3_t min,const mt_vector3_t max)
{
	mt_divider_t* divider=sys_alloc(sizeof(mt_divider_t));

	memset(divider,0,sizeof(mt_divider_t));
	
	divider->root=sys_alloc(sizeof(mt_dividernode_t));
	memset(divider->root,0,sizeof(mt_dividernode_t));
	mt_vec3copy(divider->root->min,min);
	mt_vec3copy(divider->root->max,max);
	divider->depth=depth;

	//Start subdivision
	mt_dividenode(divider->root,depth);

	return divider;
}
示例#10
0
void heap_init(heap_t* heap)
{
	assert(sizeof(void*) == 8);
	assert(sizeof(page_t) == 4096);

	memset(heap, 0, sizeof(heap_t));
	heap->ppage = (page_t*)sys_alloc(NULL, sizeof(page_t));
	heap->free_node = nodepage_init(heap->ppage);
}
示例#11
0
文件: equeue.c 项目: dnozay/CEnsemble
equeue_t equeue_create(debug_t debug, int size) {
    equeue_t q ;
    q = record_create(equeue_t, q) ;
    q->debug = debug ;
    q->size = size ;
    q->alen = 1<<5 ;		/* must be a power of 2 */
    q->head = 0 ;
    q->len = 0 ;
    q->arr = sys_alloc(size * q->alen) ;
    memset(q->arr, 0, size * q->alen) ;
    return q ;
}
示例#12
0
文件: exp1_drv.c 项目: e42s/jungerl
PRIVATESTUFF long
exp1_start(int port, char *args)
{
    descriptor	*d;

    fprintf(DEBUGIO, "exp1_start: starting, args = 0x%lx, %s\r\n", (unsigned long) args, args);

    if ((d = (descriptor *) sys_alloc(sizeof(descriptor))) == NULL)
        return -1;
    d->fd = -1;
    d->port = port;
    d->flags = 0;
    return (long) d;
}
示例#13
0
extern void uart_enable(UART_CLASS port, UART_CB *cb, void *param, int priority)
{
	if (port < UARTS_COUNT)
	{
		UART_HW* uart = (UART_HW*)sys_alloc(sizeof(UART_HW));
		if (uart)
		{
			_uart_handlers[port] = uart;
			uart->cb = cb;
			uart->param = param;
			uart->read_buf = NULL;
			uart->write_buf = NULL;
			uart->read_size = 0;
			uart->write_size = 0;
			//setup pins
#if defined(STM32F1)
			if ((USART_TX_DISABLE_MASK & (1 << port)) == 0)
				gpio_enable_afio(UART_TX_PINS[port], AFIO_MODE_PUSH_PULL);
			if ((USART_RX_DISABLE_MASK & (1 << port)) == 0)
				gpio_enable(UART_RX_PINS[port]);
#if (USART_REMAP_MASK)
			if ((1 << port) & USART_REMAP_MASK)
				afio_remap();
#endif //USART_REMAP_MASK
#elif defined(STM32F2)
			if ((USART_TX_DISABLE_MASK & (1 << port)) == 0)
				gpio_enable_afio(UART_TX_PINS[port], port < UART_4 ? AFIO_MODE_USART1_2_3 : AFIO_MODE_UART_4_5_USART_6);
			if ((USART_RX_DISABLE_MASK & (1 << port)) == 0)
				gpio_enable_afio(UART_RX_PINS[port], port < UART_4 ? AFIO_MODE_USART1_2_3 : AFIO_MODE_UART_4_5_USART_6);
#endif

			//power up
			if (port == UART_1 || port == UART_6)
				RCC->APB2ENR |= RCC_UART[port];
			else
				RCC->APB1ENR |= RCC_UART[port];

			//enable interrupts
			NVIC_EnableIRQ(UART_IRQ_VECTORS[port]);
			NVIC_SetPriority(UART_IRQ_VECTORS[port], priority);

			USART[port]->CR1 |= USART_CR1_UE;
		}
		else
			error_dev(ERROR_MEM_OUT_OF_SYSTEM_MEMORY, DEV_UART, port);
	}
	else
		error_dev(ERROR_DEVICE_INDEX_OUT_OF_RANGE, DEV_UART, port);
}
示例#14
0
static int ctl_reply(int rep, char *buf, int len, char **rbuf, int rsize)
{
    char* ptr;

    if ((len+1) > rsize) {
	ptr = (char *)sys_alloc(len+1);
        assert(ptr);
	*rbuf = ptr;
    }
    else
	ptr = *rbuf;
    *ptr++ = rep;
    memcpy(ptr, buf, len);
    return len+1;
}
示例#15
0
static void append_node()
{
	page_t* pp = heap->ppage;
	while (pp->next)
	{
		pp = pp->next;
	}
	pp->next = (page_t*)sys_alloc(NULL, sizeof(page_t));

	node_t** ppn = &heap->free_node;
	while (*ppn)
	{
		ppn = &(*ppn)->next;
	}
	*ppn = nodepage_init(pp->next);
}
示例#16
0
文件: iq.c 项目: dnozay/CEnsemble
/* Grow to a larger size array.
 */
void iq_grow(iq_t iq, seqno_t nlen) {
    len_t olen = iq->alen ;
    unsigned omask = iq->mask ;
    unsigned nmask ;
    item_t *na ;
    seqno_t abs ;

    iq_check(iq) ;
    nlen = seqno_max(nlen, olen) ;
    nlen = 1 << log2(2*nlen-1) ;
    nlen = seqno_max(nlen, 16) ;
    nmask = nlen - 1 ;

#if 0
    eprintf("nlen=%d lo=%d maxi=%d nhi=%d pop=%d\n", nlen, iq->lo, maxi(iq), iq->lo + nlen,
	    iq_population(iq)) ;
#endif

    na = sys_alloc(sizeof(na[0]) * nlen) ;

    for (abs=iq->lo;abs<maxi(iq);abs++) {
	seqno_t oi = abs & omask ;
	seqno_t ni = abs & nmask ;
	na[ni] = iq->arr[oi] ;
    }

    for (abs=maxi(iq);abs<iq->lo+nlen;abs++) {
	seqno_t ni = abs & nmask ;
	na[ni].msg = NULL ;
    }
#if 0
    for (abs=iq->lo;abs<iq->lo+nlen;abs++) {
	seqno_t ni = abs & nmask ;
	assert(na[ni].ctl == UNSET ||
	       na[ni].ctl == DATA) ;
    }
#endif

    sys_free(iq->arr) ;
    iq->arr = na ;
    iq->alen = nlen ;
    iq->mask = nmask ;
    iq_check(iq) ;
#if 0
    eprintf("pop=%d nmask=0x%08x\n", iq_population(iq), iq->mask) ;
#endif
}
static void mt_dividenode(mt_dividernode_t* node,unsigned int depth)
{
	const mt_vector3_t middle={(node->max[0]+node->min[0])/2,
		(node->max[1]+node->min[1])/2,
		(node->max[2]+node->min[2])/2};
	mt_dividernode_t* child;
	mt_vector3_t min,max;
	int i,j,k;

	node->colliders=0;
	node->numcolliders=0;
	node->maxcolliders=0;

	//Do not subdivide more than needed
	if(depth<=0)
	{
		node->children=0;
		return;
	}

	node->children=sys_alloc(sizeof(mt_dividernode_t)*8);

	//Subdivide this node between 8 children
	for(i=0;i<2;i++)
	{
		min[0]=(i==0)?node->min[0]:middle[0];
		max[0]=(i==0)?middle[0]:node->max[0];
		for(j=0;j<2;j++)
		{
			min[1]=(j==0)?node->min[1]:middle[1];
			max[1]=(j==0)?middle[1]:node->max[1];
			for(k=0;k<2;k++)
			{
				min[2]=(k==0)?node->min[2]:middle[2];
				max[2]=(k==0)?middle[2]:node->max[2];

				child=&node->children[(i<<2)+(j<<1)+k];
				mt_vec3copy(child->min,min);
				mt_vec3copy(child->max,max);
				mt_dividenode(child,depth-1);
			}
		}
	}
}
示例#18
0
static void expand_heap(size_t i)
{
	void* mem = sys_alloc(NULL, 4096);
	node_t** ppn = &heap->index[i];

	while (*ppn)
	{
		if ((*ppn)->addr > mem)
		{
			break;
		}
		ppn = &(*ppn)->next;
	}

	node_t* tmp = get_free_node();
	tmp->addr = mem;
	tmp->len = 4096;
	tmp->next = *ppn;
	tmp->stat = MEM_STAT_FREE;
	*ppn = tmp;
}
示例#19
0
文件: iq.c 项目: dnozay/CEnsemble
iq_t iq_create(debug_t debug, iq_free_t free) {
    iq_t iq = record_create(iq_t, iq) ;
    iq->lo = 0 ;
    iq->hi = 0 ;
    iq->alen = 1 ;
    iq->read = 0 ;
    iq->name = debug ;
    iq->mask = 0 ;
    iq->arr = sys_alloc(sizeof(iq->arr[0])) ;
    iq->free = free ;
    assert(iq->arr) ;
    memset(iq->arr, 0, sizeof(iq->arr[0])) ;
    iq->arr[0].msg = NULL ;
#if 0
#ifdef USE_GC
    gc_tag(iq, debug) ;
    gc_tag(iq->arr, debug) ;
#endif
#endif
    return iq ;
}
e_texture_t* e_creategradient(e_device_t* device,const unsigned char* values,unsigned int numvalues)
{	
	e_texture_t* texture;

	//We load a gradient that will be used for shading
	if(e_pow2(numvalues)!=numvalues)
		return 0;

	texture=(e_texture_t*)sys_alloc(sizeof(e_texture_t));
	texture->width=numvalues;
	texture->height=1;
	texture->bpp=8;

	glGenTextures(1,&texture->id);
	glBindTexture(GL_TEXTURE_1D,texture->id);

	glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexImage1D(GL_TEXTURE_1D,0,GL_RGBA,numvalues,0,GL_LUMINANCE,GL_UNSIGNED_BYTE,values);
	glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_WRAP_S,GL_CLAMP);

	return texture;
}
示例#21
0
static inline QUEUE* svc_queue_create(unsigned int block_size, unsigned int blocks_count, unsigned int align)
{
	QUEUE* queue = NULL;
	int i;
	//first, try to allocate space for queue data. In thread's current mempool
	unsigned int align_offset = sizeof(DLIST);
	if (align > align_offset)
		align_offset = align;
	void* mem_block = malloc_aligned(blocks_count * (block_size + align_offset), align_offset);
	if (mem_block)
	{
		queue = sys_alloc(sizeof(QUEUE));
		if (queue != NULL)
		{
			queue->align_offset = align_offset;
			queue->mem_block = mem_block;
			queue->pull_waiters = NULL;
			queue->push_waiters = NULL;
			DO_MAGIC(queue, MAGIC_QUEUE);
			//set all as free
			queue->free_blocks = NULL;
			queue->filled_blocks = NULL;
			for (i = 0; i < blocks_count; ++i)
				dlist_add_tail(&queue->free_blocks, (DLIST*)((unsigned int)mem_block + i * (block_size + align_offset)));
		}
		else
		{
			free(mem_block);
			fatal_error(ERROR_MEM_OUT_OF_SYSTEM_MEMORY, QUEUE_NAME);
		}
	}
	else
		error(ERROR_MEM_OUT_OF_HEAP, svc_thread_name(svc_thread_get_current()));

	return queue;
}
// ===========================================================================
// ===========================================================================
void fft_myrmics_cfft2_row(rid_t r_single_row, float **a, float *w, int n, 
                           int tile_size, int blk_size) {
  int i, j, k;
  int off;
  float *pa;

//printf("%d: row begin\r\n", sys_get_worker_id());

  // tmp buffer in order to hold the data of a single row
  pa = sys_alloc(2 * n * sizeof(float), r_single_row);

  for (i = 0; i < blk_size; i++) {

    off = 2 * i * blk_size;

    // merge data from all blocks "a" into a single table "pa"
    for (j = 0; j < tile_size; j++) {
      for (k = 0; k < 2*blk_size; k++) {
        pa[j*2*blk_size+k] = a[j][k+off]; 
      }
    }

    fft_myrmics_cfft2(n, pa, pa, w);

    // write results back from "pa" to block "a"
    for (j = 0; j < tile_size; j++) {
      for (k = 0; k < 2*blk_size; k++) {
        a[j][k+off] = pa[j*2*blk_size+k];
      }
    }
  }

  sys_free(pa);

printf("%d: row end\r\n", sys_get_worker_id());
}
示例#23
0
static void s1_outputv(ErlDrvData drv_data, ErlIOVec *ev)
{
    descriptor_t        *desc = (descriptor_t *) drv_data;
    unsigned char       cmd;
    int                 p = 0, q = 1;
    callstate_t         *c = NULL;
    int                 do_async_call = default_async_calls;
    unsigned long       binlen;
    int                 index;
    void                *tmp = NULL;

    binlen = binlen;
    index = index;
    tmp = tmp;
    if (desc == NULL || ev == NULL || ev->size < 1) {
        edtk_debug("%s: bad arg(s)", __FUNCTION__);
        return;
    }
    if (! EV_GET_CHAR(ev, &cmd, &p, &q)) {
        edtk_debug("%s: empty command", __FUNCTION__);
        reply_error(desc, EINVAL);
    }
    if ((c = sys_alloc(sizeof(callstate_t))) == NULL) {
        reply_error(desc, ENOMEM);
        return;
    }
    c->cmd = cmd;
    c->key = NULL;
    c->free = sys_free;
    c->xid = 0;         /* XXX unused right now */
    c->o.__expect = 1;  /* Default is that expectation is always met */

    edtk_debug("%s: my threadid = %lx, cmd = %d", __FUNCTION__, pthread_self(), cmd);
    switch (cmd) {
    case S1_DEBUG:
        EV_GET_UINT32(ev, &edtk_debug_flag, &p, &q);
        reply_ok_num(desc, edtk_debug_flag);    /* Immediate reply */
        sys_free(c);
        c = NULL;
        break;
    case S1_NULL:
        c->invoke = invoke_s1_null;
        break;
    case S1_OPEN:
        c->invoke = invoke_s1_open;
        EV_GET_UINT32(ev, &binlen, &p, &q);
        c->i.filename = (char *) EV_GETPOS(ev, p, q);
        if (edtk_ev_forward_N(ev, binlen, &p, &q, 1) < 0) {
            goto error;
        }
        EV_GET_UINT32(ev, &c->i.flags, &p, &q);
        break;
    case S1_GETFD:
        c->invoke = invoke_s1_getfd;
        EV_GET_UINT32(ev, &index, &p, &q);
        if (desc->valmap_fd[index] == -1) {
            goto error;
        } else {
            edtk_debug("%s: valmap fd index %d = 0x%lx", __FUNCTION__, index, desc->valmap_fd[index]);
            c->i.fd = desc->valmap_fd[index];
            c->i.__valmap_fd_index = index;
        }
        break;
    case S1_SENDFD:
        c->invoke = invoke_s1_sendfd;
        EV_GET_UINT32(ev, &c->i.unixdom_fd, &p, &q);
        EV_GET_UINT32(ev, &c->i.fd_to_be_sent, &p, &q);
        break;
    case S1_RECEIVEFD:
        c->invoke = invoke_s1_receivefd;
        EV_GET_UINT32(ev, &c->i.unixdom_fd, &p, &q);
        break;
    case S1_CLOSE:
        c->invoke = invoke_s1_close;
        EV_GET_UINT32(ev, &index, &p, &q);
        if (index == -1 && 0 == 1) {
            edtk_debug("%s: valmap fd index %d = default value 0x%lx", __FUNCTION__, index, -1);
            c->i.fd = -1;
            c->i.__valmap_fd_index = -1;
        } else if (desc->valmap_fd[index] == -1) {
            goto error;
        } else {
            edtk_debug("%s: valmap fd index %d = 0x%lx", __FUNCTION__, index, desc->valmap_fd[index]);
            c->i.fd = desc->valmap_fd[index];
            c->i.__valmap_fd_index = index;
        }
        break;
    }

    if (c != NULL) {
        if (do_async_call) {
            driver_async(desc->port, c->key, c->invoke, c, c->free);
        } else {
            /*
            ** Execute the bottom half right away, then send the result.
            */
            (*(c->invoke))((void *) c);
            s1_ready_async((ErlDrvData) desc, (ErlDrvThreadData) c);
            /*
            ** c is already freed for us by s1_ready_async()
            */
        }
    }
    return;

  error:
    if (c != NULL) {
        sys_free(c);
    }
    reply_error_atom(desc, am_badarg);
}
示例#24
0
文件: ulib.c 项目: wwffcc/ThuMips32
void* malloc(int size){
    return sys_alloc(size);
}
// ===========================================================================
// fft_myrmics()                Myrmics version of FFT
//                              A n x n input table is partitioned into
//                              tile_size x tile_size sub-blocks where each
//                              block has size (n/tile_size) x (n/tile_size)
// ===========================================================================
// * INPUTS
//   int n                      Number of rows and columns of the input table
//   int tile_size              Number of tile rows and columns in the 2D layout
// ===========================================================================
void fft_myrmics(int tile_size, int n) {

  rid_t                 r;
  rid_t                 *ra_row;
  rid_t                 *rb_row;
  int                   blk_size;
  int                   blk_size_sq;
  int                   i, j, k, l;
  float                 ***a;
  float                 ***a_copy;
  float                 ***b;
  float                 ***b_copy;
  float                 **w;
  unsigned int          time_start;
  void                  *args[4];
  unsigned int          deps[4];



  // Sanity checks
  if (n & (n - 1)) {
    printf("N must be a power of 2\r\n");
    return;
  }
  if (tile_size & (tile_size - 1)) {
    printf("Tile size must be a power of 2\r\n");
    return;
  }


  blk_size = n/tile_size;
  blk_size_sq = blk_size * blk_size;

  // Create holding region
  r = sys_ralloc(0, 99); // highest level
  a = sys_alloc(tile_size * sizeof(float **), r);
  b = sys_alloc(tile_size * sizeof(float **), r);
  w = sys_alloc(tile_size * sizeof(float *), r);

  // Initialize tiles and regions
  ra_row = sys_alloc(tile_size * sizeof(rid_t), r);
  rb_row = sys_alloc(tile_size * sizeof(rid_t), r);

  for (i = 0; i < tile_size; i++) {

    // Create a region for each row of tiles, for buffers a and b
    ra_row[i] = sys_ralloc(r, 0);
    rb_row[i] = sys_ralloc(r, 0);

    // Allocate pointers for each tile in the row
    a[i] = sys_alloc(tile_size * sizeof(float *), ra_row[i]);
    b[i] = sys_alloc(tile_size * sizeof(float *), rb_row[i]);

    // Allocate tiles in the row
    sys_balloc(2* blk_size_sq * sizeof(float), ra_row[i], tile_size, 
               (void *) a[i]);
    sys_balloc(2* blk_size_sq * sizeof(float), rb_row[i], tile_size, 
               (void *) b[i]);

    // Allocate w array
    w[i] = sys_alloc(2 * n * sizeof(float), r);

    // Initialize tables with some values
    for (j = 0; j  < tile_size; j++) {
      for (k = 0; k < blk_size; k++) {
        for (l = 0; l < 2 * blk_size; l++) {
          a[i][j][k * 2 * blk_size + l] = 0.01F;
        }
      }
    }
    for (j = 0; j < 2 * n; j++) {
      w[i][j] = 0.3F;
    }
  }

  // Copy the pointers of the a and b tables, because we'll need them in
  // fft_myrmics_Xpose() to spawn the tasks. We need to do this, because in
  // fft_myrmics_cfft2_phase() we delegate all r_row[*] to children tasks:
  // a[*][*] and b[*][*] are allocated in these regions, so we don't have
  // access there anymore from the master task.
  a_copy = sys_alloc(tile_size * sizeof(float **), 0);
  b_copy = sys_alloc(tile_size * sizeof(float **), 0);

  sys_balloc(tile_size * sizeof(float *), 0, tile_size, (void *) a_copy);
  sys_balloc(tile_size * sizeof(float *), 0, tile_size, (void *) b_copy);

  for (i = 0; i < tile_size; i++) {
    for (j = 0; j < tile_size; j++) {
      a_copy[i][j] = a[i][j];
      b_copy[i][j] = b[i][j];
    }
  }


  // Starting FFT
  printf("FFT 2D-block of %d x %d starting split into %d x %d tiles \r\n",
              n, n, tile_size, tile_size);

  // Start time
  time_start = ar_free_timer_get_ticks();


  // Run first phase on buffer a
  fft_myrmics_cfft2_phase(ra_row, a, w, n, tile_size, blk_size);

  // Transpose a->b
  fft_myrmics_Xpose(a_copy, rb_row, b, tile_size, blk_size);

  // Run second phase on buffer b
  fft_myrmics_cfft2_phase(rb_row, b, w, n, tile_size, blk_size);

  // Transpose b->a
  fft_myrmics_Xpose(b_copy, ra_row, a, tile_size, blk_size);



  // Stop time
  args[0] = (void *) r;
  deps[0] = SYS_TYPE_REGION_ARG | SYS_TYPE_INOUT_ARG;
  args[1] = (void *) time_start;
  deps[1] = SYS_TYPE_BYVALUE_ARG;
  sys_spawn(3, args, deps, 2); // fft_myrmics_time()
  
  // Checksum
  args[0] = (void *) r;
  deps[0] = SYS_TYPE_REGION_ARG | SYS_TYPE_INOUT_ARG;
  args[1] = (void *) a;
  deps[1] = SYS_TYPE_BYVALUE_ARG;
  args[2] = (void *) tile_size;
  deps[2] = SYS_TYPE_BYVALUE_ARG;
  args[3] = (void *) blk_size;
  deps[3] = SYS_TYPE_BYVALUE_ARG;
  sys_spawn(4, args, deps, 4); // fft_myrmics_checksum()

printf("%d: spawns done\r\n", sys_get_worker_id());
}
static e_texture_t* e_loadjpg(e_device_t* device,int mipmaps,fs_handle_t file)
{
	struct jpeg_decompress_struct cinfo;
	unsigned char* data=0;
	unsigned int width,height;
	unsigned int stride;
	e_jpgerror_t error;
	e_jpgsource_t* src=0;
	e_texture_t* texture;

	cinfo.err=jpeg_std_error(&error.mgr);
	error.mgr.error_exit=e_jpgerror;

	if(setjmp(error.buffer))
	{
		//Destroy memory
		src=(e_jpgsource_t*)cinfo.src;
		jpeg_destroy_decompress(&cinfo);
		if(src)
		{
			sys_free(src->buffer);
			sys_free(src);
		}
		if(data)
			sys_free(data);
		return 0;
	}

	cinfo.src=0;
	jpeg_create_decompress(&cinfo);

	cinfo.src=(struct jpeg_source_mgr*)sys_alloc(sizeof(e_jpgsource_t));

	//Init data source
	src=(e_jpgsource_t*)cinfo.src;
	src->mgr.init_source=e_jpgnop;
	src->mgr.fill_input_buffer=e_jpgfillbuffer;
	src->mgr.skip_input_data=e_jpgskipdata;
	src->mgr.resync_to_restart=jpeg_resync_to_restart;
	src->mgr.term_source=e_jpgnop;
	src->mgr.bytes_in_buffer=0;
	src->mgr.next_input_byte=0;
	src->file=file;

	src->size=256;
	src->buffer=sys_alloc(sizeof(unsigned char)*src->size);

	jpeg_read_header(&cinfo,TRUE);
	jpeg_start_decompress(&cinfo);

	//Do not accept textures with non-power-of-two dimensions
	if(cinfo.output_width!=e_pow2(cinfo.output_width)||
		cinfo.output_height!=e_pow2(cinfo.output_height))
		longjmp(error.buffer,1);

	//Force RGB output
	cinfo.out_color_space=JCS_RGB;
	width=cinfo.output_width;
	height=cinfo.output_height;

	stride=sizeof(unsigned char)*3*cinfo.output_width;
	data=(unsigned char*)sys_alloc(stride*cinfo.output_height);

	//Read each scanline
	while(cinfo.output_scanline<cinfo.output_height)
	{
		unsigned char* ptr=data+stride*(cinfo.output_height-(cinfo.output_scanline+1));
		unsigned char** row=&ptr;
		jpeg_read_scanlines(&cinfo,row,1);
	}

	jpeg_finish_decompress(&cinfo);
	jpeg_destroy_decompress(&cinfo);

	sys_free(src->buffer);
	sys_free(src);

	texture=(e_texture_t*)sys_alloc(sizeof(e_texture_t));
	texture->width=width;
	texture->height=height;
	texture->bpp=24;

	glGenTextures(1,&texture->id);
	glBindTexture(GL_TEXTURE_2D,texture->id);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	if(mipmaps)
		gluBuild2DMipmaps(GL_TEXTURE_2D,3,texture->width,texture->height,GL_RGB,GL_UNSIGNED_BYTE,data);
	else
		glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,texture->width,texture->height,0,GL_RGB,GL_UNSIGNED_BYTE,data);

	sys_free(data);
	return texture;
}
static e_texture_t* e_loadpng(e_device_t* device,int mipmaps,fs_handle_t file)
{
	unsigned int width,height;
	unsigned int rowbytes;
	unsigned char* data;
	unsigned char** rows;
	unsigned int i;
	int bpp,type;
	int format;

	png_structp png;
	png_infop info;
	e_texture_t* texture;

	if(!e_checkpng(file))
		return 0;

	png=png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,(png_error_ptr)0,(png_error_ptr)0);
	if(!png)
		return 0;

	info=png_create_info_struct(png);
	if(!info)
	{
		png_destroy_read_struct(&png,0,0);
		return 0;
	}

	//Needed by libpng
	if(setjmp(png_jmpbuf(png)))
	{
        png_destroy_read_struct(&png,&info,0);
		if(rows)
			sys_free(rows);
		if(data)
			sys_free(data);
        return 0;
    }

	//Read up to image data
	png_set_read_fn(png,(png_voidp)file,e_readpng);
	png_set_sig_bytes(png,8);
	png_read_info(png,info);
	png_get_IHDR(png,info,&width,&height,&bpp,&type,0,0,0);

	if(width!=e_pow2(width)||
		height!=e_pow2(height))
		longjmp(png_jmpbuf(png),1);

	//Convert this image to fit the required format
	if(type==PNG_COLOR_TYPE_PALETTE)
		png_set_palette_to_rgb(png);
	if(type==PNG_COLOR_TYPE_GRAY||
        type==PNG_COLOR_TYPE_GRAY_ALPHA)
        png_set_gray_to_rgb(png);
	if(type==PNG_COLOR_TYPE_GRAY||bpp<8)
        png_set_expand(png);
    if(png_get_valid(png,info,PNG_INFO_tRNS))
        png_set_tRNS_to_alpha(png);

	png_read_update_info(png,info);
	rowbytes=png_get_rowbytes(png,info);

	//Allocate memory to store the image
	data=(unsigned char*)sys_alloc(rowbytes*height);
	if(!data)
		longjmp(png_jmpbuf(png),1);

	rows=(unsigned char**)sys_alloc(sizeof(unsigned char*)*height);
	if(!rows)
		longjmp(png_jmpbuf(png),1);

	for(i=0;i<height;i++)
		rows[i]=data+(height-(i+1))*rowbytes;

	//Read png
	png_read_image(png,rows);
	png_read_end(png,0);
	sys_free(rows);

	texture=(e_texture_t*)sys_alloc(sizeof(e_texture_t));
	texture->width=width;
	texture->height=height;

	glGenTextures(1,&texture->id);
	glBindTexture(GL_TEXTURE_2D,texture->id);

	texture->bpp=(type&PNG_COLOR_MASK_ALPHA)?32:24;
	format=(type&PNG_COLOR_MASK_ALPHA)?GL_RGBA:GL_RGB;
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	if(mipmaps)
		gluBuild2DMipmaps(GL_TEXTURE_2D,(type&PNG_COLOR_MASK_ALPHA)?4:3,texture->width,texture->height,format,GL_UNSIGNED_BYTE,data);
	else
		glTexImage2D(GL_TEXTURE_2D,0,format,texture->width,texture->height,0,format,GL_UNSIGNED_BYTE,data);

	sys_free(data);
	return texture;
}
示例#28
0
void* myrealloc(void* addr, size_t len)
{
	size_t i1 = ind(len);
	size_t i2 = MEM_SIZ_LARGE;
	node_t* prev = NULL;
	node_t* pn = NULL;
	if (addr_find(addr, &prev, &pn, &i2))
	{
		if (i2 != MEM_SIZ_LARGE)
		{
			if (i1 == i2)
			{
				return addr;
			}
			else
			{
				void* ret = myalloc(len);
				memcpy(ret, addr, (pn->len < len) ? pn->len : len);
				myfree(addr);
				return ret;
			}
		}
		else
		{
			if (i1 == i2)
			{
				ALIGN4096(len);
				if (len <= pn->len)
				{
					return pn->addr;
				}
				else
				{
					size_t delta = len - pn->len;
					void* p = sys_try_alloc((void*)((size_t)pn->addr + pn->len), delta);
					if (p)
					{
						pn->len = len;
						return pn->addr;
					}
					else
					{
						void* src = pn->addr;
						pn->addr = sys_alloc(NULL, len);
						memcpy(pn->addr, src, pn->len);
						sys_free(src, pn->len);
						pn->len = len;
						return pn->addr;
					}
				}
			}
			else
			{
				void* ret = myalloc(len);
				memcpy(ret, addr, len);
				myfree(addr);
				return ret;
			}
		}
	}
	return NULL;
}