-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathitem.c
181 lines (160 loc) · 4.42 KB
/
item.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* MemcacheQ - Simple Queue Service over Memcache
*
* http://memcacheq.googlecode.com
*
* The source code of MemcacheQ is most based on MemcachDB:
*
* http://memcachedb.googlecode.com
*
* Copyright 2008 Steve Chu. All rights reserved.
*
* Use and distribution licensed under the BSD license. See
* the LICENSE file for full text.
*
* Authors:
* Steve Chu <[email protected]>
*
*/
#include "memcacheq.h"
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#define MAX_ITEM_FREELIST_LENGTH 4000
#define INIT_ITEM_FREELIST_LENGTH 500
static size_t item_make_header(const uint8_t nkey, const int flags, const int nbytes, char *suffix, uint8_t *nsuffix);
static item **freeitem;
static int freeitemtotal;
static int freeitemcurr;
void item_init(void) {
freeitemtotal = INIT_ITEM_FREELIST_LENGTH;
freeitemcurr = 0;
freeitem = (item **)malloc( sizeof(item *) * freeitemtotal );
if (freeitem == NULL) {
perror("malloc()");
}
return;
}
/*
* Returns a item buffer from the freelist, if any. Sholud call
* item_from_freelist for thread safty.
* */
item *do_item_from_freelist(void) {
item *s;
if (freeitemcurr > 0) {
s = freeitem[--freeitemcurr];
} else {
/* If malloc fails, let the logic fall through without spamming
* STDERR on the server. */
s = (item *)malloc( bdb_settings.re_len );
if (s != NULL){
memset(s, 0, bdb_settings.re_len);
}
}
return s;
}
/*
* Adds a item to the freelist. Should call
* item_add_to_freelist for thread safty.
*/
int do_item_add_to_freelist(item *it) {
if (freeitemcurr < freeitemtotal) {
freeitem[freeitemcurr++] = it;
return 0;
} else {
if (freeitemtotal >= MAX_ITEM_FREELIST_LENGTH){
return 1;
}
/* try to enlarge free item buffer array */
item **new_freeitem = (item **)realloc(freeitem, sizeof(item *) * freeitemtotal * 2);
if (new_freeitem) {
freeitemtotal *= 2;
freeitem = new_freeitem;
freeitem[freeitemcurr++] = it;
return 0;
}
}
return 1;
}
/**
* Generates the variable-sized part of the header for an object.
*
* key - The key
* nkey - The length of the key
* flags - key flags
* nbytes - Number of bytes to hold value and addition CRLF terminator
* suffix - Buffer for the "VALUE" line suffix (flags, size).
* nsuffix - The length of the suffix is stored here.
*
* Returns the total size of the header.
*/
static size_t item_make_header(const uint8_t nkey, const int flags, const int nbytes,
char *suffix, uint8_t *nsuffix) {
/* suffix is defined at 40 chars elsewhere.. */
*nsuffix = (uint8_t) snprintf(suffix, 40, " %d %d\r\n", flags, nbytes - 2);
return sizeof(item) + nkey + *nsuffix + nbytes;
}
/*
* alloc a item buffer, and init it.
*/
item *item_alloc1(char *key, const size_t nkey, const int flags, const int nbytes) {
uint8_t nsuffix;
item *it;
char suffix[40];
size_t ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix);
if(ntotal > bdb_settings.re_len){
return NULL;
}
it = item_from_freelist();
if (it == NULL){
return NULL;
}
if (settings.verbose > 1) {
fprintf(stderr, "alloc a item buffer from freelist.\n");
}
it->nkey = nkey;
it->nbytes = nbytes;
strcpy(ITEM_key(it), key);
memcpy(ITEM_suffix(it), suffix, (size_t)nsuffix);
it->nsuffix = nsuffix;
return it;
}
/*
* alloc a item buffer only.
*/
item *item_alloc2(void) {
item *it;
it = item_from_freelist();
if (it == NULL){
return NULL;
}
if (settings.verbose > 1) {
fprintf(stderr, "alloc a item buffer from freelist.\n");
}
return it;
}
/*
* free a item buffer.
*/
int item_free(item *it) {
if (NULL == it)
return 0;
if (0 != item_add_to_freelist(it)) {
if (settings.verbose > 1) {
fprintf(stderr, "add a item buffer to freelist fail, use free() directly.\n");
}
free(it);
}else{
if (settings.verbose > 1) {
fprintf(stderr, "add a item buffer to freelist.\n");
}
}
return 0;
}