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
Changes to src/whclob.c
| Old (779dc876de0d4c11) | New (495720ffd953e640) | |||
|---|---|---|---|---|
| 1 | #include <string.h> | 1 | #include <string.h> | |
| 2 | #include <stdlib.h> | 2 | #include <stdlib.h> | |
| 3 | #include <stdio.h> | 3 | #include <stdio.h> | |
| 4 | #include <stdarg.h> | 4 | #include <stdarg.h> | |
| 5 | 5 | |||
| 52 hidden lines | ||||
| 58 | unsigned long nUsed; | 58 | unsigned long nUsed; | |
| 59 | /* | 59 | /* | |
| 60 | The current position marker for some read operations. | 60 | The current position marker for some read operations. | |
| 61 | */ | 61 | */ | |
| 62 | unsigned long nCursor; | 62 | unsigned long nCursor; | |
| > | 63 | whclob_alloc_policy_t allocPolicy; | ||
| 63 | }; | 64 | }; | |
| 64 | < | |||
| 65 | < | |||
| 66 | /* | < | ||
| 67 | Clob_empty is an empty whclob used for init purposes. | < | ||
| 68 | */ | < | ||
| 69 | static const whclob Clob_empty = {0, /* aData */ | < | ||
| 70 | 0, /* nAlloc */ | < | ||
| 71 | 0, /* nUsed */ | < | ||
| 72 | 0 /* nCursor */ | < | ||
| 73 | }; | < | ||
| 74 | < | |||
| 75 | /** | < | ||
| 76 | A debugging-only function. Do not use it in client code. | < | ||
| 77 | */ | < | ||
| 78 | void whclob_dump( whclob * cb, int doString ); | < | ||
| 79 | 65 | |||
| 80 | 66 | |||
| 81 | /** | 67 | /** | |
| 82 | Default alloc size policy, simply returns n. | 68 | Default alloc size policy, simply returns n. | |
| 83 | */ | 69 | */ | |
| 84 | static long whclob_default_alloc_policy( long n ) | 70 | static long whclob_default_alloc_policy( long n ) | |
| 85 | { | 71 | { | |
| 86 | return n; | 72 | return n; | |
| 87 | } | 73 | } | |
| > | 74 | |||
| > | 75 | /* | ||
| > | 76 | Clob_empty is an empty whclob used for init purposes. | ||
| > | 77 | */ | ||
| > | 78 | static const whclob Clob_empty = {0, /* aData */ | ||
| > | 79 | 0, /* nAlloc */ | ||
| > | 80 | 0, /* nUsed */ | ||
| > | 81 | 0, /* nCursor */ | ||
| > | 82 | whclob_default_alloc_policy | ||
| > | 83 | }; | ||
| > | 84 | |||
| > | 85 | /** | ||
| > | 86 | A debugging-only function. Do not use it in client code. | ||
| > | 87 | */ | ||
| > | 88 | void whclob_dump( whclob * cb, int doString ); | ||
| > | 89 | |||
| > | 90 | |||
| 88 | 91 | |||
| 89 | /** | 92 | /** | |
| 90 | Alloc policy which returns n * 1.2. | 93 | Alloc policy which returns n * 1.2. | |
| 91 | */ | 94 | */ | |
| 92 | long whclob_120_alloc_policy( long n ) | 95 | long whclob_120_alloc_policy( long n ) | |
| 2 hidden lines | ||||
| 95 | } | 98 | } | |
| 96 | 99 | |||
| 97 | 100 | |||
| 98 | typedef whclob_alloc_policy_t RePol; | 101 | typedef whclob_alloc_policy_t RePol; | |
| 99 | static RePol whclob_current_alloc_policy = whclob_default_alloc_policy; | 102 | static RePol whclob_current_alloc_policy = whclob_default_alloc_policy; | |
| 100 | RePol whclob_set_alloc_policy( RePol f ) | | | 103 | RePol whclob_set_default_alloc_policy( RePol f ) |
| 101 | { | 104 | { | |
| 102 | RePol old = whclob_current_alloc_policy; | 105 | RePol old = whclob_current_alloc_policy; | |
| 103 | whclob_current_alloc_policy = f ? f : whclob_default_alloc_policy; | 106 | whclob_current_alloc_policy = f ? f : whclob_default_alloc_policy; | |
| 104 | return old; | 107 | return old; | |
| 105 | } | 108 | } | |
| 106 | | | 109 | whclob_alloc_policy_t whclob_set_alloc_policy( whclob * cb, whclob_alloc_policy_t f ) | |
| | | 110 | { | ||
| | | 111 | RePol old = cb ? cb->allocPolicy : 0; | ||
| | | 112 | if( cb ) cb->allocPolicy = f; | ||
| | | 113 | return old; | ||
| | | 114 | } | ||
| 107 | 115 | |||
| 108 | 116 | |||
| 109 | #define WHCLOB_DUMP(X,B) if(WHCLOB_DEBUG) { printf(X ": blob [%s]: ", # B ); whclob_dump(B,1); } | 117 | #define WHCLOB_DUMP(X,B) if(WHCLOB_DEBUG) { printf(X ": blob [%s]: ", # B ); whclob_dump(B,1); } | |
| 110 | void whclob_dump( whclob * cb, int doString ) | 118 | void whclob_dump( whclob * cb, int doString ) | |
| 111 | { | 119 | { | |
| 73 hidden lines | ||||
| 185 | return 0; | 193 | return 0; | |
| 186 | } | 194 | } | |
| 187 | char const * zOld = cb->aData; | 195 | char const * zOld = cb->aData; | |
| 188 | long oldUsed = cb->nUsed; | 196 | long oldUsed = cb->nUsed; | |
| 189 | long oldAlloc = cb->nAlloc; | 197 | long oldAlloc = cb->nAlloc; | |
| 190 | long allocsize = fudge + | | | 198 | long allocsize = 0; |
| 191 | (usePolicyHint && (0 != whclob_current_alloc_policy)) | | | 199 | if( (usePolicyHint && (0 != cb->allocPolicy)) ) |
| 192 | ? (*whclob_current_alloc_policy)(sz) | | | 200 | { |
| 193 | : sz; | | | 201 | allocsize = cb->allocPolicy(sz); |
| | | 202 | } | ||
| | | 203 | else if( (usePolicyHint && (0 != whclob_current_alloc_policy)) ) | ||
| | | 204 | { | ||
| | | 205 | allocsize = (*whclob_current_alloc_policy)(sz); | ||
| | | 206 | } | ||
| | | 207 | else | ||
| | | 208 | { | ||
| | | 209 | allocsize = sz; | ||
| | | 210 | } | ||
| | | 211 | allocsize += fudge; | ||
| 194 | if( allocsize < (fudge + sz) ) allocsize = fudge + sz; | 212 | if( allocsize < (fudge + sz) ) allocsize = fudge + sz; | |
| 195 | #if 0 | 213 | #if 0 | |
| 196 | char * pNew = cb->aData // oldAlloc | 214 | char * pNew = cb->aData // oldAlloc | |
| 197 | ? realloc( cb->aData, allocsize ) | 215 | ? realloc( cb->aData, allocsize ) | |
| 198 | : malloc( fudge + allocsize ); | 216 | : malloc( fudge + allocsize ); | |
| 935 hidden lines | ||||
| 1134 | } | 1152 | } | |
| 1135 | #endif /* WHCLOB_USE_FILE */ | 1153 | #endif /* WHCLOB_USE_FILE */ | |
| 1136 | 1154 | |||
| 1137 | #undef WHCLOB_USE_ZLIB | 1155 | #undef WHCLOB_USE_ZLIB | |
| 1138 | #undef WHCLOB_DEBUG | 1156 | #undef WHCLOB_DEBUG | |
Changes to src/whclob.h
| Old (f715742de815baf7) | New (18a3e3ab3fba66f2) | |||
|---|---|---|---|---|
| 1 | #ifndef WANDERINGHORSE_NET_WHCLOB_H_INCLUDED_ | 1 | #ifndef WANDERINGHORSE_NET_WHCLOB_H_INCLUDED_ | |
| 2 | #define WANDERINGHORSE_NET_WHCLOB_H_INCLUDED_ 1 | 2 | #define WANDERINGHORSE_NET_WHCLOB_H_INCLUDED_ 1 | |
| 3 | #include <stdarg.h> | 3 | #include <stdarg.h> | |
| 4 | 4 | |||
| 5 | /*! @page whclob_page_main whclob: dynamic char array utilities | 5 | /*! @page whclob_page_main whclob: dynamic char array utilities | |
| 220 hidden lines | ||||
| 226 | Equivalent to whclob_new_n(0). | 226 | Equivalent to whclob_new_n(0). | |
| 227 | */ | 227 | */ | |
| 228 | whclob * whclob_new(); | 228 | whclob * whclob_new(); | |
| 229 | 229 | |||
| 230 | /** | 230 | /** | |
| 231 | See whclob_set_alloc_policy(). | | | 231 | See whclob_set_alloc_policy() and |
| | | 232 | See whclob_set_default_alloc_policy(). | ||
| 232 | */ | 233 | */ | |
| 233 | typedef long (*whclob_alloc_policy_t)( long ); | 234 | typedef long (*whclob_alloc_policy_t)( long ); | |
| 234 | 235 | |||
| 235 | /** | 236 | /** | |
| 236 | Sets the current allocation size determination policy and returns | 237 | Sets the current allocation size determination policy and returns | |
| 23 hidden lines | ||||
| 260 | If passed 0 it will use a default policy, which simply returns the | 261 | If passed 0 it will use a default policy, which simply returns the | |
| 261 | value passed to it. You can fetch the current policy, if you like, | 262 | value passed to it. You can fetch the current policy, if you like, | |
| 262 | by passing 0 to this function, then passing that return value | 263 | by passing 0 to this function, then passing that return value | |
| 263 | back to this function. | 264 | back to this function. | |
| 264 | */ | 265 | */ | |
| 265 | whclob_alloc_policy_t whclob_set_alloc_policy( whclob_alloc_policy_t ); | | | 266 | whclob_alloc_policy_t whclob_set_default_alloc_policy( whclob_alloc_policy_t ); |
| | | 267 | |||
| | | 268 | /** | ||
| | | 269 | Works just like whclob_set_default_alloc_policy() except that it | ||
| | | 270 | applies only to cb and takes precedence over | ||
| | | 271 | whclob_set_default_alloc_policy(). | ||
| | | 272 | */ | ||
| | | 273 | whclob_alloc_policy_t whclob_set_alloc_policy( whclob * cb, whclob_alloc_policy_t ); | ||
| 266 | 274 | |||
| 267 | 275 | |||
| 268 | /** | 276 | /** | |
| 269 | Free the resources, if any, associated with cb and zeroes out the | 277 | Free the resources, if any, associated with cb and zeroes out the | |
| 270 | freed memory. After calling this, whclob_buffer(cb) will be an empty | 278 | freed memory. After calling this, whclob_buffer(cb) will be an empty | |
| 48 hidden lines | ||||
| 319 | referenced blob data to be deeply copied to cb. | 327 | referenced blob data to be deeply copied to cb. | |
| 320 | 328 | |||
| 321 | On success it returns the number of bytes allocated to cb. The | 329 | On success it returns the number of bytes allocated to cb. The | |
| 322 | number may be larger than sz, due to internal details of this API | 330 | number may be larger than sz, due to internal details of this API | |
| 323 | and the implementation of the current allocation policy (see | 331 | and the implementation of the current allocation policy (see | |
| 324 | whclob_set_alloc_policy()). | | | 332 | whclob_set_default_alloc_policy()). |
| 325 | 333 | |||
| 326 | On error it returns one of the negative numbers specified | 334 | On error it returns one of the negative numbers specified | |
| 327 | by whclob_rc: | 335 | by whclob_rc: | |
| 328 | 336 | |||
| 329 | - whclob_rc.AllocError = (re)allocation failed. | 337 | - whclob_rc.AllocError = (re)allocation failed. | |
| 582 hidden lines | ||||
| 912 | 920 | |||
| 913 | #ifdef __cplusplus | 921 | #ifdef __cplusplus | |
| 914 | } /* extern "C" */ | 922 | } /* extern "C" */ | |
| 915 | #endif | 923 | #endif | |
| 916 | #endif /* WANDERINGHORSE_NET_WHCLOB_H_INCLUDED_ */ | 924 | #endif /* WANDERINGHORSE_NET_WHCLOB_H_INCLUDED_ */ | |