talloc: add defines and functions for TALLOC_MAJOR/MINOR_VERSION
authorStefan Metzmacher <metze@samba.org>
Thu, 20 Aug 2009 11:43:42 +0000 (13:43 +0200)
committerStefan Metzmacher <metze@samba.org>
Fri, 21 Aug 2009 07:58:22 +0000 (09:58 +0200)
We also use the major and minor versions in the TALLOC_MAGIC,
so that we can detect if two conflicting versions of talloc
are loaded in one process. In this case we use talloc_log() to
output a very useful debug message before we call
talloc_abort().

metze

lib/talloc/Makefile.in
lib/talloc/configure.ac
lib/talloc/talloc.c
lib/talloc/talloc.h
lib/talloc/talloc.mk
lib/talloc/talloc.pc.in

index a45f72bedeb9ad8f269198d1fb6dd99b36fa0439..8baa28baaedd9497124cddac3d4d9075d7b6cfbb 100644 (file)
@@ -16,7 +16,10 @@ CC = @CC@
 CFLAGS = @CFLAGS@ -DHAVE_CONFIG_H= -I. -I@srcdir@
 EXTRA_TARGETS = @DOC_TARGET@
 PICFLAG = @PICFLAG@
-PACKAGE_VERSION = @PACKAGE_VERSION@
+TALLOC_VERSION = @TALLOC_VERSION@
+TALLOC_VERSION_MAJOR = @TALLOC_VERSION_MAJOR@
+TALLOC_VERSION_MINOR = @TALLOC_VERSION_MINOR@
+TALLOC_VERSION_RELEASE = @TALLOC_VERSION_RELEASE@
 SHLIBEXT = @SHLIBEXT@
 SHLD = @SHLD@
 SHLD_FLAGS = @SHLD_FLAGS@
index d6471a4aa7691f51a74865685649d811a09cfd7f..49f396cf6194dfeff75fc8bea3dc0117259dd8ff 100644 (file)
@@ -4,6 +4,26 @@ AC_CONFIG_SRCDIR([talloc.c])
 AC_SUBST(datarootdir)
 AC_CONFIG_HEADER(config.h)
 
+TALLOC_VERSION=${PACKAGE_VERSION}
+TALLOC_VERSION_MAJOR=`echo ${PACKAGE_VERSION} | cut -d '.' -f1`
+TALLOC_VERSION_MINOR=`echo ${PACKAGE_VERSION} | cut -d '.' -f2`
+TALLOC_VERSION_RELEASE=`echo ${PACKAGE_VERSION} | cut -d '.' -f3`
+
+AC_SUBST(TALLOC_VERSION)
+AC_SUBST(TALLOC_VERSION_MAJOR)
+AC_SUBST(TALLOC_VERSION_MINOR)
+AC_SUBST(TALLOC_VERSION_RELEASE)
+
+AC_DEFINE_UNQUOTED(TALLOC_BUILD_VERSION_MAJOR,
+       [${TALLOC_VERSION_MAJOR}],
+       [talloc major version])
+AC_DEFINE_UNQUOTED(TALLOC_BUILD_VERSION_MINOR,
+       [${TALLOC_VERSION_MINOR}],
+       [talloc minor version])
+AC_DEFINE_UNQUOTED(TALLOC_BUILD_VERSION_RELEASE,
+       [${TALLOC_VERSION_RELEASE}],
+       [talloc release version])
+
 AC_LIBREPLACE_ALL_CHECKS
 
 AC_LD_PICFLAG
index b5dcfca74cdb32230710a18f0984222e6c79fef7..0133b440b48be0e0571e5ec1bc0a8bd0867f9146 100644 (file)
 #include "replace.h"
 #include "talloc.h"
 
+#ifdef TALLOC_BUILD_VERSION_MAJOR
+#if (TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR)
+#error "TALLOC_VERSION_MAJOR != TALLOC_BUILD_VERSION_MAJOR"
+#endif
+#endif
+
+#ifdef TALLOC_BUILD_VERSION_MINOR
+#if (TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR)
+#error "TALLOC_VERSION_MINOR != TALLOC_BUILD_VERSION_MINOR"
+#endif
+#endif
+
 /* use this to force every realloc to change the pointer, to stress test
    code that might not cope */
 #define ALWAYS_REALLOC 0
 
 
 #define MAX_TALLOC_SIZE 0x10000000
