Revision: 14672
Author: adrian.chadd
Date: Sun May 16 16:05:06 2010
Log: Issue #108 - migrate the header management code to use a Vector type rather than Array.

This may make the codebase unstable; be warned! It requires much more testing!
It does however pass the initial test suite.


http://code.google.com/p/lusca-cache/source/detail?r=14672

Modified:
 /branches/LUSCA_HEAD/libhttp/HttpHeader.c
 /branches/LUSCA_HEAD/libhttp/HttpHeader.h

=======================================
--- /branches/LUSCA_HEAD/libhttp/HttpHeader.c   Sat Apr 17 18:14:03 2010
+++ /branches/LUSCA_HEAD/libhttp/HttpHeader.c   Sun May 16 16:05:06 2010
@@ -162,7 +162,7 @@
     debug(55, 7) ("init-ing hdr: %p owner: %d\n", hdr, owner);
     memset(hdr, 0, sizeof(*hdr));
     hdr->owner = owner;
-    arrayInit(&hdr->entries);
+    vector_init(&hdr->entries, sizeof(HttpHeaderEntry), 16);
 }

 /*!
@@ -204,10 +204,10 @@
      * has been used.  As a hack, just never count zero-sized header
      * arrays.
      */
-    if (0 != hdr->entries.count)
- statHistCount(&HttpHeaderStats[hdr->owner].hdrUCountDistr, hdr->entries.count);
+    if (vector_numentries(&hdr->entries))
+ statHistCount(&HttpHeaderStats[hdr->owner].hdrUCountDistr, vector_numentries(&hdr->entries));
     HttpHeaderStats[hdr->owner].destroyedCount++;
- HttpHeaderStats[hdr->owner].busyDestroyedCount += hdr->entries.count > 0; + HttpHeaderStats[hdr->owner].busyDestroyedCount += vector_numentries(&hdr->entries) > 0;
     while ((e = httpHeaderGetEntry(hdr, &pos))) {
         /* tmp hack to try to avoid coredumps */
         if (e->id >= HDR_ENUM_END) {
@@ -217,10 +217,9 @@
statHistCount(&HttpHeaderStats[hdr->owner].fieldTypeDistr, e->id);
             /* yes, this destroy() leaves us in an inconsistent state */
             httpHeaderEntryDestroy(e);
-           memPoolFree(pool_http_header_entry, e);
         }
     }
-    arrayClean(&hdr->entries);
+    vector_done(&hdr->entries);
 }

 /* just handy in parsing: resets and returns false */
@@ -271,15 +270,24 @@
     hdr->len += strLen(e->name) + 2 + strLen(e->value) + 2;
 }

