Beispiel #1
1
int main (int argc, char *argv[])
{
    ngx_pool_t *pool;
    ngx_str_t dst, src = ngx_string("cubicdaiya");
    u_char *encoded;
    size_t encoded_len;

    encoded_len = ngx_base64_encoded_length(src.len);
    pool        = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, NULL);
    if (pool == NULL) {
        perror("ngx_create_pool() failed.");
        return 1;
    }
    if ((encoded = ngx_pcalloc(pool, encoded_len + 1)) == NULL) {
        perror("ngx_pcalloc() failed.");
        return 1;
    }

    dst.data = encoded;
    dst.len  = encoded_len;
    ngx_encode_base64(&dst, &src);

    printf("src:%s, dst:%s\n", src.data, dst.data);

    return 0;
}
void* add_urls_to_array(ngx_pool_t *pool, ngx_hash_keys_arrays_t *ha, ngx_array_t *url, ngx_array_t *value)
{
	ngx_uint_t loop;
	ngx_int_t	rc;
	ngx_str_t	*strUrl, *strValue;
	
	memset(ha, 0, sizeof(ngx_hash_keys_arrays_t));
	ha->temp_pool = ngx_create_pool(NGX_DEFAULT_POOL_SIZE, &ngx_log);
	ha->pool = pool;
    if (ngx_hash_keys_array_init(ha, NGX_HASH_LARGE) != NGX_OK) {
        goto failed;
    }
	
	strUrl = url->elts;
	strValue = value->elts;
	for (loop = 0; loop < url->nelts; loop++)
	{
		rc = ngx_hash_add_key(ha, &strUrl[loop], &strValue[loop],
                                  NGX_HASH_WILDCARD_KEY);

            if (rc == NGX_ERROR) {
                goto failed;
            }
	}
    return ha;    

failed:
    ngx_destroy_pool(ha->temp_pool);
    return NULL;
}
Beispiel #3
0
int main()
{
    ngx_uint_t          k; //, p, h;
    ngx_pool_t*         pool;
    ngx_hash_init_t     hash_init;
    ngx_hash_t*         hash;
    ngx_array_t*        elements;
    ngx_hash_key_t*     arr_node;
    char*               find;
    int                 i;
    ngx_log_t          *log;
    
    log = (ngx_log_t *)malloc(sizeof(ngx_log_t));
    log->log_level = 0;

    ngx_cacheline_size  = 32;
    /* hash key cal start */
    ngx_str_t str       = ngx_string("hello, world");
    k                   = ngx_hash_key_lc(str.data, str.len);
    pool                = ngx_create_pool(1024*10, log);
    printf("caculated key is %u\n", k);
    /* hask key cal end */

    hash = (ngx_hash_t *)ngx_pcalloc(pool, sizeof(hash));
    hash_init.hash        = hash;                 // hash结构
    hash_init.key         = &ngx_hash_key_lc;     // hash算法函数
    hash_init.max_size    = 1024 * 10;            // max_size
    hash_init.bucket_size = 64;                   // ngx_align(64, ngx_cacheline_size);
    hash_init.name        = "yahoo_guy_hash";     // 在log里会用到
    hash_init.pool        = pool;                 // 内存池
    hash_init.temp_pool   = NULL;

    /* 创建数组 */
    elements = ngx_array_create(pool, 32, sizeof(ngx_hash_key_t));
    for(i = 0; i < 3; i++) {
        arr_node            = (ngx_hash_key_t *)ngx_array_push(elements);
        arr_node->key       = (names[i]);
        arr_node->key_hash  = ngx_hash_key_lc(arr_node->key.data, arr_node->key.len);
        arr_node->value     = (void*)descs[i];
        printf("key: %s, key_hash: %u\n", arr_node->key.data, arr_node->key_hash);
    }

    if (ngx_hash_init(&hash_init, (ngx_hash_key_t*)elements->elts, elements->nelts) != NGX_OK) {
        return 1;
    }

    /* 查找 */
    k = ngx_hash_key_lc(names[0].data, names[0].len);
    printf("%s key is %d\n", names[0].data, k);
    find = (char *)ngx_hash_find(hash, k, (u_char*)names[0].data, names[0].len);

    if (find) {
        printf("get desc of rainx: %s\n", (char *)find);
    }

    ngx_array_destroy(elements);
    ngx_destroy_pool(pool);

    return 0;
}
static char *
ngx_rtmp_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
{
    ngx_rtmp_core_srv_conf_t *prev = parent;
    ngx_rtmp_core_srv_conf_t *conf = child;

    ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 60000);
    ngx_conf_merge_msec_value(conf->ping, prev->ping, 60000);
    ngx_conf_merge_msec_value(conf->ping_timeout, prev->ping_timeout, 30000);

    ngx_conf_merge_value(conf->so_keepalive, prev->so_keepalive, 0);
    ngx_conf_merge_value(conf->max_streams, prev->max_streams, 32);
    ngx_conf_merge_value(conf->chunk_size, prev->chunk_size, 4096);
    ngx_conf_merge_uint_value(conf->ack_window, prev->ack_window, 5000000);
    ngx_conf_merge_size_value(conf->max_message, prev->max_message,
            1 * 1024 * 1024);
    ngx_conf_merge_size_value(conf->out_queue, prev->out_queue, 256);
    ngx_conf_merge_size_value(conf->out_cork, prev->out_cork,
            conf->out_queue / 8);
    ngx_conf_merge_value(conf->play_time_fix, prev->play_time_fix, 1);
    ngx_conf_merge_value(conf->publish_time_fix, prev->publish_time_fix, 1);
    ngx_conf_merge_msec_value(conf->buflen, prev->buflen, 1000);
    ngx_conf_merge_value(conf->busy, prev->busy, 0);

    if (prev->pool == NULL) {
        prev->pool = ngx_create_pool(4096, &cf->cycle->new_log);
        if (prev->pool == NULL) {
            return NGX_CONF_ERROR;
        }
    }

    conf->pool = prev->pool;

    return NGX_CONF_OK;
}
static char *
ngx_rtmp_live_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
{
    ngx_rtmp_live_app_conf_t *prev = parent;
    ngx_rtmp_live_app_conf_t *conf = child;

    ngx_conf_merge_value(conf->live, prev->live, 0);
    ngx_conf_merge_value(conf->nbuckets, prev->nbuckets, 1024);
    ngx_conf_merge_msec_value(conf->buflen, prev->buflen, 0);
    ngx_conf_merge_msec_value(conf->sync, prev->sync, 300);
    ngx_conf_merge_msec_value(conf->idle_timeout, prev->idle_timeout, 0);
    ngx_conf_merge_value(conf->interleave, prev->interleave, 0);
    ngx_conf_merge_value(conf->wait_key, prev->wait_key, 1);
    ngx_conf_merge_value(conf->wait_video, prev->wait_video, 0);
    ngx_conf_merge_value(conf->publish_notify, prev->publish_notify, 0);
    ngx_conf_merge_value(conf->play_restart, prev->play_restart, 0);
    ngx_conf_merge_value(conf->idle_streams, prev->idle_streams, 1);

    conf->pool = ngx_create_pool(4096, &cf->cycle->new_log);
    if (conf->pool == NULL) {
        return NGX_CONF_ERROR;
    }

    conf->streams = ngx_pcalloc(cf->pool,
            sizeof(ngx_rtmp_live_stream_t *) * conf->nbuckets);

    return NGX_CONF_OK;
}
ngx_http_clojure_socket_upstream_t *ngx_http_clojure_socket_upstream_create(size_t pool_size, ngx_log_t *log) {
	ngx_pool_t *pool = ngx_create_pool(pool_size, log);
	ngx_http_clojure_socket_upstream_t *u;

	if (pool == NULL) {
		return NULL;
	}

	u = ngx_pcalloc(pool, sizeof(ngx_http_clojure_socket_upstream_t));
	if (u == NULL) {
		ngx_destroy_pool(pool);
		return NULL;
	}

	u->tcp_nodelay = 1;

	u->pool = pool;

	u->resolved = ngx_pcalloc(pool, sizeof(ngx_http_upstream_resolved_t));
	if (u->resolved == NULL) {
		ngx_destroy_pool(pool);
		return NULL;
	}
	return u;
}
void ngx_array_t_test()
{
	ngx_pool_t* pool;
	int i = 0;

	printf("--------------------------------\n");
   	printf("the size of ngx_array_t: \n");
	printf("--------------------------------\n");
	printf("%lu\n\n", sizeof(ngx_array_t));

	printf("--------------------------------\n");
	printf("create a new pool: \n");
	printf("--------------------------------\n");
	pool = ngx_create_pool(1024, NULL);
	dump_pool(pool);

	printf("--------------------------------\n");
	printf("alloc an array from the pool: \n");
	printf("--------------------------------\n");
	ngx_array_t* array = ngx_array_create(pool, 10, sizeof(int));
	dump_pool(pool);

	for(i = 0; i < 100; ++i)
	{
		int* ptr = ngx_array_push(array);
		*ptr = i + 1;
	}

	dump_array(array);

	ngx_array_destroy(array);
	ngx_destroy_pool(pool);
}
Beispiel #8
0
int main(){
	//数字对齐方法ngx_align用到了/os/unix/ngx_alloc.c中ngx_cacheline_size的值
	//如果不启动nginx则该值为0,这时ngx_align就一直返回为0,所以我们这给他一个默认值
	ngx_cacheline_size = 32;

	//用于构造带通配符的散列表
	ngx_hash_keys_arrays_t  ha;

	ngx_pool_t *pool = ngx_create_pool(2024, NULL);
	
	ha.temp_pool = pool;
	ha.pool = pool;

	if(ngx_hash_keys_array_init(&ha,NGX_HASH_LARGE) != NGX_OK){
		printf("初始化失败\n");	
	
		return 0;
	}

	
	//添加元素 *.jd.com
	ngx_str_t *key = ngx_pcalloc(pool, sizeof(ngx_str_t));
	key->len = ngx_strlen("*.com");
	key->data = ngx_pcalloc(pool, ngx_strlen("*.com"));
	ngx_memcpy(key->data, "*.com", ngx_strlen("*.com"));	

	int *value = ngx_pcalloc(pool, sizeof(int));
	*value = 1234; 

	ngx_int_t rc;
	rc = ngx_hash_add_key(&ha, key, value, NGX_HASH_WILDCARD_KEY);
        if(rc != NGX_OK){
		printf("添加key错误\n");
	}	

	//构造通配符散列表
	ngx_hash_init_t hash;
	hash.hash = NULL;
	hash.key = ngx_hash_key;
	hash.max_size = 10;
	hash.bucket_size = 100;
	hash.pool = pool;
	hash.temp_pool = pool;

	//初始化前置通配符散列表	
       	rc = ngx_hash_wildcard_init(&hash, ha.dns_wc_head.elts,
					ha.dns_wc_head.nelts);		

	if(rc != NGX_OK){
		printf("初始化通配符散列表错误\n");
	}
	
	// *.com和 as.dd.com a.b.c.com都匹配
	void *result = ngx_hash_find_wc_head((ngx_hash_wildcard_t *)hash.hash,
				"as.dd.com",ngx_strlen("as.dd.com"));
	
	int *bb = (int *)result;
	printf("%d\n",*bb);

}
Beispiel #9
0
stone_server_t * stone_app_start(struct thread_info_t *threadInfo, struct FCGX_Request *fcgx){
	ngx_pool_t *pool;
	stone_server_t *server;
	stone_request_t *req;
	stone_response_t *res;
	tpl_data_table *tpl_data;

	int rc;
	//create pool first
	pool = ngx_create_pool(4096, globals_r.log);
	if (pool == NULL){
			error_handler("Error while creating pool buffer!", fcgx);
			return NULL;
	}

	//create server struct
	server = ngx_palloc(pool, sizeof(stone_server_t));
	if (server == NULL){
			error_handler("Error while initializing server info!", fcgx);
			return NULL;
	}
	
	server->pool = pool;
	server->thread = threadInfo;
    server->fcgx = fcgx;
    server->async = 0; //no async caller
    triger ( NOTIFIER_START, server );
	// create request and response
	return server;
}
int main(/* int argc, char **argv */)
{
    ngx_pool_t *pool = NULL;
    ngx_array_t *array = NULL;
    ngx_hash_t *hash;


    printf("--------------------------------\n");
    printf("create a new pool:\n");
    printf("--------------------------------\n");


    pool = ngx_create_pool(1024, &ngx_log);

    dump_pool(pool);

    printf("--------------------------------\n");
    printf("create and add urls to it:\n");
    printf("--------------------------------\n");
    array = add_urls_to_array(pool);  //in fact, here should validate array
    dump_hash_array(array);

    printf("--------------------------------\n");
    printf("the pool:\n");
    printf("--------------------------------\n");
    dump_pool(pool);

    hash = init_hash(pool, array);
    if (hash == NULL)
    {
        printf("Failed to initialize hash!\n");
        return -1;
    }

    printf("--------------------------------\n");
    printf("the hash:\n");
    printf("--------------------------------\n");
    dump_hash(hash, array);
    printf("\n");

    printf("--------------------------------\n");
    printf("the pool:\n");
    printf("--------------------------------\n");
    dump_pool(pool);

    //find test
    printf("--------------------------------\n");
    printf("find test:\n");
    printf("--------------------------------\n");
    find_test(hash, urls, Max_Num);
    printf("\n");

    find_test(hash, urls2, Max_Num2);

    //release
    ngx_array_destroy(array);
    ngx_destroy_pool(pool);

    return 0;
}
int main(void) {
    ngx_pool_t *pool;
    ngx_array_t *arr;
    int n;
    int *ele;
    pool = ngx_create_pool(4000, NULL);
    arr = ngx_array_create(pool, 10, sizeof(ngx_uint_t));
    for (n=0; n < 5; n++) {
        ele = (int *)ngx_array_push(arr);
        *ele = n;
        printf("new element %d added\n", n);
    }

    printf("arr->nelts is %d, arr->nalloc = %d\n", arr->nelts, arr->nalloc);

    for (n=5; n < 15; n++) {
        ele = (int *)ngx_array_push(arr);
        *ele = n;
        printf("new element %d added\n", n);
    }
    printf("arr->nelts is %d, arr->nalloc = %d\n", arr->nelts, arr->nalloc);

    ngx_array_destroy(arr);
    ngx_destroy_pool(pool);

    return 0;
}
static ngx_int_t
ngx_http_dyups_check_commands(ngx_array_t *arglist)
{
    ngx_int_t     rc;
    ngx_url_t     u;
    ngx_str_t    *value;
    ngx_pool_t   *pool;
    ngx_uint_t    i;
    ngx_array_t  *line;

    pool = ngx_create_pool(128, ngx_cycle->log);
    if (pool == NULL) {
        return NGX_ERROR;
    }

    line = arglist->elts;
    for (i = 0; i < arglist->nelts; i++) {
        value = line[i].elts;

        /* TODO */

        if (line[i].nelts != 2) {
            rc = NGX_ERROR;
            goto finish;
        }

        if (value[0].len == 6 &&
            ngx_strncasecmp(value[0].data, (u_char *) "server", 6) != 0)
        {
            rc = NGX_ERROR;
            goto finish;
        }

        u.url = value[1];
        u.default_port = 80;

        if (ngx_parse_url(pool, &u) != NGX_OK) {

            if (u.err) {
                ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, 0,
                              "%s in upstream \"%V\"", u.err, &u.url);
            }

            rc = NGX_ERROR;
            goto finish;
        }

    }

    rc = NGX_OK;

