X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Flibmemcachedprotocol%2Fcache.c;h=39e2fc7d9ba5f02ce726e8dab6fcbe558e9387d0;hb=aeac01630eed825d8a624e7fd7c107d47bb2e40c;hp=637665d7e86fab1a8cca899ed1a62a620b35633c;hpb=f48bae7d4b6a832b0d3a71812125770f09c76239;p=awesomized%2Flibmemcached diff --git a/src/libmemcachedprotocol/cache.c b/src/libmemcachedprotocol/cache.c index 637665d7..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) { @@ -94,10 +116,10 @@ void *cache_alloc(cache_t *cache) { object = get_object(ret); } else { object = ret = malloc(cache->bufsize); - if (ret != NULL) { + if (ret) { object = get_object(ret); - if (cache->constructor != NULL && cache->constructor(object, NULL, 0) != 0) { + if (cache->constructor && cache->constructor(object, NULL, 0)) { free(ret); object = NULL; } @@ -105,8 +127,8 @@ void *cache_alloc(cache_t *cache) { } pthread_mutex_unlock(&cache->mutex); -#ifndef NDEBUG - if (object != NULL) { +# ifndef NDEBUG + if (object) { /* add a simple form of buffer-check */ uint64_t *pre = ret; *pre = redzone_pattern; @@ -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,11 +144,11 @@ 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)) - != 0) +) { raise(SIGABRT); cache_error = 1; @@ -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