Ketama *Ketama_new() { Ketama *ketama = Alloc_alloc_T(Ketama); ketama->numpoints = 0; ketama->numservers = 0; ketama->maxservers = 0; ketama->memory = 0; ketama->continuum = NULL; ketama->servers = NULL; return ketama; }
Executor *Executor_new() { DEBUG(("alloc Executor\n")); Executor *executor = Alloc_alloc_T(Executor); if(executor == NULL) { Module_set_error(GET_MODULE(), "Out of memory while allocating Executor"); return NULL; } executor->numpairs = 0; executor->numevents = 0; return executor; }
Connection *Connection_new(const char *in_addr) { DEBUG(("alloc Connection\n")); Connection *connection = Alloc_alloc_T(Connection); if(connection == NULL) { Module_set_error(GET_MODULE(), "Out of memory while allocating Connection"); return NULL; } connection->state = CS_CLOSED; connection->current_batch = NULL; connection->current_executor = NULL; connection->parser = ReplyParser_new(); if(connection->parser == NULL) { Connection_free(connection); return NULL; } //copy socket addr if(ADDR_SIZE < snprintf(connection->addr, ADDR_SIZE, "%s", in_addr)) { Module_set_error(GET_MODULE(), "Invalid address for Connection"); Connection_free(connection); return NULL; } //parse addr int port = DEFAULT_IP_PORT; char *pport; char addr[ADDR_SIZE]; DEBUG(("parsing connection addr: %s\n", connection->addr)); if(NULL == (pport = strchr(connection->addr, ':'))) { port = DEFAULT_IP_PORT; snprintf(addr, ADDR_SIZE, "%s", connection->addr); } else { port = atoi(pport + 1); snprintf(addr, (pport - connection->addr + 1), "%s", connection->addr); } connection->sa.sin_family = AF_INET; connection->sa.sin_port = htons(port); if(!inet_pton(AF_INET, addr, &connection->sa.sin_addr)) { Module_set_error(GET_MODULE(), "Could not parse ip address"); Connection_free(connection); return NULL; } DEBUG(("Connection ip: '%s', port: %d\n", addr, port)); connection->sockfd = 0; return connection; }
Buffer *Buffer_new(size_t size) { Buffer *buffer = Alloc_alloc_T(Buffer); buffer->buff_size = size; buffer->buff = Alloc_alloc(size); buffer->data = buffer->buff; buffer->position = 0; buffer->capacity = size; buffer->limit = buffer->capacity; #ifndef NDEBUG Buffer_fill(buffer, (Byte)0xEA); #else Buffer_fill(buffer, 0); #endif return buffer; }
Connection *Connection_new(const char *in_addr) { DEBUG(("alloc Connection\n")); Connection *connection = Alloc_alloc_T(Connection); if(connection == NULL) { Module_set_error(GET_MODULE(), "Out of memory while allocating Connection"); return NULL; } connection->state = CS_CLOSED; connection->current_batch = NULL; connection->current_executor = NULL; connection->parser = ReplyParser_new(); if(connection->parser == NULL) { Connection_free(connection); return NULL; } //copy address int invalid_address = 0; char *service; if (NULL == (service = strchr(in_addr, ':'))) { invalid_address = ADDR_SIZE < snprintf(connection->addr, ADDR_SIZE, "%s", in_addr) || SERV_SIZE < snprintf(connection->serv, SERV_SIZE, "%s", XSTR(DEFAULT_IP_PORT)); } else { int addr_length = service - in_addr + 1; if (ADDR_SIZE < addr_length) { invalid_address = 1; } else { snprintf(connection->addr, addr_length, "%s", in_addr); invalid_address = SERV_SIZE < snprintf(connection->serv, SERV_SIZE, "%s", service + 1); } } if (invalid_address) { Module_set_error(GET_MODULE(), "Invalid address for Connection"); Connection_free(connection); return NULL; } DEBUG(("Connection address: '%s', service: '%s'\n", connection->addr, connection->serv)); connection->addrinfo = NULL; connection->sockfd = 0; return connection; }