finish:
    ngx_destroy_pool(pool);

    return rc;
}
Beispiel #13
0
void
ngx_event_udp_recv(ngx_event_t *ev)
{
    u_char                *buf;
    size_t                 size;
    ssize_t                n;
    ngx_listening_t       *ls;
    ngx_connection_t      *lc;
    ngx_udp_recv_event_t   event;

    lc = ev->data;
    ls = lc->listening;

    ngx_memcpy(&event.event, ev, sizeof(ngx_event_t));

    event.pool = ngx_create_pool(ls->pool_size, ngx_cycle->log);
    if (event.pool == NULL) {
        return;
    }

    event.buffer = ngx_create_temp_buf(event.pool, ls->udp_recv_buffer_size);
    if (event.buffer == NULL) {
        ngx_destroy_pool(event.pool);
        return;
    }

    buf = event.buffer->last;
    size = event.buffer->end - event.buffer->last;

    event.socklen = NGX_SOCKADDRLEN;

#if (NGX_UDT)
    n = ngx_recvfrom(lc->fd, (char *) buf, size, 0,
                     (struct sockaddr *) event.sockaddr, &event.socklen);
#else
    n = recvfrom(lc->fd, (char *) buf, size, 0,
                 (struct sockaddr *) event.sockaddr, &event.socklen);
#endif

    if (n == -1) {
#if (NGX_UDT)
        ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,
                      "ngx_recvfrom() failed");
#else
        ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno,
                      "recvfrom() failed");
