committing local changes to rmutil

master
Dvir Volk 7 years ago
parent 121f189a7a
commit af5796d5bb

@ -41,10 +41,11 @@ char *rmalloc_strndup(const char *s, size_t n);
#define strndup(s, n) rmalloc_strndup(s, n) #define strndup(s, n) rmalloc_strndup(s, n)
#else #else
#endif /* REDIS_MODULE_TARGET */
/* This function shold be called if you are working with malloc-patched code /* This function shold be called if you are working with malloc-patched code
* ouside of redis, usually for unit tests. Call it once when entering your unit * ouside of redis, usually for unit tests. Call it once when entering your unit
* tests' main() */ * tests' main() */
void RMUTil_InitAlloc(); void RMUTil_InitAlloc();
#endif /* REDIS_MODULE_TARGET */
#endif /* __RMUTIL_ALLOC__ */ #endif /* __RMUTIL_ALLOC__ */

@ -36,6 +36,11 @@
* the include of your alternate allocator if needed (not needed in order * the include of your alternate allocator if needed (not needed in order
* to use the default libc allocator). */ * to use the default libc allocator). */
#if defined(__MACH__)
#include <stdlib.h>
#else
#include <malloc.h>
#endif
//#include "zmalloc.h" //#include "zmalloc.h"
#define s_malloc malloc #define s_malloc malloc
#define s_realloc realloc #define s_realloc realloc

@ -3,7 +3,6 @@
#include <ctype.h> #include <ctype.h>
#include "strings.h" #include "strings.h"
#include "sds.h" #include "sds.h"
// RedisModuleString *RMUtil_CreateFormattedString(RedisModuleCtx *ctx, const char *fmt, ...) { // RedisModuleString *RMUtil_CreateFormattedString(RedisModuleCtx *ctx, const char *fmt, ...) {
@ -21,47 +20,51 @@
int RMUtil_StringEquals(RedisModuleString *s1, RedisModuleString *s2) { int RMUtil_StringEquals(RedisModuleString *s1, RedisModuleString *s2) {
const char *c1, *c2;
size_t l1, l2;
c1 = RedisModule_StringPtrLen(s1, &l1);
c2 = RedisModule_StringPtrLen(s2, &l2);
if (l1 != l2) return 0;
const char *c1, *c2; return strncmp(c1, c2, l1) == 0;
size_t l1, l2;
c1 = RedisModule_StringPtrLen(s1, &l1);
c2 = RedisModule_StringPtrLen(s2, &l2);
if (l1 != l2) return 0;
return strncmp(c1, c2, l1) == 0;
} }
int RMUtil_StringEqualsC(RedisModuleString *s1, const char *s2) { int RMUtil_StringEqualsC(RedisModuleString *s1, const char *s2) {
const char *c1;
size_t l1, l2 = strlen(s2);
c1 = RedisModule_StringPtrLen(s1, &l1);
if (l1 != l2) return 0;
return strncmp(c1, s2, l1) == 0;
}
int RMUtil_StringEqualsCaseC(RedisModuleString *s1, const char *s2) {
const char *c1; const char *c1;
size_t l1, l2 = strlen(s2); size_t l1, l2 = strlen(s2);
c1 = RedisModule_StringPtrLen(s1, &l1); c1 = RedisModule_StringPtrLen(s1, &l1);
if (l1 != l2) return 0; if (l1 != l2) return 0;
return strncmp(c1, s2, l1) == 0; return strncasecmp(c1, s2, l1) == 0;
} }
void RMUtil_StringToLower(RedisModuleString *s) { void RMUtil_StringToLower(RedisModuleString *s) {
size_t l; size_t l;
char *c = (char *)RedisModule_StringPtrLen(s, &l); char *c = (char *)RedisModule_StringPtrLen(s, &l);
size_t i; size_t i;
for (i = 0; i < l; i++) { for (i = 0; i < l; i++) {
*c = tolower(*c); *c = tolower(*c);
++c; ++c;
} }
} }
void RMUtil_StringToUpper(RedisModuleString *s) { void RMUtil_StringToUpper(RedisModuleString *s) {
size_t l; size_t l;
char *c = (char *)RedisModule_StringPtrLen(s, &l); char *c = (char *)RedisModule_StringPtrLen(s, &l);
size_t i; size_t i;
for (i = 0; i < l; i++) { for (i = 0; i < l; i++) {
*c = toupper(*c); *c = toupper(*c);
++c; ++c;
} }
} }

