int* fread_int_array(FILE *file, const char *array_name, unsigned int* sizeOut)
{
    unsigned int i, size;
    int* arr;

    fscanf(file, array_name);
    fscanf(file, "[%u]: ", &size);

    arr = (int*) mwMalloc(sizeof(int) * size);

    for (i = 0; i < size; i++)
    {
        if (fscanf(file, "%d", &arr[i]) != 1)
        {
            mw_printf("Error reading into %s\n", array_name);
            free(arr);
            return NULL;
        }

        if (i < size - 1)
            fscanf(file, ", ");
    }
    fscanf(file, "\n");

    if (sizeOut)
        *sizeOut = size;

    return arr;
}
real* popRealArray(lua_State* luaSt, int* outN)
{
    real* arr;
    int i, n, table;

    table = lua_gettop(luaSt);
    luaL_checktype(luaSt, table, LUA_TTABLE);
    n = luaL_getn(luaSt, table);  /* get size of table */

    arr = (real*) mwMalloc(sizeof(real) * n);
    for (i = 0; i < n; ++i)
    {
        lua_rawgeti(luaSt, table, i + 1);  /* push t[i] */
        luaL_checktype(luaSt, -1, LUA_TNUMBER);
        arr[i] = lua_tonumber(luaSt, -1);
        lua_pop(luaSt, 1);
    }

    lua_pop(luaSt, 1);

    if (outN)
        *outN = n;

    return arr;
}
static char* replaceUAVIds(const char* ilSrc, size_t* lenOut, ...)
{
    char* buf;
    va_list argPtr;
    size_t len;
    int rc;

    len = strlen(ilSrc);
    buf = (char*) mwMalloc(len + 1);
    buf[len] = '\0';

    va_start(argPtr, lenOut);
    rc = vsprintf(buf, ilSrc, argPtr);
    va_end(argPtr);

    /* Should be == len when uavid = 2 digits, slighly less when uavid = 1 digit */
    if ((size_t) rc > len)
    {
        free(buf);
        return NULL;
    }

    if (lenOut)
    {
        *lenOut = len;
    }

    return buf;
}
/*Functions for reading parameters from files */
real* fread_double_array(FILE* file, const char* array_name, unsigned int* sizeOut)
{
    unsigned int i, size;
    int rc;
    real* arr;

    fscanf(file, array_name);
    fscanf(file, "[%u]: ", &size);

    arr = (real*) mwMalloc(sizeof(real) * size);

    for (i = 0; i < size; i++)
    {
        rc = fscanf(file, READ_DOUBLE_ARRAY_READ_STR, &arr[i]);
        if (rc != 1)
        {
            mw_printf("Error reading into %s\n", array_name);
            free(arr);
            return NULL;
        }

        if (i < size - 1)
            fscanf(file, ", ");
    }
    fscanf(file, "\n");

    if (sizeOut)
        *sizeOut = size;

    return arr;
}
static int evaluateStreams(lua_State* luaSt)
{
    int table;
    int i, n;

    lua_getglobal(luaSt, STREAMS_NAME);
    table = lua_gettop(luaSt);

    if (expectTable(luaSt, table))
        luaL_error(luaSt, "Expected '%s' to be a table", STREAMS_NAME);


    n = luaL_getn(luaSt, table);

    /* CHECKME: Is this valid? */
    if (n == 0)
    {
        lua_pop(luaSt, 1);
        return 0;
    }

    _streams->number_streams = n;
    _streams->parameters = mwMalloc(n * sizeof(StreamParameters));

    for (i = 0; i < n; ++i)
    {
        lua_rawgeti(luaSt, table, i + 1);
        readStreamTable(luaSt, &_streams->parameters[i], lua_gettop(luaSt));
        lua_pop(luaSt, 1);
    }

    lua_pop(luaSt, 1);
    return 0;
}
Ejemplo n.º 6
0
void* aMallocA_ (size_t size, const char *file, int line, const char *func)
{
#ifndef MEMWATCH
	void *ret = MALLOCA(size);
#else
	void *ret = mwMalloc(size, file, line);
#endif
	// ShowMessage("%s:%d: in func %s: malloc %d\n",file,line,func,size);
	if (ret == NULL){
		ShowFatalError("%s:%d: na funcao %s: erro de malocacao, despejo de memoria!\n",file,line,func);
		exit(1);
	}

	return ret;
}
static unsigned char* readCoreBinary(FILE* f, SeparationBinaryHeader* hdr)
{
    unsigned char* bin;
    size_t readn;

    bin = (unsigned char*) mwMalloc(hdr->binSize);

    readn = fread(bin, sizeof(unsigned char), hdr->binSize, f);
    if (readn != hdr->binSize)
    {
        warn("Error reading program binary header: read "ZU", expected "ZU"\n",
             readn, hdr->binSize);
        hdr->binSize = 0;
        free(bin);
        bin = NULL;
    }

    return bin;
}
cl_device_id* mwGetAllDevices(cl_platform_id platform, cl_uint* numDevOut)
{
    cl_int err;
    cl_device_id* devs;
    cl_uint numDev = 0;
    cl_device_type type = BOINC_APPLICATION ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_ALL;
    /*
      We may want to use CPUs for debugging, but the index BOINC gives
      you seems to only use GPUs.
     */

    err = clGetDeviceIDs(platform, type, 0, NULL, &numDev);
    if (err != CL_SUCCESS)
    {
        mwPerrorCL(err, "Failed to find number of devices");
        return NULL;
    }

    if (numDev == 0)
    {
        mw_printf("Didn't find any CL devices\n");
        return NULL;
    }

    mw_printf("Found %u CL device%s\n", numDev, numDev > 1 ? "s" : "");

    devs = (cl_device_id*) mwMalloc(numDev * sizeof(cl_device_id));
    err = clGetDeviceIDs(platform, type, numDev, devs, &numDev);
    if (err != CL_SUCCESS)
    {
        mwPerrorCL(err, "Failed to get device IDs");
        free(devs);
        return NULL;
    }

    *numDevOut = numDev;
    return devs;
}
cl_platform_id* mwGetAllPlatformIDs(cl_uint* nPlatformsOut)
{
    cl_uint nPlatform = 0;
    cl_uint nPlatformActual = 0;
    cl_platform_id* ids = NULL;
    cl_int err;

    err = clGetPlatformIDs(0, NULL, &nPlatform);
    if (err != CL_SUCCESS)
    {
        mwPerrorCL(err, "Error getting number of platform");
        return NULL;
    }

    if (nPlatform == 0)
    {
        mw_printf("No CL platforms found\n");
        return NULL;
    }

    ids = mwMalloc(nPlatform * sizeof(cl_platform_id));
    err = clGetPlatformIDs(nPlatform, ids, &nPlatformActual);
    if ((err != CL_SUCCESS) || (nPlatformActual != nPlatform))
    {
        mwPerrorCL(err,
                   "Error getting platform IDs or inconsistent platform count (expected %u, actual %u)\n",
                   nPlatform,
                   nPlatformActual
            );
        free(ids);
        return NULL;
    }

    mw_printf("Found %u platform%s\n", nPlatform, nPlatform > 1 ? "s" : "");

    *nPlatformsOut = nPlatform;
    return ids;
}
/* From the extra parameters, read them as doubles */
real* mwReadRestArgs(const char** rest, unsigned int n)
{
    unsigned int i;
    real* parameters = NULL;

    if (!rest)
        return NULL;

    parameters = (real*) mwMalloc(n * sizeof(real));

    errno = 0;
    for (i = 0; i < n; ++i)
    {
        parameters[i] = (real) strtod(rest[i], NULL);
        if (errno)
        {
            mwPerror("Error parsing command line fit parameters at '%s'", rest[i]);
            free(parameters);
            return NULL;
        }
    }

    return parameters;
}
Ejemplo n.º 11
0
void* _mmalloc(size_t size, const char *file, int line, const char *func ) {
	int i;
	struct block *block;
	size_t size_hash;

	if (((long) size) < 0) {
		printf("_mmalloc: %d\n", size);
		return 0;
	}
	
	size_hash = (size+BLOCK_ALIGNMENT-1) / BLOCK_ALIGNMENT;
	if(size == 0) {
		return NULL;
	}
	memmgr_usage_bytes += size;

	/* ブロック長を超える領域の確保には、malloc() を用いる */
	/* その際、unit_head.block に NULL を代入して区別する */
	if(size_hash * BLOCK_ALIGNMENT > BLOCK_DATA_SIZE - sizeof(struct unit_head)) {
#ifdef MEMWATCH
		struct unit_head_large* p = (struct unit_head_large*)mwMalloc(sizeof(struct unit_head_large) + size,file,line);
#else
		struct unit_head_large* p = (struct unit_head_large*) MALLOC (sizeof(struct unit_head_large) + size);
#endif
		if(p != NULL) {
			p->unit_head.block = NULL;
			p->unit_head.size  = size;
			p->unit_head.file  = file;
			p->unit_head.line  = line;
			p->prev = NULL;
			if (unit_head_large_first == NULL)
				p->next = NULL;
			else {
				unit_head_large_first->prev = p;
				p->next = unit_head_large_first;
			}
			unit_head_large_first = p;
			*(int*)((char*)p + sizeof(struct unit_head_large) - sizeof(int) + size) = 0xdeadbeaf;
			return (char *)p + sizeof(struct unit_head_large) - sizeof(int);
		} else {
			ShowFatalError("administrador de memoria::memmgr_alloc falhou.\n");
			exit(1);
		}
	}

	/* 同一サイズのブロックが確保されていない時、新たに確保する */
	if(unit_unfill[size_hash] == NULL) {
		block = block_malloc();
		if(unit_first[size_hash] == NULL) {
			/* 初回確保 */
			unit_first[size_hash] = block;
			unit_last[size_hash] = block;
			block->samesize_no = 0;
			block->samesize_prev = NULL;
			block->samesize_next = NULL;
		} else {
			/* 連結作業 */
			unit_last[size_hash]->samesize_next = block;
			block->samesize_no   = unit_last[size_hash]->samesize_no + 1;
			block->samesize_prev = unit_last[size_hash];
			block->samesize_next = NULL;
			unit_last[size_hash] = block;
		}
		unit_unfill[size_hash] = block;
		block->unit_size  = size_hash * BLOCK_ALIGNMENT + sizeof(struct unit_head);
		block->unit_count = (int)(BLOCK_DATA_SIZE / block->unit_size);
		block->unit_used  = 0;
		block->unit_hash  = size_hash;
		/* 未使用Flagを立てる */
		for(i=0;i<block->unit_count;i++) {
			((struct unit_head*)(&block->data[block->unit_size * i]))->block = NULL;
		}
	}
	/* ユニット使用個数加算 */
	block = unit_unfill[size_hash];
	block->unit_used++;

	/* ユニット内を全て使い果たした */
	if(block->unit_count == block->unit_used) {
		do {
			unit_unfill[size_hash] = unit_unfill[size_hash]->samesize_next;
		} while(
			unit_unfill[size_hash] != NULL &&
			unit_unfill[size_hash]->unit_count == unit_unfill[size_hash]->unit_used
		);
	}

	/* ブロックの中の空きユニット捜索 */
	for(i=0;i<block->unit_count;i++) {
		struct unit_head *head = (struct unit_head*)(&block->data[block->unit_size * i]);
		if(head->block == NULL) {
			head->block = block;
			head->size  = size;
			head->line  = line;
			head->file  = file;
			*(int*)((char*)head + sizeof(struct unit_head) - sizeof(int) + size) = 0xdeadbeaf;
			return (char *)head + sizeof(struct unit_head) - sizeof(int);
		}
	}
	// ここに来てはいけない。
	ShowFatalError("administrador de memoria::memmgr_malloc() erro serio.\n");
	memmgr_info();
	exit(1);
	return NULL;
};