#endif
        ngx_destroy_pool(event.pool);
        return;
    }

    event.buffer->last += n;

    ngx_event_udp_aio_recv((ngx_event_t *) &event);
}
Beispiel #14
0
ngx_connection_t *nchan_create_fake_connection(ngx_pool_t *pool) {
    ngx_log_t               *log;
    ngx_connection_t        *c;
    ngx_connection_t        *saved_c = NULL;

    /* (we temporarily use a valid fd (0) to make ngx_get_connection happy) */
    if (ngx_cycle->files) {
        saved_c = ngx_cycle->files[0];
    }

    c = ngx_get_connection(0, ngx_cycle->log);

    if (ngx_cycle->files) {
        ngx_cycle->files[0] = saved_c;
    }

    if (c == NULL) {
        return NULL;
    }

    c->fd = (ngx_socket_t) -1;
    c->number = ngx_atomic_fetch_add(ngx_connection_counter, 1);

    if (pool) {
        c->pool = pool;
    }
    else {
        c->pool = ngx_create_pool(128, c->log);
        if (c->pool == NULL) {
            goto failed;
        }
    }

    log = ngx_pcalloc(c->pool, sizeof(ngx_log_t));
    if (log == NULL) {
        goto failed;
    }

    c->log = log;
    c->log->connection = c->number;
    c->log->action = NULL;
    c->log->data = NULL;

    c->log_error = NGX_ERROR_INFO;

    c->error = 1;

    return c;

failed:

    nchan_close_fake_connection(c);
    return NULL;
}
Beispiel #15
0
int main(){
	ngx_pool_t *pool;
	ngx_array_t *arr;
	int n;
	int *ele;
	pool = ngx_create_pool(4000,NULL);
	arr = ngx_array_create(pool,10,sizeof(ngx_uint_t));
    ele = (int *)ngx_array_push(arr);
	*ele = 8;

	printf("new element %d is \n",*ele);
}
void ngx_queue_t_test()
{
	int i = 0;
	ngx_pool_t* pool;
	ngx_queue_t* myque;
	my_point_queue_t* point;
	my_point_t points[POINTS_LEN] = {
		{10, 1}, {20, 9}, {9, 9}, {90, 80}, {5, 3}, {50, 20}
	};

	printf("----------------------------------------\n");
	printf("the size of ngx_queue_t: \n");
	printf("----------------------------------------\n");
	printf("%lu\n\n", sizeof(ngx_queue_t));

	printf("----------------------------------------\n");
	printf("create a new pool: \n");
	printf("----------------------------------------\n");
	pool = ngx_create_pool(1024, NULL);
	dump_pool(pool);

	printf("----------------------------------------\n");
	printf("alloc a queue head and nodes: \n");
	printf("----------------------------------------\n");
	myque = ngx_palloc(pool, sizeof(ngx_queue_t));
	ngx_queue_init(myque);

	for(i = 0; i < POINTS_LEN; ++i)
	{
		point = (my_point_queue_t*)ngx_palloc(pool, sizeof(my_point_queue_t));
		point->point.x = points[i].x;
		point->point.y = points[i].y;
		ngx_queue_init(&point->queue);

		ngx_queue_insert_head(myque, &point->queue);
	}

	dump_queue_from_tail(myque);
	printf("\n");

	printf("----------------------------------------\n");
	printf("sort the queue: \n");
	printf("----------------------------------------\n");
	ngx_queue_sort(myque, my_point_cmp);
	dump_queue_from_head(myque);
	printf("\n");

	printf("----------------------------------------\n");
	printf("the pool at the end: \n");
	printf("----------------------------------------\n");
	dump_pool(pool);
	ngx_destroy_pool(pool);
}
static ngx_int_t ngx_http_push_init_module(ngx_cycle_t *cycle) {
  ngx_core_conf_t                *ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
  ngx_http_push_worker_processes = ccf->worker_processes;
  //initialize subscriber queues
  //pool, please
  if((ngx_http_push_pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, cycle->log))==NULL) { //I trust the cycle pool size to be a well-tuned one.
    return NGX_ERROR; 
  }
  
  //initialize storage engine
  return ngx_http_push_store_local.init_module(cycle);
}
Beispiel #18
0
int main()
{
    ngx_pool_t *pool;
    ngx_queue_t *myque;
    my_point_queue_t *point;
    my_point_t points[Max_Num] = {
            {10, 1}, {20, 9}, {9, 9}, {90, 80}, {5, 3}, {50, 20}
    };
    int i;
 
    printf("--------------------------------\n");
    printf("create a new pool:\n");
    printf("--------------------------------\n");
    pool = ngx_create_pool(1024, NULL);
    dump_pool(pool);
 
    printf("--------------------------------\n");
    printf("alloc a queue head and nodes :\n");
    printf("--------------------------------\n");
    myque =ngx_palloc(pool, sizeof(ngx_queue_t));  //alloc a queue head
    ngx_queue_init(myque);  //init the queue
 
    //insert  some points into the queue
    for (i = 0; i < Max_Num; i++)
    {
        point = (my_point_queue_t*)ngx_palloc(pool, sizeof(my_point_queue_t));
        point->point.x = points[i].x;
        point->point.y = points[i].y;
        ngx_queue_init(&point->queue);
 
        //insert this point into the points queue
        ngx_queue_insert_head(myque, &point->queue);
    }
 
    dump_queue_from_tail(myque);
    printf("\n");
 
    printf("--------------------------------\n");
    printf("sort the queue:\n");
    printf("--------------------------------\n");
    ngx_queue_sort(myque, my_point_cmp);
    dump_queue_from_head(myque);
    printf("\n");
 
    printf("--------------------------------\n");
    printf("the pool at the end:\n");
    printf("--------------------------------\n");
    dump_pool(pool);
 
    ngx_destroy_pool(pool);
    return 0;
}
Beispiel #19
0
int main()
{
    ngx_pool_t*     pool;
    yahoo_guy_t*    guy;
    ngx_queue_t*    q;
    yahoo_t*        yahoo;
    pool            = ngx_create_pool(1024*10, NULL); //初始化内存池
    int             i;
    // 构建队列
    const ngx_str_t   names[] = {
        ngx_string("rainx"), ngx_string("xiaozhe"), ngx_string("zhoujian")
    } ;
    const int       ids[]   = {4611, 8322, 6111};

    yahoo = ngx_palloc(pool, sizeof(yahoo_t));
    ngx_queue_init(&yahoo->queue); //初始化queue

    for(i = 0; i < 3; i++)
    {
      guy = (yahoo_guy_t*) ngx_palloc(pool, sizeof(yahoo_guy_t));
      guy->id   = ids[i];
      //guy->name = (char*) ngx_palloc(pool, (size_t) (strlen(names[i]) + 1) );
      guy->name = (u_char*) ngx_pstrdup(pool, (ngx_str_t*) &(names[i]) );

      ngx_queue_init(&guy->queue);
      // 从头部进入队列
      ngx_queue_insert_head(&yahoo->queue, &guy->queue);
    }

    // 从尾部遍历输出
    for(q = ngx_queue_last(&yahoo->queue);
        q != ngx_queue_sentinel(&yahoo->queue);
        q = ngx_queue_prev(q) ) {

        guy = ngx_queue_data(q, yahoo_guy_t, queue);
        printf("No. %d guy in yahoo is %s \n", guy->id, guy->name);
    }

    // 排序从头部输出
    ngx_queue_sort(&yahoo->queue, yahoo_no_cmp);
    printf("sorting....\n");
    for(q = ngx_queue_prev(&yahoo->queue);
        q != ngx_queue_sentinel(&yahoo->queue);
        q = ngx_queue_last(q) ) {

        guy = ngx_queue_data(q, yahoo_guy_t, queue);
        printf("No. %d guy in yahoo is %s \n", guy->id, guy->name);
    }

    ngx_destroy_pool(pool);
    return 0;
}
Beispiel #20
0
int module_global_init(void){
    ngx_pool_t *pool;
	ngx_log_t *log;
    pool = ngx_create_pool(4096, log);
    if (pool == NULL){
		app_close(1);
        printf("Error while creating pool buffer!");
        return -1;
    }
    globals_r.pool = pool;
    globals_r.log  = log;
    return 0;
}
Beispiel #21
0
int 
main(int argc, char *argv[])
{
    ngx_uint_t   pool_size = 1024;
    ngx_pool_t  *pool;

    printf("\ncreate a new pool of %u bytes: \n", pool_size);
    pool = ngx_create_pool(pool_size, NULL);
    dump_pool(pool);

    ngx_destroy_pool(pool);

    return 0;
}
static void *ngx_http_small_light_create_srv_conf(ngx_conf_t *cf)
{
    ngx_http_small_light_conf_t *srv_conf;
    ngx_pool_t *pool, *temp_pool;
    srv_conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_small_light_conf_t));
    if (srv_conf == NULL) {
        return NGX_CONF_ERROR;
    }
    pool = ngx_create_pool(16384, cf->log);
    if (pool == NULL) {
        return NGX_CONF_ERROR;
    }
    temp_pool = ngx_create_pool(16384, cf->log);
    if (temp_pool == NULL) {
        return NGX_CONF_ERROR;
    }
    srv_conf->patterns.keys.pool = pool;
    srv_conf->patterns.temp_pool = temp_pool;
    if (ngx_hash_keys_array_init(&srv_conf->patterns, NGX_HASH_SMALL) != NGX_OK) {
        return NGX_CONF_ERROR;
    }
    return srv_conf;
}
ngx_int_t
ngx_http_tfs_remote_block_cache_insert(
    ngx_http_tfs_remote_block_cache_ctx_t *ctx,
    ngx_pool_t *pool, ngx_log_t *log,
    ngx_http_tfs_block_cache_key_t *key,
    ngx_http_tfs_block_cache_value_t *value)
{
    ngx_int_t             rc;
    ngx_pool_t           *tmp_pool;
    ngx_http_tair_data_t  tair_key, tair_value;

    ngx_log_debug2(NGX_LOG_DEBUG_HTTP, log, 0,
                   "insert remote block cache, "
                   "ns addr: %uL, block id: %uD",
                   key->ns_addr, key->block_id);

    tair_key.type = NGX_HTTP_TAIR_INT;
    tair_key.data = (u_char *)key;
    tair_key.len = NGX_HTTP_TFS_BLOCK_CACHE_KEY_SIZE;

    tair_value.len = NGX_HTTP_TFS_REMOTE_BLOCK_CACHE_VALUE_BASE_SIZE
                      + value->ds_count * sizeof(uint64_t);
    tair_value.data = ngx_pcalloc(pool, tair_value.len);
    if (tair_value.data == NULL) {
        return NGX_ERROR;
    }
    *(uint32_t*)tair_value.data = value->ds_count;
    ngx_memcpy(tair_value.data+ NGX_HTTP_TFS_REMOTE_BLOCK_CACHE_VALUE_BASE_SIZE,
               value->ds_addrs, value->ds_count * sizeof(uint64_t));
    tair_value.type = NGX_HTTP_TAIR_INT;

    /* since we do not care returns,
     * we make a tmp pool and destroy it in callback
     */
    tmp_pool = ngx_create_pool(4096, log);
    if (tmp_pool == NULL) {
        return NGX_ERROR;
    }

    rc = ngx_http_tfs_tair_put_helper(
                                  ctx->tair_instance,
                                  tmp_pool, log,
                                  &tair_key, &tair_value,
                                  0/*expire*/, 0/* do not care version */,
                                  ngx_http_tfs_remote_block_cache_dummy_handler,
                                  (void *)tmp_pool);

    return rc;
}
static ngx_int_t
ngx_http_dyups_init(ngx_conf_t *cf)
{
    ngx_uint_t                      i;
    ngx_http_dyups_srv_conf_t      *duscf;
    ngx_http_dyups_main_conf_t     *dmcf;
    ngx_http_upstream_srv_conf_t  **uscfp, *uscf;
    ngx_http_upstream_main_conf_t  *umcf;

    dmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_dyups_module);
    umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_module);

    if (!dmcf->enable) {
        return NGX_OK;
    }

    uscfp = umcf->upstreams.elts;
    for (i = 0; i < umcf->upstreams.nelts; i++) {

        uscf = uscfp[i];

#if NGX_DEBUG
        ngx_log_error(NGX_LOG_EMERG, cf->log, 0, "%ui: %V", i, &uscf->host);
#endif

        duscf = ngx_array_push(&dmcf->dy_upstreams);
        if (duscf == NULL) {
            return NGX_ERROR;
        }

        ngx_memzero(duscf, sizeof(ngx_http_dyups_srv_conf_t));

        duscf->pool = ngx_create_pool(128, cf->log);
        if (duscf->pool == NULL) {
            return NGX_ERROR;
        }

        duscf->upstream = uscfp[i];
        duscf->dynamic = (uscfp[i]->port == 0
                          && uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE);
        duscf->deleted = 0;

    }

    return NGX_OK;
}
int main()
{
	//定义用于初始化散列表的结构体
	ngx_hash_init_t	hash;
	//用于预先向散列表中添加元素,这里的元素支持带通配符
	ngx_hash_keys_arrays_t ha;
	//支持带通配符的散列表
	ngx_hash_combined_t combinedHash;

	ngx_memzero(&ha, sizeof(ngx_hash_keys_arrays_t));

	//临时内存池只是用于初始化通配符散列表,在初始化完成之后就可以销毁
	ha.temp_pool = ngx_create_pool(16384, cf->log);
	if(ha.temp_pool == NULL){
		return NGX_ERROR;
	}
	ha.pool = ha.temp_pool;:q
static char *
ngx_rtmp_sharedobject_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
{
  ngx_rtmp_sharedobject_app_conf_t *prev = parent;
  ngx_rtmp_sharedobject_app_conf_t *conf = child;

  ngx_conf_merge_value(conf->nbuckets, prev->nbuckets, 1024);

  conf->pool = ngx_create_pool(4096, &cf->cycle->new_log);
  if (conf->pool == NULL) {
    return NGX_CONF_ERROR;
  }

  conf->shared_objects =
    ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_sharedobject_t *) * conf->nbuckets);

  return NGX_CONF_OK;
}
Beispiel #27
0
int main() {
  ngx_pool_t *pool;
  ngx_array_t *arr;

  pool = ngx_create_pool(1000);

  printf("ngx_array start\n");
  arr = ngx_array_create(pool, 10, sizeof(int));
  for(int i = 0; i < 1000; ++i) {
    int *elt = ngx_array_push(arr);
    *elt = i;
  }
  int *ptr = arr->elts;
  for(int i = 0; i < arr->nelts; ++i) {
    printf("%d ", *ptr+i);
  }
  printf("\nngx_array end\n\n");
  printf("ngx_list start\n");

  ngx_list_t *list;
  list = ngx_create_list(pool, 10, sizeof(int));

  for(int i = 0; i < 1000; ++i) {
    int *ptr = ngx_list_push(list);
    *ptr = i;
  }

  ngx_list_part_t *part = &list->part;
  int *data = part->elts;
  int i;

  for(i = 0;; i++) {
    if(i >= part->nelts) {
      if (part->next == NULL) break;
      part = part->next;
      data = part->elts;
      i = 0;
    }
    printf("%d ", *data + i);
  }
  printf("\nngx_list end\n");

  return 0;
}
Beispiel #28
0
int ngx_tcp_reuse_pool_init(ngx_log_t *log)
{

	ngx_reuse_pool = ngx_create_pool(ngx_tcp_reuse_pool_size, log);
	if (ngx_reuse_pool == NULL) {
		ngx_log_error(NGX_LOG_EMERG, log, 0, "could not create ngx_reuse_pool");
		exit(1);
	}
	conns.elts = ngx_pcalloc(ngx_reuse_pool, ngx_tcp_reuse_conns_init_size * sizeof (ngx_tcp_reuse_conn_t));
	conns.nelts = 0;
	conns.size = sizeof (ngx_tcp_reuse_conn_t);
	conns.nalloc = ngx_tcp_reuse_conns_init_size;
	conns.pool = ngx_reuse_pool;

	ngx_queue_init(&empty_conns);
	ngx_queue_init(&active_conns);
	ngx_log_debug(NGX_LOG_DEBUG_HTTP, log, 0, "ngx_tcp_reuse_pool_init ok");
	return NGX_OK;
}
static char *
ngx_rtmp_live_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
{
    ngx_rtmp_live_app_conf_t *prev = parent;
    ngx_rtmp_live_app_conf_t *conf = child;

    ngx_conf_merge_value(conf->live, prev->live, 0);
    ngx_conf_merge_value(conf->nbuckets, prev->nbuckets, 1024);
    ngx_conf_merge_msec_value(conf->buflen, prev->buflen, 0);

    conf->pool = ngx_create_pool(4096, &cf->cycle->new_log);
    if (conf->pool == NULL) {
        return NGX_CONF_ERROR;
    }

    conf->streams = ngx_pcalloc(cf->pool, 
            sizeof(ngx_rtmp_live_stream_t *) * conf->nbuckets);

    return NGX_CONF_OK;
}
Beispiel #30
0
static ngx_int_t
ngx_event_post_one_udp_recv(ngx_listening_t *ls, ngx_udp_recv_event_t *event)
{
    int        rc;
    DWORD      flags;
    WSABUF     wsabuf;
    ngx_err_t  err;

    event->pool = ngx_create_pool(ls->pool_size, ngx_cycle->log);
    if (event->pool == NULL) {
        return NGX_ERROR;
    }

    event->buffer = ngx_create_temp_buf(event->pool, ls->udp_recv_buffer_size);
    if (event->buffer == NULL) {
        ngx_destroy_pool(event->pool);
        return NGX_ERROR;
    }

    wsabuf.buf = (CHAR *) event->buffer->last;
    wsabuf.len = (DWORD) (event->buffer->end - event->buffer->last);

    flags = 0;

    event->socklen = NGX_SOCKADDRLEN;

    rc = WSARecvFrom(ls->connection->fd, &wsabuf, 1, NULL, &flags,
                     (struct sockaddr *) event->sockaddr,
                     (LPINT) &event->socklen,
                     (LPWSAOVERLAPPED) &event->event.ovlp, NULL);

    err = ngx_socket_errno;

    if (rc == SOCKET_ERROR && err != WSA_IO_PENDING) {
        ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err,
                      "WSARecvFrom() failed");
        return NGX_ERROR;
    }

    return NGX_OK;
}