You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@commons.apache.org by gs...@apache.org on 2003/11/01 20:13:15 UTC

svn commit: rev 66 - commons/serf/branches/gen2/buckets

Author: gstein
Date: Sat Nov  1 11:13:14 2003
New Revision: 66

Modified:
   commons/serf/branches/gen2/buckets/buckets.c
Log:
Prevent an allocation until metadata is actually used.

* gen2/buckets/buckets.c:
  (serf_bucket_create): initialize the metadata field to NULL rather than
    allocating a second structure.
  (serf_default_set_metadata): fastpath the deletion of values when no
    metadata structure or hash table exists. if setting a value, then
    create the structures as needed.
  (serf_default_get_metadata): key on the existence of the metadata field
    itself, rather than the hash table within it.
  (serf_default_destroy): test for the existence of the metadata before
    freeing it.


Modified: commons/serf/branches/gen2/buckets/buckets.c
==============================================================================
--- commons/serf/branches/gen2/buckets/buckets.c	(original)
+++ commons/serf/branches/gen2/buckets/buckets.c	Sat Nov  1 11:13:14 2003
@@ -127,8 +127,7 @@
 
     bkt->type = type;
     bkt->data = data;
-    bkt->metadata = serf_bucket_mem_alloc(allocator, sizeof(*bkt->metadata));
-    bkt->metadata->hash = NULL;
+    bkt->metadata = NULL;
     bkt->allocator = allocator;
 
     return bkt;
@@ -141,18 +140,38 @@
 {
     apr_hash_t *md_hash;
 
-    md_hash = NULL;
+    if (bucket->metadata == NULL) {
+        if (md_value == NULL) {
+            /* If we're trying to delete the value, then we're already done
+             * since there isn't any metadata in the bucket. */
+            return APR_SUCCESS;
+        }
+
+        /* Create the metadata container. */
+        bucket->metadata = serf_bucket_mem_alloc(allocator,
+                                                 sizeof(*bucket->metadata));
 
-    if (!bucket->metadata->hash) {
+        /* ### pool usage! */
         bucket->metadata->hash = apr_hash_make(bucket->allocator->pool);
     }
-    else {
-        md_hash = apr_hash_get(bucket->metadata->hash, md_type,
-                               APR_HASH_KEY_STRING);
-    }
+
+    /* Look up the hash table for this md_type */
+    md_hash = apr_hash_get(bucket->metadata->hash, md_type,
+                           APR_HASH_KEY_STRING);
 
     if (!md_hash) {
+        if (md_value == NULL) {
+            /* The hash table isn't present, so there is no work to delete
+             * a value.
+             */
+            return APR_SUCCESS;
+        }
+
+        /* Create the missing hash table. */
+        /* ### pool usage! */
         md_hash = apr_hash_make(bucket->allocator->pool);
+
+        /* Put the new hash table back into the type hash. */
         apr_hash_set(bucket->metadata->hash, md_type, APR_HASH_KEY_STRING,
                      md_hash);
     }
@@ -171,7 +190,7 @@
     /* Initialize return value to not being found. */
     *md_value = NULL;
 
-    if (bucket->metadata->hash) {
+    if (bucket->metadata) {
         apr_hash_t *md_hash;
 
         md_hash = apr_hash_get(bucket->metadata->hash, md_type,
@@ -194,7 +213,9 @@
 
 SERF_DECLARE(void) serf_default_destroy(serf_bucket_t *bucket)
 {
-    serf_bucket_mem_free(bucket->allocator, bucket->metadata);
+    if (bucket->metadata != NULL) {
+        serf_bucket_mem_free(bucket->allocator, bucket->metadata);
+    }
     serf_bucket_mem_free(bucket->allocator, bucket);
 }