/*
- * $Id: MemPool.c,v 1.4 1998/02/25 17:39:46 rousskov Exp $
+ * $Id: MemPool.c,v 1.5 1998/02/26 17:47:59 wessels Exp $
*
* AUTHOR: Alex Rousskov
*
* pre-allocated objects and 10% for dynamic stack. Any of the first two
* parameters can be 0. If name is NULL "anonymous" will be used instead
*/
- MemPool *mp1 = memPoolCreate(objCnt, objCnt/10, objSz, "urls");
+MemPool * mp1 = memPoolCreate(objCnt, objCnt / 10, objSz, "urls");
/*
* getting a new object from a pool; object buffer is initialized with 0s
*/
- void *buf = memPoolGetObj(mp1);
+void *buf = memPoolGetObj(mp1);
/*
* returning an object back
*/
- memPoolPutObj(mp1, buf);
+memPoolPutObj(mp1, buf);
/*
* accounting: generate report as an ASCII string
* warning: static buffer is used, strdup your copy!
*/
- char *report = xstrdup(memPoolReport());
+char *report = xstrdup(memPoolReport());
/* destroy your pools when done playing! */
- memPoolDestroy(mp1);
+memPoolDestroy(mp1);
#endif /* synopsis */
mp->buf = xcalloc(preallocCnt, objSz);
mp->obj_size = objSz;
mp->name = xstrdup(poolName ? poolName : "anonymous");
- mp->_buf_end = mp->buf + objSz*preallocCnt; /* internal, never dereference this! */
+ mp->_buf_end = mp->buf + objSz * preallocCnt; /* internal, never dereference this! */
mp->static_stack = stackCreate(preallocCnt);
mp->dynamic_stack = stackCreate(dynStackCnt);
/* other members are initialized with 0 because of calloc() */
/* push all pre-allocated memory on stack because it is currently free */
- while(preallocCnt-- > 0)
- stackPush(mp->static_stack, mp->buf + objSz*preallocCnt);
+ while (preallocCnt-- > 0)
+ stackPush(mp->static_stack, mp->buf + objSz * preallocCnt);
return mp;
}
void
-memPoolDestroy(MemPool *mp)
+memPoolDestroy(MemPool * mp)
{
assert(mp);
/* could also warn if some objects are left */
* never fails
*/
void *
-memPoolGetObj(MemPool *mp)
+memPoolGetObj(MemPool * mp)
{
assert(mp);
if (mp->static_stack->count)
return stackPop(mp->static_stack);
- else
- if (mp->dynamic_stack->count)
+ else if (mp->dynamic_stack->count)
return stackPop(mp->dynamic_stack);
/* have to alloc, monitor high whater mark */
if (++mp->alloc_count - mp->free_count > mp->alloc_high_water)
* corresponding stack is full
*/
void
-memPoolPutObj(MemPool *mp, void *obj)
+memPoolPutObj(MemPool * mp, void *obj)
{
assert(mp);
/* static object? */
- if (mp->buf <= (char*)obj && mp->_buf_end > (char*)obj) {
- assert(!mp->static_stack->is_full); /* never full if we got here! */
+ if (mp->buf <= (char *) obj && mp->_buf_end > (char *) obj) {
+ assert(!mp->static_stack->is_full); /* never full if we got here! */
stackPush(mp->static_stack, obj);
} else
- /* dynamic object, but stack may be full */
+ /* dynamic object, but stack may be full */
if (!mp->dynamic_stack->is_full) {
assert(mp->alloc_count);
stackPush(mp->dynamic_stack, obj);
} else {
- /* free-ing is the last option */
+ /* free-ing is the last option */
mp->free_count++;
assert(mp->free_count <= mp->alloc_count);
- xfree(obj); /* do this after assert */
+ xfree(obj); /* do this after assert */
}
}
const char *
-memPoolReport(MemPool *mp)
+memPoolReport(MemPool * mp)
{
- static char buf[1024]; /* we do not use LOCALL_ARRAY in squid/lib, do we? */
+ static char buf[1024]; /* we do not use LOCALL_ARRAY in squid/lib, do we? */
assert(mp);
snprintf(buf, sizeof(buf),
mp->name,
mp->obj_size,
mp->static_stack->capacity,
- (double)(mp->obj_size*mp->static_stack->capacity)/1024.,
+ (double) (mp->obj_size * mp->static_stack->capacity) / 1024.,
mp->dynamic_stack->capacity,
mp->static_stack->capacity + mp->static_stack->pop_count - mp->static_stack->push_count,
mp->alloc_count - mp->free_count,
/*
- * $Id: Stack.c,v 1.2 1998/02/21 00:56:38 rousskov Exp $
+ * $Id: Stack.c,v 1.3 1998/02/26 17:49:54 wessels Exp $
*
* AUTHOR: Alex Rousskov
*
#if 0
-Synopsis:
-
- /*
- * creating a stack that can hold up to objCnt pointers.
- * If objCnt is zero, the stack is always full (disabled)
- */
- Stack *s1 = stackCreate(objCnt);
- Stack *s2 = stackCreate(objCnt*2);
-
- /*
- * pop/push works as expected; it is OK to push a null pointer
- */
- if (!s2->is_full && s1->count)
- stackPush(s2, stackPop(s1));
+Synopsis(void)
+{
- /* destroying a stack */
- stackDestroy(s1);
+ /*
+ * creating a stack that can hold up to objCnt pointers.
+ * If objCnt is zero, the stack is always full (disabled)
+ */
+ Stack *s1 = stackCreate(objCnt);
+ Stack *s2 = stackCreate(objCnt * 2);
+
+ /*
+ * pop/push works as expected; it is OK to push a null pointer
+ */
+ if (!s2->is_full && s1->count)
+ stackPush(s2, stackPop(s1));
+
+ /* destroying a stack */
+ stackDestroy(s1);
+}
#endif /* Synopsis */
stackCreate(size_t capacity)
{
Stack *s = xcalloc(1, sizeof(Stack));
- s->buf = capacity > 0 ? xcalloc(capacity, sizeof(void*)) : NULL;
+ s->buf = capacity > 0 ? xcalloc(capacity, sizeof(void *)) : NULL;
s->capacity = capacity;
- s->count = 0;
- s->is_full = stackIsFull(s);
- /* other members are set to 0 in calloc */
+ s->count = 0;
+ s->is_full = stackIsFull(s);
+ /* other members are set to 0 in calloc */
return s;
}
void
-stackDestroy(Stack *s)
+stackDestroy(Stack * s)
{
assert(s);
/* could also warn if some objects are left */
}
void *
-stackPop(Stack *s)
+stackPop(Stack * s)
{
void *popped;
assert(s);
assert(s->count);
popped = s->buf[--s->count];
s->is_full = stackIsFull(s);
- s->pop_count++; /* might overflow eventually, but ok */
+ s->pop_count++; /* might overflow eventually, but ok */
return popped;
}
void
-stackPush(Stack *s, void *obj)
+stackPush(Stack * s, void *obj)
{
assert(s);
assert(!s->is_full);
s->buf[s->count++] = obj;
s->is_full = stackIsFull(s);
- s->push_count++; /* might overflow eventually, but ok */
+ s->push_count++; /* might overflow eventually, but ok */
}
if (!base64_initialized)
base64_init();
- while ((c = *decoded_str++) && out_cnt < sizeof(result)-1) {
- bits += c;
- char_count++;
- if (char_count == 3) {
- result[out_cnt++] = base64_code[bits >> 18];
- result[out_cnt++] = base64_code[(bits >> 12) & 0x3f];
- result[out_cnt++] = base64_code[(bits >> 6) & 0x3f];
- result[out_cnt++] = base64_code[bits & 0x3f];
- bits = 0;
- char_count = 0;
+ while ((c = *decoded_str++) && out_cnt < sizeof(result) - 1) {
+ bits += c;
+ char_count++;
+ if (char_count == 3) {
+ result[out_cnt++] = base64_code[bits >> 18];
+ result[out_cnt++] = base64_code[(bits >> 12) & 0x3f];
+ result[out_cnt++] = base64_code[(bits >> 6) & 0x3f];
+ result[out_cnt++] = base64_code[bits & 0x3f];
+ bits = 0;
+ char_count = 0;
} else {
- bits <<= 8;
+ bits <<= 8;
}
}
if (char_count != 0) {
- bits <<= 16 - (8 * char_count);
- result[out_cnt++] = base64_code[bits >> 18];
- result[out_cnt++] = base64_code[(bits >> 12) & 0x3f];
- if (char_count == 1) {
- result[out_cnt++] = '=';
- result[out_cnt++] = '=';
+ bits <<= 16 - (8 * char_count);
+ result[out_cnt++] = base64_code[bits >> 18];
+ result[out_cnt++] = base64_code[(bits >> 12) & 0x3f];
+ if (char_count == 1) {
+ result[out_cnt++] = '=';
+ result[out_cnt++] = '=';
} else {
- result[out_cnt++] = base64_code[(bits >> 6) & 0x3f];
- result[out_cnt++] = '=';
+ result[out_cnt++] = base64_code[(bits >> 6) & 0x3f];
+ result[out_cnt++] = '=';
}
}
- result[out_cnt] = '\0'; /* terminate */
+ result[out_cnt] = '\0'; /* terminate */
return result;
}