Beispiel #1
0
static void
ngx_procs_process_exit(ngx_cycle_t *cycle, ngx_proc_module_t *module)
{
    ngx_uint_t         i;
    ngx_connection_t  *c;

#if (NGX_THREADS)
    ngx_terminate = 1;

    ngx_wakeup_worker_threads(cycle);
#endif

    if (module->exit) {
        module->exit(cycle);
    }

    if (ngx_exiting) {
        c = cycle->connections;
        for (i = 0; i < cycle->connection_n; i++) {
            if (c[i].fd != -1
                && c[i].read
                && !c[i].read->accept
                && !c[i].read->channel
                && !c[i].read->resolver)
            {
                ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
                              "open socket #%d left in connection %ui",
                              c[i].fd, i);
                ngx_debug_quit = 1;
            }
        }

        if (ngx_debug_quit) {
            ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "aborting");
            ngx_debug_point();
        }
    }

    /*
     * Copy ngx_cycle->log related data to the special static exit cycle,
     * log, and log file structures enough to allow a signal handler to log.
     * The handler may be called when standard ngx_cycle->log allocated from
     * ngx_cycle->pool is already destroyed.
     */

    ngx_procs_exit_log_file.fd = ngx_cycle->log->file->fd;

    ngx_procs_exit_log = *ngx_cycle->log;
    ngx_procs_exit_log.file = &ngx_procs_exit_log_file;

    ngx_procs_exit_cycle.log = &ngx_procs_exit_log;
    ngx_cycle = &ngx_procs_exit_cycle;

    ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, "process %V exit",
                  &module->name);

    ngx_destroy_pool(cycle->pool);

    exit(0);
}
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 #3
0
ngx_int_t websocket_subscriber_destroy(subscriber_t *sub) {
  full_subscriber_t   *fsub = (full_subscriber_t  *)sub;
  nchan_request_ctx_t *ctx;
  if(!fsub->awaiting_destruction) {
    ctx = ngx_http_get_module_ctx(fsub->sub.request, nchan_module);
    ctx->sub = NULL;
  }
  
  if(fsub->upstream_stuff && fsub->upstream_stuff->psr_data.tmp_pool) {
    ngx_destroy_pool(fsub->upstream_stuff->psr_data.tmp_pool);
  }
   
  if(sub->reserved > 0) {
    DBG("%p not ready to destroy (reserved for %i) for req %p", sub, sub->reserved, fsub->sub.request);
    fsub->awaiting_destruction = 1;
  }
  else {
    DBG("%p destroy for req %p", sub, fsub->sub.request);
    nchan_free_msg_id(&fsub->sub.last_msgid);
#if NCHAN_SUBSCRIBER_LEAK_DEBUG
    subscriber_debug_remove(&fsub->sub);
#endif
    nchan_free_msg_id(&sub->last_msgid);
    //debug 
    ngx_memset(fsub, 0x13, sizeof(*fsub));
    ngx_free(fsub);
  }
  return NGX_OK;
}
static void 
ngx_tcp_check_clear_all_events() 
{
    ngx_uint_t                     i;
    static ngx_flag_t              has_cleared = 0;
    ngx_tcp_check_peer_shm_t      *peer_shm;
    ngx_tcp_check_peers_shm_t     *peers_shm;
    ngx_tcp_check_peer_conf_t     *peer_conf;
    ngx_tcp_check_peers_conf_t    *peers_conf;

    if (has_cleared || check_peers_ctx == NULL) {
        return;
    }

    has_cleared = 1;

    peers_conf = check_peers_ctx;
    peers_shm = peers_conf->peers_shm;

    peer_conf = peers_conf->peers.elts;
    peer_shm = peers_shm->peers;
    for (i = 0; i < peers_conf->peers.nelts; i++) {
        if (peer_conf[i].check_ev.timer_set) {
            ngx_del_timer(&peer_conf[i].check_ev);
        }
        if (peer_shm[i].owner == ngx_pid) {
            ngx_tcp_check_clean_event(&peer_conf[i]);
        }
        if (peer_conf[i].pool != NULL) {
            ngx_destroy_pool(peer_conf[i].pool);
        }
    }
}
Beispiel #5
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));
   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;
}
Beispiel #6
0
/* 工作进程退出 */
static void
ngx_worker_process_exit(ngx_cycle_t *cycle)
{
    ngx_uint_t         i;
    ngx_connection_t  *c;

    for (i = 0; ngx_modules[i]; i++) {
        if (ngx_modules[i]->exit_process) {
            ngx_modules[i]->exit_process(cycle);
        }
    }

    if (ngx_exiting) {
        c = cycle->connections;
        for (i = 0; i < cycle->connection_n; i++) {
            if (c[i].fd != -1
                && c[i].read
                && !c[i].read->accept
                && !c[i].read->channel
                && !c[i].read->resolver)
            {
                ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
                              "*%uA open socket #%d left in connection %ui",
                              c[i].number, c[i].fd, i);
                ngx_debug_quit = 1;
            }
        }

        if (ngx_debug_quit) {
            ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "aborting");
            ngx_debug_point();
        }
    }

    /*
     * Copy ngx_cycle->log related data to the special static exit cycle,
     * log, and log file structures enough to allow a signal handler to log.
     * The handler may be called when standard ngx_cycle->log allocated from
     * ngx_cycle->pool is already destroyed.
     */

    ngx_exit_log = *ngx_log_get_file_log(ngx_cycle->log);

    ngx_exit_log_file.fd = ngx_exit_log.file->fd;
    ngx_exit_log.file = &ngx_exit_log_file;
    ngx_exit_log.next = NULL;
    ngx_exit_log.writer = NULL;

    ngx_exit_cycle.log = &ngx_exit_log;
    ngx_exit_cycle.files = ngx_cycle->files;
    ngx_exit_cycle.files_n = ngx_cycle->files_n;
    ngx_cycle = &ngx_exit_cycle;

    ngx_destroy_pool(cycle->pool);

    ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, "exit");

    /* 此时进程就退出了,进程之后的代码就都不会执行了 */
    exit(0);
}
Beispiel #7
0
void
ngx_mail_close_connection(ngx_connection_t *c)
{
    ngx_pool_t  *pool;

    ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
                   "close mail connection: %d", c->fd);

