Merged silc_1_1_branch to trunk.
[silc.git] / lib / silcutil / silcbuffer.h
index 673c4a7ff0608da0bd421c4f42be134060c5c313..5ce671175b628994820e5edd63a20efb026cb504 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1998 - 2006 Pekka Riikonen
+  Copyright (C) 1998 - 2007 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
  *
  * SOURCE
  */
-typedef struct {
+typedef struct SilcBufferObject {
   unsigned char *head;
   unsigned char *data;
   unsigned char *tail;
@@ -120,106 +120,114 @@ typedef struct {
 
 /* Macros */
 
-/****d* silcutil/SilcBufferAPI/silc_buffer_truelen
+/****f* silcutil/SilcBufferAPI/silc_buffer_data
  *
  * NAME
  *
- *    SilcUInt32 silc_buffer_truelen(SilcBuffer sb)
+ *    unsigned char *silc_buffer_data(SilcBuffer sb)
  *
  * DESCRIPTION
  *
- *    Returns the true length of the buffer.
+ *    Returns pointer to the data area of the buffer.
  *
  * SOURCE
  */
-#define silc_buffer_truelen(x) (SilcUInt32)((x)->end - (x)->head)
+#define silc_buffer_data(x) (x)->data
 /***/
 
-/****d* silcutil/SilcBufferAPI/silc_buffer_len
+/****f* silcutil/SilcBufferAPI/silc_buffer_datalen
  *
  * NAME
  *
- *    SilcUInt32 silc_buffer_len(SilcBuffer sb)
+ *    #define silc_buffer_datalen ...
  *
  * DESCRIPTION
  *
- *    Returns the current length of the data area of the buffer.
+ *    Macro that can be used in function argument list to give the data
+ *    pointer and the data length, instead of calling both silc_buffer_data
+ *    and silc_buffer_len separately.
+ *
+ * EXAMPLE
+ *
+ *    // Following are the same thing
+ *    silc_foo_function(foo, silc_buffer_datalen(buf));
+ *    silc_foo_function(foo, silc_buffer_data(buf), silc_buffer_len(buf));
  *
  * SOURCE
  */
-#define silc_buffer_len(x) (SilcUInt32)((x)->tail - (x)->data)
+#define silc_buffer_datalen(x) (x) ? silc_buffer_data((x)) : NULL, \
+  (x) ? silc_buffer_len((x)) : 0
 /***/
 
-/****d* silcutil/SilcBufferAPI/silc_buffer_headlen
+/* Inline functions */
+
+/****d* silcutil/SilcBufferAPI/silc_buffer_truelen
  *
  * NAME
  *
- *    SilcUInt32 silc_buffer_headlen(SilcBuffer sb)
+ *    SilcUInt32 silc_buffer_truelen(SilcBuffer sb)
  *
  * DESCRIPTION
  *
- *    Returns the current length of the head data area of the buffer.
+ *    Returns the true length of the buffer.
  *
- * SOURCE
- */
-#define silc_buffer_headlen(x) (SilcUInt32)((x)->data - (x)->head)
-/***/
+ ***/
+static inline
+SilcUInt32 silc_buffer_truelen(SilcBuffer x)
+{
+  return (SilcUInt32)(x->end - x->head);
+}
 
-/****d* silcutil/SilcBufferAPI/silc_buffer_taillen
+/****d* silcutil/SilcBufferAPI/silc_buffer_len
  *
  * NAME
  *
- *    SilcUInt32 silc_buffer_taillen(SilcBuffer sb)
+ *    SilcUInt32 silc_buffer_len(SilcBuffer sb)
  *
  * DESCRIPTION
  *
- *    Returns the current length of the tail data area of the buffer.
+ *    Returns the current length of the data area of the buffer.
  *
- * SOURCE
- */
-#define silc_buffer_taillen(x) (SilcUInt32)((x)->end - (x)->tail)
-/***/
+ ***/
+static inline
+SilcUInt32 silc_buffer_len(SilcBuffer x)
+{
+  return (SilcUInt32)(x->tail - x->data);
+}
 
-/****f* silcutil/SilcBufferAPI/silc_buffer_data
+/****d* silcutil/SilcBufferAPI/silc_buffer_headlen
  *
  * NAME
  *
- *    unsigned char *silc_buffer_data(SilcBuffer sb)
+ *    SilcUInt32 silc_buffer_headlen(SilcBuffer sb)
  *
  * DESCRIPTION
  *
- *    Returns pointer to the data area of the buffer.
+ *    Returns the current length of the head data area of the buffer.
  *
- * SOURCE
- */
-#define silc_buffer_data(x) (x)->data
-/***/
+ ***/
+static inline
+SilcUInt32 silc_buffer_headlen(SilcBuffer x)
+{
+  return (SilcUInt32)(x->data - x->head);
+}
 
-/****f* silcutil/SilcBufferAPI/silc_buffer_datalen
+/****d* silcutil/SilcBufferAPI/silc_buffer_taillen
  *
  * NAME
  *
- *    #define silc_buffer_datalen ...
+ *    SilcUInt32 silc_buffer_taillen(SilcBuffer sb)
  *
  * DESCRIPTION
  *
- *    Macro that can be used in function argument list to give the data
- *    pointer and the data length, instead of calling both silc_buffer_data
- *    and silc_buffer_len separately.
- *
- * EXAMPLE
- *
- *    // Following are the same thing
- *    silc_foo_function(foo, silc_buffer_datalen(buf));
- *    silc_foo_function(foo, silc_buffer_data(buf), silc_buffer_len(buf));
+ *    Returns the current length of the tail data area of the buffer.
  *
- * SOURCE
- */
-#define silc_buffer_datalen(x) (x) ? silc_buffer_data((x)) : NULL, \
-  (x) ? silc_buffer_len((x)) : 0
-/***/
-
-/* Inline functions */
+ ***/
+static inline
+SilcUInt32 silc_buffer_taillen(SilcBuffer x)
+{
+  return (SilcUInt32)(x->end - x->tail);
+}
 
 /****f* silcutil/SilcBufferAPI/silc_buffer_alloc
  *
@@ -969,7 +977,7 @@ SilcBuffer silc_buffer_salloc(SilcStack stack, SilcUInt32 len)
     return NULL;
 
   /* Allocate the actual data area */
-  sb->head = (unsigned char *)silc_smalloc_ua(stack, len);
+  sb->head = (unsigned char *)silc_smalloc(stack, len);
   if (silc_unlikely(!sb->head))
     return NULL;
 
@@ -1046,12 +1054,12 @@ SilcBuffer silc_buffer_srealloc(SilcStack stack,
 
   hlen = silc_buffer_headlen(sb);
   dlen = silc_buffer_len(sb);
-  h = (unsigned char *)silc_srealloc_ua(stack, silc_buffer_truelen(sb),
-                                       sb->head, newsize);
+  h = (unsigned char *)silc_srealloc(stack, silc_buffer_truelen(sb),
+                                    sb->head, newsize);
   if (!h) {
     /* Do slow and stack wasting realloc.  The old sb->head is lost and
        is freed eventually. */
-    h = (unsigned char *)silc_smalloc_ua(stack, newsize);
+    h = (unsigned char *)silc_smalloc(stack, newsize);
     if (silc_unlikely(!h))
       return NULL;
     memcpy(h, sb->head, silc_buffer_truelen(sb));