W3C home > Mailing lists > Public > www-lib@w3.org > July to September 2000

Creating a chunk from an allocated buffer

From: Wayne Davison <wayned@users.sourceforge.net>
Date: Wed, 23 Aug 2000 17:06:06 -0700 (PDT)
To: www-lib <www-lib@w3.org>
Message-ID: <Pine.LNX.4.21.0008231657490.6089-100000@phong.blorf.net>
I to be able to transfer some allocated memory to a chunk (to be able to
hand off an ImageMagick blob) without having to allocate more memory, copy
it into the blob, and then delete the original.  To handle this, I wrote
HTChunk_fromBuffer().  Here's a patch.  I hope you like it.

Also, I improved a comment in HTChunk.html so that it is clearer what
gets destroyed when HTChunk_toCString() is called.

..wayne..

---8<------8<------8<------8<---cut here--->8------>8------>8------>8---
Index: Library/src/HTChunk.c
@@ -117,6 +117,22 @@
     return ch;
 }
 
+/*	Create a chunk from an allocated buffer
+**	---------------------------------------
+*/
+PUBLIC HTChunk * HTChunk_fromBuffer (char * buf, int buflen, int size, int grow)
+{
+    HTChunk * ch;
+    ch = HTChunk_new(grow);
+    if (buf) {
+	ch->data = buf;
+	ch->size = ch->allocated = buflen;
+	if (size < buflen)
+	    HTChunk_setSize(ch, size);	/* This ensures the end is 0-filled */
+    }
+    return ch;
+}
+
 /*	Free a chunk but keep the data
 **	------------------------------
 */
Index: Library/src/HTChunk.html
@@ -155,10 +155,23 @@
 A Chunk may be built from an allocated string. The chunk assumes control
 of the passed string, eliminating the need for additional allocations and
 string copies.<BR>
-When you take control of the CString from a chunk, it is destroyed.
+When you take control of the CString from a chunk, the chunk is destroyed.
 <PRE>
 extern HTChunk * HTChunk_fromCString	(char * str, int grow);
 extern char * HTChunk_toCString		(HTChunk * ch);
+</PRE>
+<H2>
+ Creating a Chunk from an allocated buffer
+</H2>
+<P>
+A Chunk may be built from an allocted buffer.  You must specify how much
+memory is allocated in the buffer (buflen) and what the size the new
+Chunk should be (size).  All memory between size and buflen is zeroed.
+Note that is is legal to specify a size equal to the buflen if you don't
+expect the Chunk to be null terminated.  The chunk takes control of the
+memory, and will free it when the Chunk is destroyed.
+<PRE>
+extern HTChunk * HTChunk_fromBuffer (char * buf, int buflen, int size, int grow);
 </PRE>
 <H2>
   Old Interface Names
---8<------8<------8<------8<---cut here--->8------>8------>8------>8---
Received on Wednesday, 23 August 2000 20:06:38 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 23 April 2007 18:18:37 GMT