#if (NGX_MAIL_SSL)

    if (c->ssl) {
        if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
            c->ssl->handler = ngx_mail_close_connection;
            return;
        }
    }

#endif

#if (NGX_STAT_STUB)
    (void) ngx_atomic_fetch_add(ngx_stat_active, -1);
#endif

    c->destroyed = 1;

    pool = c->pool;

    ngx_close_connection(c);

    ngx_destroy_pool(pool);
}
static void
ngx_rtmp_gop_cache_cleanup(ngx_rtmp_session_t *s)
{
    ngx_rtmp_gop_cache_ctx_t       *ctx;
    ngx_rtmp_gop_cache_t           *cache;

    ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_gop_cache_module);
    if (ctx == NULL) {
        return;
    }

    for (cache = ctx->cache_head; cache; cache = cache->next) {
        ngx_rtmp_gop_cache_free_cache(s, cache);
    }

    if (ctx->pool) {
        ngx_destroy_pool(ctx->pool);
        ctx->pool = NULL;
    }

    ctx->video_seq_header = NULL;
    ctx->audio_seq_header = NULL;
    ctx->meta = NULL;

    ctx->cache_tail = ctx->cache_head = NULL;
    ctx->gop_cache_count = 0;
    ctx->free_cache = NULL;
    ctx->free_frame = NULL;
    ctx->video_frame_in_all = 0;
    ctx->audio_frame_in_all = 0;
}
// 关闭stream连接,销毁线程池
void
ngx_stream_close_connection(ngx_connection_t *c)
{
    ngx_pool_t  *pool;

    ngx_log_debug1(NGX_LOG_DEBUG_STREAM, c->log, 0,
                   "close stream connection: %d", c->fd);

#if (NGX_STREAM_SSL)

    if (c->ssl) {
        if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
            c->ssl->handler = ngx_stream_close_connection;
            return;
        }
    }

#endif

#if (NGX_STAT_STUB)
    (void) ngx_atomic_fetch_add(ngx_stat_active, -1);
