Added SILC_ASSERT.
authorPekka Riikonen <priikone@silcnet.org>
Wed, 1 Nov 2006 17:50:01 +0000 (17:50 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Wed, 1 Nov 2006 17:50:01 +0000 (17:50 +0000)
lib/silcutil/silcasync.c
lib/silcutil/silcbuffer.h
lib/silcutil/silcfsm.c
lib/silcutil/silclog.h
lib/silcutil/silcmemory.h

index 3e7294d778018c0fd7312f0cd0bee287f2b51204..216e30af0fe57ec8ed91f3d1c93972ed6803902c 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2005 Pekka Riikonen
+  Copyright (C) 2005, 2006 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
@@ -87,7 +87,7 @@ SilcBool silc_async_init(SilcAsyncOperation op,
                         SilcAsyncOperationPause pause_cb,
                         void *context)
 {
-  assert(abort_cb);
+  SILC_ASSERT(abort_cb);
   op->abort_cb = abort_cb;
   op->pause_cb = pause_cb;
   op->context = context;
index 5356b3b2485a6bfc298493120dfac3a379adae1a..78d99df2f736c2556e6a8a231b2b54ff8543a401 100644 (file)
@@ -409,7 +409,7 @@ unsigned char *silc_buffer_pull(SilcBuffer sb, SilcUInt32 len)
 {
   unsigned char *old_data = sb->data;
 #if defined(SILC_DEBUG)
-  assert(len <= silc_buffer_len(sb));
+  SILC_ASSERT(len <= silc_buffer_len(sb));
 #else
   if (len > silc_buffer_len(sb))
     return NULL;
@@ -453,7 +453,7 @@ unsigned char *silc_buffer_push(SilcBuffer sb, SilcUInt32 len)
 {
   unsigned char *old_data = sb->data;
 #if defined(SILC_DEBUG)
-  assert((sb->data - len) >= sb->head);
+  SILC_ASSERT((sb->data - len) >= sb->head);
 #else
   if ((sb->data - len) < sb->head)
     return NULL;
@@ -497,7 +497,7 @@ unsigned char *silc_buffer_pull_tail(SilcBuffer sb, SilcUInt32 len)
 {
   unsigned char *old_tail = sb->tail;
 #if defined(SILC_DEBUG)
-  assert(len <= silc_buffer_taillen(sb));
+  SILC_ASSERT(len <= silc_buffer_taillen(sb));
 #else
   if (len > silc_buffer_taillen(sb))
     return NULL;
@@ -541,7 +541,7 @@ unsigned char *silc_buffer_push_tail(SilcBuffer sb, SilcUInt32 len)
 {
   unsigned char *old_tail = sb->tail;
 #if defined(SILC_DEBUG)
-  assert((sb->tail - len) >= sb->data);
+  SILC_ASSERT((sb->tail - len) >= sb->data);
 #else
   if ((sb->tail - len) < sb->data)
     return NULL;
@@ -582,7 +582,7 @@ unsigned char *silc_buffer_put_head(SilcBuffer sb,
                                    SilcUInt32 len)
 {
 #if defined(SILC_DEBUG)
-  assert(len <= silc_buffer_headlen(sb));
+  SILC_ASSERT(len <= silc_buffer_headlen(sb));
 #else
   if (len > silc_buffer_headlen(sb))
     return NULL;
@@ -622,7 +622,7 @@ unsigned char *silc_buffer_put(SilcBuffer sb,
                               SilcUInt32 len)
 {
 #if defined(SILC_DEBUG)
-  assert(len <= silc_buffer_len(sb));
+  SILC_ASSERT(len <= silc_buffer_len(sb));
 #else
   if (len > silc_buffer_len(sb))
     return NULL;
@@ -662,7 +662,7 @@ unsigned char *silc_buffer_put_tail(SilcBuffer sb,
                                    SilcUInt32 len)
 {
 #if defined(SILC_DEBUG)
-  assert(len <= silc_buffer_taillen(sb));
+  SILC_ASSERT(len <= silc_buffer_taillen(sb));
 #else
   if (len > silc_buffer_taillen(sb))
     return NULL;
index f2fb362db717a9b6d338423566b1f9eec3f81606..2c95ccd19f110fdba9503f376b1508d7cd079f95 100644 (file)
@@ -106,7 +106,7 @@ void silc_fsm_thread_init(SilcFSMThread thread,
                  thread, real_thread ? "real" : "FSM"));
 
 #if defined(SILC_DEBUG)
-  assert(!fsm->thread);
+  SILC_ASSERT(!fsm->thread);
 #endif /* SILC_DEBUG */
 
   thread->fsm_context = thread_context;
@@ -138,11 +138,11 @@ SILC_TASK_CALLBACK(silc_fsm_free_final)
 
 #if defined(SILC_DEBUG)
   /* We must be finished */
-  assert(f->finished);
+  SILC_ASSERT(f->finished);
 
   /* Machine must not have active threads */
   if (!f->thread && f->u.m.threads)
-    assert(f->u.m.threads == 0);
+    SILC_ASSERT(f->u.m.threads == 0);
 #endif /* SILC_DEBUG */
 
   if (!f->thread && f->u.m.lock)
@@ -277,7 +277,7 @@ SilcSchedule silc_fsm_get_schedule(void *fsm)
 
 SilcFSM silc_fsm_get_machine(SilcFSMThread thread)
 {
-  assert(thread->thread);
+  SILC_ASSERT(thread->thread);
   return (SilcFSM)thread->u.t.fsm;
 }
 
@@ -320,7 +320,7 @@ SilcBool silc_fsm_thread_wait(void *fsm, void *thread)
   SilcFSM t = thread;
 
 #if defined(SILC_DEBUG)
-  assert(t->thread);
+  SILC_ASSERT(t->thread);
 #endif /* SILC_DEBUG */
 
   if (t->finished)
@@ -364,7 +364,7 @@ SILC_TASK_CALLBACK(silc_fsm_run)
     /* Finish the state machine */
     SILC_LOG_DEBUG(("State finish %p", fsm));
 #if defined(SILC_DEBUG)
-    assert(!fsm->finished);
+    SILC_ASSERT(!fsm->finished);
 #endif /* SILC_DEBUG */
     fsm->finished = TRUE;
 
@@ -451,7 +451,7 @@ void silc_fsm_sema_init(SilcFSMSema sema, SilcFSM fsm, SilcUInt32 value)
 {
   SILC_LOG_DEBUG(("Initializing semaphore %p", sema));
 #if defined(SILC_DEBUG)
-  assert(!fsm->thread);
+  SILC_ASSERT(!fsm->thread);
 #endif /* SILC_DEBUG */
   memset(sema, 0, sizeof(*sema));
   sema->fsm = fsm;
@@ -500,7 +500,7 @@ SilcUInt32 silc_fsm_sema_wait(SilcFSMSema sema, void *fsm)
     SilcFSM entry;
     silc_list_start(sema->waiters);
     while ((entry = silc_list_get(sema->waiters)) != SILC_LIST_END)
-      assert(entry != fsm);
+      SILC_ASSERT(entry != fsm);
 #endif /* SILC_DEBUG */
 
     SILC_LOG_DEBUG(("Waiting for semaphore %p", sema));
index acf6ed3342910f1ec51e36ad94e797db30afa5f4..0b12a2a4c300e021940cff04f8590b6ffdfa8d79 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2005 Pekka Riikonen
+  Copyright (C) 1997 - 2006 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
@@ -285,6 +285,27 @@ typedef SilcBool (*SilcLogHexdumpCb)(char *file, char *function, int line,
 #endif /* SILC_DEBUG */
 /***/
 
+/****d* silcutil/SilcLogAPI/SILC_ASSERT
+ *
+ * NAME
+ *
+ *    #define SILC_ASSERT(experssion)
+ *
+ * DESCRIPTION
+ *
+ *    Assert macro that prints error message to stderr and calls abort()
+ *    if the `expression' is is false (ie. compares equal to zero).  If
+ *    SILC_DEBUG is not defined this macro as no effect.
+ *
+ * SOURCE
+ */
+#if defined(SILC_DEBUG)
+#define SILC_ASSERT(expr) assert((expr));
+#else
+#define SILC_ASSERT(expr) do { } while(0)
+#endif /* SILC_DEBUG */
+/***/
+
 /* Prototypes */
 
 /****f* silcutil/SilcLogAPI/silc_log_set_file
@@ -308,8 +329,8 @@ typedef SilcBool (*SilcLogHexdumpCb)(char *file, char *function, int line,
  *    save HD activity.
  *
  ***/
-SilcBool silc_log_set_file(SilcLogType type, char *filename, SilcUInt32 maxsize,
-                      SilcSchedule scheduler);
+SilcBool silc_log_set_file(SilcLogType type, char *filename,
+                          SilcUInt32 maxsize, SilcSchedule scheduler);
 
 /****f* silcutil/SilcLogAPI/silc_log_get_file
  *
index 65286de3faadd762c08de6dd7059088857fd43db..7bd7bef6d6dddc5ebe555341ea390b9c3d72195b 100644 (file)
@@ -25,9 +25,6 @@
  * applications use these functions when they need to allocate, manipulate
  * and free memory.
  *
- * Currently all allocation routines assert() that the memory was allocated
- * successfully. Hence, if memory allocation fails it is fatal error.
- *
  ***/
 
 #ifndef SILCMEMORY_H
@@ -46,7 +43,8 @@
  * DESCRIPTION
  *
  *    Allocates memory of `size' bytes and returns pointer to the allocated
- *    memory area.  Free the memory by calling silc_free.
+ *    memory area.  Free the memory by calling silc_free.  Returns NULL on
+ *    error.
  *
  ***/
 void *silc_malloc(size_t size);
@@ -61,7 +59,8 @@ void *silc_malloc(size_t size);
  *
  *    Allocates memory of for an array of `items' elements of `size' bytes
  *    and returns pointer to the allocated memory area.  The memory area is
- *    also zeroed.  Free the memory by calling silc_free.
+ *    also zeroed.  Free the memory by calling silc_free.  Returns NULL on
+ *    error.
  *
  ***/
 void *silc_calloc(size_t items, size_t size);