offchannel fixes; more memory verification
[srvx.git] / src / common.h
index ad5af73c737a7d8b2e0f08ae065a3deaf9d5cae3..5982512fe3670eda84f87721e133e8193342ed56 100644 (file)
@@ -1,11 +1,12 @@
 /* common.h - Common functions/includes
  * Copyright 2000-2004 srvx Development Team
  *
- * This program is free software; you can redistribute it and/or modify
+ * This file is part of srvx.
+ *
+ * srvx is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.  Important limitations are
- * listed in the COPYING file that accompanies this software.
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -13,7 +14,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, email srvx-maintainers@srvx.net.
+ * along with srvx; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
  */
 
 #ifndef COMMON_H
@@ -62,25 +64,48 @@ extern struct tm *localtime_r(const time_t *clock, struct tm *res);
 #endif
 
 #if defined(WITH_MALLOC_DMALLOC)
-#define DMALLOC_FUNC_CHECK 1
-#include <string.h>
-#include <dmalloc.h>
+# define DMALLOC_FUNC_CHECK 1
+# include <string.h>
+# include <dmalloc.h>
 #elif defined(WITH_MALLOC_MPATROL)
-#include <string.h>
-#include <mpatrol.h>
+# include <string.h>
+# include <mpatrol.h>
 #elif defined(WITH_MALLOC_BOEHM_GC)
-#if !defined(NDEBUG)
-#define GC_DEBUG 1
+# if !defined(NDEBUG)
+#  define GC_DEBUG 1
+# endif
+# include <stdlib.h>
+# include <string.h>
+# include <gc/gc.h>
+# define malloc(n) GC_MALLOC(n)
+# define calloc(m,n) GC_MALLOC((m)*(n))
+# define realloc(p,n) GC_REALLOC((p),(n))
+# define free(p) GC_FREE(p)
+# undef  HAVE_STRDUP
+# undef strdup
+#elif defined(WITH_MALLOC_SRVX)
+# undef malloc
+# define malloc(n) srvx_malloc(__FILE__, __LINE__, (n))
+# undef calloc
+# define calloc(m,n) srvx_malloc(__FILE__, __LINE__, (m)*(n))
+# undef realloc
+# define realloc(p,n) srvx_realloc(__FILE__, __LINE__, (p), (n))
+# undef free
+# define free(p) srvx_free(__FILE__, __LINE__, (p))
+# undef strdup
+# define strdup(s) srvx_strdup(__FILE__, __LINE__, (s))
+extern void *srvx_malloc(const char *, unsigned int, size_t);
+extern void *srvx_realloc(const char *, unsigned int, void *, size_t);
+extern char *srvx_strdup(const char *, unsigned int, const char *);
+extern void srvx_free(const char *, unsigned int, void *);
+# if !defined(NDEBUG)
+extern void verify(const void *ptr);
+#  define verify(x) verify(x)
+# endif
 #endif
-#include <stdlib.h>
-#include <string.h>
-#include <gc/gc.h>
-#define malloc(n) GC_MALLOC(n)
-#define calloc(m,n) GC_MALLOC((m)*(n))
-#define realloc(p,n) GC_REALLOC((p),(n))
-#define free(p) GC_FREE(p)
-#undef  HAVE_STRDUP
-#undef strdup
+
+#ifndef verify
+# define verify(ptr) (void)(ptr)
 #endif
 
 extern time_t now;
@@ -138,6 +163,7 @@ void STRUCTNAME##_init(struct STRUCTNAME *list) {\
   list->list = malloc(list->size*sizeof(list->list[0]));\
 }\
 void STRUCTNAME##_append(struct STRUCTNAME *list, ITEMTYPE new_item) {\
+  verify(list->list);\
   if (list->used == list->size) {\
     list->size = list->size ? (list->size << 1) : 4;\
     list->list = realloc(list->list, list->size*sizeof(list->list[0]));\
@@ -146,6 +172,7 @@ void STRUCTNAME##_append(struct STRUCTNAME *list, ITEMTYPE new_item) {\
 }\
 int STRUCTNAME##_remove(struct STRUCTNAME *list, ITEMTYPE new_item) {\
     unsigned int n, found;\
+    verify(list->list);\
     for (found=n=0; n<list->used; n++) {\
        if (list->list[n] == new_item) {\
            memmove(list->list+n, list->list+n+1, (list->used-n-1)*sizeof(list->list[n]));\
@@ -158,14 +185,17 @@ int STRUCTNAME##_remove(struct STRUCTNAME *list, ITEMTYPE new_item) {\
 void STRUCTNAME##_clean(struct STRUCTNAME *list) {\
   list->used = list->size = 0;\
   free(list->list);\
+  list->list = NULL;\
 }
 
-/* The longest string that's likely to be produced is "10 minutes, and 10
-   seconds." (27 characters) */
-#define INTERVALLEN    32
+/* The longest string that is likely to be produced in English is "10
+ * minutes, and 10 seconds" (27 characters).  Other languages will
+ * vary, so there's plenty of leeway.
+ */
+#define INTERVALLEN    50
 
-char *intervalString2(char *output, time_t interval, int brief);
-#define intervalString(OUTPUT, INTERVAL) intervalString2((OUTPUT), (INTERVAL), 0)
+struct handle_info;
+char *intervalString(char *output, time_t interval, struct handle_info *hi);
 int getipbyname(const char *name, unsigned long *ip);
 int set_policer_param(const char *param, void *data, void *extra);
 const char *strtab(unsigned int ii);