#include <stdlib.h>
#include <assert.h>
#include "request.h"
#include "memcache.h"
#include "../common.h"
struct memcache_req *memcache_req_alloc (struct memcache *mc, enum memcache_command cmd_type, const struct memcache_key *key, void *cb_arg) {
struct memcache_req *req = NULL;
// allocate it
if ((req = calloc(1, sizeof(*req))) == NULL)
ERROR("calloc");
// state
req->state = MEMCACHE_STATE_INVALID;
// copy the key
if ((req->key.buf = malloc(key->len)) == NULL)
ERROR("malloc key buf");
// copy over the key
memcpy(req->key.buf, key->buf, key->len);
req->key.len = key->len;
// store the other data
req->mc = mc;
req->cmd_type = cmd_type;
req->cb_arg = cb_arg;
// success
return req;
error:
if (req) {
free(req->key.buf);
free(req);
}
return NULL;
}
static int _memcache_req_notify (struct memcache_req *req) {
return req->mc->cb_fn(req, req->cb_arg);
}
void memcache_req_error (struct memcache_req *req) {
// forget our connection
req->conn = NULL;
// enter ERROR state
req->state = MEMCACHE_STATE_ERROR;
// notify
if (_memcache_req_notify(req))
WARNING("req error callback failed, ignoring");
}
void memcache_req_free (struct memcache_req *req) {
// must be unused
assert(req->conn == NULL);
assert(req->state == MEMCACHE_STATE_INVALID || req->state == MEMCACHE_STATE_ERROR);
free(req->key.buf);
free(req);
}