memcache/request.c
changeset 40 9cecd22e643a
child 41 540737bf6bac
equal deleted inserted replaced
39:0e21a65074a6 40:9cecd22e643a
       
     1 
       
     2 #include <stdlib.h>
       
     3 #include <assert.h>
       
     4 
       
     5 #include "request.h"
       
     6 #include "memcache.h"
       
     7 #include "../common.h"
       
     8 
       
     9 struct memcache_req *memcache_req_alloc (struct memcache *mc, struct memcache_key *key, void *cb_arg) {
       
    10     struct memcache_req *req = NULL;
       
    11     
       
    12     // allocate it
       
    13     if ((req = calloc(1, sizeof(*req))) == NULL)
       
    14         ERROR("calloc");
       
    15     
       
    16     // state
       
    17     req->state = STATE_INVALID;
       
    18 
       
    19     // copy the key
       
    20     if ((req->key.buf = malloc(key->len)) == NULL)
       
    21         ERROR("malloc key buf");
       
    22     
       
    23     // copy over the key
       
    24     memcpy(req->key.buf, key->buf, key->len);
       
    25     req->key.len = key->len;
       
    26 
       
    27     // store the mc + callback argument
       
    28     req->mc = mc;
       
    29     req->cb_arg = cb_arg;
       
    30 
       
    31     // success
       
    32     return req;
       
    33 
       
    34 error:
       
    35     if (req) {
       
    36         free(req->key.buf);
       
    37         free(req);
       
    38     }
       
    39 
       
    40     return NULL;
       
    41 }
       
    42 
       
    43 static int _memcache_req_notify (struct memcache_req *req) {
       
    44     return req->mc->cb_fn(req, req->cb_arg);
       
    45 }
       
    46 
       
    47 void memcache_req_error (struct memcache_req *req) {
       
    48     // forget our connection
       
    49     req->conn = NULL;
       
    50     
       
    51     // enter ERROR state
       
    52     req->state = STATE_ERROR;
       
    53 
       
    54     // notify
       
    55     if (_memcache_req_notify(req))
       
    56         WARNING("req error callback failed, ignoring");
       
    57 }
       
    58 
       
    59 void memcache_req_free (struct memcache_req *req) {
       
    60     // must be unused
       
    61     assert(req->conn == NULL);
       
    62     assert(req->state == STATE_INVALID || req->state == STATE_ERROR);
       
    63 
       
    64     free(req->key.buf);
       
    65     free(req);
       
    66 }
       
    67