X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Flibmemcachedprotocol%2Fcache.c;h=39e2fc7d9ba5f02ce726e8dab6fcbe558e9387d0;hb=796d80d8d604e5f1ed73161e44190817d3559be4;hp=1699f73513348647c551b2bf1ca1feba48abd641;hpb=cb40bfe8923a2b06160a965d95b45ca0ea3421ab;p=awesomized%2Flibmemcached diff --git a/src/libmemcachedprotocol/cache.c b/src/libmemcachedprotocol/cache.c index 1699f735..39e2fc7d 100644 --- a/src/libmemcachedprotocol/cache.c +++ b/src/libmemcachedprotocol/cache.c @@ -19,16 +19,38 @@ #include #include -#ifndef NDEBUG -# include -#endif - #include "libmemcachedprotocol/common.h" -#ifndef NDEBUG +#ifndef HAVE_UMEM_H + +# ifdef _MSC_VER +typedef SECURITY_ATTRIBUTES pthread_mutexattr_t; + +static inline int pthread_mutex_init(pthread_mutex_t *m, pthread_mutexattr_t *a) { + *m = CreateMutexA(a, FALSE, NULL); + return !*m; +} +static inline int pthread_mutex_destroy(pthread_mutex_t *m) { + return !CloseHandle(*m); +} +static inline int pthread_mutex_lock(pthread_mutex_t *m) { + if (WaitForSingleObject(*m, INFINITE)) { + return 0; + } + return GetLastError(); +} +static inline int pthread_mutex_unlock(pthread_mutex_t *m) { + return !ReleaseMutex(*m); +} +# endif + +# ifndef NDEBUG +# include + const uint64_t redzone_pattern = 0xdeadbeefcafebabe; int cache_error = 0; -#endif + +# endif const size_t initial_pool_size = 64; @@ -52,11 +74,11 @@ cache_t *cache_create(const char *name, size_t bufsize, size_t align, ret->constructor = constructor; ret->destructor = destructor; -#ifndef NDEBUG +# ifndef NDEBUG ret->bufsize = bufsize + 2 * sizeof(redzone_pattern); -#else +# else ret->bufsize = bufsize; -#endif +# endif (void) align; @@ -64,12 +86,12 @@ cache_t *cache_create(const char *name, size_t bufsize, size_t align, } static inline void *get_object(void *ptr) { -#ifndef NDEBUG +# ifndef NDEBUG uint64_t *pre = ptr; return pre + 1; -#else +# else return ptr; -#endif +# endif } void cache_destroy(cache_t *cache) { @@ -105,7 +127,7 @@ void *cache_alloc(cache_t *cache) { } pthread_mutex_unlock(&cache->mutex); -#ifndef NDEBUG +# ifndef NDEBUG if (object) { /* add a simple form of buffer-check */ uint64_t *pre = ret; @@ -114,7 +136,7 @@ void *cache_alloc(cache_t *cache) { memcpy(((char *) ret) + cache->bufsize - (2 * sizeof(redzone_pattern)), &redzone_pattern, sizeof(redzone_pattern)); } -#endif +# endif return object; } @@ -122,7 +144,7 @@ void *cache_alloc(cache_t *cache) { void cache_free(cache_t *cache, void *ptr) { pthread_mutex_lock(&cache->mutex); -#ifndef NDEBUG +# ifndef NDEBUG /* validate redzone... */ if (memcmp(((char *) ptr) + cache->bufsize - (2 * sizeof(redzone_pattern)), &redzone_pattern, sizeof(redzone_pattern)) @@ -142,7 +164,7 @@ void cache_free(cache_t *cache, void *ptr) { return; } ptr = pre; -#endif +# endif if (cache->freecurr < cache->freetotal) { cache->ptr[cache->freecurr++] = ptr; } else { @@ -162,3 +184,5 @@ void cache_free(cache_t *cache, void *ptr) { } pthread_mutex_unlock(&cache->mutex); } + +#endif // HAVE_UMEM_H