Пример #1
0
int main(int argc, char **argv)
{
	int i;
	int *arr;

	if(argc < 2) {
		printf("pass a bunch of numbers to sort them\n");
		return 1;
	}

	/* create a new dynamic array of string pointers */
	arr = dynarr_alloc(0, sizeof *arr);

	for(i=1; i<argc; i++) {
		char *endp;
		int n = strtol(argv[i], &endp, 0);
		if(endp == argv[i]) {
			fprintf(stderr, "%s is not a number\n", argv[i]);
			dynarr_free(arr);
			return 1;
		}

		/* add a new string to the array (the argument is copied) */
		arr = dynarr_push(arr, &n);
	}

	qsort(arr, dynarr_size(arr), sizeof *arr, cmpfunc);

	for(i=0; i<dynarr_size(arr); i++) {
		printf("%d\n", arr[i]);
	}

	dynarr_free(arr);
	return 0;
}
Пример #2
0
int anm_add_node_animation(struct anm_node *node)
{
    struct anm_animation newanim;
    anm_init_animation(&newanim);

    node->animations = dynarr_push(node->animations, &newanim);
    return 0;
}
Пример #3
0
int lsystem_addProduction (LSYSTEM * l, const char s, const char * p) {
  PRODUCTION * sp = lsystem_getProduction (l, s);
  if (sp == NULL) {
    sp = production_create (s, p);
    dynarr_push (l->p, sp);
    dynarr_sort (l->p, production_sort);
  } else {
    production_addRule (sp, p);
  }
  return dynarr_size (sp->exp);
}
Пример #4
0
void chaser_target (Entity chaser, Entity target, float weight)
{
    struct target_info
        * info;
    Chaser
    chaseData = component_getData (entity_getAs (chaser, "chaser"));
    if (!chaseData)
        return;
    info = xph_alloc (sizeof (struct target_info));
    info->target = target;
    info->weight = weight;
    dynarr_push (chaseData->targets, info);
}
Пример #5
0
static int add_resource(struct resman *rman, const char *fname, void *data)
{
	int i, idx = -1, size = dynarr_size(rman->res);
	struct resource *res;
	struct resource **tmparr;

	/* allocate a new resource */
	if(!(res = malloc(sizeof *res))) {
		return -1;
	}
	memset(res, 0, sizeof *res);

	res->name = strdup(fname);
	assert(res->name);
	res->data = data;
	pthread_mutex_init(&res->lock, 0);

	/* check to see if there's an empty (previously erased) slot */
	for(i=0; i<size; i++) {
		if(!rman->res[i]) {
			idx = i;
			break;
		}
	}

	if(idx == -1) {
		/* free slot not found, append a new one */
		idx = size;

		if(!(tmparr = dynarr_push(rman->res, &res))) {
			free(res->name);
			free(res);
			return -1;
		}
		rman->res = tmparr;
	} else {
		/* free slot found, just use it */
		res = rman->res[idx];
	}

	res->id = idx;	/* set the resource id */

	resman_reload(rman, rman->res[idx]);
	return idx;
}
Пример #6
0
void production_addRule (PRODUCTION * p, const char * exp) {
  char * pexp = xph_alloc_name (strlen (exp) + 1, "PRODUCTION->exp[]");
  strcpy (pexp, exp);
  //printf ("adding rule \"%c\" -> \"%s\" (total of %d)\n", p->l, pexp, dynarr_size (p->exp) + 1);
  dynarr_push (p->exp, pexp);
}
Пример #7
0
int resman_init(struct resman *rman)
{
	const char *env;

	/* initialize timer */
	resman_get_time_msec();

	if(!thread_pool) {
		/* create the thread pool if there isn't one */
		int num_threads = 0;	/* automatically determine number of threads */
		if((env = getenv("RESMAN_THREADS"))) {
			num_threads = atoi(env);
		}
		if(num_threads < 1) {
			if((num_threads = resman_tpool_num_processors() - 1) < 1) {
				num_threads = 1;
			}
		}
		if(!(thread_pool = resman_tpool_create(num_threads))) {
			return -1;
		}
	}
	resman_tpool_addref(thread_pool);

	memset(rman, 0, sizeof *rman);
	rman->tpool = thread_pool;

#if defined(WIN32) || defined(__WIN32__)
	if(!(rman->wait_handles = dynarr_alloc(0, sizeof *rman->wait_handles))) {
		return -1;
	}
	rman->tpool_wait_handle = resman_tpool_get_wait_handle(rman->tpool);
	if(!(rman->wait_handles = dynarr_push(rman->wait_handles, &rman->tpool_wait_handle))) {
		return -1;
	}
#else
	if(!(rman->wait_fds = dynarr_alloc(0, sizeof *rman->wait_fds))) {
		return -1;
	}
	rman->tpool_wait_fd = resman_tpool_get_wait_fd(rman->tpool);
	if(!(rman->wait_fds = dynarr_push(rman->wait_fds, &rman->tpool_wait_fd))) {
		return -1;
	}
	/* make pipe read end nonblocking */
	fcntl(rman->tpool_wait_fd, F_SETFL, fcntl(rman->tpool_wait_fd, F_GETFL) | O_NONBLOCK);
#endif


	if(resman_init_file_monitor(rman) == -1) {
		return -1;
	}

	if(!(rman->res = dynarr_alloc(0, sizeof *rman->res))) {
		return -1;
	}

	rman->opt[RESMAN_OPT_TIMESLICE] = 16;

	pthread_mutex_init(&rman->lock, 0);
	return 0;
}
Пример #8
0
int resman_start_watch(struct resman *rman, struct resource *res)
{
    char *path = 0, *last_slash;
    struct watch_dir *wdir = 0;
    struct watch_item *witem = 0;

    /* construct an absolute path for the directory containing this file (must free it afterwards) */
    if(!(path = abs_path(res->name))) {
        return -1;
    }
    clean_path(path);

    /* we need the directory path, so let's find the last slash and cut it there */
    if(!(last_slash = strrchr(path, '\\'))) {
        goto err;
    }
    *last_slash = 0;

    /* check to see if we already have a watch handle for this directory */
    if((wdir = rb_find(rman->watchdirs, path))) {
        wdir->nref++;	/* ... if so, increase the refcount */
    } else {
        /* otherwise start a new watch */
        if(!(wdir = malloc(sizeof *wdir))) {
            perror("failed to allocate watchdir");
            goto err;
        }
        memset(wdir, 0, sizeof *wdir);
        wdir->nref = 1;

        /* open the directory we need to watch */
        wdir->handle = CreateFile(path, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                                  0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, 0);
        if(wdir->handle == INVALID_HANDLE_VALUE) {
            fprintf(stderr, "failed to watch %s: failed to open directory: %s\n", res->name, path);
            goto err;
        }

        if(!(wdir->buf_unaligned = malloc(RES_BUF_SIZE + 3))) {
            fprintf(stderr, "failed to allocate watch result buffer (%d bytes)\n", RES_BUF_SIZE);
            goto err;
        }
        wdir->buf = (char*)(((intptr_t)wdir->buf_unaligned + 3) & ~(intptr_t)0x3);

        memset(&wdir->over, 0, sizeof wdir->over);
        wdir->over.hEvent = CreateEvent(0, TRUE, FALSE, 0);

        if(!ReadDirectoryChangesW(wdir->handle, wdir->buf, RES_BUF_SIZE, 0, FILE_NOTIFY_CHANGE_LAST_WRITE, 0, &wdir->over, 0)) {
            fprintf(stderr, "failed to start async dirchange monitoring\n");
            goto err;
        }

        wdir->watch_path = path;

        rb_insert(rman->watchdirs, path, wdir);
        rb_insert(rman->wdirbyev, wdir->over.hEvent, wdir);
        rman->watch_handles = dynarr_push(rman->watch_handles, &wdir->over.hEvent);
    }

    /* add a new watch item to this watch dir */
    if(!(witem = malloc(sizeof *witem))) {
        perror("failed to allocate watch item");
        goto err;
    }
    witem->next = wdir->items;
    wdir->items = witem;
    witem->res = res;

    res->watch_path = path;
    return 0;
err:
    free(path);
    if(wdir) {
        if(wdir->handle && wdir->handle != INVALID_HANDLE_VALUE) {
            CloseHandle(wdir->handle);
        }
        free(wdir->buf_unaligned);
        if(wdir->over.hEvent && wdir->over.hEvent != INVALID_HANDLE_VALUE) {
            CloseHandle(wdir->over.hEvent);
        }
    }
    return -1;
}