From 740f16fe6268c8ee9b54a15d0d970b7a2f6154d5 Mon Sep 17 00:00:00 2001 From: "roberto@maverick64" Date: Thu, 27 Jan 2011 18:04:35 +0100 Subject: [PATCH] added exception for python cache_set --- cache.c | 6 +++--- plugins/python/uwsgi_pymodule.c | 9 +++++++-- uwsgi.c | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/cache.c b/cache.c index 4cce5089..c00a6248 100644 --- a/cache.c +++ b/cache.c @@ -59,7 +59,7 @@ char *uwsgi_cache_get(char *key, uint16_t keylen, uint16_t *valsize) { *valsize = uwsgi.cache_items[index].valsize; // no locking needed, as this is only an hint uwsgi.cache_items[index].hits++; - return uwsgi.cache+(index*32768); + return uwsgi.cache+(index*0xffff); } return NULL; @@ -98,7 +98,7 @@ int uwsgi_cache_set(char *key, uint16_t keylen, char *val, uint16_t vallen, uint uwsgi_lock(uwsgi.cache_lock); if (uwsgi.shared->cache_first_available_item >= uwsgi.cache_max_items) goto end; - uwsgi_log("putting cache data in key %.*s\n", keylen, key); + //uwsgi_log("putting cache data in key %.*s %d\n", keylen, key, vallen); index = uwsgi_cache_get_index(key, keylen); if (!index) { index = uwsgi.shared->cache_first_available_item; @@ -108,7 +108,7 @@ int uwsgi_cache_set(char *key, uint16_t keylen, char *val, uint16_t vallen, uint uci->hits = 0; uci->used = 1; memcpy(uci->key, key, keylen); - memcpy(uwsgi.cache+(index*32768), val, vallen); + memcpy(uwsgi.cache+(index*0xffff), val, vallen); // set this as late as possibile (to minimize locking) uci->valsize = vallen; uci->keysize = keylen; diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index bd59e2e8..cde07f69 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -2357,14 +2357,19 @@ PyObject *py_uwsgi_cache_set(PyObject * self, PyObject * args) { char *key ; char *value ; + Py_ssize_t vallen; + uint64_t expires = 0; - if (!PyArg_ParseTuple(args, "ss|i:cache_set", &key, &value, &expires)) { + if (!PyArg_ParseTuple(args, "ss#|i:cache_set", &key, &value, &vallen, &expires)) { return NULL; } + if (vallen > 0xffff) { + return PyErr_Format(PyExc_ValueError, "uWSGI cache items size must be < 64K, requested %d bytes", (int )vallen); + } - if (uwsgi_cache_set(key, strlen(key), value, strlen(value), expires)) { + if (uwsgi_cache_set(key, strlen(key), value, vallen, expires)) { Py_INCREF(Py_None); return Py_None; } diff --git a/uwsgi.c b/uwsgi.c index 628ea95d..5fb03e6b 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -1097,7 +1097,7 @@ int uwsgi_start(void *v_argv) { exit(1); } - uwsgi.cache = mmap(NULL, 32768 * uwsgi.cache_max_items, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); + uwsgi.cache = mmap(NULL, 0xffff * uwsgi.cache_max_items, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0); if (!uwsgi.cache) { uwsgi_error("mmap()"); exit(1);