#endif

    // 暂时保留内存池
    pool = c->pool;

    // 关闭连接
    ngx_close_connection(c);

    // 最后再销毁内存池
    ngx_destroy_pool(pool);
}
Beispiel #10
0
static void
ngx_worker_process_exit(ngx_cycle_t *cycle)
{
    ngx_uint_t         i;
    ngx_connection_t  *c;

    ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exit");

    for (i = 0; ngx_modules[i]; i++) {
        if (ngx_modules[i]->exit_process) {
            ngx_modules[i]->exit_process(cycle);
        }
    }

    if (ngx_exiting) {
        c = cycle->connections;
        for (i = 0; i < cycle->connection_n; i++) {
            if (c[i].fd != -1
                && c[i].read
                && !c[i].read->accept
                && !c[i].read->channel
                && !c[i].read->resolver)
            {
                ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
                              "open socket #%d left in connection %ui",
                              c[i].fd, i);
            }
        }
    }

    ngx_destroy_pool(cycle->pool);

    exit(0);
}
Beispiel #11
0
static void
ngx_master_process_exit(ngx_cycle_t *cycle)
{
    ngx_uint_t  i;

    ngx_delete_pidfile(cycle);

    ngx_close_handle(ngx_cache_manager_mutex);
    ngx_close_handle(ngx_stop_event);
    ngx_close_handle(ngx_quit_event);
    ngx_close_handle(ngx_reopen_event);
    ngx_close_handle(ngx_reload_event);
    ngx_close_handle(ngx_master_process_event);

    ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exit");

    for (i = 0; ngx_modules[i]; i++) {
        if (ngx_modules[i]->exit_master) {
            ngx_modules[i]->exit_master(cycle);
        }
    }

    ngx_destroy_pool(cycle->pool);

    exit(0);
}
static void
ngx_mail_zmauth_wait_handler(ngx_event_t *ev) {
    ngx_connection_t *c;
    ngx_mail_session_t *s;
    ngx_mail_zmauth_ctx_t *ctx;

    ngx_log_debug0(NGX_LOG_DEBUG_MAIL, ev->log, 0, "mail zmauth wait handler");

    c = ev->data;
    s = c->data;
    ctx = ngx_mail_get_module_ctx(s, ngx_mail_zmauth_module);

    if (ev->timedout) {
        /* we need to close the connection immediately */

        ngx_destroy_pool(ctx->pool);
        ngx_mail_set_ctx(s, NULL, ngx_mail_zmauth_module);
        s->quit = 1;
        ngx_mail_send(c->write);

        return;
    }

    if (ev->active) {
        if (ngx_handle_read_event(ev, 0) != NGX_OK) {
            ngx_mail_close_connection(c);
        }
    }
}
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;
}
// 发生了错误,关闭一个连接
static void
ngx_close_accepted_connection(ngx_connection_t *c)
{
    ngx_socket_t  fd;

    // 释放连接,加入空闲链表
    // in core/ngx_connection.c
    ngx_free_connection(c);

    // 连接的描述符置为无效
    fd = c->fd;
    c->fd = (ngx_socket_t) -1;

    // 关闭socket
    if (!c->shared && ngx_close_socket(fd) == -1) {
        ngx_log_error(NGX_LOG_ALERT, c->log, ngx_socket_errno,
                      ngx_close_socket_n " failed");
    }

    // 释放连接相关的所有内存
    if (c->pool) {
        ngx_destroy_pool(c->pool);
    }

#if (NGX_STAT_STUB)
    (void) ngx_atomic_fetch_add(ngx_stat_active, -1);
#endif
}
Beispiel #15
0
// hash table的一些基本操作
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_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, NULL);
    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;
}
void
ngx_postgres_upstream_free_connection(ngx_log_t *log, ngx_connection_t *c,
    PGconn *pgconn, ngx_postgres_upstream_srv_conf_t *pgscf)
{
    ngx_event_t  *rev, *wev;

    dd("entering");

    PQfinish(pgconn);

    if (c) {
        rev = c->read;
        wev = c->write;

        if (rev->timer_set) {
            ngx_del_timer(rev);
        }

        if (wev->timer_set) {
            ngx_del_timer(wev);
        }

        if (ngx_del_conn) {
           ngx_del_conn(c, NGX_CLOSE_EVENT);
        } else {
            if (rev->active || rev->disabled) {
                ngx_del_event(rev, NGX_READ_EVENT, NGX_CLOSE_EVENT);
            }

            if (wev->active || wev->disabled) {
                ngx_del_event(wev, NGX_WRITE_EVENT, NGX_CLOSE_EVENT);
            }
        }

        if (rev->prev) {
            ngx_delete_posted_event(rev);
        }

        if (wev->prev) {
            ngx_delete_posted_event(wev);
        }

        rev->closed = 1;
        wev->closed = 1;

#if defined(nginx_version) && (nginx_version >= 1001004)
        if (c->pool) {
            ngx_destroy_pool(c->pool);
        }
#endif

        ngx_free_connection(c);
    }

    /* free spot in keepalive connection pool */
    pgscf->active_conns--;

    dd("returning");
}
static void
ngx_http_tfs_remote_block_cache_dummy_handler(ngx_int_t rc, void *data)
{
    ngx_pool_t          *pool = (ngx_pool_t *)data;
    ngx_destroy_pool(pool);

    return;
}
Beispiel #18
0
void app_close(int num){
    triger ( NOTIFIER_EXIT, &globals_r);
    //extern struct GLOBAL_RAPHTERS globals_r;
	//cleanup_handlers(globals_r.pool);
    // should free the total memory
    if ( globals_r.pool ) ngx_destroy_pool(globals_r.pool);
//	globals_r.pool = NULL;
//	globals_r.con = NULL;
}
Beispiel #19
0
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, NULL);

    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;
}
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 #21
0
static void ngx_master_exit(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx)
{
    ngx_delete_pidfile(cycle);

    ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exit");

    ngx_destroy_pool(cycle->pool);

    exit(0);
}
void
ngx_http_memcachep_close_connection(ngx_connection_t *c)
{
    ngx_pool_t  *pool;

    c->destroyed = 1;
    pool = c->pool;

    ngx_close_connection(c);
    ngx_destroy_pool(pool);
}
static void
ngx_rtmp_close_connection(ngx_connection_t *c)
{
    ngx_pool_t                         *pool;

    ngx_log_debug0(NGX_LOG_DEBUG_RTMP, c->log, 0, "close connection");

    pool = c->pool;
    ngx_close_connection(c);
    ngx_destroy_pool(pool);
}
static void ngx_http_upstream_dynamic_servers_exit_process(ngx_cycle_t *cycle) {
    ngx_http_upstream_dynamic_server_main_conf_t  *udsmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_upstream_dynamic_servers_module);
    ngx_http_upstream_dynamic_server_conf_t       *dynamic_server = udsmcf->dynamic_servers.elts;
    ngx_uint_t i;

    for (i = 0; i < udsmcf->dynamic_servers.nelts; i++) {
        if (dynamic_server[i].pool) {
            ngx_destroy_pool(dynamic_server[i].pool);
            dynamic_server[i].pool = NULL;
        }
    }
}
static void
ngx_ssl_ocsp_done(ngx_ssl_ocsp_ctx_t *ctx)
{
    ngx_log_debug0(NGX_LOG_DEBUG_EVENT, ctx->log, 0,
                   "ssl ocsp done");

    if (ctx->peer.connection) {
        ngx_close_connection(ctx->peer.connection);
    }

    ngx_destroy_pool(ctx->pool);
}
Beispiel #26
0
static void nchan_exit_worker(ngx_cycle_t *cycle) {
  nchan_store_memory.exit_worker(cycle);
  nchan_store_redis.exit_worker(cycle);
  nchan_output_shutdown();
  ngx_destroy_pool(nchan_pool); // just for this worker
#if NCHAN_SUBSCRIBER_LEAK_DEBUG
  subscriber_debug_assert_isempty();
#endif
#if NCHAN_MSG_LEAK_DEBUG
  msg_debug_assert_isempty();
#endif
}
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);
}
Beispiel #28
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 #29
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 #30
0
static void
ngx_close_udp_connection(ngx_connection_t *c)
{
    ngx_free_connection(c);

    if (c->pool) {
        ngx_destroy_pool(c->pool);
    }

#if (NGX_STAT_STUB)
    (void) ngx_atomic_fetch_add(ngx_stat_active, -1);
#endif
}