//togo_pool.c void togo_pool_test() { togo_log(DEBUG, "Testing togo_pool.c start ============================"); void * p; void * p1; //togo_pool_create TOGO_POOL * pool = togo_pool_create(10 * 1024); if (pool->total_size == 10288 && pool->max == 10 * 1024) { togo_log(DEBUG, "Testing function:togo_pool_create .............................OK"); } else { togo_log(DEBUG, "Testing function:togo_pool_create .............................FAIL"); togo_exit(); } //togo_pool_calloc p1 = togo_pool_calloc(pool, 10 * 1024); p = togo_pool_calloc(pool, 10); togo_strcpy(p, "woshishen"); if (strcmp(p, "woshishen") == 0 && togo_strlen(p) == 9 && pool->large->size == 10 * 1024) { togo_log(DEBUG, "Testing function:togo_pool_calloc .............................OK"); } else { togo_log(DEBUG, "Testing function:togo_pool_calloc .............................FAIL"); togo_exit(); } //togo_pool_realloc(pool, 20); p = togo_pool_realloc(pool, (void *) p, 10, 15); togo_strcpy(p, "woshishen11234"); if (strcmp(p, "woshishen11234") == 0 && togo_strlen(p) == 14) { togo_log(DEBUG, "Testing function:togo_pool_realloc .............................OK"); } else { togo_log(DEBUG, "Testing function:togo_pool_realloc .............................FAIL"); togo_exit(); } //togo_pool_free_data togo_pool_free_data(pool, (void *) p); togo_pool_free_large(pool, (void *) p1); if (pool->large == NULL) { togo_log(DEBUG, "Testing function:togo_pool_free_large .............................OK"); } else { togo_log(DEBUG, "Testing function:togo_pool_free_large .............................FAIL"); togo_exit(); } togo_log(DEBUG, "Testing togo_pool.c end ============================\r\n"); }
void togo_m_queue_init(void) { togo_m_queue_pool = togo_pool_create(TOGO_M_QUEUE_POOL_SIZE); if (togo_m_queue_pool == NULL) { togo_log(ERROR, "Initialize modules_queue's pool fail."); togo_exit(); } togo_m_queue_hashtable = togo_hashtable_init(togo_m_queue_pool); if (togo_m_queue_hashtable == NULL) { togo_log(ERROR, "Initialize modules_queue's hashtable fail."); togo_exit(); } togo_m_queue_fblock = (TOGO_M_QUEUE_FBLOCK *) togo_pool_calloc( togo_m_queue_pool, sizeof(TOGO_M_QUEUE_FBLOCK)); if (togo_m_queue_fblock == NULL) { togo_log(ERROR, "Initialize modules_queue's free block fail."); togo_exit(); } pthread_mutex_init(&togo_m_queue_fblock->flock, NULL); pthread_mutex_init(&togo_m_queue_glock, NULL); }
void togo_m_cache_init(void) { uint32_t total_area, curr, i; uint32_t * area_table; TOGO_M_CACHE_AREA * area; togo_m_cache_pool = togo_pool_create(TOGO_M_CACHE_POOL_SIZE); if (togo_m_cache_pool == NULL) { togo_log(ERROR, "Initialize modules_cache's POOL fail."); togo_exit(); } togo_m_cache_hashtable = togo_hashtable_init(togo_m_cache_pool); if (togo_m_cache_hashtable == NULL) { togo_log(ERROR, "Initialize modules_cache's HASHTABLE fail."); togo_exit(); } togo_m_cache = (TOGO_M_CACHE *) togo_pool_calloc(togo_m_cache_pool, sizeof(TOGO_M_CACHE)); if (togo_m_cache == NULL) { togo_log(ERROR, "Initialize modules_cache's TOGO_M_CACHE fail."); togo_exit(); } togo_m_cache->pool = togo_m_cache_pool; togo_m_cache->total_area = 0; togo_m_cache->total_hit = 0; togo_m_cache->total_read = 0; togo_m_cache->total_size = 0; togo_m_cache->total_write = 0; togo_m_cache->is_flush = FALSE; pthread_mutex_init(&togo_m_cache->glock, NULL); /* Initialize the area table!*/ area_table = (uint32_t *) togo_pool_alloc(togo_m_cache_pool, sizeof(uint32_t) * TOGO_M_CACHE_AREA_TABLE_DEFAULT_SIZE); if (area_table == NULL) { togo_log(ERROR, "Initialize modules_cache's area_table fail."); togo_exit(); } togo_m_cache->area_table = area_table; total_area = i = 1; curr = TOGO_M_CACHE_ITEM_START; *area_table = curr; while (curr != TOGO_M_CACHE_CHUNK_SIZE) { total_area++; curr = (uint32_t)(curr * TOGO_M_CACHE_ITEM_POWER); if (curr >= TOGO_M_CACHE_CHUNK_SIZE) { curr = TOGO_M_CACHE_CHUNK_SIZE; } *(area_table + i) = curr; i++; } togo_m_cache->total_area = total_area; /* Initialize the memory of area!*/ area = (TOGO_M_CACHE_AREA *) togo_pool_calloc(togo_m_cache_pool, sizeof(TOGO_M_CACHE_AREA) * total_area); if (area == NULL) { togo_log(ERROR, "Initialize modules_cache's TOGO_M_CACHE_AREA fail."); togo_exit(); } togo_m_cache->area = area; for (i = 0; i < total_area; i++) { curr = *(area_table + i); togo_m_cache_create_area(curr, i, area); } }
static void togo_mt_doaccept(evutil_socket_t fd, short event, void *arg) { struct sockaddr_in client_addr; int in_size = sizeof(struct sockaddr_in); u_char * rbuf; u_char * sbuf; int client_socketfd = accept(fd, (struct sockaddr *) &client_addr, &in_size); if (client_socketfd < 0) { togo_log(ERROR, "Accept error."); return; } /** * We through the way of polling to select a thread, * Who will taking over a connection. */ int worker_thread_num = togo_global_c.worker_thread_num; int tid = (last_thread + 1) % worker_thread_num; TOGO_WORKER_THREAD * worker_thread = togo_worker_threads + tid; last_thread = tid; /** * Initialize a socket_item. Each connection will have one socket_item. * Through this socket_item , We store each connection details. * When this connection is disconnect, The socket_item will be freed. */ TOGO_THREAD_ITEM * socket_item = NULL; /* Search the free list */ if (togo_thread_flist->next != NULL) { pthread_mutex_lock(&togo_thread_flist->lock); if (togo_thread_flist->next != NULL) { socket_item = togo_thread_flist->next; togo_thread_flist->next = socket_item->next; togo_thread_flist->total--; } pthread_mutex_unlock(&togo_thread_flist->lock); } if (socket_item == NULL) { TOGO_POOL * worker_pool = togo_pool_create(TOGO_WORKER_POOL_SIZE); socket_item = (TOGO_THREAD_ITEM *) togo_pool_calloc(worker_pool, sizeof(TOGO_THREAD_ITEM)); rbuf = (u_char *) togo_pool_alloc(worker_pool, sizeof(u_char) * TOGO_S_RBUF_INIT_SIZE); sbuf = (u_char *) togo_pool_alloc(worker_pool, TOGO_S_SBUF_INIT_SIZE); if (rbuf == NULL || sbuf == NULL) { togo_log(ERROR, "togo_pool_alloc a rbuf or sbuf error."); return; } socket_item->worker_pool = worker_pool; socket_item->rbuf = rbuf; socket_item->sbuf = sbuf; socket_item->rsize = sizeof(u_char) * TOGO_S_RBUF_INIT_SIZE; socket_item->sbuf_size = sizeof(u_char) * TOGO_S_SBUF_INIT_SIZE; } socket_item->sfd = client_socketfd; socket_item->rcurr = socket_item->rbuf; socket_item->rbytes = 0; socket_item->ssize = 0; togo_q_push(worker_thread, socket_item); u_char buf[1]; buf[0] = 'c'; if (write(worker_thread->notify_send_fd, buf, 1) != 1) { togo_log(ERROR, "Write pipe error."); } }
static void togo_pool_init() { togo_pool = togo_pool_create(TOGO_POOL_SIZE); togo_m_queue_init(); }