-/* appends an entry;
- * does not call httpHeaderEntryClone() so one should not reuse "*e"
- */
-void
-httpHeaderAddEntry(HttpHeader * hdr, HttpHeaderEntry * e)
-{
- debug(55, 7) ("%p adding entry: %d at %d\n", hdr, e->id, hdr->entries.count);
-    httpHeaderAddInfo(hdr, e);
-    arrayAppend(&hdr->entries, e);
+HttpHeaderEntry *
+httpHeaderAllocNewEntry(HttpHeader *hdr)
+{
+       HttpHeaderEntry *e;
+
+       e = vector_append(&hdr->entries);
+       e->active = 0;
+       return e;
+}
+
+HttpHeaderEntry *
+httpHeaderAllocInsertEntry(HttpHeader *hdr, int pos)
+{
+       HttpHeaderEntry *e;
+
+       e = vector_insert(&hdr->entries, pos);
+       e->active = 0;
+       return e;
 }

 /*!
@@ -312,21 +320,22 @@
 /*
  * -1 means "don't know length, call strlen()
  */
-int
+HttpHeaderEntry *
httpHeaderAddEntryStr2(HttpHeader *hdr, http_hdr_type id, const char *a, int al, const char *v, int vl)
 {
-       HttpHeaderEntry *e = memPoolAlloc(pool_http_header_entry);
+       HttpHeaderEntry *e = httpHeaderAllocNewEntry(hdr);
        httpHeaderEntryCreate(e, id, a, al, v, vl);
-       httpHeaderAddEntry(hdr, e);
-       return(hdr->entries.count - 1);
+       httpHeaderAddInfo(hdr, e);
+       return e;
 }

-void
+HttpHeaderEntry *
httpHeaderAddEntryString(HttpHeader *hdr, http_hdr_type id, const String *a, const String *v)
 {
-       HttpHeaderEntry *e = memPoolAlloc(pool_http_header_entry);
+       HttpHeaderEntry *e = httpHeaderAllocNewEntry(hdr);
        httpHeaderEntryCreateStr(e, id, a, v);
-       httpHeaderAddEntry(hdr, e);
+       httpHeaderAddInfo(hdr, e);
+       return e;
 }

 /*!
@@ -354,31 +363,23 @@
 void
httpHeaderInsertEntryStr(HttpHeader *hdr, int pos, http_hdr_type id, const char *attrib, const char *value)
 {
-       HttpHeaderEntry *e = memPoolAlloc(pool_http_header_entry);
+       HttpHeaderEntry *e = httpHeaderAllocInsertEntry(hdr, pos);
        httpHeaderEntryCreate(e, id, attrib, -1, value, -1);
-       httpHeaderInsertEntry(hdr, e, pos);
+       httpHeaderAddInfo(hdr, e);
 }

-/* inserts an entry at the given position;
- * does not call httpHeaderEntryClone() so one should not reuse "*e"
- */
-void
-httpHeaderInsertEntry(HttpHeader * hdr, HttpHeaderEntry * e, int pos)
-{
- debug(55, 7) ("%p adding entry: %d at %d\n", hdr, e->id, hdr->entries.count);
-    httpHeaderAddInfo(hdr, e);
-    arrayInsert(&hdr->entries, e, pos);
-}

 /* returns next valid entry */
 HttpHeaderEntry *
 httpHeaderGetEntry(const HttpHeader * hdr, HttpHeaderPos * pos)
 {
+    HttpHeaderEntry *e;
     assert(hdr && pos);
-    assert(*pos >= HttpHeaderInitPos && *pos < hdr->entries.count);
-    for ((*pos)++; *pos < hdr->entries.count; (*pos)++) {
-        if (hdr->entries.items[*pos])
-            return hdr->entries.items[*pos];
+ assert(*pos >= HttpHeaderInitPos && *pos < vector_numentries(&hdr->entries));
+    for ((*pos)++; *pos < vector_numentries(&hdr->entries); (*pos)++) {
+       e = vector_get(&hdr->entries, *pos);
+       if (e->active)
+               return e;
     }
     return NULL;
 }
@@ -386,9 +387,9 @@
 void
 httpHeaderAddClone(HttpHeader * hdr, const HttpHeaderEntry * e)
 {
-    HttpHeaderEntry *ne = memPoolAlloc(pool_http_header_entry);
+    HttpHeaderEntry *ne = httpHeaderAllocNewEntry(hdr);
     httpHeaderEntryClone(ne, e);
-    httpHeaderAddEntry(hdr, ne);
+    httpHeaderAddInfo(hdr, ne);
 }

 /*!
@@ -474,14 +475,11 @@
 httpHeaderDelAt(HttpHeader * hdr, HttpHeaderPos pos)
 {
     HttpHeaderEntry *e;
-    assert(pos >= HttpHeaderInitPos && pos < hdr->entries.count);
-    e = hdr->entries.items[pos];
-    hdr->entries.items[pos] = NULL;
-    /* decrement header length, allow for ": " and crlf */
+ assert(pos >= HttpHeaderInitPos && pos < vector_numentries(&hdr->entries));
+    e = vector_get(&hdr->entries, pos);
     hdr->len -= strLen(e->name) + 2 + strLen(e->value) + 2;
     assert(hdr->len >= 0);
     httpHeaderEntryDestroy(e);
-    memPoolFree(pool_http_header_entry, e);
 }

 int
@@ -621,21 +619,24 @@
 {
     HttpHeaderPos dp = HttpHeaderInitPos;
     HttpHeaderPos pos = HttpHeaderInitPos;
+    HttpHeaderEntry *e;

/* XXX breaks layering for now! ie, getting grubby fingers in without httpHeaderEntryGet() */
     dp = 0;
     pos = 0;
-    while (dp < hdr->entries.count) {
- for (; dp < hdr->entries.count && hdr->entries.items[dp] == NULL; dp++);
-        if (dp >= hdr->entries.count)
+    while (dp < vector_numentries(&hdr->entries)) {
+ for (; dp < vector_numentries(&hdr->entries) && ((HttpHeaderEntry *) vector_get(&hdr->entries, dp))->active == 0; dp++);
+        if (dp >= vector_numentries(&hdr->entries))
             break;
-        hdr->entries.items[pos] = hdr->entries.items[dp];
-        if (dp != pos)
-            hdr->entries.items[dp] = NULL;
+        if (dp != pos) {
+               (void) vector_copy_item(&hdr->entries, pos, dp);
+               e = vector_get(&hdr->entries, dp);
+               e->active = 0;
+       }
         pos++;
         dp++;
     }
-    arrayShrink(&hdr->entries, pos);
+    vector_shrink(&hdr->entries, pos);
 }

 /* use fresh entries to replace old ones */
=======================================
--- /branches/LUSCA_HEAD/libhttp/HttpHeader.h   Sat Apr 17 18:14:03 2010
+++ /branches/LUSCA_HEAD/libhttp/HttpHeader.h   Sun May 16 16:05:06 2010
@@ -13,7 +13,7 @@

 struct _HttpHeader {
     /* protected, do not use these, use interface functions instead */
-    Array entries;              /* parsed entries in raw format */
+    Vector entries;            /* parsed entries in raw format */
     HttpHeaderMask mask;        /* bit set <=> entry present */
     http_hdr_owner_type owner;  /* request or reply */
int len; /* length when packed, not counting terminating '\0' */
@@ -29,7 +29,6 @@
 typedef struct _TimeOrTag TimeOrTag;

 extern HttpHeaderFieldInfo *Headers;
-extern MemPool * pool_http_header_entry;

 /* XXX as mentioned in HttpHeader.c ; these probably shouldn't be here? */
 extern HttpHeaderMask ListHeadersMask;
@@ -48,16 +47,14 @@
 extern void httpHeaderClean(HttpHeader * hdr);
 extern int httpHeaderReset(HttpHeader * hdr);
extern void httpHeaderAddClone(HttpHeader * hdr, const HttpHeaderEntry * e);
-extern void httpHeaderAddEntry(HttpHeader * hdr, HttpHeaderEntry * e);
-extern void httpHeaderInsertEntry(HttpHeader * hdr, HttpHeaderEntry * e, int pos);
 extern void httpHeaderAppend(HttpHeader * dest, const HttpHeader * src);
extern HttpHeaderEntry *httpHeaderGetEntry(const HttpHeader * hdr, HttpHeaderPos * pos); extern HttpHeaderEntry *httpHeaderFindEntry(const HttpHeader * hdr, http_hdr_type id); extern HttpHeaderEntry *httpHeaderFindLastEntry(const HttpHeader * hdr, http_hdr_type id);

extern void httpHeaderAddEntryStr(HttpHeader *hdr, http_hdr_type id, const char *attrib, const char *value); -extern int httpHeaderAddEntryStr2(HttpHeader *hdr, http_hdr_type id, const char *attrib, int attrib_len, const char *value, int value_len); -extern void httpHeaderAddEntryString(HttpHeader *hdr, http_hdr_type id, const String *a, const String *v); +extern HttpHeaderEntry * httpHeaderAddEntryStr2(HttpHeader *hdr, http_hdr_type id, const char *attrib, int attrib_len, const char *value, int value_len); +extern HttpHeaderEntry * httpHeaderAddEntryString(HttpHeader *hdr, http_hdr_type id, const String *a, const String *v);

extern void httpHeaderInsertEntryStr(HttpHeader *hdr, int pos, http_hdr_type id, const char *attrib, const char *value);

@@ -75,4 +72,9 @@
 /* append/update */
extern void httpHeaderUpdate(HttpHeader * old, const HttpHeader * fresh, const HttpHeaderMask * denied_mask);

+/* new stuff */
+extern HttpHeaderEntry * httpHeaderAllocNewEntry(HttpHeader *hdr);
+extern HttpHeaderEntry * httpHeaderAllocInsertEntry(HttpHeader *hdr, int pos);
+
+
 #endif

--
You received this message because you are subscribed to the Google Groups 
"lusca-commit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/lusca-commit?hl=en.

Reply via email to