@ -8,16 +8,18 @@
* Note that RedisModuleString objects CANNOT be used as formatting arguments. * Note that RedisModuleString objects CANNOT be used as formatting arguments.
*/ */
// DEPRECATED since it was added to the RedisModule API. Replaced with a macro below // DEPRECATED since it was added to the RedisModule API. Replaced with a macro below
//RedisModuleString *RMUtil_CreateFormattedString(RedisModuleCtx *ctx, const char *fmt, ...); // RedisModuleString *RMUtil_CreateFormattedString(RedisModuleCtx *ctx, const char *fmt, ...);
#define RMUtil_CreateFormattedString RedisModule_CreateStringPrintf #define RMUtil_CreateFormattedString RedisModule_CreateStringPrintf
/* Return 1 if the two strings are equal. Case *sensitive* */ /* Return 1 if the two strings are equal. Case *sensitive* */
int RMUtil_StringEquals(RedisModuleString *s1, RedisModuleString *s2); int RMUtil_StringEquals(RedisModuleString *s1, RedisModuleString *s2);
/* Return 1 if the string is equal to a C NULL terminated string. Case *sensitive* */ /* Return 1 if the string is equal to a C NULL terminated string. Case *sensitive* */
int RMUtil_StringEqualsC(RedisModuleString *s1, const char *s2); int RMUtil_StringEqualsC(RedisModuleString *s1, const char *s2);
/* Return 1 if the string is equal to a C NULL terminated string. Case *insensitive* */
int RMUtil_StringEqualsCaseC(RedisModuleString *s1, const char *s2);
/* Converts a redis string to lowercase in place without reallocating anything */ /* Converts a redis string to lowercase in place without reallocating anything */
void RMUtil_StringToLower(RedisModuleString *s); void RMUtil_StringToLower(RedisModuleString *s);

@ -71,7 +71,6 @@ RMUtilInfo *RMUtil_GetRedisInfo(RedisModuleCtx *ctx) {
char *key = strsep(&line, ":"); char *key = strsep(&line, ":");
info->entries[i].key = key; info->entries[i].key = key;
info->entries[i].val = line; info->entries[i].val = line;
//printf("Got info '%s' = '%s'\n", key, line);
i++; i++;
if (i >= cap) { if (i >= cap) {
cap *= 2; cap *= 2;
@ -123,7 +122,6 @@ int RMUtilInfo_GetDouble(RMUtilInfo *info, const char *key, double *d) {
} }
*d = strtod(p, NULL); *d = strtod(p, NULL);
//printf("p: %s, d: %f\n", p, *d);
if ((errno == ERANGE && (*d == HUGE_VAL || *d == -HUGE_VAL)) || (errno != 0 && *d == 0)) { if ((errno == ERANGE && (*d == HUGE_VAL || *d == -HUGE_VAL)) || (errno != 0 && *d == 0)) {
return 0; return 0;
} }

@ -6,7 +6,7 @@ inline int __vector_PushPtr(Vector *v, void *elem) {
Vector_Resize(v, v->cap ? v->cap * 2 : 1); Vector_Resize(v, v->cap ? v->cap * 2 : 1);
} }
__vector_PutPtr(v, v->top, elem); __vector_PutPtr(v, v->top++, elem);
return v->top; return v->top;
} }
@ -43,7 +43,7 @@ inline int __vector_PutPtr(Vector *v, size_t pos, void *elem) {
} else { } else {
memset(v->data + pos * v->elemSize, 0, v->elemSize); memset(v->data + pos * v->elemSize, 0, v->elemSize);
} }
// move the end offset to pos + 1 if we grew // move the end offset to pos if we grew
if (pos >= v->top) { if (pos >= v->top) {
v->top = pos + 1; v->top = pos + 1;
} }
@ -80,6 +80,8 @@ void Vector_Free(Vector *v) {
free(v); free(v);
} }
/* return the used size of the vector, regardless of capacity */
inline int Vector_Size(Vector *v) { return v->top; } inline int Vector_Size(Vector *v) { return v->top; }
/* return the actual capacity */ /* return the actual capacity */

@ -10,10 +10,10 @@
* Works like C++ std::vector with an underlying resizable buffer * Works like C++ std::vector with an underlying resizable buffer
*/ */
typedef struct { typedef struct {
char *data; char *data;
size_t elemSize; size_t elemSize;
size_t cap; size_t cap;
size_t top; size_t top;
} Vector; } Vector;
@ -47,13 +47,11 @@ int Vector_Pop(Vector *v, void *ptr);
* Put an element at pos. * Put an element at pos.
* Note: If pos is outside the vector capacity, we resize it accordingly * Note: If pos is outside the vector capacity, we resize it accordingly
*/ */
#define Vector_Put(v, pos, elem) \ #define Vector_Put(v, pos, elem) __vector_PutPtr(v, pos, elem ? &(typeof(elem)){elem} : NULL)
__vector_PutPtr(v, pos, elem ? &(typeof(elem)){elem} : NULL)
/* Push an element at the end of v, resizing it if needed. This macro wraps /* Push an element at the end of v, resizing it if needed. This macro wraps
* __vector_PushPtr */ * __vector_PushPtr */
#define Vector_Push(v, elem) \ #define Vector_Push(v, elem) __vector_PushPtr(v, elem ? &(typeof(elem)){elem} : NULL)
__vector_PushPtr(v, elem ? &(typeof(elem)){elem} : NULL)
int __vector_PushPtr(Vector *v, void *elem); int __vector_PushPtr(Vector *v, void *elem);

Loading…
Cancel
Save