summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@netsurf-browser.org>2010-12-04 15:47:32 +0000
committerDaniel Silverstone <dsilvers@netsurf-browser.org>2010-12-04 15:47:32 +0000
commit780d398d7b29ac874df30854f85072a45aaec9a9 (patch)
tree21bfb9bb62a81cf13b3966662648bfaa10e58d4a
parenta79cf8437b936e1c3e730a9b216867cfaa9520bc (diff)
downloadlibwapcaplet-780d398d7b29ac874df30854f85072a45aaec9a9.tar.gz
libwapcaplet-780d398d7b29ac874df30854f85072a45aaec9a9.tar.bz2
Remove initialisation, use system allocation functions. r=vince
svn path=/trunk/libwapcaplet/; revision=10963
-rw-r--r--include/libwapcaplet/libwapcaplet.h36
-rw-r--r--src/libwapcaplet.c30
-rw-r--r--test/basictests.c109
3 files changed, 20 insertions, 155 deletions
diff --git a/include/libwapcaplet/libwapcaplet.h b/include/libwapcaplet/libwapcaplet.h
index a92dc4e..a5ec3d0 100644
--- a/include/libwapcaplet/libwapcaplet.h
+++ b/include/libwapcaplet/libwapcaplet.h
@@ -19,16 +19,6 @@ extern "C"
#include <stdint.h>
/**
- * Memory allocator function type
- *
- * @param ptr The old pointer to reallocate (NULL for new allocations).
- * @param size The size of block to allocate.
- * @param pw The private pointer for the allocator.
- * @return The newly allocated/resized pointer or NULL on error.
- */
-typedef void *(*lwc_allocator_fn)(void *ptr, size_t size, void *pw);
-
-/**
* An interned string.
*/
typedef struct lwc_string_s lwc_string;
@@ -46,9 +36,8 @@ typedef void (*lwc_iteration_callback_fn)(lwc_string *str, void *pw);
*/
typedef enum lwc_error_e {
lwc_error_ok = 0, /**< No error. */
- lwc_error_initialised = 1, /**< Library already initialised. */
- lwc_error_oom = 2, /**< Out of memory. */
- lwc_error_range = 3, /**< Substring internment out of range. */
+ lwc_error_oom = 1, /**< Out of memory. */
+ lwc_error_range = 2, /**< Substring internment out of range. */
} lwc_error;
/**
@@ -57,27 +46,6 @@ typedef enum lwc_error_e {
typedef uint32_t lwc_hash;
/**
- * Initialise the library.
- *
- * Initialise the library with an allocator function. All strings
- * interned will be allocated via this function, as will any
- * structures required to manage libwapcaplet. In this manner, all
- * interned strings are directly comparable, no matter what interned
- * them.
- *
- * @note If you require to know how much memory libwapcaplet is using
- * then you should use a counting allocator function.
- *
- * @param alloc The allocator to use for libwapcaplet allocations.
- * @param pw The private word to pass to \a alloc.
- * @param buckets The number of buckets to use by default, or zero to
- * allow the implementation to choose for itself.
- * @return The result of initialising. If not OK do not use
- * any further wapcaplet functions.
- */
-extern lwc_error lwc_initialise(lwc_allocator_fn alloc, void *pw, lwc_hash buckets);
-
-/**
* Intern a string.
*
* Take a copy of the string data referred to by \a s and \a slen and
diff --git a/src/libwapcaplet.c b/src/libwapcaplet.c
index 59e8637..0d76168 100644
--- a/src/libwapcaplet.c
+++ b/src/libwapcaplet.c
@@ -6,6 +6,7 @@
* Daniel Silverstone <dsilvers@netsurf-browser.org>
*/
+#include <stdlib.h>
#include <string.h>
#include <assert.h>
@@ -47,43 +48,39 @@ struct lwc_string_s {
#define NR_BUCKETS_DEFAULT (4091)
typedef struct lwc_context_s {
- lwc_allocator_fn alloc;
- void * alloc_pw;
lwc_string ** buckets;
lwc_hash bucketcount;
} lwc_context;
static lwc_context *ctx = NULL;
-#define LWC_ALLOC(s) ctx->alloc(NULL, s, ctx->alloc_pw)
-#define LWC_FREE(p) ctx->alloc(p, 0, ctx->alloc_pw)
+#define LWC_ALLOC(s) malloc(s)
+#define LWC_FREE(p) free(p)
typedef lwc_hash (*lwc_hasher)(const char *, size_t);
typedef int (*lwc_strncmp)(const char *, const char *, size_t);
typedef void (*lwc_memcpy)(char *, const char *, size_t);
-lwc_error
-lwc_initialise(lwc_allocator_fn alloc, void *pw, lwc_hash buckets)
+static lwc_error
+_lwc_initialise(void)
{
assert(alloc);
if (ctx != NULL)
- return lwc_error_initialised;
+ return lwc_error_ok;
- ctx = alloc(NULL, sizeof(lwc_context), pw);
+ ctx = LWC_ALLOC(sizeof(lwc_context));
if (ctx == NULL)
return lwc_error_oom;
memset(ctx, 0, sizeof(lwc_context));
- ctx->bucketcount = (buckets > 0) ? buckets : NR_BUCKETS_DEFAULT;
- ctx->alloc = alloc;
- ctx->alloc_pw = pw;
- ctx->buckets = alloc(NULL, sizeof(lwc_string *) * ctx->bucketcount, pw);
+ ctx->bucketcount = NR_BUCKETS_DEFAULT;
+ ctx->buckets = LWC_ALLOC(sizeof(lwc_string *) * ctx->bucketcount);
if (ctx->buckets == NULL) {
- alloc(ctx, 0, pw);
+ LWC_FREE(ctx);
return lwc_error_oom;
}
@@ -102,10 +99,17 @@ __lwc_intern(const char *s, size_t slen,
lwc_hash h;
lwc_hash bucket;
lwc_string *str;
+ lwc_error eret;
assert((s != NULL) || (slen == 0));
assert(ret);
+ if (ctx == NULL) {
+ eret = _lwc_initialise();
+ if (eret != lwc_error_ok)
+ return eret;
+ }
+
h = hasher(s, slen);
bucket = h % ctx->bucketcount;
str = ctx->buckets[bucket];
diff --git a/test/basictests.c b/test/basictests.c
index cd1fa32..c55e1bd 100644
--- a/test/basictests.c
+++ b/test/basictests.c
@@ -16,14 +16,6 @@
#define UNUSED(x) (void)(x)
#endif
-static void *last_pw = NULL;
-static void *
-trivial_alloc_fn(void *p, size_t s, void *pw)
-{
- last_pw = pw;
- return realloc(p, s);
-}
-
#ifndef NDEBUG
/* All the basic assert() tests */
START_TEST (test_lwc_intern_string_aborts1)
@@ -47,8 +39,6 @@ END_TEST
START_TEST (test_lwc_intern_substring_aborts2)
{
lwc_string *str;
- fail_unless(lwc_initialise(trivial_alloc_fn, NULL, 0) == lwc_error_ok,
- "unable to initialise the library");
fail_unless(lwc_intern_string("Jam", 3, &str) == lwc_error_ok,
"unable to intern 'Jam'");
@@ -88,99 +78,12 @@ END_TEST
#endif
-START_TEST (test_lwc_double_initialise_fails)
-{
- fail_unless(lwc_initialise(trivial_alloc_fn, NULL, 0) == lwc_error_ok,
- "Unable to initialise library");
- fail_unless(lwc_initialise(trivial_alloc_fn, NULL, 0) == lwc_error_initialised,
- "Able to initialise library a second time");
-}
-END_TEST
-
-static void *enomem_allocator(void *ptr, size_t n, void *pw)
-{
- int *pi = (int*)pw;
- UNUSED(ptr);
- UNUSED(n);
-
- if (*pi > 0) {
- *pi -= 1;
- return realloc(ptr, n);
- }
-
- return NULL;
-}
-
-START_TEST (test_lwc_initialise_fails_with_no_memory)
-{
- int permitted = 0;
- fail_unless(lwc_initialise(enomem_allocator, &permitted, 0) == lwc_error_oom,
- "Able to initialise library with no memory available?!");
-}
-END_TEST
-
-START_TEST (test_lwc_initialise_fails_with_low_memory)
-{
- int permitted = 1;
- fail_unless(lwc_initialise(enomem_allocator, &permitted, 0) == lwc_error_oom,
- "Able to initialise library with no memory available?!");
-}
-END_TEST
-
-START_TEST (test_lwc_intern_fails_with_no_memory)
-{
- int permitted = 2; /* context and buckets */
- lwc_string *str;
-
- fail_unless(lwc_initialise(enomem_allocator, &permitted, 0) == lwc_error_ok,
- "Unable to initialise library");
- fail_unless(lwc_intern_string("Hello", 5, &str) == lwc_error_oom,
- "Able to allocate string despite enomem.");
-
-}
-END_TEST
-
-START_TEST (test_lwc_caseless_compare_fails_with_no_memory1)
-{
- int permitted = 3; /* ctx, buckets, 1 string */
- lwc_string *str;
- bool result = true;
-
- fail_unless(lwc_initialise(enomem_allocator, &permitted, 0) == lwc_error_ok,
- "Unable to initialise library");
- fail_unless(lwc_intern_string("Hello", 5, &str) == lwc_error_ok,
- "Unable to allocate string.");
- fail_unless(lwc_string_caseless_isequal(str, str, &result) == lwc_error_oom,
- "Able to caselessly compare despite no memory");
-
-}
-END_TEST
-
-START_TEST (test_lwc_caseless_compare_fails_with_no_memory2)
-{
- int permitted = 5; /* ctx, buckets, 3 strings */
- lwc_string *str1, *str2;
- bool result = true;
-
- fail_unless(lwc_initialise(enomem_allocator, &permitted, 0) == lwc_error_ok,
- "Unable to initialise library");
- fail_unless(lwc_intern_string("Hello", 5, &str1) == lwc_error_ok,
- "Unable to allocate string.");
- fail_unless(lwc_intern_string("World", 5, &str2) == lwc_error_ok,
- "Unable to allocate string.");
- fail_unless(lwc_string_caseless_isequal(str1, str2, &result) == lwc_error_oom,
- "Able to caselessly compare despite no memory");
-
-}
-END_TEST
-
/**** The next set of tests need a fixture set ****/
static void
with_simple_context_setup(void)
{
- fail_unless(lwc_initialise(trivial_alloc_fn, NULL, 0) == lwc_error_ok,
- "Unable to initialise library");
+ /* Nothing to set up */
}
static void
@@ -234,9 +137,6 @@ static lwc_string *intern_one = NULL, *intern_two = NULL, *intern_three = NULL,
static void
with_filled_context_setup(void)
{
- fail_unless(lwc_initialise(trivial_alloc_fn, NULL, 2) == lwc_error_ok,
- "Unable to initialise library");
-
fail_unless(lwc_intern_string("one", 3, &intern_one) == lwc_error_ok,
"Unable to intern 'one'");
fail_unless(lwc_intern_string("two", 3, &intern_two) == lwc_error_ok,
@@ -480,13 +380,6 @@ lwc_basic_suite(SRunner *sr)
SIGABRT);
#endif
- tcase_add_test(tc_basic, test_lwc_double_initialise_fails);
- tcase_add_test(tc_basic, test_lwc_initialise_fails_with_no_memory);
- tcase_add_test(tc_basic, test_lwc_initialise_fails_with_low_memory);
- tcase_add_test(tc_basic, test_lwc_intern_fails_with_no_memory);
- tcase_add_test(tc_basic, test_lwc_caseless_compare_fails_with_no_memory1);
- tcase_add_test(tc_basic, test_lwc_caseless_compare_fails_with_no_memory2);
-
suite_add_tcase(s, tc_basic);
tc_basic = tcase_create("Ops with a context");