Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| SHA1 Hash: | c6b826b25e7f245410640d47aae36f9920da1bf2 |
|---|---|
| Date: | 2008-11-03 21:47:13 |
| User: | stephan |
| Comment: | added per-clob allocator hints. |
Tags And Properties
- branch=trunk inherited from [a870fea998]
- sym-trunk inherited from [a870fea998]
Changes
[hide diffs]Changes to src/whclob.c
@@ -58,35 +58,38 @@
unsigned long nUsed;
/*
The current position marker for some read operations.
*/
unsigned long nCursor;
-};
-
-
-/*
- Clob_empty is an empty whclob used for init purposes.
-*/
-static const whclob Clob_empty = {0, /* aData */
- 0, /* nAlloc */
- 0, /* nUsed */
- 0 /* nCursor */
-};
-
-/**
- A debugging-only function. Do not use it in client code.
-*/
-void whclob_dump( whclob * cb, int doString );
+ whclob_alloc_policy_t allocPolicy;
+};
/**
Default alloc size policy, simply returns n.
*/
static long whclob_default_alloc_policy( long n )
{
return n;
}
+
+/*
+ Clob_empty is an empty whclob used for init purposes.
+*/
+static const whclob Clob_empty = {0, /* aData */
+ 0, /* nAlloc */
+ 0, /* nUsed */
+ 0, /* nCursor */
+ whclob_default_alloc_policy
+};
+
+/**
+ A debugging-only function. Do not use it in client code.
+*/
+void whclob_dump( whclob * cb, int doString );
+
+
/**
Alloc policy which returns n * 1.2.
*/
long whclob_120_alloc_policy( long n )
@@ -95,17 +98,22 @@
}
typedef whclob_alloc_policy_t RePol;
static RePol whclob_current_alloc_policy = whclob_default_alloc_policy;
-RePol whclob_set_alloc_policy( RePol f )
+RePol whclob_set_default_alloc_policy( RePol f )
{
RePol old = whclob_current_alloc_policy;
whclob_current_alloc_policy = f ? f : whclob_default_alloc_policy;
return old;
}
-
+whclob_alloc_policy_t whclob_set_alloc_policy( whclob * cb, whclob_alloc_policy_t f )
+{
+ RePol old = cb ? cb->allocPolicy : 0;
+ if( cb ) cb->allocPolicy = f;
+ return old;
+}
#define WHCLOB_DUMP(X,B) if(WHCLOB_DEBUG) { printf(X ": blob [%s]: ", # B ); whclob_dump(B,1); }
void whclob_dump( whclob * cb, int doString )
{
@@ -185,14 +193,24 @@
return 0;
}
char const * zOld = cb->aData;
long oldUsed = cb->nUsed;
long oldAlloc = cb->nAlloc;
- long allocsize = fudge +
- (usePolicyHint && (0 != whclob_current_alloc_policy))
- ? (*whclob_current_alloc_policy)(sz)
- : sz;
+ long allocsize = 0;
+ if( (usePolicyHint && (0 != cb->allocPolicy)) )
+ {
+ allocsize = cb->allocPolicy(sz);
+ }
+ else if( (usePolicyHint && (0 != whclob_current_alloc_policy)) )
+ {
+ allocsize = (*whclob_current_alloc_policy)(sz);
+ }
+ else
+ {
+ allocsize = sz;
+ }
+ allocsize += fudge;
if( allocsize < (fudge + sz) ) allocsize = fudge + sz;
#if 0
char * pNew = cb->aData // oldAlloc
? realloc( cb->aData, allocsize )
: malloc( fudge + allocsize );
Changes to src/whclob.h
@@ -226,11 +226,12 @@
Equivalent to whclob_new_n(0).
*/
whclob * whclob_new();
/**
- See whclob_set_alloc_policy().
+ See whclob_set_alloc_policy() and
+ See whclob_set_default_alloc_policy().
*/
typedef long (*whclob_alloc_policy_t)( long );
/**
Sets the current allocation size determination policy and returns
@@ -260,11 +261,18 @@
If passed 0 it will use a default policy, which simply returns the
value passed to it. You can fetch the current policy, if you like,
by passing 0 to this function, then passing that return value
back to this function.
*/
-whclob_alloc_policy_t whclob_set_alloc_policy( whclob_alloc_policy_t );
+whclob_alloc_policy_t whclob_set_default_alloc_policy( whclob_alloc_policy_t );
+
+/**
+ Works just like whclob_set_default_alloc_policy() except that it
+ applies only to cb and takes precedence over
+ whclob_set_default_alloc_policy().
+*/
+whclob_alloc_policy_t whclob_set_alloc_policy( whclob * cb, whclob_alloc_policy_t );
/**
Free the resources, if any, associated with cb and zeroes out the
freed memory. After calling this, whclob_buffer(cb) will be an empty
@@ -319,11 +327,11 @@
referenced blob data to be deeply copied to cb.
On success it returns the number of bytes allocated to cb. The
number may be larger than sz, due to internal details of this API
and the implementation of the current allocation policy (see
- whclob_set_alloc_policy()).
+ whclob_set_default_alloc_policy()).
On error it returns one of the negative numbers specified
by whclob_rc:
- whclob_rc.AllocError = (re)allocation failed.