int main(void) { lcb_pool_t pool; lcb_t instance; struct lcb_create_st options; lcb_error_t error; /* set up the options to represent your cluster (hostname etc) */ /* ... */ /* Create the pool */ error = pool_create(10, &options, &pool, initiate); if (error != LCB_SUCCESS) { fprintf(stderr, "Failed to create pool: %s\n", lcb_strerror(NULL, error)); exit(EXIT_FAILURE); } /* * Every time you want to use libcouchbase you would grab an instance * from the pool by: */ instance = pool_pop(pool); /* use the instance for whatever you wanted to do */ /* * When you're done using the instance you would put it back into the * pool (and ready for others to use) by: */ pool_push(pool, instance); return 0; }
void pool_free(size_t index, void* p) { #ifdef USE_VALGRIND VALGRIND_DISABLE_ERROR_REPORTING; #endif TRACK_FREE(p, POOL_MIN << index); assert(index < POOL_COUNT); pool_local_t* thread = &pool_local[index]; pool_global_t* global = &pool_global[index]; if(thread->length >= global->count) pool_push(thread, global); pool_item_t* lp = (pool_item_t*)p; lp->next = thread->pool; thread->pool = (pool_item_t*)p; thread->length++; #ifdef USE_VALGRIND VALGRIND_ENABLE_ERROR_REPORTING; VALGRIND_FREELIKE_BLOCK(p, 0); #endif }
inline static void http1_free(http1_protocol_s* http) { http_request_clear(&http->request); validate_mem(); if (((void*)http) >= http1_pool.memory && ((void*)http) <= (http1_pool.memory + HTTP1_POOL_MEMORY_SIZE)) { pool_push(http); } else free(http); }
static void http1_init(void) { static spn_lock_i inner_lock = SPN_LOCK_INIT; spn_lock(&inner_lock); if (http1_pool.memory == NULL) { // Allocate the memory http1_pool.memory = mmap(NULL, HTTP1_POOL_MEMORY_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (http1_pool.memory == NULL) return; // setup `atexit` cleanup rutine atexit(http1_cleanup); // initialize pool void* pos = http1_pool.memory; while (pos < http1_pool.memory + (HTTP1_POOL_MEMORY_SIZE - HTTP1_PROTOCOL_SIZE)) { pool_push(pos); pos += HTTP1_PROTOCOL_SIZE; } } spn_unlock(&inner_lock); }