-#define TALLOC_MAGIC_V1 0xe814ec70
-#define TALLOC_MAGIC_V2 0xe814ec80
-#define TALLOC_MAGIC    TALLOC_MAGIC_V2
+#define TALLOC_MAGIC_BASE 0xe814ec70
+#define TALLOC_MAGIC ( \
+       TALLOC_MAGIC_BASE + \
+       (TALLOC_VERSION_MAJOR << 12) + \
+       (TALLOC_VERSION_MINOR << 4) \
+)
+
 #define TALLOC_FLAG_FREE 0x01
 #define TALLOC_FLAG_LOOP 0x02
 #define TALLOC_FLAG_POOL 0x04          /* This is a talloc pool */
@@ -123,6 +139,16 @@ struct talloc_chunk {
 #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
 #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
 
+int talloc_version_major(void)
+{
+       return TALLOC_VERSION_MAJOR;
+}
+
+int talloc_version_minor(void)
+{
+       return TALLOC_VERSION_MINOR;
+}
+
 static void (*talloc_log_fn)(const char *message);
 
 void talloc_set_log_fn(void (*log_fn)(const char *message))
@@ -176,9 +202,15 @@ static void talloc_abort(const char *reason)
        talloc_abort_fn(reason);
 }
 
-static void talloc_abort_magic_v1(void)
+static void talloc_abort_magic(unsigned magic)
 {
-       talloc_abort("Bad talloc magic value - old magic v1 used");
+       unsigned striped = magic - TALLOC_MAGIC_BASE;
+       unsigned major = (striped & 0xFFFFF000) >> 12;
+       unsigned minor = (striped & 0x00000FF0) >> 4;
+       talloc_log("Bad talloc magic[0x%08X/%u/%u] expected[0x%08X/%u/%u]\n",
+                  magic, major, minor,
+                  TALLOC_MAGIC, TALLOC_VERSION_MAJOR, TALLOC_VERSION_MINOR);
+       talloc_abort("Bad talloc magic value - wrong talloc version used/mixed");
 }
 
 static void talloc_abort_double_free(void)
@@ -197,8 +229,8 @@ static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
        const char *pp = (const char *)ptr;
        struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
        if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) { 
-               if ((tc->flags & (~0xF)) == TALLOC_MAGIC_V1) {
-                       talloc_abort_magic_v1();
+               if ((tc->flags & (~0xFFF)) == TALLOC_MAGIC_BASE) {
+                       talloc_abort_magic(tc->flags & (~0xF));
                        return NULL;
                }
 
index 54be1699dc32b58055829ef3e4f25c2d33a12356..8241eeb306d2663b4611d054f4200b423d914aa2 100644 (file)
 #include <stdio.h>
 #include <stdarg.h>
 
+#define TALLOC_VERSION_MAJOR 2
+#define TALLOC_VERSION_MINOR 0
+
+int talloc_version_major(void);
+int talloc_version_minor(void);
+
 /* this is only needed for compatibility with the old talloc */
 typedef void TALLOC_CTX;
 
index fb57e44c4b6accb39839b31a26ccc211e63ac71a..2a2e1100fc845bb1fd42b8a6b308ad5a62d2c6ca 100644 (file)
@@ -1,7 +1,7 @@
 TALLOC_OBJ = $(tallocdir)/talloc.o 
 
-TALLOC_SOLIB = libtalloc.$(SHLIBEXT).$(PACKAGE_VERSION)
-TALLOC_SONAME = libtalloc.$(SHLIBEXT).1
+TALLOC_SOLIB = libtalloc.$(SHLIBEXT).$(TALLOC_VERSION)
+TALLOC_SONAME = libtalloc.$(SHLIBEXT).$(TALLOC_VERSION_MAJOR)
 TALLOC_STLIB = libtalloc.a
 
 all:: $(TALLOC_STLIB) $(TALLOC_SOLIB) testsuite
index 459cce70b1dcb5814e9a45c4cdb134ca6eb19071..5ce2109866cb81084933d4a55925ba928b8e775d 100644 (file)
@@ -5,7 +5,7 @@ includedir=@includedir@
 
 Name: talloc 
 Description: A hierarchical pool based memory system with destructors
-Version: @PACKAGE_VERSION@
+Version: @TALLOC_VERSION@
 Libs: -L${libdir} -ltalloc
 Cflags: -I${includedir} 
 URL: http://talloc.samba.org/