OSSP CVS Repository

ossp - Check-in [2618]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 2618
Date: 2002-Oct-18 14:24:53 (local)
2002-Oct-18 12:24:53 (UTC)
User:mlelstv
Branch:
Comment: change in al_attach_buffer API

back out rse's change to al_traverse parameters. parameter order is: object ptr(if any), input parameters, output parameters

PR: Submitted by: Reviewed by: Approved by: Obtained from:

Tickets:
Inspections:
Files:
ossp-pkg/sio/al.c      1.23 -> 1.24     26 inserted, 13 deleted
ossp-pkg/sio/al.h      1.11 -> 1.12     2 inserted, 2 deleted
ossp-pkg/sio/al_test.c      1.11 -> 1.12     7 inserted, 2 deleted

ossp-pkg/sio/al.c 1.23 -> 1.24

--- al.c 2002/10/18 12:08:52     1.23
+++ al.c 2002/10/18 12:24:53     1.24
@@ -82,7 +82,8 @@
     char             *mem;             /* reference to underlying chunk of data */
     size_t            size;            /* size of underlying chunk of data */
     int               usecount;        /* reference count (from al_chunk_t) */
-    int               freemem;         /* boolean flag whether chunk of data has to be free(3)ed */
+    void           (*freemem)(char *p, size_t n, void *u); /* callback function to reclaim memory when it is no longer referenced */
+    void           *userdata;          /* arbitrary pointer to pass context data to the callback function */
 };
 
 struct al_tx_st {
@@ -139,6 +140,17 @@
     do { (alc)->begin -= n; (al)->bytes += (n); } while (0)
 
 /*
+ * callback to release buffer memory allocated by the library
+ */
+static
+void freemem(char *p, size_t n, void *u)
+{
+printf("*** FREEMEM(LIBRARY) buffer %p size %d\n",p,n);
+    al_t *al = (al_t *)u;
+    (al->m.bfree)(p);
+}
+
+/*
  * allocate backing store and its control structure from heap
  *
  * can be freed when usecount drops to zero
@@ -157,7 +169,8 @@
         return AL_ERR_MEM;
     }
 
-    buf->freemem  = 1;
+    buf->freemem  = freemem;
+    buf->userdata = NULL;
     buf->size     = n;
     buf->usecount = 0;
 
@@ -173,7 +186,7 @@
         return AL_ERR_INT;
 
     if (buf->freemem)
-        (al->m.bfree)(buf->mem);
+        (buf->freemem)(buf->mem, buf->size, buf->userdata);
 
     (al->m.free)(buf);
     return AL_OK;
@@ -184,8 +197,8 @@
  * and attach to existing memory
  * only the control structure will be freed later
  */
-static al_rc_t
-make_buffer(al_t *al, char *p, size_t n, al_buffer_t **bufp)
+static
+al_rc_t make_buffer(al_t *al, char *p, size_t n, void (*freemem)(char *, size_t, void *), void *u, al_buffer_t **bufp)
 {
     al_buffer_t *buf;
 
@@ -194,7 +207,8 @@
 
     buf->mem      = p;
 
-    buf->freemem  = 0;
+    buf->freemem  = freemem;
+    buf->userdata = u;
     buf->size     = n;
     buf->usecount = 0;
 
@@ -539,8 +553,7 @@
  * XXX - some list operations modify the buffer
  *
  */
-al_rc_t
-al_attach_buffer(al_t *al, char *p, size_t n)
+al_rc_t al_attach_buffer(al_t *al, char *p, size_t n, void (*freemem)(char *, size_t, void *), void *u)
 {
     al_rc_t rc;
     al_buffer_t *buf;
@@ -550,7 +563,7 @@
     if (al == NULL || p == NULL || n < 0)
         return AL_RC(AL_ERR_ARG);
 
-    rc = make_buffer(al, p, n, &buf);
+    rc = make_buffer(al, p, n, freemem, u, &buf);
     if (rc != AL_OK)
         return AL_RC(rc);
     rc = new_chunk(al,buf, &cur);
@@ -789,7 +802,7 @@
  * - save traversal parameters
  */
 al_rc_t
-al_traverse(al_t *al, al_tx_t *tx, size_t off, size_t n, al_td_t dir)
+al_traverse(al_t *al, size_t off, size_t n, al_td_t dir, al_tx_t *tx)
 {
     al_rc_t rc;
 
@@ -880,7 +893,7 @@
     al_tx_t tx;                /* XXX - private tx structure on stack */
     al_chunk_t *view;
 
-    rc = al_traverse(al, &tx, off, n, dir);
+    rc = al_traverse(al, off, n, dir, &tx);
     if (rc != AL_OK)
         return AL_RC(rc);
 
@@ -915,7 +928,7 @@
 
     *lenp = 0; /* keep caller on safe side */
 
-    rc = al_traverse(al, &tx, off, n, AL_FORWARD);
+    rc = al_traverse(al, off, n, AL_FORWARD, &tx);
     if (rc != AL_OK)
         return AL_RC(rc);
 
@@ -949,7 +962,7 @@
     al_chunk_t *view;
     size_t step;
 
-    rc = al_traverse(al, &tx, off, n, AL_FORWARD);
+    rc = al_traverse(al, off, n, AL_FORWARD, &tx);
     if (rc != AL_OK)
         return AL_RC(rc);
 


ossp-pkg/sio/al.h 1.11 -> 1.12

--- al.h 2002/10/18 11:01:59     1.11
+++ al.h 2002/10/18 12:24:53     1.12
@@ -58,10 +58,10 @@
 al_rc_t al_destroy      (al_t *al);
 al_rc_t al_append_bytes (al_t *al, const char *src, size_t n);
 al_rc_t al_prepend_bytes(al_t *al, const char *src, size_t n);
-al_rc_t al_attach_buffer(al_t *al, char *p, size_t n);
+al_rc_t al_attach_buffer(al_t *al, char *p, size_t n, void (*freemem)(char *, size_t, void *), void *u);
 al_rc_t al_txalloc      (al_t *al, al_tx_t **txp);
 al_rc_t al_txfree       (al_t *al, al_tx_t *tx);
-al_rc_t al_traverse     (al_t *al, al_tx_t *tx, size_t off, size_t n, al_td_t dir);
+al_rc_t al_traverse     (al_t *al, size_t off, size_t n, al_td_t dir, al_tx_t *tx);
 al_rc_t al_traverse_next(al_t *al, al_tx_t *tx, al_chunk_t **alcp);
 al_rc_t al_traverse_end (al_t *al, al_tx_t *tx, int final);
 al_rc_t al_traverse_cb  (al_t *al, size_t off, size_t n, al_td_t dir, al_rc_t (*cb)(al_chunk_t *, void *), void *u);


ossp-pkg/sio/al_test.c 1.11 -> 1.12

--- al_test.c    2002/10/18 11:03:07     1.11
+++ al_test.c    2002/10/18 12:24:53     1.12
@@ -116,7 +116,7 @@
     total = 0;
 
     al_txalloc(al, &tx);
-    al_traverse(al, tx, 0, -1, AL_FORWARD);
+    al_traverse(al, 0, -1, AL_FORWARD, tx);
     while (al_traverse_next(al, tx, &cur) == AL_OK)
         total += al_chunk_len(cur);
     al_traverse_end(al, tx, 1);
@@ -129,6 +129,11 @@
             tag,(void *)al,total,total2);
 }
 
+void reclaim(char *p, size_t n, void *u)
+{
+    printf("*** reclaiming buffer %p size %d ***\n",p,n);
+}
+
 int main(int argc, char *argv[])
 {
     al_rc_t rc;
@@ -141,7 +146,7 @@
     al_create(&al3);
 
     al_append_bytes(al, S("Hello world\n"));
-    al_attach_buffer(al, S(baf));
+    al_attach_buffer(al, S(baf), reclaim, NULL);
 
     for (i=0; i<500; ++i)
         al_append_bytes(al, S("Huhu world\n"));

CVSTrac 2.0.1