--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/memcache/request.c Wed Aug 27 10:13:38 2008 +0300
@@ -0,0 +1,67 @@
+
+#include <stdlib.h>
+#include <assert.h>
+
+#include "request.h"
+#include "memcache.h"
+#include "../common.h"
+
+struct memcache_req *memcache_req_alloc (struct memcache *mc, 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 = 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 mc + callback argument
+ req->mc = mc;
+ 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 = 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 == STATE_INVALID || req->state == STATE_ERROR);
+
+ free(req->key.buf);
+ free(req);
+}
+